Mark, alloc and free functions in C extensions

I have a class A written in C that contains a C structure. I have
defined the functions mark, alloc and free.

I have another class B, written in C, that derives from class A and
adds another piece of C data.

Should the class B define mark, alloc and free functions for both,
class A and class B, C structures?

Or sould the class B care for its own C structure only? Will Ruby’s
marking, allocation and liberation mechanism call their respective
functions?

I got confused. Thanks in advance.

Gerardo S. Gómez Garrido wrote:

marking, allocation and liberation mechanism call their respective
functions?

I got confused. Thanks in advance.

Class B needs to take care of both the A and B members, somehow, in its
mark, free, and alloc. Ruby doesn’t know anything about how the structs
are related.

Btw, my cgen project[1] is a framework that supports defining classes
with C structs that inherit all the housekeeping stuff, not just
mark/free/alloc, but also persistence and accessors:

[1]
http://redshift.sourceforge.net/cgen
http://redshift.sourceforge.net/cgen/lib/cgen/cshadow.html

Hi,

In message “Re: mark, alloc and free functions in C extensions”
on Sun, 30 Mar 2008 20:12:38 +0900,
“=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=”
[email protected] writes:

|I have a class A written in C that contains a C structure. I have
|defined the functions mark, alloc and free.
|
|I have another class B, written in C, that derives from class A and
|adds another piece of C data.
|
|Should the class B define mark, alloc and free functions for both,
|class A and class B, C structures?
|
|Or sould the class B care for its own C structure only? Will Ruby’s
|marking, allocation and liberation mechanism call their respective
|functions?

It’s hard to say for sure without seeing your code, but basically your
functions for class B should handle whole body of C structure (both A
and B).

          matz.

On 3/30/08, Joel VanderWerf [email protected] wrote:

Or sould the class B care for its own C structure only? Will Ruby’s
marking, allocation and liberation mechanism call their respective
functions?

I got confused. Thanks in advance.

Class B needs to take care of both the A and B members, somehow, in its
mark, free, and alloc. Ruby doesn’t know anything about how the structs
are related.

I see. This C structure would contain pointers to database resources
that I need to set free. The derived class would add another pointer.
If I understood you correctly, I need to take care of all of them in
the derived class’ functions.

Thanks Joel

Btw, my cgen project[1] is a framework that supports defining classes
with C structs that inherit all the housekeeping stuff, not just
mark/free/alloc, but also persistence and accessors:

[1]
CGenerator
http://redshift.sourceforge.net/cgen/lib/cgen/cshadow.html

That’s exactly what I would do :slight_smile: I’ll check it again later.

On 3/31/08, Yukihiro M. [email protected] wrote:

|I have another class B, written in C, that derives from class A and
functions for class B should handle whole body of C structure (both A
and B).

Thank you Matz.

Right now, I’ve coded both classes separately, thus duplicating code.
That’s why it occurred to me to derive one from the other.

Most of the C structure is shared among both classes, except for one
field.

This field, and another one that is common to both classes, are
pointers to database resources that must be allocated at
initialization and freed when the object is destroyed.

I wasn’t sure that, for example, in the free function, I may free only
the derived class’ database resource and expect that the free function
of the base class frees the other database resource and the structure
itself.

Or, that at initialization, I could expect that, after calling super,
the derived class can be confident that the C structure and the first
database resource were already allocated, and allocate the other
database resource.

On Sun, Mar 30, 2008 at 08:12:38PM +0900, Gerardo S. G?mez Garrido
wrote:

I have a class A written in C that contains a C structure. I have
defined the functions mark, alloc and free.

I have another class B, written in C, that derives from class A and
adds another piece of C data.

Should the class B define mark, alloc and free functions for both,
class A and class B, C structures?

Mark and free functions for data objects are per-object, not per-class.

How you implement mark and free is up to you, but ruby will only call
one mark function per object per GC run and will only call one free
function per object when it is being destroyed.

You can implement B’s mark function in terms of A’s mark function if B
contains an A:

struct B
{
struct A base;
}

void mark_B(struct B * b)
{
mark_A(&b->base);
-or-
mark_A((struct A *)b);
}

Paul