diff options
Diffstat (limited to 'samples')
-rw-r--r-- | samples/getpid.rb.orig | 6 | ||||
-rw-r--r-- | samples/qsort.rb | 17 |
2 files changed, 23 insertions, 0 deletions
diff --git a/samples/getpid.rb.orig b/samples/getpid.rb.orig new file mode 100644 index 0000000..a4d194b --- /dev/null +++ b/samples/getpid.rb.orig @@ -0,0 +1,6 @@ +require 'ffi' +module Foo + extend FFI::Library + attach_function :getpid, [ ], :int +end +puts "My pid=#{Foo.getpid do; end}" diff --git a/samples/qsort.rb b/samples/qsort.rb new file mode 100644 index 0000000..167a014 --- /dev/null +++ b/samples/qsort.rb @@ -0,0 +1,17 @@ +require 'ffi' + +module LibC + extend FFI::Library + callback :qsort_cmp, [ :pointer, :pointer ], :int + attach_function :qsort, [ :pointer, :int, :int, :qsort_cmp ], :int +end + +p = MemoryPointer.new(:int, 2) +p.put_array_of_int32(0, [ 2, 1 ]) +puts "Before qsort #{p.get_array_of_int32(0, 2).join(', ')}" +LibC.qsort(p, 2, 4) do |p1, p2| + i1 = p1.get_int32(0) + i2 = p2.get_int32(0) + i1 < i2 ? -1 : i1 > i2 ? 1 : 0 +end +puts "After qsort #{p.get_array_of_int32(0, 2).join(', ')}" |