Questions about GC, memory management and C extensions

Z

Zed A. Shaw

So I've been doing C extensions for a while now, and there's always been
some nagging questions in my mind about how C extensions mesh with the
GC in Ruby. I've either seen conflicting documentation or no
documentation on certain subjects. I'd like to get some definitive
answers to the following myths or questions.

1. Not providing a mark function to Data_Make_Object or
Data_Wrap_Object means your DATA_PTR memory is not garbage collected.
True or false? My tests say false, but I'm not sure.

2. Using rb_gc_register/rb_gc_unregister is expensive because they are
put into a linked list. True or false?

3. Is there a way to map from my DATA_PTR to the object it is connected
with? Seems like the wisdom is you need your own internal look-up for
this.

4. Can I set a bunch of VALUE variables based on rb_intern("blah") once
during Init(), or do I have to constantly call rb_intern to prevent my
"blah" internal from moving on me? In otherwords, do the interns move
around after they are created?

5. Many C libraries (like libevent) allocate their own memory for
structures and initialize in one shot, but the Data_Wrap_Object macros
and other functions seem to require pre-existing memory. This leads to
a contradiction where I must allocate the libevent structure in my
_alloc method, but initialize it in my _initialize method which I can't
do since allocation and initialization are done in one call. Is there a
way to create, initialize, and attach C library structures inside the
_initialize method only?

6. Is there a way to "protect" ruby objects from moving, collection, or
other "evil" workings while they are needed inside a C extension? My
concrete example is when a handler object is registered inside the
libevent dispatch loop. I want libevent to tell the GC to go to hell
and libevent keeps the object until it is done. Right now I just have
to tell the users to keep their handler objects around, which leads to
all sorts of nastiness.

I think that will do for now. If anyone can give some good feedback on
this then I'll be a very happy camper.

Zed A. Shaw
http://www.zedshaw.com/
 
E

evanwebb

A few answers:

1. GC does nothing with the DATA_PTR. The mark function provides the
owner of the Data object (you) the ability to do additional marking of
objects during the mark phase. The primo example of this is that the
struct you've stuffed into the DATA_PTR of your object contains a VALUE
element that is a ruby object (perhaps, say, a handler object). You
want to make sure that the GC knows that this object is still live, so
you'd call gc_mark(my_struct->element) within your mark function. This
also anwsers #6, how to protect objects from the GC. Remember, the GC
is trying to find all live objects, so if you've got a live object that
the GC can't see any other way, you need to mark it.

2. Yes, they go into a linked list. They are as expensive as a linked
list.

3. No, there is no mechanism to map something stuffed into a DATA_PTR
back to the object it was stuffed into. The reason is that ruby doesnt
want to know what you put in a DATA_PTR, thats your business, not it's.
You can easily (and nicely) have many objects that have the same struct
stuffed into their DATA_PTR.

4. Yes, you can call rb_intern() within an Init safely. The behavior of
rb_intern is as they say, deterministic during all phases of runtime.

rb_intern("blah") == rb_intern("blah") is always true.

5. A common way to tackle this problem is to define the method "new" on
your class, where that function creates and initializes a new instance,
then returns it. I kind of remember matz and friends saying that this
was a bad practice, but I don't know what the other method is (other
than what you've already got).

6. See #1

Hope this helps!
 
M

Matthew Desmarais

I've been poking around OpenOffice tonight and I noticed that the Python
people have managed to get Python more-or-less integrated into
OpenOffice 2.0. Does anyone know of an effort to get Ruby a similar
deal (OOo bindings and such)?

I'm relatively new to the list so I might have missed some discussion on
the topic. Be gentle.

Thanks,
mattD
 
J

James Britt

Matthew said:
I've been poking around OpenOffice tonight and I noticed that the Python
people have managed to get Python more-or-less integrated into
OpenOffice 2.0. Does anyone know of an effort to get Ruby a similar
deal (OOo bindings and such)?

Not exactly. I started a project to allow pure-Ruby manipulation of OOo
files (OOo4R), which I tend to work on when I need it to do something
in particular.

See http://www.neurogami.com/jamesbritt.html

Calling Ruby from OOo would be quite nice, but I'm unaware of anyone
doing this.


James

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
 
M

Matthew Desmarais

James said:
Not exactly. I started a project to allow pure-Ruby manipulation of
OOo files (OOo4R), which I tend to work on when I need it to do
something in particular.
Calling Ruby from OOo would be quite nice, but I'm unaware of anyone
doing this.
Thanks James. I'm going to take a closer look at OOo4R. It seems a
better than screwing around with that darn ole stuff some more.

I've been involved in some stuff at work with Excel using VBA that's
taken things just _way_ too far. I'd like to start looking at
re-implementing some of the stuff we've done in OOo, but I'd really like
to be inside the OOo process. Not to mention that using another form of
basic is not appealing.

I think that something like this could be a huge step for Ruby. If
anyone else has any information on the Python uno bindings or anything
else related I'd love to hear it.
 
M

Matthew Desmarais

James said:
Not exactly. I started a project to allow pure-Ruby manipulation of
OOo files (OOo4R), which I tend to work on when I need it to do
something in particular.
Calling Ruby from OOo would be quite nice, but I'm unaware of anyone
doing this.
Thanks James. I'm going to take a closer look at OOo4R. It seems a
better than screwing around with that darn ole stuff some more.

I've been involved in some stuff at work with Excel using VBA that's
taken things just _way_ too far. I'd like to start looking at
re-implementing some of the stuff we've done in OOo, but I'd really like
to be inside the OOo process. Not to mention that using another form of
basic is not appealing.

I think that something like this could be a huge step for Ruby. If
anyone else has any information on the Python uno bindings or anything
else related I'd love to hear it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top