Subject:
Re: [ruby-ffi] Alignment problem when passing values for struct on x86_64-linux
From:
Wayne Meissner
Date:
12/2/09 4:53 AM
To:
ruby-ffi@googlegroups.com

2009/12/2 rhp <ronald.pijnacker@gmail.com>:
> --- ffi_test.rb
> #!/usr/bin/ruby -w
>
> require 'rubygems'
> require 'ffi'
>
> module TestFFI extend FFI::Library
>    ffi_lib 'ffi'
>    attach_function :test, [:pointer, :int, :int, :int], :void
> end
>
> ptr = FFI::MemoryPointer.new :float, 10
> TestFFI.test(ptr, 10, 1, 2)

That won't pass a struct by value on x86_64 (and it only does it on
i386 due to the way arguments are packed).

Instead, you need:

class S < FFI::Struct
  layout :a, :int, :b, :int
end

module TestFFI
  extend FFI::Library
  attach_function :test, [ :pointer, :int, S.by_value ], :void
end

s = S.new
s[:a] = 1
s[:b] = 2
ptr = FFI::MemoryPointer.new :float, 10
TestFFI.test(ptr, 10, s)