Initial ObjectSpace.memsize_of implementation
MODIFY
mrbgems/mruby-objectspace/mrbgem.rake
mrbgems/mruby-objectspace/mrbgem.rake
@@ -2,4 +2,8 @@ MRuby::Gem::Specification.new('mruby-objectspace') do |spec|
spec.license = 'MIT'
spec.author = 'mruby developers'
spec.summary = 'ObjectSpace class'
+
+ spec.add_test_dependency('mruby-metaprog')
+ spec.add_test_dependency('mruby-method')
+ spec.add_test_dependency('mruby-fiber')
end

MODIFY
mrbgems/mruby-objectspace/src/mruby_objectspace.c
mrbgems/mruby-objectspace/src/mruby_objectspace.c
#include <mruby/gc.h>
#include <mruby/hash.h>
#include <mruby/class.h>
+#include <mruby/object.h>
+#include <mruby/numeric.h>
+#include <mruby/string.h>
+#include <mruby/array.h>
+#include <mruby/variable.h>
+#include <mruby/proc.h>
+#include <mruby/value.h>
+#include <mruby/range.h>
struct os_count_struct {
mrb_int total;
@@ -168,12 +176,205 @@ os_each_object(mrb_state *mrb, mrb_value self)
return mrb_fixnum_value(d.count);
}
+static void os_memsize_of_object(mrb_state*,mrb_value,mrb_bool,mrb_int*);
+
+static int
+os_memsize_ivar_cb(mrb_state *mrb, mrb_sym _name, mrb_value obj, void *data)
+{
+ mrb_int *cb_data = (mrb_int *)data;
+ mrb_int recurse = *(&cb_data[0]);
+ mrb_int* total = &cb_data[1];
+
+ os_memsize_of_object(mrb, obj, (mrb_bool)(recurse), total);
+ return 0;
+}
+
+static void
+os_memsize_of_ivars(mrb_state* mrb, mrb_value obj, mrb_bool recurse, mrb_int *t)
+{
+ /* need iv segment table size */
+ if(recurse) {
+ mrb_int r = (mrb_int)recurse;
+ mrb_int *cb_data[2] = { &r, t };
+ mrb_iv_foreach(mrb, obj, os_memsize_ivar_cb, t);
+ }
+}
+
+static void
+os_memsize_of_irep(mrb_state* state, struct mrb_irep *irep, mrb_int* t)
+{
+ mrb_int i;
+ (*t) += (irep->slen * sizeof(mrb_sym)) +
+ (irep->plen * sizeof(mrb_code)) +
+ (irep->ilen * sizeof(mrb_code));
+
+ for(i = 0; i < irep->rlen; i++) {
+ os_memsize_of_irep(state, irep->reps[i], t);
+ }
+}
+
+static void
+os_memsize_of_method(mrb_state* mrb, mrb_value method_obj, mrb_int* t)
+{
+ mrb_value proc_value = mrb_obj_iv_get(mrb, mrb_obj_ptr(method_obj),
+ mrb_intern_lit(mrb, "_proc"));
+ struct RProc *proc = mrb_proc_ptr(proc_value);
+
+ (*t) += sizeof(struct RProc);
+ if(!MRB_PROC_CFUNC_P(proc)) os_memsize_of_irep(mrb, proc->body.irep, t);
+}
+
+static void
+os_memsize_of_methods(mrb_state* mrb, mrb_value obj, mrb_int* t)
+{
+ mrb_value method_list;
+ mrb_int i;
+ if(!mrb_respond_to(mrb, obj, mrb_intern_lit(mrb, "instance_methods"))) return;
+ method_list = mrb_funcall(mrb, obj, "instance_methods", 1, mrb_false_value());
+ for(i = 0; i < RARRAY_LEN(method_list); i++) {
+ mrb_value method = mrb_funcall(mrb, obj, "instance_method", 1,
+ mrb_ary_ref(mrb, method_list, i));
+ os_memsize_of_method(mrb, method, t);
+ }
+}
+
+static void
+os_memsize_of_object(mrb_state* mrb, mrb_value obj, mrb_bool recurse, mrb_int* t)
+{
+ switch(obj.tt) {
+ case MRB_TT_STRING:
+ (*t) += RSTRING_LEN(obj);
+ break;
+ case MRB_TT_CLASS:
+ case MRB_TT_MODULE:
+ case MRB_TT_EXCEPTION:
+ case MRB_TT_SCLASS:
+ case MRB_TT_ICLASS:
+ case MRB_TT_OBJECT: {
+ os_memsize_of_ivars(mrb, obj, recurse, t);
+ if(mrb_obj_is_kind_of(mrb, obj, mrb_class_get(mrb, "UnboundMethod"))) {
+ os_memsize_of_method(mrb, obj, t);
+ }
+ else {
+ os_memsize_of_methods(mrb, obj, t);
+ }
+ break;
+ }
+ case MRB_TT_HASH: {
+ struct htable* htable = RHASH_TBL(obj);
+ /* Need htable & segment struct defs */
+ break;
+ }
+ case MRB_TT_ARRAY: {
+ mrb_int len, i;
+ len = RARRAY_LEN(obj);
+ /* Arrays that do not fit within an RArray perform a heap allocation
+ * storing an array of pointers to the original objects*/
+ if(len > MRB_ARY_EMBED_LEN_MAX) (*t) += sizeof(mrb_value *) * len;
+
+ if(recurse) {
+ for(i = 0; i < len; i++) {
+ os_memsize_of_object(mrb, ARY_PTR(mrb_ary_ptr(obj))[i], recurse, t);
+ }
+ }
+ break;
+ }
+ case MRB_TT_PROC: {
+ struct RProc* proc = mrb_proc_ptr(obj);
+ (*t) += MRB_ENV_LEN(proc->e.env) * sizeof(mrb_value);
+ if(!MRB_PROC_CFUNC_P(proc)) os_memsize_of_irep(mrb, proc->body.irep, t);
+ break;
+ }
+ case MRB_TT_DATA:
+ if(mrb_respond_to(mrb, obj, mrb_intern_lit(mrb, "memsize"))) {
+ (*t) += mrb_fixnum(mrb_funcall(mrb, obj, "memsize", 0));
+ }
+ break;
+ #ifndef MRB_WITHOUT_FLOAT
+ case MRB_TT_FLOAT:
+ #ifdef MRB_WORD_BOXING
+ (*t) += sizeof(struct RFloat);
+ #endif
+ break;
+ #endif
+ case MRB_TT_RANGE:
+ #ifndef MRB_RANGE_EMBED
+ (*t) += sizeof(struct mrb_range_edges);
+ #endif
+ break;
+ case MRB_TT_FIBER:
+ struct RFiber* fiber = (struct RFiber*)mrb_ptr(obj);
+ (*t) += sizeof(struct mrb_context);
+ break;
+ /* zero heap size types.
+ * immediate VM stack values, contained within mrb_state, mrb_heap_page,
+ * or on C stack */
+ case MRB_TT_TRUE:
+ case MRB_TT_FALSE:
+ case MRB_TT_FIXNUM:
+ case MRB_TT_BREAK:
+ case MRB_TT_CPTR:
+ case MRB_TT_SYMBOL:
+ case MRB_TT_FREE:
+ case MRB_TT_UNDEF:
+ case MRB_TT_ENV:
+ case MRB_TT_ISTRUCT:
+ /* never used, silences compiler warning
+ * not having a default: clause lets the compiler tell us when there is a new
+ * TT not accounted for */
+ case MRB_TT_MAXDEFINE:
+ break;
+ }
+}
+
+/*
+ * call-seq:
+ * ObjectSpace.memsize_of(obj, recurse: false) -> Numeric
+ *
+ * Returns the amount of heap memory allocated for object in size_t units.
+ * Not all objects cause additional heap allocations beyond their object pointer
+ * in the heap page and may return 0.
+ *
+ * The return value depends on the definition of size_t on that platform,
+ * therefore the value is not comparable across platform types.
+ *
+ * Immediate values such as integers, booleans, symbols and unboxed float numbers
+ * return 0. Additionally special objects which are small enough to fit inside an
+ * object * pointer, termed embedded objects, also return 0. Strings and arrays
+ * below a compile-time defined size may be embedded.
+ *
+ * Setting recurse: true descends into instance variables, array members,
+ * and hash values recursively, calculating the child objects and adding to
+ * the final sum.
+ *
+ */
+
+static mrb_value
+os_memsize_of(mrb_state *mrb, mrb_value self)
+{
+ mrb_int total;
+ mrb_value obj;
+ mrb_bool recurse;
+ const char *kw_names[1] = { "recurse" };
+ mrb_value kw_values[1];
+ const mrb_kwargs kwargs = { 1, kw_values, kw_names, 0, NULL };
+
+ mrb_get_args(mrb, "o:", &obj, &kwargs);
+ recurse = mrb_obj_eq(mrb, kw_values[0], mrb_true_value()) ? TRUE : FALSE;
+
+ total = 0;
+ os_memsize_of_object(mrb, obj, recurse, &total);
+
+ return mrb_fixnum_value(total);
+}
+
void
mrb_mruby_objectspace_gem_init(mrb_state *mrb)
{
struct RClass *os = mrb_define_module(mrb, "ObjectSpace");
mrb_define_class_method(mrb, os, "count_objects", os_count_objects, MRB_ARGS_OPT(1));
mrb_define_class_method(mrb, os, "each_object", os_each_object, MRB_ARGS_OPT(1));
+ mrb_define_class_method(mrb, os, "memsize_of", os_memsize_of, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
}
void

Initial ObjectSpace.memsize_of implementation