如何在Ruby FFI gem中处理Ruby数组?

7

我希望使用Ruby FFI宝石调用一个具有数组作为输入变量和输出为数组的C函数。也就是说,C函数看起来像这样:

double *my_function(double array[], int size)

我已经创建了以下Ruby绑定:

module MyModule
  extend FFI::Library
  ffi_lib 'c'
  ffi_lib 'my_c_lib'
  attach_function :my_function, [:pointer, int], :pointer

我想在Ruby代码中发起一次电话:

result_array = MyModule.my_function([4, 6, 4], 3)

我该如何开始处理这个问题呢?

1个回答

9
假设这是您希望在Ruby脚本中使用的库文件,我们称其为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

现在,它已经可以在你的Ruby代码(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
FFI::MemoryPointer类使用自动垃圾回收来分配本机内存。当一个MemoryPointer超出其范围时,内存将会被释放,作为垃圾回收过程的一部分。
因此,您不需要直接调用pointer.free。另外,为了检查是否必须手动释放result_pointer,我在提取数组并打印后调用了result_pointer.free,并得到了这个警告。
warning: calling free on non allocated pointer #<FFI::Pointer address=0x007fd32b611ec0>

看起来你不需要手动释放result_pointer


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接