【问题标题】:Ruby FFI to call C function with array of arraysRuby FFI 使用数组调用 C 函数
【发布时间】:2019-09-29 16:44:44
【问题描述】:

我有以下C接口:

int foo(void* bar, void* baz);

基本上,它的作用是获取一个 RGB 值数组,对其进行处理,然后返回一个新的 RGB 值数组。

我为它编写了以下 Ruby FFI 包装器:

module MyLibrary
  extend FFI::Library
  ffi_lib "path/to/mylibrary.so"
  attach_function :foo, [:pointer, :pointer], :int
end

但是,我并没有真正成功地将 Ruby 数组传递给这个 FFI 包装器。在 Ruby 中,我有类似的东西:

pixels = [[3, 34, 123], [32, 253, 34], ..., [1, 1, 34]]
result = [[0, 0, 0], [0, 0, 0], ..., [0, 0, 0]]

# This does not work!
MyLibrary.foo(pixels, result)

我查看了 Ruby FFI 文档,但是我不知道应该如何将 Ruby 数组传递给 FFI 包装器。

【问题讨论】:

    标签: arrays ruby image pointers ffi


    【解决方案1】:

    要将数据传递给您需要使用MemoryPointer 的函数,首先将数据从Ruby 数组复制到其中,以便在C 代码看到它时它的格式正确。使用write_array_of_* 方法之一复制一维数组的数据相当简单。对于多维数组,这有点棘手,您需要将每个数组复制到管理到MemoryPointer 的内存中的正确位置。

    同样,对于函数通过指针返回的数据,您需要提供大小合适的MemoryPointer,然后将数据复制到 Ruby 数组中。同样,这对于使用 read_array_of* 方法的单维数组来说是相当容易的,而对于多维数组来说,这需要更多的工作。

    这是一个简单的例子。这里我假设 C 函数的参数总是由三个三元素 int 数组组成——int[3][3]。

    C 函数:

    int foo(void* bar, void* baz) {
        // assume both arrays are [3][3]
        int(*bar_)[3] = (int (*)[3]) bar;
        int(*baz_)[3] = (int (*)[3]) baz;
    
        // Highly complex processing - double each entry.
        for (int i = 0; i< 3; i++) {
            for (int j = 0; j < 3; j++) {
                baz_[i][j] = 2 * bar_[i][j];
            }
        }
    
        return 0;
    }
    

    这是访问它的 Ruby 代码:

    require 'ffi'
    
    module MyLibrary
      extend FFI::Library
      ffi_lib "path/to/mylibrary.so"
    
      # Give function a different name. You might also want to make
      # it private.
      attach_function(:ffi_foo, :foo, [:pointer, :pointer], :int)
    
      # Wrap the C function with a friendly method that packages
      # and unpackages the data.
      def self.foo(pixels)
        # Create the MemoryPointers for input and output. They are
        # both 9 entry (3 * 3) arrays of uint32.
        input = FFI::MemoryPointer.new(:uint32, 9)
        output = FFI::MemoryPointer.new(:uint32, 9)
    
        # Copy the input data into the input MemoryPointer
        pixels.each_with_index do |ary, idx|
          # The offset here is in bytes. int32 is 4 bytes, each
          # array is three elements so total is 3 * 4 = 12.
          input.put_array_of_int32(idx * 12, ary)
        end
    
        # Call the C function.
        ffi_foo(input, output)
    
        result = []
    
        # Copy the data back into a Ruby array.
        3.times do |idx|
          result << output.get_array_of_int32(idx * 12, 3)
        end
    
        # Return the final result
        result
    
      end
    end
    

    然后你可以像这样使用它:

    pixels = [[3, 34, 123], [32, 253, 34], [1, 1, 34]]
    p MyLibrary.foo(pixels) #=>[[6, 68, 246], [64, 506, 68], [2, 2, 68]]
    

    显然,您需要对其进行调整以匹配您自己函数的详细信息。您可能还应该添加错误检查,否则您可能会遇到段错误。

    【讨论】:

    • 太棒了!感谢您指出了这一点。还有一件事:是否可以使用已经分配的 Ruby 数组,而不是分配 separate 指针并将数组也转储到那里?目前数组被复制两次(输入和输出),如果它们很大,这可能会导致一些显着的开销。
    • @linkyndy 我认为这是不可能的。尽管 Ruby 数组由 C 数组支持(至少在 MRI Ruby 中),但它是一个 Ruby 对象数组,而不是适当的 C 类型。您可以将 MemoryPointer 包装在一个类中并为其提供合适的 API,让您直接构建 C 数组并避免使用 Ruby 数组。
    • 我明白了。似乎太麻烦了,至少现在是这样。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多