Sha256: d5aa617b1e0d04a5e046a97fd7220d584dd8913b5b45ca58afcd87dbb0859449

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

#include <ruby/ruby.h>

#ifdef T_IMEMO

VALUE rb_iseqw_new(VALUE v);
void rb_objspace_each_objects(
    int (*callback)(void *start, void *end, size_t stride, void *data),
    void *data);
size_t rb_obj_memsize_of(VALUE);

enum imemo_type {
    imemo_none = 0,
    imemo_cref = 1,
    imemo_svar = 2,
    imemo_throw_data = 3,
    imemo_ifunc = 4,
    imemo_memo = 5,
    imemo_ment = 6,
    imemo_iseq = 7,
    imemo_mask = 0x07
};

static inline enum imemo_type
imemo_type(VALUE imemo)
{
    return (RBASIC(imemo)->flags >> FL_USHIFT) & imemo_mask;
}

static inline int
rb_obj_is_iseq(VALUE iseq)
{
    return RB_TYPE_P(iseq, T_IMEMO) && imemo_type(iseq) == imemo_iseq;
}

struct iseq_i_data {
    void (*func)(VALUE v, void *data);
    void *data;
};

int
iseq_i(void *vstart, void *vend, size_t stride, void *ptr)
{
    VALUE v;
    struct iseq_i_data *data = (struct iseq_i_data *)ptr;

    for (v = (VALUE)vstart; v != (VALUE)vend; v += stride) {
	if (RBASIC(v)->flags) {
	    switch (BUILTIN_TYPE(v)) {
	      case T_IMEMO:
		if (rb_obj_is_iseq(v)) {
		    data->func(v, data->data);
		}
		continue;
	      default:
		continue;
	    }
	}
    }

    return 0;
}

static void
each_iseq_i(VALUE v, void *ptr)
{
    rb_yield(rb_iseqw_new(v));
}

static VALUE
each_iseq(VALUE self)
{
    struct iseq_i_data data = {each_iseq_i, NULL};
    rb_objspace_each_objects(iseq_i, &data);
    return Qnil;
}

static void
count_iseq_i(VALUE v, void *ptr)
{
    size_t *sizep = (size_t *)ptr;
    *sizep += 1;
}

static VALUE
count_iseq(VALUE self)
{
    size_t size = 0;
    struct iseq_i_data data = {count_iseq_i, &size};
    rb_objspace_each_objects(iseq_i, &data);
    return SIZET2NUM(size);
}

void
Init_iseq_collector(void)
{
    VALUE rb_mObjSpace = rb_const_get(rb_cObject, rb_intern("ObjectSpace"));
    rb_define_singleton_method(rb_mObjSpace, "each_iseq", each_iseq, 0);
    rb_define_singleton_method(rb_mObjSpace, "count_iseq", count_iseq, 0);
}

#endif

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
iseq_collector-0.5.0 ext/iseq_collector/iseq_collector.c
iseq_collector-0.4.0 ext/iseq_collector/iseq_collector.c
iseq_collector-0.3.0 ext/iseq_collector/iseq_collector.c