我想从Ruby的FFI乱动,也就是现在的Ruby标准库的一部分进行切换。
I am trying to switch from Ruby FFI to Fiddle, which is now part of Ruby std lib.
有虽然无证pretty,和我有搞清楚如何处理数组和指针之苦。特别是,我怎么能写一个小提琴接口,这样的C函数:
It is pretty undocumented though, and I am having a hard time in figuring out how to deal with arrays and pointers. In particular, how can I write a Fiddle interface to a C function like this:
void my_func(double *, size_t len)映射入Ruby是pretty容易:
Mapping it into Ruby is pretty easy:
module Test extend Fiddle::Importer dlload './lib/libTest.dylib' extern 'void my_func(double *, size_t)' end不过,我又如何能建立一个指向数组的指针,将作为第一个参数传递?谢谢!
But then how can I build a pointer to an array, to be passed as a first argument? Thanks!
推荐答案你的意思是建立一个数组c,这array`s指针红宝石?
you mean build an c array and this array`s pointer in ruby?
你可以尝试这样的:
free = Fiddle::Function.new(Fiddle::RUBY_FREE, [TYPE_VOIDP], TYPE_VOID) p = Pointer.malloc(SIZEOF_DOUBLE*len, free)和调用是这样的:
my_func(p,len)红宝石GC将调用free函数时,该内存块将不会被使用。
ruby GC will call the free function when this memory block will not be used.
更多推荐
如何应对红宝石小提琴指针数组
发布评论