PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > mark, alloc and free functions in C extensions
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
mark, alloc and free functions in C extensions

Réponse
 
LinkBack Outils de la discussion
Vieux 30/03/2008, 12h12   #1
Gerardo Santana Gómez Garrido
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 Santana

  Réponse avec citation
Vieux 30/03/2008, 19h24   #2
Joel VanderWerf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: mark, alloc and free functions in C extensions

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.sourceforge.net/cgen
http://redshift.sourceforge.net/cgen...n/cshadow.html

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

  Réponse avec citation
Vieux 31/03/2008, 16h00   #3
Gerardo Santana Gómez Garrido
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: mark, alloc and free functions in C extensions

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.sourceforge.net/cgen
> http://redshift.sourceforge.net/cgen...n/cshadow.html


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

--
Gerardo Santana

  Réponse avec citation
Vieux 31/03/2008, 17h05   #4
Gerardo Santana Gómez Garrido
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: mark, alloc and free functions in C extensions

On 3/31/08, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> 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?="
> <gerardo.santana@gmail.com> 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).


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.

--
Gerardo Santana

  Réponse avec citation
Vieux 31/03/2008, 17h11   #5
Paul Brannan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: mark, alloc and free functions in C extensions

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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h55.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14240 seconds with 13 queries