Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data_wrap_struct and mark function

Im writing a Ruby extension and Im using the function Data_wrap_struct.

In order to participate in Ruby's mark-and-sweep garbage collection process, I need to define a routine to free my structure, and a routine to mark any references from my structure to other structures. I pass the classic free function to free the memory but I dont know how to use a mark function.

my structs sound like this

typedef struct
{
  int x;
  int y;
} A;

typedef struct
{
  A collection[10];
  int current;
} B;

I think that I need a mark function to mark the references in collection of struct B.

Someone can show me a example to see how a mark function works?

like image 926
Pioz Avatar asked Dec 06 '11 11:12

Pioz


1 Answers

The mark function is used to mark any Ruby objects that your C structure owns.

typedef struct {
    VALUE ruby_object;
} MyStruct;

void mark(void * p) {
    /* p is the wrapped pointer to the MyStruct instance */
    MyStruct * my_struct = (MyStruct *) p;
    rb_gc_mark(my_struct->ruby_object);
}

If the object owned by your structure isn't marked, the garbage collector could sweep it and your code may end up trying to use a finalized object.

like image 114
Matheus Moreira Avatar answered Sep 21 '22 13:09

Matheus Moreira