mruby/mruby — pull request #837 by masamitsu-murase (2013-02-11 13:45:23+00:00)
Title: Add dmark and dmarksize to manage `RData` object.

I suggest to add `dmark` and `dmarksize` to `mrb_data_type` structure.
`dmark` is called in `gc_mark_children` to mark children of `RData`.
`dmarksize` returns the number of the children to be marked.

In the current implementation, if `RData` object has children, we have to add them to `iv` hash to prevent them from GC.
This patch adds `dmark` and `dmarksize` to manage children of `RData` object.

Here is the sample usage.
```c
struct mrb_my_regexp
{
struct RString *str; // mruby object.
struct regexp_helper_structure *reg; // arbitary data.
};

static void
my_regexp_free(mrb_state *mrb, void *ptr)
{
if (ptr){
struct mrb_my_regexp *p = (struct mrb_my_regexp *)ptr;

// 'str' is freed automatically because it is a mruby object.
p->str = NULL;

// We are responsible for the management of 'reg'
// because 'reg' is *not* a mruby object.
mrb_free(mrb, p->reg);
p->reg = NULL;
}
mrb_free(mrb, ptr);
}

static void
my_regexp_mark(mrb_state *mrb, void *ptr)
{
if (ptr){
struct mrb_my_regexp *p = (struct mrb_my_regexp *)ptr;
if (p->str){
// Mark str to keep from GC.
mrb_gc_mark(mrb, (struct RBasic *)(p->str));
}
}
}

static size_t
my_regexp_mark_size(mrb_state *mrb, void *ptr)
{
if (ptr){
struct mrb_my_regexp *p = (struct mrb_my_regexp *)ptr;
if (p->str){
return 1;
}
}
return 0;
}

static struct mrb_data_type mrb_my_regexp_type = {
"MyRegexp",
my_regexp_free,
my_regexp_mark, // NEW!
my_regexp_mark_size // NEW!
};
```
In this sample, we do not have to add `str` to `iv` hash and access it easily.