3 #include <mruby/hash.h>
4 #include <mruby/class.h>
6 struct os_count_struct {
9 mrb_int counts[MRB_TT_MAXDEFINE+1];
13 os_count_object_type(mrb_state *mrb, struct RBasic *obj, void *data)
15 struct os_count_struct *obj_count;
16 obj_count = (struct os_count_struct*)data;
20 if (mrb_object_dead_p(mrb, obj)) {
24 obj_count->counts[obj->tt]++;
26 return MRB_EACH_OBJ_OK;
31 * ObjectSpace.count_objects([result_hash]) -> hash
33 * Counts objects for each type.
35 * It returns a hash, such as:
44 * If the optional argument +result_hash+ is given,
45 * it is overwritten and returned. This is intended to avoid probe effect.
50 os_count_objects(mrb_state *mrb, mrb_value self)
52 struct os_count_struct obj_count = { 0 };
56 if (mrb_get_args(mrb, "|H", &hash) == 0) {
57 hash = mrb_hash_new(mrb);
60 if (!mrb_hash_empty_p(mrb, hash)) {
61 mrb_hash_clear(mrb, hash);
64 mrb_objspace_each_objects(mrb, os_count_object_type, &obj_count);
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));
69 for (i = MRB_TT_FALSE; i < MRB_TT_MAXDEFINE; i++) {
72 #define COUNT_TYPE(t) case (MRB_T ## t): type = mrb_symbol_value(mrb_intern_lit(mrb, #t)); break;
91 COUNT_TYPE(T_EXCEPTION);
98 type = mrb_fixnum_value(i); break;
100 if (obj_count.counts[i])
101 mrb_hash_set(mrb, hash, type, mrb_fixnum_value(obj_count.counts[i]));
107 struct os_each_object_data {
109 struct RClass *target_module;
114 os_each_object_cb(mrb_state *mrb, struct RBasic *obj, void *ud)
116 struct os_each_object_data *d = (struct os_each_object_data*)ud;
118 /* filter dead objects */
119 if (mrb_object_dead_p(mrb, obj)) {
120 return MRB_EACH_OBJ_OK;
123 /* filter internal objects */
127 return MRB_EACH_OBJ_OK;
132 /* filter half baked (or internal) objects */
133 if (!obj->c) return MRB_EACH_OBJ_OK;
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;
140 mrb_yield(mrb, d->block, mrb_obj_value(obj));
142 return MRB_EACH_OBJ_OK;
147 * ObjectSpace.each_object([module]) {|obj| ... } -> fixnum
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+.
155 * If no block is given, ArgumentError is raised.
160 os_each_object(mrb_state *mrb, mrb_value self)
162 mrb_value cls = mrb_nil_value();
163 struct os_each_object_data d;
164 mrb_get_args(mrb, "&|C", &d.block, &cls);
166 if (mrb_nil_p(d.block)) {
167 mrb_raise(mrb, E_ARGUMENT_ERROR, "Expected block in ObjectSpace.each_object.");
170 d.target_module = mrb_nil_p(cls) ? NULL : mrb_class_ptr(cls);
172 mrb_objspace_each_objects(mrb, os_each_object_cb, &d);
173 return mrb_fixnum_value(d.count);
177 mrb_mruby_objectspace_gem_init(mrb_state *mrb)
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));
185 mrb_mruby_objectspace_gem_final(mrb_state *mrb)