Exemplo n.º 1
0
static mrb_value
os_count_objects(mrb_state *mrb, mrb_value self)
{
    struct os_count_struct obj_count = { 0 };
    mrb_int i;
    mrb_value hash;

    if (mrb_get_args(mrb, "|H", &hash) == 0) {
        hash = mrb_hash_new(mrb);
    }

    if (!mrb_test(mrb_hash_empty_p(mrb, hash))) {
        mrb_hash_clear(mrb, hash);
    }

    mrb_objspace_each_objects(mrb, os_count_object_type, &obj_count);

    mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_lit(mrb, "TOTAL")), mrb_fixnum_value(obj_count.total));
    mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_lit(mrb, "FREE")), mrb_fixnum_value(obj_count.freed));

    for (i = MRB_TT_FALSE; i < MRB_TT_MAXDEFINE; i++) {
        mrb_value type;
        switch (i) {
#define COUNT_TYPE(t) case (MRB_T ## t): type = mrb_symbol_value(mrb_intern_lit(mrb, #t)); break;
            COUNT_TYPE(T_FALSE);
            COUNT_TYPE(T_FREE);
            COUNT_TYPE(T_TRUE);
            COUNT_TYPE(T_FIXNUM);
            COUNT_TYPE(T_SYMBOL);
            COUNT_TYPE(T_UNDEF);
            COUNT_TYPE(T_FLOAT);
            COUNT_TYPE(T_CPTR);
            COUNT_TYPE(T_OBJECT);
            COUNT_TYPE(T_CLASS);
            COUNT_TYPE(T_MODULE);
            COUNT_TYPE(T_ICLASS);
            COUNT_TYPE(T_SCLASS);
            COUNT_TYPE(T_PROC);
            COUNT_TYPE(T_ARRAY);
            COUNT_TYPE(T_HASH);
            COUNT_TYPE(T_STRING);
            COUNT_TYPE(T_RANGE);
            COUNT_TYPE(T_EXCEPTION);
            COUNT_TYPE(T_FILE);
            COUNT_TYPE(T_ENV);
            COUNT_TYPE(T_DATA);
            COUNT_TYPE(T_FIBER);
#undef COUNT_TYPE
        default:
            type = mrb_fixnum_value(i);
            break;
        }
        if (obj_count.counts[i])
            mrb_hash_set(mrb, hash, type, mrb_fixnum_value(obj_count.counts[i]));
    }

    return hash;
}
Exemplo n.º 2
0
static mrb_value
os_each_object(mrb_state *mrb, mrb_value self)
{
  mrb_value cls = mrb_nil_value();
  struct os_each_object_data d;
  mrb_get_args(mrb, "&|C", &d.block, &cls);

  if (mrb_nil_p(d.block)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "Expected block in ObjectSpace.each_object.");
  }

  d.target_module = mrb_nil_p(cls) ? NULL : mrb_class_ptr(cls);
  d.count = 0;
  mrb_objspace_each_objects(mrb, os_each_object_cb, &d);
  return mrb_fixnum_value(d.count);
}