[ruby-talk:00502] One question about classes written in C.
From:
Clemens.Hintze@...
Date:
1999-07-16 14:23:15 UTC
List:
ruby-talk #502
Hello,
I have begun (and will hopefully finish it soon) my first extension :-)
It does not do any valueable. Only to learn how things work! I think I
have learned a lot. But one question remained (for yet) ;-)
If I want to implement a new class, it seems to me, that I EVER have
to go the Data_Make_Struct way! Is that correct? I assume the way via
defining a own structure like
#define RPOINT(obj) (R_CAST(pt_RPoint)(obj))
#define T_POINT 0x1FDD
typedef struct pt_RPoint {
struct RBasic basic;
VALUE x;
VALUE y;
} pt_RPoint;
is not very desirable. First, how do I decide, what value T_POINT
should get? Second, if any member of that struct would contain a
memory region allocated via e.g. `ALLOC_N', the garbage collector
wouldn't know, how to mark/free that memory!
Is it true, that this kind of class implementation is only forseen
for builtin classes, not for classes defined via C-written extension?
Let me do some remarks:
1. In his implementation of `frac-1.1', Takaaki Tateishi has used a
macro, which I have missed in `ruby.h'. I have generalized and
renamed it. Now it becomes:
#define IS_A(obj,klass) \
((rb_obj_is_kind_of((obj),(klass))==Qtrue)?1:0)
Could we get it into `ruby.h', please? I feel I will often use
such a test?
2. I think, that the macro ALLOCA_N should only be defined in
`ruby.h' if the function `alloca' is also available. The module
`frac-1.1' e.g. use that macro. There will probably be problems,
if I try to use that extension on a platform without an `alloca'
function.
If you, however, claim that all platforms would have that
function, you could remove the test of `alloca' existance at
whole, couldn't you? ;-)
3. You know, I was very confused about the term "true" garbage
collection. I have read the advertisements again and I think,
that I understand now, how I could got such wrong impression.
May I explain?
Often in the docs it is mentioned that Ruby has "true" GC.
On the script level, that is also true for Python, Perl, Tcl and
the like.
But the difference will be found on the C level! Here Ruby differs
from all these languages. If here, anybody simply speaks from true
GC, I silently assume, that this is also valid for normally
allocated memory. Especially if I have a look into the sources.
Here I often can find something like
ptr = ALLOC_N(<type>, <count>);
AND NEARLY NO CALL TO `free(ptr)'. If I do not know the
dependencies, I really can get the impression, that there is a
"real" GC on work!
So perhaps it would be better, if referring to ruby's GC, to
mention, that the "true" GC is done for all ruby objects. Also
on C level. In other languages one would have to increment or
decrement a reference counter to indicate, if an object is used
yet, or not.
That all should not be an excuse for my dullness, of course! :-)
4. I have another Feature request: Could we think about a way, how
to connect an C variable with an instance variable? If ruby set
that instance variable, the corresponding C variable should also
be set. If the C variable was set, the corr. instance variable
should also be influenced.
I know we have such a beast for global variables. Would that also
be possible for instance variables? Why... hmmm I have e.g.
implemented a new class using C. Now I derive a class written in
Ruby from the C coded one. It would be nice, if the subclass (the
Ruby one), could also access the internal values of the
superclass (the C coded one). Okay I could store the values
also via `rb_ivar_set'. Or only use `rb_ivar_set', but that
seems not very elegant for me.
Okay that was it for now.
I hope, when I have finished my extension, I can publish it in contrib,
as example to show other beginners how to write extensions. I am
currently writing a medium sized README, in which I try to explain some
experiences I have made during that trip. I hope that you and others
could read it and propose modifications or correct things, where I was
wrong!
Ciao,
\cle