rubyからffiを使ってみた

18
「安心・安全・安定・信頼」できるインターネットサービスを 株式会社クルウィット 井澤 志充 RubyからFFIを使ってみた 2014/3/15 (Sat) Kanazawa.rb #19 @Cafe? IKAGAWA DO

Upload: yukimitsu-izawa

Post on 01-Jul-2015

1.372 views

Category:

Technology


4 download

DESCRIPTION

Using Ruby FFI (A Portable Foreign Function Interface Library)

TRANSCRIPT

  • 1. RubyFFI 2014/3/15 (Sat) Kanazawa.rb #19 @Cafe? IKAGAWA DO

2. () () !2 () @Yukimitsu_Izawa 3. () () !2 () @Yukimitsu_Izawa 4. FFI FFI (A Portable Foreign Function Interface Library) ! lib https://sourceware.org/lib/ ! Python, Java, javascript, Common Lisp, Scheme, Ruby !3 5. ruby- https://github.com// ! URL: Intuitive DSL Support All C native types C structs (also nested), enums and global variables Callbacks from C to ruby Automatic garbage collection of native memory ! FFIruby !4 6. install ! % gem install ffi Its very simple :) !5 SWIGnative code compile*.bundle 7. getprotobynumber(3) ruby /etc/protocols !6 (man) LIBRARY Standard C Library (libc, -lc) ! SYNOPSIS struct protoent * getprotobynumber(int proto); ! struct protoent { char *p_name; /* official name of protocol */ char **p_aliases; /* alias list */ int p_proto; /* protocol number */ }; 8. module Libc extend FFI::Library ffi_lib FFI::Library::LIBC # FFI::Library::LIBC #=> "libc.dylib" # or ffi_lib 'c' # or ffi_lib "/usr/lib/libc.dylib" ! attach_function :getprotobynumber, [:int], :pointer # or attach_function :my_func_name, :getprotobynumber, [:int], :pointer class Protoent < FFI::Struct layout( :p_name, :string, :p_aliases, :pointer, :p_proto, :int ) end end !7 9. () : protoent = Libc.getprotobynumber(41) myent = Libc::Protoent.new(protoent) p myent[:p_name] ! #=> "ipv6" !8 10. () : protoent = Libc.getprotobynumber(41) myent = Libc::Protoent.new(protoent) p myent[:p_name] ! #=> "ipv6" !8 11. !9 :char and :uchar - 8 bit signed and unsigned values :short and :ushort - 16 bit signed and unsigned values :int and :uint - 32 bit signed and unsigned values :long_long and :ulong_long - 64 bit signed and unsigned values :string - C string, NULL terminated. :pointer - a C pointer :bool , :size_t, :in_addr_t : https://github.com///wiki/Types 12. native libraryFFI ! getprotobynumber(3) ! Web() !10 13. One more thing USB !11 14. USB !12 SSD ( R:260MB/s, W:240MB/s) 15. USB % ls -l FreeBSD-10.0-RELEASE-amd64-dvd1.iso -rw-rr-- 1 izawa staff 2484742144 3 14 23:52 ! % time cp FreeBSD-10.0-RELEASE-amd64-dvd1.iso /Volumes/SANDISK ! 0.00s user 1.72s system 16% cpu 10.364 total !12 SSD ( R:260MB/s, W:240MB/s) 16. USB % ls -l FreeBSD-10.0-RELEASE-amd64-dvd1.iso -rw-rr-- 1 izawa staff 2484742144 3 14 23:52 ! % time cp FreeBSD-10.0-RELEASE-amd64-dvd1.iso /Volumes/SANDISK ! 0.00s user 1.72s system 16% cpu 10.364 total !12 SSD ( R:260MB/s, W:240MB/s) 2484742144 10.364 239747408.72250096487842531840 239.7MB/s 17. VM ! Sandisk USB3.0 USB 128GB Extreme PRO Read 260MB/s SDCZ88-128G 20,000 !13 18. Thank You! If you have any comments, please send to: Mail: [email protected] / [email protected] Twitter: @Yukimitsu_Izawa !14