# Mark, alloc and free functions in C extensions

**URL:** <https://www.ruby-forum.com/t/mark-alloc-and-free-functions-in-c-extensions/133339>\
**Category:** Ruby\
**Created:** [March 30, 2008, 1:13pm UTC](https://www.ruby-forum.com/t/mark-alloc-and-free-functions-in-c-extensions/133339 "2008-03-30T13:13:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c37758/32.png) [@Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/u/Gerardo_SSGSSS_mez)\
**Post date:** [March 30, 2008, 1:13pm UTC](https://www.ruby-forum.com/t/mark-alloc-and-free-functions-in-c-extensions/133339/1 "2008-03-30T13:13:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c37758/32.png) [@Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/u/Gerardo_SSGSSS_mez)\
**Post date:** [March 30, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/mark-alloc-and-free-functions-in-c-extensions/133339/2 "2008-03-30T20:27:50Z")

</div>

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)  
[http://redshift.sourceforge.net/cgen/lib/cgen/cshadow.html](http://redshift.sourceforge.net/cgen/lib/cgen/cshadow.html)

---

<div class="post-metadata">

**Author:** ![Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c37758/32.png) [@Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/u/Gerardo_SSGSSS_mez)\
**Post date:** [March 31, 2008, 8:33am UTC](https://www.ruby-forum.com/t/mark-alloc-and-free-functions-in-c-extensions/133339/3 "2008-03-31T08:33:34Z")

</div>

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?=”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

```

---

<div class="post-metadata">

**Author:** ![Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c37758/32.png) [@Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/u/Gerardo_SSGSSS_mez)\
**Post date:** [March 31, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/mark-alloc-and-free-functions-in-c-extensions/133339/4 "2008-03-31T17:00:56Z")

</div>

On 3/30/08, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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)  
> [http://redshift.sourceforge.net/cgen/lib/cgen/cshadow.html](http://redshift.sourceforge.net/cgen/lib/cgen/cshadow.html)

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

---

<div class="post-metadata">

**Author:** ![Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c37758/32.png) [@Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/u/Gerardo_SSGSSS_mez)\
**Post date:** [March 31, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/mark-alloc-and-free-functions-in-c-extensions/133339/5 "2008-03-31T18:06:40Z")

</div>

On 3/31/08, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c37758/32.png) [@Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/u/Gerardo_SSGSSS_mez)\
**Post date:** [March 31, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/mark-alloc-and-free-functions-in-c-extensions/133339/6 "2008-03-31T18:11:35Z")

</div>

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
