A couple of Ruby API questions

J

Jon McClintock

Hi Rubyists,

I'm working on wrapping a C library, and I've got a couple of things I
can't quite figure out how to manage:

1) Wrapping functions that take variable-length arguments (varargs). Is
there are standard way to do this? The best I've found is libffi,
which lets you construct the stack frames you need to do this, but it
seems like this would be a problem someone would have solved a while
ago.

2) Wrapping structures with flexible arrays. How do I realloc the
structure held by a VALUE? Or do I have to create an entirely new
Ruby object each time I need to resize the structure?

Thanks,

-Jon
 
T

Tim Hunter

Jon said:
2) Wrapping structures with flexible arrays. How do I realloc the
structure held by a VALUE? Or do I have to create an entirely new
Ruby object each time I need to resize the structure?

If you have created a VALUE by wrapping a structure with Data_Wrap_Struct or
Data_Make_Struct and you need to change the structure that the VALUE wraps,
use the DATA_PTR macro:

VALUE my_obj;
MyStruct my_struct;

my_struct = malloc(sizeof(MyStruct));
DATA_PTR(my_obj) = my_struct;

As far as Ruby is concerned the object is unchanged.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top