Hashing VALUES to C-Structs

(Brian Schröder) #1

Hello Group,

in an extension I'm writing I'd like to hash ruby objects to
c-structs. I'd like to do this without wrapping the c-structs again in
ruby objects, because that would double the number of object creations
and GC actions. I could take the st_hash and partially redo ruby's
hash wrapping to achieve what I want, but maybe there is a nicer way
to do this?

I want my hash to behave like a ruby hash regarding wrapping of string
arguments etc. That means I do not simply want to hash VALUES to void*
pointers but first freeze strings an do the things ruby does (if
possible without copying the code in hash.c, such that my extension
will always be in sync with ruby's behaviour.)

best regards,

Brian

···

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

(Joel VanderWerf) #2

Brian Schröder wrote:

Hello Group,

in an extension I'm writing I'd like to hash ruby objects to
c-structs. I'd like to do this without wrapping the c-structs again in
ruby objects, because that would double the number of object creations
and GC actions. I could take the st_hash and partially redo ruby's
hash wrapping to achieve what I want, but maybe there is a nicer way
to do this?

I want my hash to behave like a ruby hash regarding wrapping of string
arguments etc. That means I do not simply want to hash VALUES to void*
pointers but first freeze strings an do the things ruby does (if
possible without copying the code in hash.c, such that my extension
will always be in sync with ruby's behaviour.)

Not sure I understand, but what about using a ruby Hash whose values are
ruby Integers that happen to be the addresses of your c-structs? To
access from C, all you have to do is use NUM2ULONG() to convert an
Integer hash value to the pointer. Of course, if the address doesn't fit
in a Fixnum, there will be an extra allocation anyway...

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407