如何处理红宝石ffi宝石中的红宝石阵列? [英] How do I handle ruby arrays in ruby ffi gem?
问题描述
我想使用ruby ffi gem调用一个c函数,该函数具有一个数组作为输入变量,而输出是一个数组.也就是说,c函数如下所示:
I want to use the ruby ffi gem to call a c function which has an array as an input variable and the output is an array. That is, the c function looks like:
double *my_function(double array[], int size)
我创建的红宝石绑定为:
I have created the ruby binding as:
module MyModule
extend FFI::Library
ffi_lib 'c'
ffi_lib 'my_c_lib'
attach_function :my_function, [:pointer, int], :pointer
我想用以下类似的红宝石代码拨打电话:
I will like to make a call in ruby code like:
result_array = MyModule.my_function([4, 6, 4], 3)
我该怎么办?
推荐答案
假设这是您希望在ruby脚本中使用的库,将其命名为my_c_lib.c
:
Let's say that this is the library you wish to use in your ruby script, call it my_c_lib.c
:
#include <stdlib.h>
double *my_function(double array[], int size)
{
int i = 0;
double *new_array = malloc(sizeof(double) * size);
for (i = 0; i < size; i++) {
new_array[i] = array[i] * 2;
}
return new_array;
}
您可以像这样编译它:
$ gcc -Wall -c my_c_lib.c -o my_c_lib.o
$ gcc -shared -o my_c_lib.so my_c_lib.o
现在,可以在您的红宝石代码(my_c_lib.rb
)中使用它了:
Now, it's ready to use in in your ruby code (my_c_lib.rb
):
require 'ffi'
module MyModule
extend FFI::Library
# Assuming the library files are in the same directory as this script
ffi_lib "./my_c_lib.so"
attach_function :my_function, [:pointer, :int], :pointer
end
array = [4, 6, 4]
size = array.size
offset = 0
# Create the pointer to the array
pointer = FFI::MemoryPointer.new :double, size
# Fill the memory location with your data
pointer.put_array_of_double offset, array
# Call the function ... it returns an FFI::Pointer
result_pointer = MyModule.my_function(pointer, size)
# Get the array and put it in `result_array` for use
result_array = result_pointer.read_array_of_double(size)
# Print it out!
p result_array
这是运行脚本的结果:
$ ruby my_c_lib.rb
[8.0, 12.0, 8.0]
有关内存管理的说明...来自文档 https://github.com/ffi /ffi/wiki/Pointers :
A note on memory management...from the docs https://github.com/ffi/ffi/wiki/Pointers:
FFI :: MemoryPointer类分配具有自动垃圾收集功能的本机内存作为甜味剂.当MemoryPointer超出范围时,将在垃圾回收过程中释放内存.
The FFI::MemoryPointer class allocates native memory with automatic garbage collection as a sweetener. When a MemoryPointer goes out of scope, the memory is freed up as part of the garbage collection process.
因此您不必直接致电pointer.free
.另外,只是为了检查是否必须手动释放result_pointer
,我在打印提取数组后调用了result_pointer.free
并得到了此警告
So you shouldn't have to call pointer.free
directly. Also, just to check whether you had to manually free result_pointer
, I called result_pointer.free
after printing extracting the array and got this warning
warning: calling free on non allocated pointer #<FFI::Pointer address=0x007fd32b611ec0>
所以看起来您也不必手动释放result_pointer
.
So it looks like you don't have to manually free result_pointer
either.
这篇关于如何处理红宝石ffi宝石中的红宝石阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!