Upgrade to 1.46.0
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-objectspace / src / mruby_objectspace.c
1 #include <mruby.h>
2 #include <mruby/gc.h>
3 #include <mruby/hash.h>
4 #include <mruby/class.h>
5
6 struct os_count_struct {
7   mrb_int total;
8   mrb_int freed;
9   mrb_int counts[MRB_TT_MAXDEFINE+1];
10 };
11
12 static int
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   obj_count->total++;
19
20   if (mrb_object_dead_p(mrb, obj)) {
21     obj_count->freed++;
22   }
23   else {
24     obj_count->counts[obj->tt]++;
25   }
26   return MRB_EACH_OBJ_OK;
27 }
28
29 /*
30  *  call-seq:
31  *     ObjectSpace.count_objects([result_hash]) -> hash
32  *
33  *  Counts objects for each type.
34  *
35  *  It returns a hash, such as:
36  *  {
37  *    :TOTAL=>10000,
38  *    :FREE=>3011,
39  *    :T_OBJECT=>6,
40  *    :T_CLASS=>404,
41  *    # ...
42  *  }
43  *
44  *  If the optional argument +result_hash+ is given,
45  *  it is overwritten and returned. This is intended to avoid probe effect.
46  *
47  */
48
49 static mrb_value
50 os_count_objects(mrb_state *mrb, mrb_value self)
51 {
52   struct os_count_struct obj_count = { 0 };
53   mrb_int i;
54   mrb_value hash;
55
56   if (mrb_get_args(mrb, "|H", &hash) == 0) {
57     hash = mrb_hash_new(mrb);
58   }
59
60   if (!mrb_hash_empty_p(mrb, hash)) {
61     mrb_hash_clear(mrb, hash);
62   }
63
64   mrb_objspace_each_objects(mrb, os_count_object_type, &obj_count);
65
66   mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_lit(mrb, "TOTAL")), mrb_fixnum_value(obj_count.total));
67   mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_lit(mrb, "FREE")), mrb_fixnum_value(obj_count.freed));
68
69   for (i = MRB_TT_FALSE; i < MRB_TT_MAXDEFINE; i++) {
70     mrb_value type;
71     switch (i) {
72 #define COUNT_TYPE(t) case (MRB_T ## t): type = mrb_symbol_value(mrb_intern_lit(mrb, #t)); break;
73       COUNT_TYPE(T_FALSE);
74       COUNT_TYPE(T_FREE);
75       COUNT_TYPE(T_TRUE);
76       COUNT_TYPE(T_FIXNUM);
77       COUNT_TYPE(T_SYMBOL);
78       COUNT_TYPE(T_UNDEF);
79       COUNT_TYPE(T_FLOAT);
80       COUNT_TYPE(T_CPTR);
81       COUNT_TYPE(T_OBJECT);
82       COUNT_TYPE(T_CLASS);
83       COUNT_TYPE(T_MODULE);
84       COUNT_TYPE(T_ICLASS);
85       COUNT_TYPE(T_SCLASS);
86       COUNT_TYPE(T_PROC);
87       COUNT_TYPE(T_ARRAY);
88       COUNT_TYPE(T_HASH);
89       COUNT_TYPE(T_STRING);
90       COUNT_TYPE(T_RANGE);
91       COUNT_TYPE(T_EXCEPTION);
92       COUNT_TYPE(T_ENV);
93       COUNT_TYPE(T_DATA);
94       COUNT_TYPE(T_FIBER);
95 #undef COUNT_TYPE
96     default:
97       type = mrb_fixnum_value(i); break;
98     }
99     if (obj_count.counts[i])
100       mrb_hash_set(mrb, hash, type, mrb_fixnum_value(obj_count.counts[i]));
101   }
102
103   return hash;
104 }
105
106 struct os_each_object_data {
107   mrb_value block;
108   struct RClass *target_module;
109   mrb_int count;
110 };
111
112 static int
113 os_each_object_cb(mrb_state *mrb, struct RBasic *obj, void *ud)
114 {
115   struct os_each_object_data *d = (struct os_each_object_data*)ud;
116
117   /* filter dead objects */
118   if (mrb_object_dead_p(mrb, obj)) {
119     return MRB_EACH_OBJ_OK;
120   }
121
122   /* filter internal objects */
123   switch (obj->tt) {
124   case MRB_TT_ENV:
125   case MRB_TT_ICLASS:
126     return MRB_EACH_OBJ_OK;
127   default:
128     break;
129   }
130
131   /* filter half baked (or internal) objects */
132   if (!obj->c) return MRB_EACH_OBJ_OK;
133
134   /* filter class kind if target module defined */
135   if (d->target_module && !mrb_obj_is_kind_of(mrb, mrb_obj_value(obj), d->target_module)) {
136     return MRB_EACH_OBJ_OK;
137   }
138
139   mrb_yield(mrb, d->block, mrb_obj_value(obj));
140   ++d->count;
141   return MRB_EACH_OBJ_OK;
142 }
143
144 /*
145  *  call-seq:
146  *     ObjectSpace.each_object([module]) {|obj| ... } -> fixnum
147  *
148  *  Calls the block once for each object in this Ruby process.
149  *  Returns the number of objects found.
150  *  If the optional argument +module+ is given,
151  *  calls the block for only those classes or modules
152  *  that match (or are a subclass of) +module+.
153  *
154  *  If no block is given, ArgumentError is raised.
155  *
156  */
157
158 static mrb_value
159 os_each_object(mrb_state *mrb, mrb_value self)
160 {
161   mrb_value cls = mrb_nil_value();
162   struct os_each_object_data d;
163   mrb_get_args(mrb, "&!|C", &d.block, &cls);
164
165   d.target_module = mrb_nil_p(cls) ? NULL : mrb_class_ptr(cls);
166   d.count = 0;
167   mrb_objspace_each_objects(mrb, os_each_object_cb, &d);
168   return mrb_fixnum_value(d.count);
169 }
170
171 void
172 mrb_mruby_objectspace_gem_init(mrb_state *mrb)
173 {
174   struct RClass *os = mrb_define_module(mrb, "ObjectSpace");
175   mrb_define_class_method(mrb, os, "count_objects", os_count_objects, MRB_ARGS_OPT(1));
176   mrb_define_class_method(mrb, os, "each_object", os_each_object, MRB_ARGS_OPT(1));
177 }
178
179 void
180 mrb_mruby_objectspace_gem_final(mrb_state *mrb)
181 {
182 }