ext/ruby_prof/rp_allocation.c in ruby-prof-1.1.0 vs ext/ruby_prof/rp_allocation.c in ruby-prof-1.2.0

- old
+ new

@@ -3,40 +3,36 @@ #include "rp_allocation.h" VALUE cRpAllocation; -prof_allocation_t* -allocations_table_lookup(st_table *table, st_data_t key) +prof_allocation_t* allocations_table_lookup(st_table* table, st_data_t key) { prof_allocation_t* result = NULL; st_data_t value; - if (st_lookup(table, key, &value)) + if (rb_st_lookup(table, key, &value)) { result = (prof_allocation_t*)value; } return result; } -void -allocations_table_insert(st_table *table, st_data_t key, prof_allocation_t * allocation) +void allocations_table_insert(st_table* table, st_data_t key, prof_allocation_t* allocation) { - st_insert(table, (st_data_t)key, (st_data_t)allocation); + rb_st_insert(table, (st_data_t)key, (st_data_t)allocation); } -st_data_t -allocations_key(VALUE klass, int source_line) +st_data_t allocations_key(VALUE klass, int source_line) { return (klass << 4) + source_line; } /* ====== prof_allocation_t ====== */ -prof_allocation_t* -prof_allocation_create(void) +prof_allocation_t* prof_allocation_create(void) { - prof_allocation_t *result = ALLOC(prof_allocation_t); + prof_allocation_t* result = ALLOC(prof_allocation_t); result->count = 0; result->klass = Qnil; result->klass_name = Qnil; result->object = Qnil; result->memory = 0; @@ -45,12 +41,11 @@ result->key = 0; return result; } -prof_allocation_t* -prof_allocate_increment(prof_method_t* method, rb_trace_arg_t* trace_arg) +prof_allocation_t* prof_allocate_increment(prof_method_t* method, rb_trace_arg_t* trace_arg) { VALUE object = rb_tracearg_object(trace_arg); if (BUILTIN_TYPE(object) == T_IMEMO) return NULL; @@ -76,76 +71,69 @@ allocation->memory += rb_obj_memsize_of(object); return allocation; } -static void -prof_allocation_ruby_gc_free(void *data) +static void prof_allocation_ruby_gc_free(void* data) { prof_allocation_t* allocation = (prof_allocation_t*)data; + allocation->object = Qnil; +} +void prof_allocation_free(prof_allocation_t* allocation) +{ /* Has this allocation object been accessed by Ruby? If yes clean it up so to avoid a segmentation fault. */ if (allocation->object != Qnil) { RDATA(allocation->object)->dmark = NULL; RDATA(allocation->object)->dfree = NULL; RDATA(allocation->object)->data = NULL; allocation->object = Qnil; } -} -void -prof_allocation_free(prof_allocation_t* allocation) -{ - prof_allocation_ruby_gc_free(allocation); xfree(allocation); } -size_t -prof_allocation_size(const void* data) +size_t prof_allocation_size(const void* data) { return sizeof(prof_allocation_t); } -void -prof_allocation_mark(void *data) +void prof_allocation_mark(void* data) { prof_allocation_t* allocation = (prof_allocation_t*)data; + if (allocation->object != Qnil) + rb_gc_mark(allocation->object); + if (allocation->klass != Qnil) rb_gc_mark(allocation->klass); - + if (allocation->klass_name != Qnil) rb_gc_mark(allocation->klass_name); - if (allocation->object != Qnil) - rb_gc_mark(allocation->object); - if (allocation->source_file != Qnil) rb_gc_mark(allocation->source_file); } -VALUE -prof_allocation_wrap(prof_allocation_t *allocation) +VALUE prof_allocation_wrap(prof_allocation_t* allocation) { if (allocation->object == Qnil) { - allocation->object = Data_Wrap_Struct(cRpAllocation, prof_allocation_mark , prof_allocation_ruby_gc_free, allocation); + allocation->object = Data_Wrap_Struct(cRpAllocation, prof_allocation_mark, prof_allocation_ruby_gc_free, allocation); } return allocation->object; } -static VALUE -prof_allocation_allocate(VALUE klass) +static VALUE prof_allocation_allocate(VALUE klass) { prof_allocation_t* allocation = prof_allocation_create(); allocation->object = prof_allocation_wrap(allocation); return allocation->object; } -prof_allocation_t* -prof_allocation_get(VALUE self) +prof_allocation_t* prof_allocation_get(VALUE self) { /* Can't use Data_Get_Struct because that triggers the event hook ending up in endless recursion. */ prof_allocation_t* result = DATA_PTR(self); if (!result) @@ -156,12 +144,11 @@ /* call-seq: klass -> Class Returns the type of Class being allocated. */ -static VALUE -prof_allocation_klass_name(VALUE self) +static VALUE prof_allocation_klass_name(VALUE self) { prof_allocation_t* allocation = prof_allocation_get(self); if (allocation->klass_name == Qnil) allocation->klass_name = resolve_klass_name(allocation->klass, &allocation->klass_flags); @@ -172,64 +159,58 @@ /* call-seq: klass_flags -> integer Returns the klass flags */ -static VALUE -prof_allocation_klass_flags(VALUE self) +static VALUE prof_allocation_klass_flags(VALUE self) { prof_allocation_t* allocation = prof_allocation_get(self); return INT2FIX(allocation->klass_flags); } /* call-seq: source_file -> string Returns the the line number where objects were allocated. */ -static VALUE -prof_allocation_source_file(VALUE self) +static VALUE prof_allocation_source_file(VALUE self) { prof_allocation_t* allocation = prof_allocation_get(self); return allocation->source_file; } /* call-seq: line -> number Returns the the line number where objects were allocated. */ -static VALUE -prof_allocation_source_line(VALUE self) +static VALUE prof_allocation_source_line(VALUE self) { prof_allocation_t* allocation = prof_allocation_get(self); return INT2FIX(allocation->source_line); } /* call-seq: count -> number Returns the number of times this class has been allocated. */ -static VALUE -prof_allocation_count(VALUE self) +static VALUE prof_allocation_count(VALUE self) { prof_allocation_t* allocation = prof_allocation_get(self); return INT2FIX(allocation->count); } /* call-seq: memory -> number Returns the amount of memory allocated. */ -static VALUE -prof_allocation_memory(VALUE self) +static VALUE prof_allocation_memory(VALUE self) { prof_allocation_t* allocation = prof_allocation_get(self); return ULL2NUM(allocation->memory); } /* :nodoc: */ -static VALUE -prof_allocation_dump(VALUE self) +static VALUE prof_allocation_dump(VALUE self) { prof_allocation_t* allocation = DATA_PTR(self); VALUE result = rb_hash_new(); @@ -243,11 +224,10 @@ return result; } /* :nodoc: */ -static VALUE -prof_allocation_load(VALUE self, VALUE data) +static VALUE prof_allocation_load(VALUE self, VALUE data) { prof_allocation_t* allocation = DATA_PTR(self); allocation->object = self; allocation->key = FIX2LONG(rb_hash_aref(data, ID2SYM(rb_intern("key"))));