如何应对红宝石小提琴指针数组 [英] How to deal with array pointers in ruby Fiddle
问题描述
我想从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.
这篇关于如何应对红宝石小提琴指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!