diff options
Diffstat (limited to 'vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples')
9 files changed, 252 insertions, 0 deletions
diff --git a/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/getlogin.rb b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/getlogin.rb new file mode 100644 index 0000000..6713021 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/getlogin.rb @@ -0,0 +1,8 @@ +require 'ffi' + +module Foo + extend FFI::Library + ffi_lib FFI::Library::LIBC + attach_function :getlogin, [ ], :string +end +puts "getlogin=#{Foo.getlogin}" diff --git a/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/getpid.rb b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/getpid.rb new file mode 100644 index 0000000..1720635 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/getpid.rb @@ -0,0 +1,8 @@ +require 'ffi' + +module Foo + extend FFI::Library + ffi_lib FFI::Library::LIBC + attach_function :getpid, [ ], :int +end +puts "My pid=#{Foo.getpid}" diff --git a/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/gettimeofday.rb b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/gettimeofday.rb new file mode 100644 index 0000000..864bbb6 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/gettimeofday.rb @@ -0,0 +1,18 @@ +require 'ffi' +require 'rbconfig' + +class Timeval < FFI::Struct + layout tv_sec: :ulong, tv_usec: :ulong +end +module LibC + extend FFI::Library + if FFI::Platform.windows? + ffi_lib RbConfig::CONFIG["LIBRUBY_SO"] + else + ffi_lib FFI::Library::LIBC + end + attach_function :gettimeofday, [ :pointer, :pointer ], :int +end +t = Timeval.new +LibC.gettimeofday(t.pointer, nil) +puts "t.tv_sec=#{t[:tv_sec]} t.tv_usec=#{t[:tv_usec]}" diff --git a/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/hello.rb b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/hello.rb new file mode 100644 index 0000000..f2ccf37 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/hello.rb @@ -0,0 +1,8 @@ +require 'ffi' + +module Foo + extend FFI::Library + ffi_lib FFI::Library::LIBC + attach_function("cputs", "puts", [ :string ], :int) +end +Foo.cputs("Hello, World via libc puts using FFI on MRI ruby") diff --git a/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/hello_ractor.rb b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/hello_ractor.rb new file mode 100644 index 0000000..9a01704 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/hello_ractor.rb @@ -0,0 +1,19 @@ +# See the ffi wiki for how to use FFI with Ractors: +# https://github.com/ffi/ffi/wiki/Ractors + +require 'ffi' + +class Ractor + # compat with Ruby-3.4 and older + alias value take unless method_defined? :value +end + +module Foo + extend FFI::Library + ffi_lib FFI::Library::LIBC + attach_function("cputs", "puts", [ :string ], :int) + freeze +end +Ractor.new do + Foo.cputs("Hello, World via libc puts using FFI in a Ractor") +end.value diff --git a/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/inotify.rb b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/inotify.rb new file mode 100644 index 0000000..018d78c --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/inotify.rb @@ -0,0 +1,60 @@ +require 'ffi' + +module Inotify + extend FFI::Library + ffi_lib FFI::Library::LIBC + class Event < FFI::Struct + layout \ + :wd, :int, + :mask, :uint, + :cookie, :uint, + :len, :uint + end + attach_function :init, :inotify_init, [ ], :int + attach_function :add_watch, :inotify_add_watch, [ :int, :string, :uint ], :int + attach_function :rm_watch, :inotify_rm_watch, [ :int, :uint ], :int + attach_function :read, [ :int, :buffer_out, :uint ], :int + IN_ACCESS=0x00000001 + IN_MODIFY=0x00000002 + IN_ATTRIB=0x00000004 + IN_CLOSE_WRITE=0x00000008 + IN_CLOSE_NOWRITE=0x00000010 + IN_CLOSE=(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) + IN_OPEN=0x00000020 + IN_MOVED_FROM=0x00000040 + IN_MOVED_TO=0x00000080 + IN_MOVE= (IN_MOVED_FROM | IN_MOVED_TO) + IN_CREATE=0x00000100 + IN_DELETE=0x00000200 + IN_DELETE_SELF=0x00000400 + IN_MOVE_SELF=0x00000800 + # Events sent by the kernel. + IN_UNMOUNT=0x00002000 + IN_Q_OVERFLOW=0x00004000 + IN_IGNORED=0x00008000 + IN_ONLYDIR=0x01000000 + IN_DONT_FOLLOW=0x02000000 + IN_MASK_ADD=0x20000000 + IN_ISDIR=0x40000000 + IN_ONESHOT=0x80000000 + IN_ALL_EVENTS=(IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE \ + | IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM \ + | IN_MOVED_TO | IN_CREATE | IN_DELETE \ + | IN_DELETE_SELF | IN_MOVE_SELF) + +end +if $0 == __FILE__ + fd = Inotify.init + puts "fd=#{fd}" + wd = Inotify.add_watch(fd, "/tmp/", Inotify::IN_ALL_EVENTS) + fp = FFI::IO.for_fd(fd) + puts "wfp=#{fp}" + while true + buf = FFI::Buffer.alloc_out(Inotify::Event.size + 4096, 1, false) + ev = Inotify::Event.new buf + ready = IO.select([ fp ], nil, nil, nil) + n = Inotify.read(fd, buf, buf.total) + puts "Read #{n} bytes from inotify fd" + puts "event.wd=#{ev[:wd]} mask=#{ev[:mask]} len=#{ev[:len]} name=#{ev[:len] > 0 ? buf.get_string(16) : 'unknown'}" + end +end diff --git a/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/pty.rb b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/pty.rb new file mode 100644 index 0000000..8b6b885 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/pty.rb @@ -0,0 +1,75 @@ +require 'ffi' + +module PTY + private + module LibC + extend FFI::Library + ffi_lib FFI::Library::LIBC + attach_function :forkpty, [ :buffer_out, :buffer_out, :buffer_in, :buffer_in ], :int + attach_function :openpty, [ :buffer_out, :buffer_out, :buffer_out, :buffer_in, :buffer_in ], :int + attach_function :login_tty, [ :int ], :int + attach_function :close, [ :int ], :int + attach_function :strerror, [ :int ], :string + attach_function :fork, [], :int + attach_function :execv, [ :string, :buffer_in ], :int + attach_function :execvp, [ :string, :buffer_in ], :int + attach_function :dup2, [ :int, :int ], :int + attach_function :dup, [ :int ], :int + end + Buffer = FFI::Buffer + def self.build_args(args) + cmd = args.shift + cmd_args = args.map do |arg| + MemoryPointer.from_string(arg) + end + exec_args = MemoryPointer.new(:pointer, 1 + cmd_args.length + 1) + exec_cmd = MemoryPointer.from_string(cmd) + exec_args[0].put_pointer(0, exec_cmd) + cmd_args.each_with_index do |arg, i| + exec_args[i + 1].put_pointer(0, arg) + end + [ cmd, exec_args ] + end + public + def self.getpty(*args) + mfdp = Buffer.new :int + name = Buffer.new 1024 + # + # All the execv setup is done in the parent, since doing anything other than + # execv in the child after fork is really flakey + # + exec_cmd, exec_args = build_args(args) + pid = LibC.forkpty(mfdp, name, nil, nil) + raise "forkpty failed: #{LibC.strerror(FFI.errno)}" if pid < 0 + if pid == 0 + LibC.execvp(exec_cmd, exec_args) + exit 1 + end + masterfd = mfdp.get_int(0) + rfp = FFI::IO.for_fd(masterfd, "r") + wfp = FFI::IO.for_fd(LibC.dup(masterfd), "w") + if block_given? + yield rfp, wfp, pid + rfp.close unless rfp.closed? + wfp.close unless wfp.closed? + else + [ rfp, wfp, pid ] + end + end + def self.spawn(*args, &block) + self.getpty("/bin/sh", "-c", args[0], &block) + end +end +module LibC + extend FFI::Library + attach_function :close, [ :int ], :int + attach_function :write, [ :int, :buffer_in, :ulong ], :long + attach_function :read, [ :int, :buffer_out, :ulong ], :long +end +PTY.getpty("/bin/ls", "-alR", "/") { |rfd, wfd, pid| +#PTY.spawn("ls -laR /") { |rfd, wfd, pid| + puts "child pid=#{pid}" + while !rfd.eof? && (buf = rfd.gets) + puts "child: '#{buf.strip}'" + end +} diff --git a/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/qsort.rb b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/qsort.rb new file mode 100644 index 0000000..58622c1 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/qsort.rb @@ -0,0 +1,20 @@ +require 'ffi' + +module LibC + extend FFI::Library + ffi_lib FFI::Library::LIBC + callback :qsort_cmp, [ :pointer, :pointer ], :int + attach_function :qsort, [ :pointer, :ulong, :ulong, :qsort_cmp ], :int +end + +p = FFI::MemoryPointer.new(:int, 2) +p.put_array_of_int32(0, [ 2, 1 ]) +puts "ptr=#{p.inspect}" +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) + puts "In block: comparing #{i1} and #{i2}" + i1 < i2 ? -1 : i1 > i2 ? 1 : 0 +end +puts "After qsort #{p.get_array_of_int32(0, 2).join(', ')}" diff --git a/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/qsort_ractor.rb b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/qsort_ractor.rb new file mode 100644 index 0000000..7fe9afa --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/ffi-1.17.4-arm64-darwin/samples/qsort_ractor.rb @@ -0,0 +1,36 @@ +# See the ffi wiki for how to use FFI with Ractors: +# https://github.com/ffi/ffi/wiki/Ractors + +require 'ffi' + +class Ractor + # compat with Ruby-3.4 and older + alias value take unless method_defined? :value +end + +module LibC + extend FFI::Library + ffi_lib FFI::Library::LIBC + callback :qsort_cmp, [ :pointer, :pointer ], :int + attach_function :qsort, [ :pointer, :ulong, :ulong, :qsort_cmp ], :int + + freeze # Freeze the module variables, so that it can be shared across ractors. +end + +p = FFI::MemoryPointer.new(:int, 3) +p.put_array_of_int32(0, [ 2, 3, 1 ]) # Write some unsorted data into the memory +# Ractor.make_shareable(p) # freeze the pointer to be shared between ractors instead of copied +puts "main -ptr=#{p.inspect}" +res = Ractor.new(p) do |p| + puts "ractor-ptr=#{p.inspect}" + puts "Before qsort #{p.get_array_of_int32(0, 3).join(', ')}" + LibC.qsort(p, 3, 4) do |p1, p2| + i1 = p1.get_int32(0) + i2 = p2.get_int32(0) + puts "In block: comparing #{i1} and #{i2}" + i1 < i2 ? -1 : i1 > i2 ? 1 : 0 + end + puts "After qsort #{p.get_array_of_int32(0, 3).join(', ')}" +end.value + +puts "After ractor termination #{p.get_array_of_int32(0, 3).join(', ')}" |
