# Wrapping a C Library: Allocation and duplication issues

**URL:** <https://www.ruby-forum.com/t/wrapping-a-c-library-allocation-and-duplication-issues/68497>\
**Category:** Ruby\
**Created:** [September 13, 2006, 4:53am UTC](https://www.ruby-forum.com/t/wrapping-a-c-library-allocation-and-duplication-issues/68497 "2006-09-13T04:53:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 13, 2006, 4:53am UTC](https://www.ruby-forum.com/t/wrapping-a-c-library-allocation-and-duplication-issues/68497/1 "2006-09-13T04:53:43Z")

</div>

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/](http://librdf.org/)

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 13, 2006, 6:50am UTC](https://www.ruby-forum.com/t/wrapping-a-c-library-allocation-and-duplication-issues/68497/2 "2006-09-13T06:50:22Z")

</div>

Justin B. wrote:

> 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/](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.

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 13, 2006, 9:19am UTC](https://www.ruby-forum.com/t/wrapping-a-c-library-allocation-and-duplication-issues/68497/3 "2006-09-13T09:19:38Z")

</div>

I don’t think that approach will work: librdf\_new\_classname() allocates  
the memory it needs and returns a pointer to the structure. Part of the  
problem is that struct may be different depending on the arguments  
passed in (a particular storage backend, etc) and that the C header  
files don’t define the internals of the structures themselves (so I get  
incomplete type errors during compilation.

-Justin

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 13, 2006, 10:00am UTC](https://www.ruby-forum.com/t/wrapping-a-c-library-allocation-and-duplication-issues/68497/4 "2006-09-13T10:00:21Z")

</div>

Acutally, I’m not a C or Ruby/C guru, but would any of the following be  
possible?

- Wrapping a null pointer in the allocation function, and then modifying  
the DATA object to use the pointer returned by the  
librdf\_new\_classname() call.

- Creating a pointer to the data structure, and then setting it’s value  
to the pointer returned by the librdf\_new\_classname() call.

The following code compiles and runs, but doesn’t seem to work correctly  
(other functions that access the C structure wrapped by the object  
behave strangely.)

static VALUE URI\_alloc(VALUE klass)  
{  
librdf\_uri\* uri;  
return Data\_Wrap\_Struct(klass, 0, librdf\_free\_uri, uri);  
}

static VALUE URI\_initialize(VALUE self, VALUE uri\_string)  
{  
librdf\_uri\* uri;  
Data\_Get\_Struct(self, librdf\_uri, uri);  
uri = librdf\_new\_uri(World, StringValuePtr(uri\_string));  
return self;  
}

Thanks,  
Justin

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 13, 2006, 10:58am UTC](https://www.ruby-forum.com/t/wrapping-a-c-library-allocation-and-duplication-issues/68497/5 "2006-09-13T10:58:42Z")

</div>

> > > > > “J” == Justin B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

J\> The following code compiles and runs, but doesn’t seem to work  
correctly  
J\> (other functions that access the C structure wrapped by the object  
J\> behave strangely.)

Look at ruby-1.8.5/ext/dbm/dbm.c, in the ruby source, to see an example  
how you can do this (fdbm\_alloc(), free\_dbm() and fdbm\_initialize())

Guy Decoux

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 13, 2006, 3:51pm UTC](https://www.ruby-forum.com/t/wrapping-a-c-library-allocation-and-duplication-issues/68497/6 "2006-09-13T15:51:24Z")

</div>

On 9/12/06, Justin B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m writing a wrapper for a C library [1] written in a very object  
> orientated style…

Just curious: Are the Ruby bindings that come with Redland just not  
suitable for your project? I’m wondering why you’re re-inventing the  
wheel here.

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 13, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/wrapping-a-c-library-allocation-and-duplication-issues/68497/7 "2006-09-13T18:38:27Z")

</div>

ts wrote:

> Look at ruby-1.8.5/ext/dbm/dbm.c, in the ruby source, to see an example  
> how you can do this (fdbm\_alloc(), free\_dbm() and fdbm\_initialize())

Thanks, this is exactly what I was looking to due.

Lyle J. wrote:

> Just curious: Are the Ruby bindings that come with Redland just not  
> suitable for your project? I’m wondering why you’re re-inventing the  
> wheel here.

The RDF-Redland binding is outdated and decidedly un-ruby-like. The  
other problem is the SWIG binding for Redland doesn’t expose all of the  
functions I wanted to access. In addition to wrapping Redland I need to  
get at some of the lower level functions from Rasqal.

We’re also planning on building an Ontology-aware level above Redland,  
with goals similar to that of the (now-defunct) Semitar project and want  
to get the lowerlevel code right before working on that. Mixing the  
existing binding with higher-level C code would be a little ugly - we’d  
prefer to keep the lower level code in C.

-Justin

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 13, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/wrapping-a-c-library-allocation-and-duplication-issues/68497/8 "2006-09-13T19:37:58Z")

</div>

On 9/13/06, Justin B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The RDF-Redland binding is outdated and decidedly un-ruby-like. The  
> other problem is the SWIG binding for Redland doesn’t expose all of the  
> functions I wanted to access. In addition to wrapping Redland I need to  
> get at some of the lower level functions from Rasqal.
> 
> We’re also planning on building an Ontology-aware level above Redland,  
> with goals similar to that of the (now-defunct) Semitar project and want  
> to get the lowerlevel code right before working on that. Mixing the  
> existing binding with higher-level C code would be a little ugly - we’d  
> prefer to keep the lower level code in C.

Cool. I hope that you’ll share when you’re done. 😉
