Garbage Collection: Marking RData ptr

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

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.


[2] RDF Triple (librdf_statement)
[3] RDF term (librdf_node)

“J” == Justin B. [email protected] writes:

J> The safe (and naive) approach is to copy anything going into or out
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
be used by the caller after this call.


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

Guy Decoux

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 :slight_smile: