alinta-ffi 1.9.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +49 -0
  3. data/LICENSE +24 -0
  4. data/README.md +112 -0
  5. data/Rakefile +243 -0
  6. data/ext/ffi_c/AbstractMemory.c +1109 -0
  7. data/ext/ffi_c/AbstractMemory.h +175 -0
  8. data/ext/ffi_c/ArrayType.c +162 -0
  9. data/ext/ffi_c/ArrayType.h +59 -0
  10. data/ext/ffi_c/Buffer.c +365 -0
  11. data/ext/ffi_c/Call.c +517 -0
  12. data/ext/ffi_c/Call.h +110 -0
  13. data/ext/ffi_c/ClosurePool.c +283 -0
  14. data/ext/ffi_c/ClosurePool.h +57 -0
  15. data/ext/ffi_c/DataConverter.c +91 -0
  16. data/ext/ffi_c/DynamicLibrary.c +339 -0
  17. data/ext/ffi_c/DynamicLibrary.h +98 -0
  18. data/ext/ffi_c/Function.c +998 -0
  19. data/ext/ffi_c/Function.h +87 -0
  20. data/ext/ffi_c/FunctionInfo.c +271 -0
  21. data/ext/ffi_c/LastError.c +184 -0
  22. data/ext/ffi_c/LastError.h +47 -0
  23. data/ext/ffi_c/LongDouble.c +63 -0
  24. data/ext/ffi_c/LongDouble.h +51 -0
  25. data/ext/ffi_c/MappedType.c +168 -0
  26. data/ext/ffi_c/MappedType.h +59 -0
  27. data/ext/ffi_c/MemoryPointer.c +197 -0
  28. data/ext/ffi_c/MemoryPointer.h +53 -0
  29. data/ext/ffi_c/MethodHandle.c +358 -0
  30. data/ext/ffi_c/MethodHandle.h +55 -0
  31. data/ext/ffi_c/Platform.c +129 -0
  32. data/ext/ffi_c/Platform.h +45 -0
  33. data/ext/ffi_c/Pointer.c +508 -0
  34. data/ext/ffi_c/Pointer.h +63 -0
  35. data/ext/ffi_c/Struct.c +829 -0
  36. data/ext/ffi_c/Struct.h +106 -0
  37. data/ext/ffi_c/StructByReference.c +190 -0
  38. data/ext/ffi_c/StructByReference.h +50 -0
  39. data/ext/ffi_c/StructByValue.c +150 -0
  40. data/ext/ffi_c/StructByValue.h +55 -0
  41. data/ext/ffi_c/StructLayout.c +698 -0
  42. data/ext/ffi_c/Thread.c +352 -0
  43. data/ext/ffi_c/Thread.h +95 -0
  44. data/ext/ffi_c/Type.c +397 -0
  45. data/ext/ffi_c/Type.h +62 -0
  46. data/ext/ffi_c/Types.c +139 -0
  47. data/ext/ffi_c/Types.h +89 -0
  48. data/ext/ffi_c/Variadic.c +304 -0
  49. data/ext/ffi_c/compat.h +78 -0
  50. data/ext/ffi_c/extconf.rb +71 -0
  51. data/ext/ffi_c/ffi.c +98 -0
  52. data/ext/ffi_c/libffi.bsd.mk +40 -0
  53. data/ext/ffi_c/libffi.darwin.mk +105 -0
  54. data/ext/ffi_c/libffi.gnu.mk +32 -0
  55. data/ext/ffi_c/libffi.mk +18 -0
  56. data/ext/ffi_c/libffi.vc.mk +26 -0
  57. data/ext/ffi_c/libffi.vc64.mk +26 -0
  58. data/ext/ffi_c/rbffi.h +57 -0
  59. data/ext/ffi_c/rbffi_endian.h +59 -0
  60. data/ext/ffi_c/win32/stdbool.h +8 -0
  61. data/ext/ffi_c/win32/stdint.h +201 -0
  62. data/ffi.gemspec +23 -0
  63. data/gen/Rakefile +30 -0
  64. data/lib/ffi.rb +20 -0
  65. data/lib/ffi/autopointer.rb +203 -0
  66. data/lib/ffi/buffer.rb +4 -0
  67. data/lib/ffi/callback.rb +4 -0
  68. data/lib/ffi/enum.rb +296 -0
  69. data/lib/ffi/errno.rb +43 -0
  70. data/lib/ffi/ffi.rb +44 -0
  71. data/lib/ffi/io.rb +62 -0
  72. data/lib/ffi/library.rb +590 -0
  73. data/lib/ffi/managedstruct.rb +84 -0
  74. data/lib/ffi/memorypointer.rb +1 -0
  75. data/lib/ffi/platform.rb +164 -0
  76. data/lib/ffi/platform/aarch64-linux/types.conf +104 -0
  77. data/lib/ffi/platform/arm-linux/types.conf +104 -0
  78. data/lib/ffi/platform/i386-cygwin/types.conf +3 -0
  79. data/lib/ffi/platform/i386-darwin/types.conf +100 -0
  80. data/lib/ffi/platform/i386-freebsd/types.conf +152 -0
  81. data/lib/ffi/platform/i386-gnu/types.conf +107 -0
  82. data/lib/ffi/platform/i386-linux/types.conf +103 -0
  83. data/lib/ffi/platform/i386-netbsd/types.conf +126 -0
  84. data/lib/ffi/platform/i386-openbsd/types.conf +128 -0
  85. data/lib/ffi/platform/i386-solaris/types.conf +122 -0
  86. data/lib/ffi/platform/i386-windows/types.conf +105 -0
  87. data/lib/ffi/platform/ia64-linux/types.conf +104 -0
  88. data/lib/ffi/platform/mips-linux/types.conf +102 -0
  89. data/lib/ffi/platform/mips64el-linux/types.conf +104 -0
  90. data/lib/ffi/platform/mipsel-linux/types.conf +102 -0
  91. data/lib/ffi/platform/powerpc-aix/types.conf +180 -0
  92. data/lib/ffi/platform/powerpc-darwin/types.conf +100 -0
  93. data/lib/ffi/platform/powerpc-linux/types.conf +100 -0
  94. data/lib/ffi/platform/powerpc64-linux/types.conf +104 -0
  95. data/lib/ffi/platform/s390-linux/types.conf +102 -0
  96. data/lib/ffi/platform/s390x-linux/types.conf +102 -0
  97. data/lib/ffi/platform/sparc-linux/types.conf +102 -0
  98. data/lib/ffi/platform/sparc-solaris/types.conf +128 -0
  99. data/lib/ffi/platform/sparc64-linux/types.conf +102 -0
  100. data/lib/ffi/platform/sparcv9-solaris/types.conf +128 -0
  101. data/lib/ffi/platform/x86_64-cygwin/types.conf +3 -0
  102. data/lib/ffi/platform/x86_64-darwin/types.conf +126 -0
  103. data/lib/ffi/platform/x86_64-freebsd/types.conf +128 -0
  104. data/lib/ffi/platform/x86_64-linux/types.conf +102 -0
  105. data/lib/ffi/platform/x86_64-netbsd/types.conf +128 -0
  106. data/lib/ffi/platform/x86_64-openbsd/types.conf +134 -0
  107. data/lib/ffi/platform/x86_64-solaris/types.conf +122 -0
  108. data/lib/ffi/platform/x86_64-windows/types.conf +120 -0
  109. data/lib/ffi/pointer.rb +161 -0
  110. data/lib/ffi/struct.rb +371 -0
  111. data/lib/ffi/struct_layout_builder.rb +227 -0
  112. data/lib/ffi/tools/const_generator.rb +229 -0
  113. data/lib/ffi/tools/generator.rb +60 -0
  114. data/lib/ffi/tools/generator_task.rb +36 -0
  115. data/lib/ffi/tools/struct_generator.rb +194 -0
  116. data/lib/ffi/tools/types_generator.rb +134 -0
  117. data/lib/ffi/types.rb +194 -0
  118. data/lib/ffi/union.rb +43 -0
  119. data/lib/ffi/variadic.rb +78 -0
  120. data/lib/ffi/version.rb +4 -0
  121. data/libtest/Benchmark.c +52 -0
  122. data/libtest/BoolTest.c +34 -0
  123. data/libtest/BufferTest.c +31 -0
  124. data/libtest/ClosureTest.c +205 -0
  125. data/libtest/EnumTest.c +51 -0
  126. data/libtest/FunctionTest.c +70 -0
  127. data/libtest/GNUmakefile +149 -0
  128. data/libtest/GlobalVariable.c +62 -0
  129. data/libtest/LastErrorTest.c +21 -0
  130. data/libtest/NumberTest.c +132 -0
  131. data/libtest/PointerTest.c +63 -0
  132. data/libtest/ReferenceTest.c +23 -0
  133. data/libtest/StringTest.c +34 -0
  134. data/libtest/StructTest.c +243 -0
  135. data/libtest/UnionTest.c +43 -0
  136. data/libtest/VariadicTest.c +99 -0
  137. data/spec/ffi/LICENSE.SPECS +22 -0
  138. data/spec/ffi/async_callback_spec.rb +35 -0
  139. data/spec/ffi/bitmask_spec.rb +575 -0
  140. data/spec/ffi/bool_spec.rb +32 -0
  141. data/spec/ffi/buffer_spec.rb +279 -0
  142. data/spec/ffi/callback_spec.rb +773 -0
  143. data/spec/ffi/custom_param_type.rb +37 -0
  144. data/spec/ffi/custom_type_spec.rb +74 -0
  145. data/spec/ffi/dup_spec.rb +52 -0
  146. data/spec/ffi/enum_spec.rb +423 -0
  147. data/spec/ffi/errno_spec.rb +20 -0
  148. data/spec/ffi/ffi_spec.rb +28 -0
  149. data/spec/ffi/fixtures/Benchmark.c +52 -0
  150. data/spec/ffi/fixtures/BitmaskTest.c +51 -0
  151. data/spec/ffi/fixtures/BoolTest.c +34 -0
  152. data/spec/ffi/fixtures/BufferTest.c +31 -0
  153. data/spec/ffi/fixtures/ClosureTest.c +205 -0
  154. data/spec/ffi/fixtures/EnumTest.c +51 -0
  155. data/spec/ffi/fixtures/FunctionTest.c +142 -0
  156. data/spec/ffi/fixtures/GNUmakefile +149 -0
  157. data/spec/ffi/fixtures/GlobalVariable.c +62 -0
  158. data/spec/ffi/fixtures/LastErrorTest.c +21 -0
  159. data/spec/ffi/fixtures/NumberTest.c +132 -0
  160. data/spec/ffi/fixtures/PipeHelper.h +21 -0
  161. data/spec/ffi/fixtures/PipeHelperPosix.c +41 -0
  162. data/spec/ffi/fixtures/PipeHelperWindows.c +72 -0
  163. data/spec/ffi/fixtures/PointerTest.c +63 -0
  164. data/spec/ffi/fixtures/ReferenceTest.c +23 -0
  165. data/spec/ffi/fixtures/StringTest.c +34 -0
  166. data/spec/ffi/fixtures/StructTest.c +243 -0
  167. data/spec/ffi/fixtures/UnionTest.c +43 -0
  168. data/spec/ffi/fixtures/VariadicTest.c +99 -0
  169. data/spec/ffi/fixtures/classes.rb +438 -0
  170. data/spec/ffi/function_spec.rb +97 -0
  171. data/spec/ffi/io_spec.rb +16 -0
  172. data/spec/ffi/library_spec.rb +286 -0
  173. data/spec/ffi/long_double.rb +30 -0
  174. data/spec/ffi/managed_struct_spec.rb +68 -0
  175. data/spec/ffi/memorypointer_spec.rb +78 -0
  176. data/spec/ffi/number_spec.rb +247 -0
  177. data/spec/ffi/platform_spec.rb +114 -0
  178. data/spec/ffi/pointer_spec.rb +285 -0
  179. data/spec/ffi/rbx/attach_function_spec.rb +34 -0
  180. data/spec/ffi/rbx/memory_pointer_spec.rb +198 -0
  181. data/spec/ffi/rbx/spec_helper.rb +6 -0
  182. data/spec/ffi/rbx/struct_spec.rb +18 -0
  183. data/spec/ffi/spec_helper.rb +93 -0
  184. data/spec/ffi/string_spec.rb +118 -0
  185. data/spec/ffi/strptr_spec.rb +50 -0
  186. data/spec/ffi/struct_by_ref_spec.rb +43 -0
  187. data/spec/ffi/struct_callback_spec.rb +69 -0
  188. data/spec/ffi/struct_initialize_spec.rb +35 -0
  189. data/spec/ffi/struct_packed_spec.rb +50 -0
  190. data/spec/ffi/struct_spec.rb +882 -0
  191. data/spec/ffi/typedef_spec.rb +91 -0
  192. data/spec/ffi/union_spec.rb +67 -0
  193. data/spec/ffi/variadic_spec.rb +132 -0
  194. data/spec/spec.opts +4 -0
  195. metadata +309 -0
@@ -0,0 +1,43 @@
1
+ #
2
+ # Copyright (C) 2008-2010 Wayne Meissner
3
+ #
4
+ # This file is part of ruby-ffi.
5
+ #
6
+ # All rights reserved.
7
+ #
8
+ # Redistribution and use in source and binary forms, with or without
9
+ # modification, are permitted provided that the following conditions are met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright notice, this
12
+ # list of conditions and the following disclaimer.
13
+ # * Redistributions in binary form must reproduce the above copyright notice
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
17
+ # may be used to endorse or promote products derived from this software
18
+ # without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#
30
+
31
+ module FFI
32
+ # @return (see FFI::LastError.error)
33
+ # @see FFI::LastError.error
34
+ def self.errno
35
+ FFI::LastError.error
36
+ end
37
+ # @param error (see FFI::LastError.error=)
38
+ # @return (see FFI::LastError.error=)
39
+ # @see FFI::LastError.error=
40
+ def self.errno=(error)
41
+ FFI::LastError.error = error
42
+ end
43
+ end
@@ -0,0 +1,44 @@
1
+ #
2
+ # Copyright (C) 2008-2010 JRuby project
3
+ #
4
+ # This file is part of ruby-ffi.
5
+ #
6
+ # All rights reserved.
7
+ #
8
+ # Redistribution and use in source and binary forms, with or without
9
+ # modification, are permitted provided that the following conditions are met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright notice, this
12
+ # list of conditions and the following disclaimer.
13
+ # * Redistributions in binary form must reproduce the above copyright notice
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
17
+ # may be used to endorse or promote products derived from this software
18
+ # without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ require 'ffi/platform'
32
+ require 'ffi/types'
33
+ require 'ffi/library'
34
+ require 'ffi/errno'
35
+ require 'ffi/pointer'
36
+ require 'ffi/memorypointer'
37
+ require 'ffi/struct'
38
+ require 'ffi/union'
39
+ require 'ffi/managedstruct'
40
+ require 'ffi/callback'
41
+ require 'ffi/io'
42
+ require 'ffi/autopointer'
43
+ require 'ffi/variadic'
44
+ require 'ffi/enum'
@@ -0,0 +1,62 @@
1
+ #
2
+ # Copyright (C) 2008, 2009 Wayne Meissner
3
+ #
4
+ # This file is part of ruby-ffi.
5
+ #
6
+ # All rights reserved.
7
+ #
8
+ # Redistribution and use in source and binary forms, with or without
9
+ # modification, are permitted provided that the following conditions are met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright notice, this
12
+ # list of conditions and the following disclaimer.
13
+ # * Redistributions in binary form must reproduce the above copyright notice
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
17
+ # may be used to endorse or promote products derived from this software
18
+ # without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#
30
+
31
+ module FFI
32
+
33
+ # This module implements a couple of class methods to play with IO.
34
+ module IO
35
+ # @param [Integer] fd file decriptor
36
+ # @param [String] mode mode string
37
+ # @return [::IO]
38
+ # Synonym for IO::for_fd.
39
+ def self.for_fd(fd, mode = "r")
40
+ ::IO.for_fd(fd, mode)
41
+ end
42
+
43
+ # @param [#read] io io to read from
44
+ # @param [AbstractMemory] buf destination for data read from +io+
45
+ # @param [nil, Numeric] len maximul number of bytes to read from +io+. If +nil+,
46
+ # read until end of file.
47
+ # @return [Numeric] length really read, in bytes
48
+ #
49
+ # A version of IO#read that reads data from an IO and put then into a native buffer.
50
+ #
51
+ # This will be optimized at some future time to eliminate the double copy.
52
+ #
53
+ def self.native_read(io, buf, len)
54
+ tmp = io.read(len)
55
+ return -1 unless tmp
56
+ buf.put_bytes(0, tmp)
57
+ tmp.length
58
+ end
59
+
60
+ end
61
+ end
62
+
@@ -0,0 +1,590 @@
1
+ #
2
+ # Copyright (C) 2008-2010 Wayne Meissner
3
+ #
4
+ # This file is part of ruby-ffi.
5
+ #
6
+ # All rights reserved.
7
+ #
8
+ # Redistribution and use in source and binary forms, with or without
9
+ # modification, are permitted provided that the following conditions are met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright notice, this
12
+ # list of conditions and the following disclaimer.
13
+ # * Redistributions in binary form must reproduce the above copyright notice
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
17
+ # may be used to endorse or promote products derived from this software
18
+ # without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#
30
+
31
+ module FFI
32
+ CURRENT_PROCESS = USE_THIS_PROCESS_AS_LIBRARY = Object.new
33
+
34
+ # @param [#to_s] lib library name
35
+ # @return [String] library name formatted for current platform
36
+ # Transform a generic library name to a platform library name
37
+ # @example
38
+ # # Linux
39
+ # FFI.map_library_name 'c' # -> "libc.so.6"
40
+ # FFI.map_library_name 'jpeg' # -> "libjpeg.so"
41
+ # # Windows
42
+ # FFI.map_library_name 'c' # -> "msvcrt.dll"
43
+ # FFI.map_library_name 'jpeg' # -> "jpeg.dll"
44
+ def self.map_library_name(lib)
45
+ # Mangle the library name to reflect the native library naming conventions
46
+ lib = lib.to_s unless lib.kind_of?(String)
47
+ lib = Library::LIBC if lib == 'c'
48
+
49
+ if lib && File.basename(lib) == lib
50
+ lib = Platform::LIBPREFIX + lib unless lib =~ /^#{Platform::LIBPREFIX}/
51
+ r = Platform::IS_GNU ? "\\.so($|\\.[1234567890]+)" : "\\.#{Platform::LIBSUFFIX}$"
52
+ lib += ".#{Platform::LIBSUFFIX}" unless lib =~ /#{r}/
53
+ end
54
+
55
+ lib
56
+ end
57
+
58
+ # Exception raised when a function is not found in libraries
59
+ class NotFoundError < LoadError
60
+ def initialize(function, *libraries)
61
+ super("Function '#{function}' not found in [#{libraries[0].nil? ? 'current process' : libraries.join(", ")}]")
62
+ end
63
+ end
64
+
65
+ # This module is the base to use native functions.
66
+ #
67
+ # A basic usage may be:
68
+ # require 'ffi'
69
+ #
70
+ # module Hello
71
+ # extend FFI::Library
72
+ # ffi_lib FFI::Library::LIBC
73
+ # attach_function 'puts', [ :string ], :int
74
+ # end
75
+ #
76
+ # Hello.puts("Hello, World")
77
+ #
78
+ #
79
+ module Library
80
+ CURRENT_PROCESS = FFI::CURRENT_PROCESS
81
+ LIBC = FFI::Platform::LIBC
82
+
83
+ # @param mod extended object
84
+ # @return [nil]
85
+ # @raise {RuntimeError} if +mod+ is not a Module
86
+ # Test if extended object is a Module. If not, raise RuntimeError.
87
+ def self.extended(mod)
88
+ raise RuntimeError.new("must only be extended by module") unless mod.kind_of?(Module)
89
+ end
90
+
91
+
92
+ # @param [Array] names names of libraries to load
93
+ # @return [Array<DynamicLibrary>]
94
+ # @raise {LoadError} if a library cannot be opened
95
+ # Load native libraries.
96
+ def ffi_lib(*names)
97
+ raise LoadError.new("library names list must not be empty") if names.empty?
98
+
99
+ lib_flags = defined?(@ffi_lib_flags) ? @ffi_lib_flags : FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL
100
+ ffi_libs = names.map do |name|
101
+
102
+ if name == FFI::CURRENT_PROCESS
103
+ FFI::DynamicLibrary.open(nil, FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL)
104
+
105
+ else
106
+ libnames = (name.is_a?(::Array) ? name : [ name ]).map { |n| [ n, FFI.map_library_name(n) ].uniq }.flatten.compact
107
+ lib = nil
108
+ errors = {}
109
+
110
+ libnames.each do |libname|
111
+ begin
112
+ orig = libname
113
+ lib = FFI::DynamicLibrary.open(libname, lib_flags)
114
+ break if lib
115
+
116
+ rescue Exception => ex
117
+ ldscript = false
118
+ if ex.message =~ /(([^ \t()])+\.so([^ \t:()])*):([ \t])*(invalid ELF header|file too short|invalid file format)/
119
+ if File.read($1) =~ /(?:GROUP|INPUT) *\( *([^ \)]+)/
120
+ libname = $1
121
+ ldscript = true
122
+ end
123
+ end
124
+
125
+ if ldscript
126
+ retry
127
+ else
128
+ # TODO better library lookup logic
129
+ libname = libname.to_s
130
+ unless libname.start_with?("/")
131
+ path = ['/usr/lib/','/usr/local/lib/'].find do |pth|
132
+ File.exist?(pth + libname)
133
+ end
134
+ if path
135
+ libname = path + libname
136
+ retry
137
+ end
138
+ end
139
+
140
+ libr = (orig == libname ? orig : "#{orig} #{libname}")
141
+ errors[libr] = ex
142
+ end
143
+ end
144
+ end
145
+
146
+ if lib.nil?
147
+ raise LoadError.new(errors.values.join(".\n"))
148
+ end
149
+
150
+ # return the found lib
151
+ lib
152
+ end
153
+ end
154
+
155
+ @ffi_libs = ffi_libs
156
+ end
157
+
158
+ # Set the calling convention for {#attach_function} and {#callback}
159
+ #
160
+ # @see http://en.wikipedia.org/wiki/Stdcall#stdcall
161
+ # @note +:stdcall+ is typically used for attaching Windows API functions
162
+ #
163
+ # @param [Symbol] convention one of +:default+, +:stdcall+
164
+ # @return [Symbol] the new calling convention
165
+ def ffi_convention(convention = nil)
166
+ @ffi_convention ||= :default
167
+ @ffi_convention = convention if convention
168
+ @ffi_convention
169
+ end
170
+
171
+ # @see #ffi_lib
172
+ # @return [Array<FFI::DynamicLibrary>] array of currently loaded FFI libraries
173
+ # @raise [LoadError] if no libraries have been loaded (using {#ffi_lib})
174
+ # Get FFI libraries loaded using {#ffi_lib}.
175
+ def ffi_libraries
176
+ raise LoadError.new("no library specified") if !defined?(@ffi_libs) || @ffi_libs.empty?
177
+ @ffi_libs
178
+ end
179
+
180
+ # Flags used in {#ffi_lib}.
181
+ #
182
+ # This map allows you to supply symbols to {#ffi_lib_flags} instead of
183
+ # the actual constants.
184
+ FlagsMap = {
185
+ :global => DynamicLibrary::RTLD_GLOBAL,
186
+ :local => DynamicLibrary::RTLD_LOCAL,
187
+ :lazy => DynamicLibrary::RTLD_LAZY,
188
+ :now => DynamicLibrary::RTLD_NOW
189
+ }
190
+
191
+ # Sets library flags for {#ffi_lib}.
192
+ #
193
+ # @example
194
+ # ffi_lib_flags(:lazy, :local) # => 5
195
+ #
196
+ # @param [Symbol, …] flags (see {FlagsMap})
197
+ # @return [Fixnum] the new value
198
+ def ffi_lib_flags(*flags)
199
+ @ffi_lib_flags = flags.inject(0) { |result, f| result | FlagsMap[f] }
200
+ end
201
+
202
+
203
+ ##
204
+ # @overload attach_function(func, args, returns, options = {})
205
+ # @example attach function without an explicit name
206
+ # module Foo
207
+ # extend FFI::Library
208
+ # ffi_lib FFI::Library::LIBC
209
+ # attach_function :malloc, [:size_t], :pointer
210
+ # end
211
+ # # now callable via Foo.malloc
212
+ # @overload attach_function(name, func, args, returns, options = {})
213
+ # @example attach function with an explicit name
214
+ # module Bar
215
+ # extend FFI::Library
216
+ # ffi_lib FFI::Library::LIBC
217
+ # attach_function :c_malloc, :malloc, [:size_t], :pointer
218
+ # end
219
+ # # now callable via Bar.c_malloc
220
+ #
221
+ # Attach C function +func+ to this module.
222
+ #
223
+ #
224
+ # @param [#to_s] name name of ruby method to attach as
225
+ # @param [#to_s] func name of C function to attach
226
+ # @param [Array<Symbol>] args an array of types
227
+ # @param [Symbol] returns type of return value
228
+ # @option options [Boolean] :blocking (@blocking) set to true if the C function is a blocking call
229
+ # @option options [Symbol] :convention (:default) calling convention (see {#ffi_convention})
230
+ # @option options [FFI::Enums] :enums
231
+ # @option options [Hash] :type_map
232
+ #
233
+ # @return [FFI::VariadicInvoker]
234
+ #
235
+ # @raise [FFI::NotFoundError] if +func+ cannot be found in the attached libraries (see {#ffi_lib})
236
+ def attach_function(name, func, args, returns = nil, options = nil)
237
+ mname, a2, a3, a4, a5 = name, func, args, returns, options
238
+ cname, arg_types, ret_type, opts = (a4 && (a2.is_a?(String) || a2.is_a?(Symbol))) ? [ a2, a3, a4, a5 ] : [ mname.to_s, a2, a3, a4 ]
239
+
240
+ # Convert :foo to the native type
241
+ arg_types = arg_types.map { |e| find_type(e) }
242
+ options = {
243
+ :convention => ffi_convention,
244
+ :type_map => defined?(@ffi_typedefs) ? @ffi_typedefs : nil,
245
+ :blocking => defined?(@blocking) && @blocking,
246
+ :enums => defined?(@ffi_enums) ? @ffi_enums : nil,
247
+ }
248
+
249
+ @blocking = false
250
+ options.merge!(opts) if opts && opts.is_a?(Hash)
251
+
252
+ # Try to locate the function in any of the libraries
253
+ invokers = []
254
+ ffi_libraries.each do |lib|
255
+ if invokers.empty?
256
+ begin
257
+ function = nil
258
+ function_names(cname, arg_types).find do |fname|
259
+ function = lib.find_function(fname)
260
+ end
261
+ raise LoadError unless function
262
+
263
+ invokers << if arg_types.length > 0 && arg_types[arg_types.length - 1] == FFI::NativeType::VARARGS
264
+ VariadicInvoker.new(function, arg_types, find_type(ret_type), options)
265
+
266
+ else
267
+ Function.new(find_type(ret_type), arg_types, function, options)
268
+ end
269
+
270
+ rescue LoadError
271
+ end
272
+ end
273
+ end
274
+ invoker = invokers.compact.shift
275
+ raise FFI::NotFoundError.new(cname.to_s, ffi_libraries.map { |lib| lib.name }) unless invoker
276
+
277
+ invoker.attach(self, mname.to_s)
278
+ invoker
279
+ end
280
+
281
+ # @param [#to_s] name function name
282
+ # @param [Array] arg_types function's argument types
283
+ # @return [Array<String>]
284
+ # This function returns a list of possible names to lookup.
285
+ # @note Function names on windows may be decorated if they are using stdcall. See
286
+ # * http://en.wikipedia.org/wiki/Name_mangling#C_name_decoration_in_Microsoft_Windows
287
+ # * http://msdn.microsoft.com/en-us/library/zxk0tw93%28v=VS.100%29.aspx
288
+ # * http://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions#STDCALL
289
+ # Note that decorated names can be overridden via def files. Also note that the
290
+ # windows api, although using, doesn't have decorated names.
291
+ def function_names(name, arg_types)
292
+ result = [name.to_s]
293
+ if ffi_convention == :stdcall
294
+ # Get the size of each parameter
295
+ size = arg_types.inject(0) do |mem, arg|
296
+ size = arg.size
297
+ # The size must be a multiple of 4
298
+ size += (4 - size) % 4
299
+ mem + size
300
+ end
301
+
302
+ result << "_#{name.to_s}@#{size}" # win32
303
+ result << "#{name.to_s}@#{size}" # win64
304
+ end
305
+ result
306
+ end
307
+
308
+ # @overload attach_variable(mname, cname, type)
309
+ # @param [#to_s] mname name of ruby method to attach as
310
+ # @param [#to_s] cname name of C variable to attach
311
+ # @param [DataConverter, Struct, Symbol, Type] type C variable's type
312
+ # @example
313
+ # module Bar
314
+ # extend FFI::Library
315
+ # ffi_lib 'my_lib'
316
+ # attach_variable :c_myvar, :myvar, :long
317
+ # end
318
+ # # now callable via Bar.c_myvar
319
+ # @overload attach_variable(cname, type)
320
+ # @param [#to_s] mname name of ruby method to attach as
321
+ # @param [DataConverter, Struct, Symbol, Type] type C variable's type
322
+ # @example
323
+ # module Bar
324
+ # extend FFI::Library
325
+ # ffi_lib 'my_lib'
326
+ # attach_variable :myvar, :long
327
+ # end
328
+ # # now callable via Bar.myvar
329
+ # @return [DynamicLibrary::Symbol]
330
+ # @raise {FFI::NotFoundError} if +cname+ cannot be found in libraries
331
+ #
332
+ # Attach C variable +cname+ to this module.
333
+ def attach_variable(mname, a1, a2 = nil)
334
+ cname, type = a2 ? [ a1, a2 ] : [ mname.to_s, a1 ]
335
+ address = nil
336
+ ffi_libraries.each do |lib|
337
+ begin
338
+ address = lib.find_variable(cname.to_s)
339
+ break unless address.nil?
340
+ rescue LoadError
341
+ end
342
+ end
343
+
344
+ raise FFI::NotFoundError.new(cname, ffi_libraries) if address.nil? || address.null?
345
+ if type.is_a?(Class) && type < FFI::Struct
346
+ # If it is a global struct, just attach directly to the pointer
347
+ s = type.new(address)
348
+ self.module_eval <<-code, __FILE__, __LINE__
349
+ @@ffi_gvar_#{mname} = s
350
+ def self.#{mname}
351
+ @@ffi_gvar_#{mname}
352
+ end
353
+ code
354
+
355
+ else
356
+ sc = Class.new(FFI::Struct)
357
+ sc.layout :gvar, find_type(type)
358
+ s = sc.new(address)
359
+ #
360
+ # Attach to this module as mname/mname=
361
+ #
362
+ self.module_eval <<-code, __FILE__, __LINE__
363
+ @@ffi_gvar_#{mname} = s
364
+ def self.#{mname}
365
+ @@ffi_gvar_#{mname}[:gvar]
366
+ end
367
+ def self.#{mname}=(value)
368
+ @@ffi_gvar_#{mname}[:gvar] = value
369
+ end
370
+ code
371
+
372
+ end
373
+
374
+ address
375
+ end
376
+
377
+
378
+ # @overload callback(name, params, ret)
379
+ # @param name callback name to add to type map
380
+ # @param [Array] params array of parameters' types
381
+ # @param [DataConverter, Struct, Symbol, Type] ret callback return type
382
+ # @overload callback(params, ret)
383
+ # @param [Array] params array of parameters' types
384
+ # @param [DataConverter, Struct, Symbol, Type] ret callback return type
385
+ # @return [FFI::CallbackInfo]
386
+ def callback(*args)
387
+ raise ArgumentError, "wrong number of arguments" if args.length < 2 || args.length > 3
388
+ name, params, ret = if args.length == 3
389
+ args
390
+ else
391
+ [ nil, args[0], args[1] ]
392
+ end
393
+
394
+ native_params = params.map { |e| find_type(e) }
395
+ raise ArgumentError, "callbacks cannot have variadic parameters" if native_params.include?(FFI::Type::VARARGS)
396
+ options = Hash.new
397
+ options[:convention] = ffi_convention
398
+ options[:enums] = @ffi_enums if defined?(@ffi_enums)
399
+ cb = FFI::CallbackInfo.new(find_type(ret), native_params, options)
400
+
401
+ # Add to the symbol -> type map (unless there was no name)
402
+ unless name.nil?
403
+ typedef cb, name
404
+ end
405
+
406
+ cb
407
+ end
408
+
409
+ # Register or get an already registered type definition.
410
+ #
411
+ # To register a new type definition, +old+ should be a {FFI::Type}. +add+
412
+ # is in this case the type definition.
413
+ #
414
+ # If +old+ is a {DataConverter}, a {Type::Mapped} is returned.
415
+ #
416
+ # If +old+ is +:enum+
417
+ # * and +add+ is an +Array+, a call to {#enum} is made with +add+ as single parameter;
418
+ # * in others cases, +info+ is used to create a named enum.
419
+ #
420
+ # If +old+ is a key for type map, #typedef get +old+ type definition.
421
+ #
422
+ # @param [DataConverter, Symbol, Type] old
423
+ # @param [Symbol] add
424
+ # @param [Symbol] info
425
+ # @return [FFI::Enum, FFI::Type]
426
+ def typedef(old, add, info=nil)
427
+ @ffi_typedefs = Hash.new unless defined?(@ffi_typedefs)
428
+
429
+ @ffi_typedefs[add] = if old.kind_of?(FFI::Type)
430
+ old
431
+
432
+ elsif @ffi_typedefs.has_key?(old)
433
+ @ffi_typedefs[old]
434
+
435
+ elsif old.is_a?(DataConverter)
436
+ FFI::Type::Mapped.new(old)
437
+
438
+ elsif old == :enum
439
+ if add.kind_of?(Array)
440
+ self.enum(add)
441
+ else
442
+ self.enum(info, add)
443
+ end
444
+
445
+ else
446
+ FFI.find_type(old)
447
+ end
448
+ end
449
+
450
+ private
451
+ # Generic enum builder
452
+ # @param [Class] klass can be one of FFI::Enum or FFI::Bitmask
453
+ # @param args (see #enum or #bitmask)
454
+ def generic_enum(klass, *args)
455
+ native_type = args.first.kind_of?(FFI::Type) ? args.shift : nil
456
+ name, values = if args[0].kind_of?(Symbol) && args[1].kind_of?(Array)
457
+ [ args[0], args[1] ]
458
+ elsif args[0].kind_of?(Array)
459
+ [ nil, args[0] ]
460
+ else
461
+ [ nil, args ]
462
+ end
463
+ @ffi_enums = FFI::Enums.new unless defined?(@ffi_enums)
464
+ @ffi_enums << (e = native_type ? klass.new(native_type, values, name) : klass.new(values, name))
465
+
466
+ # If called with a name, add a typedef alias
467
+ typedef(e, name) if name
468
+ e
469
+ end
470
+
471
+ public
472
+ # @overload enum(name, values)
473
+ # Create a named enum.
474
+ # @example
475
+ # enum :foo, [:zero, :one, :two] # named enum
476
+ # @param [Symbol] name name for new enum
477
+ # @param [Array] values values for enum
478
+ # @overload enum(*args)
479
+ # Create an unnamed enum.
480
+ # @example
481
+ # enum :zero, :one, :two # unnamed enum
482
+ # @param args values for enum
483
+ # @overload enum(values)
484
+ # Create an unnamed enum.
485
+ # @example
486
+ # enum [:zero, :one, :two] # unnamed enum, equivalent to above example
487
+ # @param [Array] values values for enum
488
+ # @overload enum(native_type, name, values)
489
+ # Create a named enum and specify the native type.
490
+ # @example
491
+ # enum FFI::Type::UINT64, :foo, [:zero, :one, :two] # named enum
492
+ # @param [FFI::Type] native_type native type for new enum
493
+ # @param [Symbol] name name for new enum
494
+ # @param [Array] values values for enum
495
+ # @overload enum(native_type, *args)
496
+ # Create an unnamed enum and specify the native type.
497
+ # @example
498
+ # enum FFI::Type::UINT64, :zero, :one, :two # unnamed enum
499
+ # @param [FFI::Type] native_type native type for new enum
500
+ # @param args values for enum
501
+ # @overload enum(native_type, values)
502
+ # Create an unnamed enum and specify the native type.
503
+ # @example
504
+ # enum Type::UINT64, [:zero, :one, :two] # unnamed enum, equivalent to above example
505
+ # @param [FFI::Type] native_type native type for new enum
506
+ # @param [Array] values values for enum
507
+ # @return [FFI::Enum]
508
+ # Create a new {FFI::Enum}.
509
+ def enum(*args)
510
+ generic_enum(FFI::Enum, *args)
511
+ end
512
+
513
+ # @overload bitmask(name, values)
514
+ # Create a named bitmask
515
+ # @example
516
+ # bitmask :foo, [:red, :green, :blue] # bits 0,1,2 are used
517
+ # bitmask :foo, [:red, :green, 5, :blue] # bits 0,5,6 are used
518
+ # @param [Symbol] name for new bitmask
519
+ # @param [Array<Symbol, Integer>] values for new bitmask
520
+ # @overload bitmask(*args)
521
+ # Create an unamed bitmask
522
+ # @example
523
+ # bm = bitmask :red, :green, :blue # bits 0,1,2 are used
524
+ # bm = bitmask :red, :green, 5, blue # bits 0,5,6 are used
525
+ # @param [Symbol, Integer] args values for new bitmask
526
+ # @overload bitmask(values)
527
+ # Create an unamed bitmask
528
+ # @example
529
+ # bm = bitmask [:red, :green, :blue] # bits 0,1,2 are used
530
+ # bm = bitmask [:red, :green, 5, blue] # bits 0,5,6 are used
531
+ # @param [Array<Symbol, Integer>] values for new bitmask
532
+ # @overload bitmask(native_type, name, values)
533
+ # Create a named enum and specify the native type.
534
+ # @example
535
+ # bitmask FFI::Type::UINT64, :foo, [:red, :green, :blue]
536
+ # @param [FFI::Type] native_type native type for new bitmask
537
+ # @param [Symbol] name for new bitmask
538
+ # @param [Array<Symbol, Integer>] values for new bitmask
539
+ # @overload bitmask(native_type, *args)
540
+ # @example
541
+ # bitmask FFI::Type::UINT64, :red, :green, :blue
542
+ # @param [FFI::Type] native_type native type for new bitmask
543
+ # @param [Symbol, Integer] args values for new bitmask
544
+ # @overload bitmask(native_type, values)
545
+ # Create a named enum and specify the native type.
546
+ # @example
547
+ # bitmask FFI::Type::UINT64, [:red, :green, :blue]
548
+ # @param [FFI::Type] native_type native type for new bitmask
549
+ # @param [Array<Symbol, Integer>] values for new bitmask
550
+ # @return [FFI::Bitmask]
551
+ # Create a new FFI::Bitmask
552
+ def bitmask(*args)
553
+ generic_enum(FFI::Bitmask, *args)
554
+ end
555
+
556
+ # @param name
557
+ # @return [FFI::Enum]
558
+ # Find an enum by name.
559
+ def enum_type(name)
560
+ @ffi_enums.find(name) if defined?(@ffi_enums)
561
+ end
562
+
563
+ # @param symbol
564
+ # @return [FFI::Enum]
565
+ # Find an enum by a symbol it contains.
566
+ def enum_value(symbol)
567
+ @ffi_enums.__map_symbol(symbol)
568
+ end
569
+
570
+ # @param [DataConverter, Type, Struct, Symbol] t type to find
571
+ # @return [Type]
572
+ # Find a type definition.
573
+ def find_type(t)
574
+ if t.kind_of?(Type)
575
+ t
576
+
577
+ elsif defined?(@ffi_typedefs) && @ffi_typedefs.has_key?(t)
578
+ @ffi_typedefs[t]
579
+
580
+ elsif t.is_a?(Class) && t < Struct
581
+ Type::POINTER
582
+
583
+ elsif t.is_a?(DataConverter)
584
+ # Add a typedef so next time the converter is used, it hits the cache
585
+ typedef Type::Mapped.new(t), t
586
+
587
+ end || FFI.find_type(t)
588
+ end
589
+ end
590
+ end