From: Joel VanderWerf Date: 2006-09-13T13:49:00+09:00 Subject: Re: Wrapping a C Library: Allocation and duplication issues Justin Bonnar wrote: > I'm writing a wrapper for a C library [1] written in a very object > orientated style. All of the "classes" are created with a > librdf_new_classname method which acts almost the same as Ruby's > Class#new. > > The problem I'm having is cleanly separating allocation from > initialization. Since most of the class construction functions require > arguments to initialize the object, I can't define separate alloc and > initialize functions. > > The only option I see is to use Ruby 1.6 style code and define a > Klass.new method for my classes. The problems with this is it prevents > clean subclassing and object cloning. Is there a better way to wrap the > library? > > Thanks, > Justin > > [1] http://librdf.org/ > What about calling librdf_new_classname() from the initialize method, and not doing anything special in the alloc function? README.EXT says: > To define and undefine the `allocate' class method, > > void rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE klass)); > void rb_undef_alloc_func(VALUE klass); > > func have to take the klass as the argument and return a newly > allocated instance. This instance should be empty as possible, > without any expensive (including external) resources. The ary_alloc function for Array, for example, does not allocate storage for the actual C array of VALUE. It only allocates the basic RArray object, with a null ptr. Then rb_ary_initialize() looks at the arguments and allocates storage for ptr, as needed. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407