Garbage Collection: Marking RData ptr

J

Justin Bonnar

I'm working on creating a binding for an object-based library[1] and am
trying to find a way to make it work well with the garbage collector.
Constructor functions from the library return a pointer to their data
structure, making it easy to wrap in a Ruby object using
Data_Wrap_Struct.

The problem I'm having is when one of the C objects takes responsibility
for it's associated object(s). For example, a Statement[2] has three
member Nodes[3]. When the statement's free function is called, it will
also free the associated nodes. There are functions to get and set each
of these nodes.

The problem I'm having is marking the associated nodes so the garbage
collector won't prematurely free them. There may be zero or more Ruby
objects wrapping each one of the nodes, if any one of these is destroyed
the statement will enter an undefined state.

I'd like to define a mark function for the statement's that will mark an
RData object pointing to a specific pointer. As I understand it,
rb_gc_mark will only mark a VALUE, is there a good way to do this?

An alternative I'm considering would be to create my own struct that
points to the librdf_statement as well as VALUEs for each of the nodes.
I'd have to write helper functions to make sure the VALUE is updated if
the statement is changed externally, but I think it should work.

The safe (and naive) approach is to copy anything going into or out of
the statement, but I'd rather not do that.

Thanks,
Justin

[1] http://librdf.org/
[2] http://librdf.org/docs/api/redland-statement.html
[3] http://librdf.org/docs/api/redland-node.html
 
T

ts

J> The safe (and naive) approach is to copy anything going into or out of
J> the statement, but I'd rather not do that.

Well, what do you do is someone re-use a node after it was set in the
statement ?

You have

The object passed in becomes owned by the statement object and must not
^^^^^^^^
be used by the caller after this call.
^^^^^^^

and

This method returns a SHARED pointer to the object which must be copied by
^^^^^^^^^^^^^^
the caller if needed.
 
J

Justin Bonnar

Redland nodes are immutable so it's not really a problem. I just have
to make sure that any object that takes possession of or depends on a
node makes a copy for itself. In practice this is just statements, for
which I have to do this anyway.

I was looking for a more efficient way to provide access to the the
nodes in a statement than copying the underlying datastructure and
rewrapping it on every access. I think I'll leave it as is for now and
if it becomes a performance problem I can cache the nodes in the object.

Unless of course there's away to mark all RData objects wrapping a
particular pointer :)

-Justin
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top