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