[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

mark, alloc and free functions in C extensions

Gerardo Santana Gómez Garrido

3/30/2008 11:13:00 AM

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 Santana

3 Answers

Joel VanderWerf

3/30/2008 6:25:00 PM

0

Gerardo Santana 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?
>
> 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.

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.sourceforg...
http://redshift.sourceforg.../lib/cgen/cshadow.html

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Gerardo Santana Gómez Garrido

3/31/2008 3:00:00 PM

0

On 3/30/08, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> Gerardo Santana G=F3mez 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?
> >
> > 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]
> http://redshift.sourceforg...
> http://redshift.sourceforg.../lib/cgen/cshadow.html

That's exactly what I would do :) I'll check it again later.

--
Gerardo Santana

Paul Brannan

3/31/2008 4:11:00 PM

0

On Sun, Mar 30, 2008 at 08:12:38PM +0900, Gerardo Santana 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