diff --git a/ext/bio/cgranges/cgranges.c b/ext/bio/cgranges/cgranges.c index 87580e8..903cda8 100644 --- a/ext/bio/cgranges/cgranges.c +++ b/ext/bio/cgranges/cgranges.c @@ -83,6 +83,13 @@ static cgranges_t *get_cgranges(VALUE self) return ptr; } +/* + * Allocates memory for a CGRanges object. + * This is called by rb_define_alloc_func and is responsible for + * memory allocation only, not initialization. + * Raises NoMemoryError if memory allocation fails. + */ + static VALUE cgranges_allocate(VALUE klass) { @@ -94,17 +101,21 @@ cgranges_allocate(VALUE klass) return Qnil; } + // Wrap the C struct in a Ruby object and link with garbage collection return TypedData_Wrap_Struct(klass, &cgranges_type, ptr); } -/* Create a new cgranges object - * - * @return [Bio::CGRanges] +/* + * Initializes the CGRanges object. + * Called after memory is allocated by the allocate function. + * This method sets instance variable @indexed to false. + * @return [Bio::CGRanges] self */ static VALUE cgranges_init(VALUE self) { + // Retrieve the cgranges object cgranges_t *cr = DATA_PTR(self); if (!cr) @@ -113,6 +124,7 @@ cgranges_init(VALUE self) return Qnil; } + // Set the @indexed instance variable to false rb_ivar_set(self, rb_intern("@indexed"), Qfalse); return self;