Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mruby_objectspace.c
Go to the documentation of this file.
1 #include <mruby.h>
2 #include <mruby/gc.h>
3 #include <mruby/hash.h>
4 #include <mruby/value.h>
5 
7  size_t total;
8  size_t freed;
10 };
11 
12 void
13 os_count_object_type(mrb_state *mrb, struct RBasic* obj, void *data)
14 {
15  struct os_count_struct* obj_count;
16  obj_count = (struct os_count_struct*)(data);
17 
18  if (is_dead(mrb, obj)) {
19  obj_count->freed++;
20  } else {
21  obj_count->counts[obj->tt]++;
22  obj_count->total++;
23  }
24 }
25 
26 /*
27  * call-seq:
28  * ObjectSpace.count_objects([result_hash]) -> hash
29  *
30  * Counts objects for each type.
31  *
32  * It returns a hash, such as:
33  * {
34  * :TOTAL=>10000,
35  * :FREE=>3011,
36  * :MRB_TT_OBJECT=>6,
37  * :MRB_TT_CLASS=>404,
38  * # ...
39  * }
40  *
41  * If the optional argument +result_hash+ is given,
42  * it is overwritten and returned. This is intended to avoid probe effect.
43  *
44  */
45 
48 {
49  struct os_count_struct obj_count;
50  size_t i;
51  mrb_value hash;
52 
53  if (mrb_get_args(mrb, "|H", &hash) == 0) {
54  hash = mrb_hash_new(mrb);
55  }
56 
57  if (!mrb_test(mrb_hash_empty_p(mrb, hash))) {
58  mrb_hash_clear(mrb, hash);
59  }
60 
61  for (i = 0; i <= MRB_TT_MAXDEFINE; i++) {
62  obj_count.counts[i] = 0;
63  }
64  obj_count.total = 0;
65  obj_count.freed = 0;
66 
68 
69  mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "TOTAL")), mrb_fixnum_value(obj_count.total));
70  mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "FREE")), mrb_fixnum_value(obj_count.freed));
71 
72  for (i = 0; i < MRB_TT_MAXDEFINE; i++) {
74  switch (i) {
75 #define COUNT_TYPE(t) case (t): type = mrb_symbol_value(mrb_intern_cstr(mrb, #t)); break;
98 #undef COUNT_TYPE
99  default:
100  type = mrb_fixnum_value(i); break;
101  }
102  if (obj_count.counts[i])
103  mrb_hash_set(mrb, hash, type, mrb_fixnum_value(obj_count.counts[i]));
104  }
105 
106  return hash;
107 }
108 
109 void
111  struct RClass *os = mrb_define_module(mrb, "ObjectSpace");
112  mrb_define_class_method(mrb, os, "count_objects", os_count_objects, MRB_ARGS_ANY());
113 }
114 
115 void
117 }