Apply PIE to nghttpx
[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_FILE);
93       COUNT_TYPE(T_ENV);
94       COUNT_TYPE(T_DATA);
95       COUNT_TYPE(T_FIBER);
96 #undef COUNT_TYPE
97     default:
98       type = mrb_fixnum_value(i); break;
99     }
100     if (obj_count.counts[i])
101       mrb_hash_set(mrb, hash, type, mrb_fixnum_value(obj_count.counts[i]));
102   }
103
104   return hash;
105 }
106
107 struct os_each_object_data {
108   mrb_value block;
109   struct RClass *target_module;
110   mrb_int count;
111 };
112
113 static int
114 os_each_object_cb(mrb_state *mrb, struct RBasic *obj, void *ud)
115 {
116   struct os_each_object_data *d = (struct os_each_object_data*)ud;
117
118   /* filter dead objects */
119   if (mrb_object_dead_p(mrb, obj)) {
120     return MRB_EACH_OBJ_OK;
121   }
122
123   /* filter internal objects */
124   switch (obj->tt) {
125   case MRB_TT_ENV:
126   case MRB_TT_ICLASS:
127     return MRB_EACH_OBJ_OK;
128   default:
129     break;
130   }
131
132   /* filter half baked (or internal) objects */
133   if (!obj->c) return MRB_EACH_OBJ_OK;
134
135   /* filter class kind if target module defined */
136   if (d->target_module && !mrb_obj_is_kind_of(mrb, mrb_obj_value(obj), d->target_module)) {
137     return MRB_EACH_OBJ_OK;
138   }
139
140   mrb_yield(mrb, d->block, mrb_obj_value(obj));
141   ++d->count;
142   return MRB_EACH_OBJ_OK;
143 }
144
145 /*
146  *  call-seq:
147  *     ObjectSpace.each_object([module]) {|obj| ... } -> fixnum
148  *
149  *  Calls the block once for each object in this Ruby process.
150  *  Returns the number of objects found.
151  *  If the optional argument +module+ is given,
152  *  calls the block for only those classes or modules
153  *  that match (or are a subclass of) +module+.
154  *
155  *  If no block is given, ArgumentError is raised.
156  *
157  */
158
159 static mrb_value
160 os_each_object(mrb_state *mrb, mrb_value self)
161 {
162   mrb_value cls = mrb_nil_value();
163   struct os_each_object_data d;
164   mrb_get_args(mrb, "&|C", &d.block, &cls);
165
166   if (mrb_nil_p(d.block)) {
167     mrb_raise(mrb, E_ARGUMENT_ERROR, "Expected block in ObjectSpace.each_object.");
168   }
169
170   d.target_module = mrb_nil_p(cls) ? NULL : mrb_class_ptr(cls);
171   d.count = 0;
172   mrb_objspace_each_objects(mrb, os_each_object_cb, &d);
173   return mrb_fixnum_value(d.count);
174 }
175
176 void
177 mrb_mruby_objectspace_gem_init(mrb_state *mrb)
178 {
179   struct RClass *os = mrb_define_module(mrb, "ObjectSpace");
180   mrb_define_class_method(mrb, os, "count_objects", os_count_objects, MRB_ARGS_OPT(1));
181   mrb_define_class_method(mrb, os, "each_object", os_each_object, MRB_ARGS_OPT(1));
182 }
183
184 void
185 mrb_mruby_objectspace_gem_final(mrb_state *mrb)
186 {
187 }