alinta-ffi 1.9.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 94a355f1a48329270b49c74905f48cea9ae266ba336dc64c8261ae15601fbe1f
4
+ data.tar.gz: d7b5fcbb1cbb2590f20f4b7ef927a9ba262d250c3165de5344068995a6ce71e2
5
+ SHA512:
6
+ metadata.gz: a155313f3a50ba25184d0e808aaa0b0b15a0d7ae6e7988ae5b92be03d1aab396e75d482455b65c2c1c128ff6b07f619a180c5d0d0edffd476a81e0c698fd5225
7
+ data.tar.gz: 6065568f0d3698d029c3ef5614f2712a5730a6df678545921669d874dd974e6ad8167ea0b86b386ab49cadb786717480def494131a7e7b625468ed3a5774c9b1
data/COPYING ADDED
@@ -0,0 +1,49 @@
1
+ Copyright (c) 2008-2013, Ruby FFI project contributors
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * Neither the name of the Ruby FFI project nor the
12
+ names of its contributors may be used to endorse or promote products
13
+ derived from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+
26
+ libffi, used by this project, is licensed under the MIT license:
27
+
28
+ libffi - Copyright (c) 1996-2011 Anthony Green, Red Hat, Inc and others.
29
+ See source files for details.
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ ``Software''), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49
+
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2008-2016, Ruby FFI project contributors
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * Neither the name of the Ruby FFI project nor the
12
+ names of its contributors may be used to endorse or promote products
13
+ derived from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,112 @@
1
+ # ruby-ffi https://wiki.github.com/ffi/ffi [![Build Status](https://travis-ci.org/ffi/ffi.png?branch=master)](https://travis-ci.org/ffi/ffi)
2
+
3
+ ## Description
4
+
5
+ Ruby-FFI is a ruby extension for programmatically loading dynamic
6
+ libraries, binding functions within them, and calling those functions
7
+ from Ruby code. Moreover, a Ruby-FFI extension works without changes
8
+ on Ruby and JRuby. [Discover why you should write your next extension
9
+ using Ruby-FFI](https://wiki.github.com/ffi/ffi/why-use-ffi).
10
+
11
+ ## Features/problems
12
+
13
+ * Intuitive DSL
14
+ * Supports all C native types
15
+ * C structs (also nested), enums and global variables
16
+ * Callbacks from C to ruby
17
+ * Automatic garbage collection of native memory
18
+
19
+ ## Synopsis
20
+
21
+ ```ruby
22
+ require 'ffi'
23
+
24
+ module MyLib
25
+ extend FFI::Library
26
+ ffi_lib 'c'
27
+ attach_function :puts, [ :string ], :int
28
+ end
29
+
30
+ MyLib.puts 'Hello, World using libc!'
31
+ ```
32
+
33
+ For less minimalistic and more sane examples you may look at:
34
+
35
+ * the samples/ folder
36
+ * the examples on the [wiki](https://wiki.github.com/ffi/ffi)
37
+ * the projects using FFI listed on this page (https://wiki.github.com/ffi/ffi/projects-using-ffi)
38
+
39
+ ## Requirements
40
+
41
+ You need a sane building environment in order to compile the extension.
42
+ At a minimum, you will need:
43
+ * A C compiler (e.g. Xcode on OSX, gcc on everything else)
44
+ * libffi development library - this is commonly in the libffi-dev or libffi-devel
45
+
46
+ On Linux systems running with [PaX](https://en.wikipedia.org/wiki/PaX) (Gentoo, Alpine, etc.) FFI may trigger `mprotect` errors. You may need to disable [mprotect](https://en.wikibooks.org/wiki/Grsecurity/Appendix/Grsecurity_and_PaX_Configuration_Options#Restrict_mprotect.28.29) for ruby (`paxctl -m [/path/to/ruby]`) for the time being until a solution is found.
47
+
48
+ ## Installation
49
+
50
+ From rubygems:
51
+
52
+ [sudo] gem install ffi
53
+
54
+ or from the git repository on github:
55
+
56
+ git clone git://github.com/ffi/ffi.git
57
+ git submodule update --init --recursive
58
+ cd ffi
59
+ rake install
60
+
61
+ ## License
62
+
63
+ The ffi library is covered by the BSD license, also see the LICENSE file.
64
+ The specs are shared with Rubyspec and are licensed by the same license
65
+ as Rubyspec, see the LICENSE.SPECS file.
66
+
67
+ ## Credits
68
+
69
+ The following people have submitted code, bug reports, or otherwise contributed to the success of this project:
70
+
71
+ * Alban Peignier <alban.peignier@free.fr>
72
+ * Aman Gupta <aman@tmm1.net>
73
+ * Andrea Fazzi <andrea.fazzi@alcacoop.it>
74
+ * Andreas Niederl <rico32@gmx.net>
75
+ * Andrew Cholakian <andrew@andrewvc.com>
76
+ * Antonio Terceiro <terceiro@softwarelivre.org>
77
+ * Brian Candler <B.Candler@pobox.com>
78
+ * Brian D. Burns <burns180@gmail.com>
79
+ * Bryan Kearney <bkearney@redhat.com>
80
+ * Charlie Savage <cfis@zerista.com>
81
+ * Chikanaga Tomoyuki <nagachika00@gmail.com>
82
+ * Hongli Lai <hongli@phusion.nl>
83
+ * Ian MacLeod <ian@nevir.net>
84
+ * Jake Douglas <jake@shiftedlabs.com>
85
+ * Jean-Dominique Morani <jdmorani@mac.com>
86
+ * Jeremy Hinegardner <jeremy@hinegardner.org>
87
+ * Jesús García Sáez <blaxter@gmail.com>
88
+ * Joe Khoobyar <joe@ankhcraft.com>
89
+ * Jurij Smakov <jurij@wooyd.org>
90
+ * KISHIMOTO, Makoto <ksmakoto@dd.iij4u.or.jp>
91
+ * Kim Burgestrand <kim@burgestrand.se>
92
+ * Lars Kanis <kanis@comcard.de>
93
+ * Luc Heinrich <luc@honk-honk.com>
94
+ * Luis Lavena <luislavena@gmail.com>
95
+ * Matijs van Zuijlen <matijs@matijs.net>
96
+ * Matthew King <automatthew@gmail.com>
97
+ * Mike Dalessio <mike.dalessio@gmail.com>
98
+ * NARUSE, Yui <naruse@airemix.jp>
99
+ * Park Heesob <phasis@gmail.com>
100
+ * Shin Yee <shinyee@speedgocomputing.com>
101
+ * Stephen Bannasch <stephen.bannasch@gmail.com>
102
+ * Suraj N. Kurapati <sunaku@gmail.com>
103
+ * Sylvain Daubert <sylvain.daubert@laposte.net>
104
+ * Victor Costan
105
+ * beoran@gmail.com
106
+ * ctide <christide@christide.com>
107
+ * emboss <Martin.Bosslet@googlemail.com>
108
+ * hobophobe <unusualtears@gmail.com>
109
+ * meh <meh@paranoici.org>
110
+ * postmodern <postmodern.mod3@gmail.com>
111
+ * wycats@gmail.com <wycats@gmail.com>
112
+ * Wayne Meissner <wmeissner@gmail.com>
@@ -0,0 +1,243 @@
1
+ require 'rubygems/tasks'
2
+ require 'rbconfig'
3
+ require 'rake/clean'
4
+ require File.expand_path("./lib/ffi/version")
5
+
6
+ USE_RAKE_COMPILER = (RUBY_PLATFORM =~ /java/) ? false : true
7
+ if USE_RAKE_COMPILER
8
+ require 'rake/extensiontask'
9
+ end
10
+
11
+ require 'date'
12
+ require 'fileutils'
13
+ require 'rbconfig'
14
+ require 'rspec/core/rake_task'
15
+ require 'rubygems/package_task'
16
+
17
+ LIBEXT = case RbConfig::CONFIG['host_os'].downcase
18
+ when /darwin/
19
+ "dylib"
20
+ when /mswin|mingw/
21
+ "dll"
22
+ else
23
+ RbConfig::CONFIG['DLEXT']
24
+ end
25
+
26
+ CPU = case RbConfig::CONFIG['host_cpu'].downcase
27
+ when /i[3456]86/
28
+ # Darwin always reports i686, even when running in 64bit mode
29
+ if RbConfig::CONFIG['host_os'] =~ /darwin/ && 0xfee1deadbeef.is_a?(Fixnum)
30
+ "x86_64"
31
+ else
32
+ "i386"
33
+ end
34
+
35
+ when /amd64|x86_64/
36
+ "x86_64"
37
+
38
+ when /ppc64|powerpc64/
39
+ "powerpc64"
40
+
41
+ when /ppc|powerpc/
42
+ "powerpc"
43
+
44
+ when /^arm/
45
+ "arm"
46
+
47
+ else
48
+ RbConfig::CONFIG['host_cpu']
49
+ end
50
+
51
+ OS = case RbConfig::CONFIG['host_os'].downcase
52
+ when /linux/
53
+ "linux"
54
+ when /darwin/
55
+ "darwin"
56
+ when /freebsd/
57
+ "freebsd"
58
+ when /openbsd/
59
+ "openbsd"
60
+ when /sunos|solaris/
61
+ "solaris"
62
+ when /mswin|mingw/
63
+ "win32"
64
+ else
65
+ RbConfig::CONFIG['host_os'].downcase
66
+ end
67
+
68
+ def which(name)
69
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
70
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
71
+ exts.each do |ext|
72
+ app = File.join(path, name+ext)
73
+ return app if File.executable? app
74
+ end
75
+ end
76
+ nil
77
+ end
78
+
79
+ GMAKE = which('gmake').nil? ? 'make' : 'gmake'
80
+
81
+ LIBTEST = "build/libtest.#{LIBEXT}"
82
+ BUILD_DIR = "build"
83
+ BUILD_EXT_DIR = File.join(BUILD_DIR, "#{RbConfig::CONFIG['arch']}", 'ffi_c', RUBY_VERSION)
84
+
85
+ def gem_spec
86
+ @gem_spec ||= Gem::Specification.load('ffi.gemspec')
87
+ end
88
+
89
+ TEST_DEPS = [ LIBTEST ]
90
+ if RUBY_PLATFORM == "java"
91
+ RSpec::Core::RakeTask.new(:spec) do |config|
92
+ config.rspec_opts = YAML.load_file 'spec/spec.opts'
93
+ end
94
+ else
95
+ RSpec::Core::RakeTask.new(:spec => :compile) do |config|
96
+ config.rspec_opts = YAML.load_file 'spec/spec.opts'
97
+ end
98
+
99
+ TEST_DEPS.unshift :compile
100
+ end
101
+
102
+ desc "Build all packages"
103
+ task :package => 'gem:package'
104
+
105
+ CLOBBER.include 'lib/ffi/types.conf'
106
+ CLOBBER.include 'pkg'
107
+ CLOBBER.include 'log'
108
+
109
+ CLEAN.include 'build'
110
+ CLEAN.include 'conftest.dSYM'
111
+ CLEAN.include 'spec/ffi/fixtures/libtest.{dylib,so,dll}'
112
+ CLEAN.include 'spec/ffi/fixtures/*.o'
113
+ CLEAN.include "pkg/ffi-*-{mingw32,java}"
114
+ CLEAN.include 'lib/1.*'
115
+ CLEAN.include 'lib/2.*'
116
+ CLEAN.include 'bin'
117
+
118
+ task :distclean => :clobber
119
+
120
+ desc "Build the native test lib"
121
+ file "build/libtest.#{LIBEXT}" => FileList['libtest/**/*.[ch]'] do
122
+ sh %{#{GMAKE} -f libtest/GNUmakefile CPU=#{CPU} OS=#{OS} }
123
+ end
124
+
125
+
126
+ desc "Build test helper lib"
127
+ task :libtest => "build/libtest.#{LIBEXT}"
128
+
129
+ desc "Test the extension"
130
+ task :test => [ :spec ]
131
+
132
+
133
+ namespace :bench do
134
+ ITER = ENV['ITER'] ? ENV['ITER'].to_i : 100000
135
+ bench_libs = "-Ilib -I#{BUILD_DIR}" unless RUBY_PLATFORM == "java"
136
+ bench_files = Dir["bench/bench_*.rb"].reject { |f| f == "bench_helper.rb" }
137
+ bench_files.each do |bench|
138
+ task File.basename(bench, ".rb")[6..-1] => TEST_DEPS do
139
+ sh %{#{Gem.ruby} #{bench_libs} #{bench} #{ITER}}
140
+ end
141
+ end
142
+ task :all => TEST_DEPS do
143
+ bench_files.each do |bench|
144
+ sh %{#{Gem.ruby} #{bench_libs} #{bench}}
145
+ end
146
+ end
147
+ end
148
+
149
+ task 'spec:run' => TEST_DEPS
150
+ task 'spec:specdoc' => TEST_DEPS
151
+
152
+ task :default => :spec
153
+
154
+ namespace 'java' do
155
+
156
+ java_gem_spec = Gem::Specification.new do |s|
157
+ s.name = gem_spec.name
158
+ s.version = gem_spec.version
159
+ s.author = gem_spec.author
160
+ s.email = gem_spec.email
161
+ s.homepage = gem_spec.homepage
162
+ s.summary = gem_spec.summary
163
+ s.description = gem_spec.description
164
+ s.files = %w(LICENSE COPYING README.md Rakefile)
165
+ s.has_rdoc = false
166
+ s.license = gem_spec.license
167
+ s.platform = 'java'
168
+ end
169
+
170
+ Gem::PackageTask.new(java_gem_spec) do |pkg|
171
+ pkg.need_zip = true
172
+ pkg.need_tar = true
173
+ pkg.package_dir = 'pkg'
174
+ end
175
+ end
176
+
177
+ task 'gem:java' => 'java:gem'
178
+
179
+
180
+ if USE_RAKE_COMPILER
181
+ Rake::ExtensionTask.new('ffi_c', gem_spec) do |ext|
182
+ ext.name = 'ffi_c' # indicate the name of the extension.
183
+ # ext.lib_dir = BUILD_DIR # put binaries into this folder.
184
+ ext.tmp_dir = BUILD_DIR # temporary folder used during compilation.
185
+ ext.cross_compile = true # enable cross compilation (requires cross compile toolchain)
186
+ ext.cross_platform = %w[i386-mingw32 x64-mingw32] # forces the Windows platform instead of the default one
187
+ end
188
+
189
+ ENV['RUBY_CC_VERSION'] ||= '1.9.3:2.0.0:2.1.6:2.2.2:2.3.0'
190
+
191
+ # To reduce the gem file size strip mingw32 dlls before packaging
192
+ ENV['RUBY_CC_VERSION'].to_s.split(':').each do |ruby_version|
193
+ task "build/i386-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so" do |t|
194
+ sh "i686-w64-mingw32-strip -S build/i386-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so"
195
+ end
196
+
197
+ task "build/x64-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so" do |t|
198
+ sh "x86_64-w64-mingw32-strip -S build/x64-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so"
199
+ end
200
+ end
201
+
202
+ desc "build a windows gem without all the ceremony."
203
+ task "gem:windows" do
204
+ require "rake_compiler_dock"
205
+ RakeCompilerDock.sh "sudo apt-get update && sudo apt-get install -y libltdl-dev && bundle && rake cross native gem MAKE='nice make -j`nproc`'"
206
+ end
207
+ end
208
+
209
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
210
+ require 'ffi/platform'
211
+ types_conf = File.expand_path(File.join(FFI::Platform::CONF_DIR, 'types.conf'))
212
+ logfile = File.join(File.dirname(__FILE__), 'types_log')
213
+
214
+ file types_conf => File.join("lib", "ffi", "version.rb") do |task|
215
+ require 'fileutils'
216
+ require 'ffi/tools/types_generator'
217
+ options = {}
218
+ FileUtils.mkdir_p(File.dirname(task.name), { :mode => 0755 })
219
+ File.open(task.name, File::CREAT|File::TRUNC|File::RDWR, 0644) do |f|
220
+ f.puts FFI::TypesGenerator.generate(options)
221
+ end
222
+ File.open(logfile, 'w') do |log|
223
+ log.puts(types_conf)
224
+ end
225
+ end
226
+
227
+ task :types_conf => types_conf do
228
+ end
229
+
230
+ Gem::Tasks.new do |t|
231
+ t.scm.tag.format = '%s'
232
+ end
233
+
234
+ begin
235
+ require 'yard'
236
+
237
+ namespace :doc do
238
+ YARD::Rake::YardocTask.new do |yard|
239
+ end
240
+ end
241
+ rescue LoadError
242
+ warn "[warn] YARD unavailable"
243
+ end
@@ -0,0 +1,1109 @@
1
+ /*
2
+ * Copyright (c) 2008, 2009, Wayne Meissner
3
+ * Copyright (C) 2009 Jake Douglas <jake@shiftedlabs.com>
4
+ * Copyright (C) 2008 Luc Heinrich <luc@honk-honk.com>
5
+ *
6
+ * Copyright (c) 2008-2013, Ruby FFI project contributors
7
+ * All rights reserved.
8
+ *
9
+ * Redistribution and use in source and binary forms, with or without
10
+ * modification, are permitted provided that the following conditions are met:
11
+ * * Redistributions of source code must retain the above copyright
12
+ * notice, this list of conditions and the following disclaimer.
13
+ * * Redistributions in binary form must reproduce the above copyright
14
+ * notice, this list of conditions and the following disclaimer in the
15
+ * documentation and/or other materials provided with the distribution.
16
+ * * Neither the name of the Ruby FFI project nor the
17
+ * names of its contributors may be used to endorse or promote products
18
+ * derived from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
24
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+
32
+ #include <sys/types.h>
33
+ #ifndef _MSC_VER
34
+ # include <sys/param.h>
35
+ # include <stdint.h>
36
+ # include <stdbool.h>
37
+ #else
38
+ # include "win32/stdbool.h"
39
+ # include "win32/stdint.h"
40
+ #endif
41
+
42
+ #include <limits.h>
43
+ #include <ruby.h>
44
+
45
+ #include "rbffi.h"
46
+ #include "compat.h"
47
+ #include "AbstractMemory.h"
48
+ #include "Pointer.h"
49
+ #include "Function.h"
50
+ #include "LongDouble.h"
51
+
52
+ #ifdef PRIsVALUE
53
+ # define RB_OBJ_CLASSNAME(obj) rb_obj_class(obj)
54
+ # define RB_OBJ_STRING(obj) (obj)
55
+ #else
56
+ # define PRIsVALUE "s"
57
+ # define RB_OBJ_CLASSNAME(obj) rb_obj_classname(obj)
58
+ # define RB_OBJ_STRING(obj) StringValueCStr(obj)
59
+ #endif
60
+
61
+ static inline char* memory_address(VALUE self);
62
+ VALUE rbffi_AbstractMemoryClass = Qnil;
63
+ static VALUE NullPointerErrorClass = Qnil;
64
+ static ID id_to_ptr = 0, id_plus = 0, id_call = 0;
65
+
66
+ static VALUE
67
+ memory_allocate(VALUE klass)
68
+ {
69
+ AbstractMemory* memory;
70
+ VALUE obj;
71
+ obj = Data_Make_Struct(klass, AbstractMemory, NULL, -1, memory);
72
+ memory->flags = MEM_RD | MEM_WR;
73
+
74
+ return obj;
75
+ }
76
+ #define VAL(x, swap) (unlikely(((memory->flags & MEM_SWAP) != 0)) ? swap((x)) : (x))
77
+
78
+ #define NUM_OP(name, type, toNative, fromNative, swap) \
79
+ static void memory_op_put_##name(AbstractMemory* memory, long off, VALUE value); \
80
+ static void \
81
+ memory_op_put_##name(AbstractMemory* memory, long off, VALUE value) \
82
+ { \
83
+ type tmp = (type) VAL(toNative(value), swap); \
84
+ checkWrite(memory); \
85
+ checkBounds(memory, off, sizeof(type)); \
86
+ memcpy(memory->address + off, &tmp, sizeof(tmp)); \
87
+ } \
88
+ static VALUE memory_put_##name(VALUE self, VALUE offset, VALUE value); \
89
+ static VALUE \
90
+ memory_put_##name(VALUE self, VALUE offset, VALUE value) \
91
+ { \
92
+ AbstractMemory* memory; \
93
+ Data_Get_Struct(self, AbstractMemory, memory); \
94
+ memory_op_put_##name(memory, NUM2LONG(offset), value); \
95
+ return self; \
96
+ } \
97
+ static VALUE memory_write_##name(VALUE self, VALUE value); \
98
+ static VALUE \
99
+ memory_write_##name(VALUE self, VALUE value) \
100
+ { \
101
+ AbstractMemory* memory; \
102
+ Data_Get_Struct(self, AbstractMemory, memory); \
103
+ memory_op_put_##name(memory, 0, value); \
104
+ return self; \
105
+ } \
106
+ static VALUE memory_op_get_##name(AbstractMemory* memory, long off); \
107
+ static VALUE \
108
+ memory_op_get_##name(AbstractMemory* memory, long off) \
109
+ { \
110
+ type tmp; \
111
+ checkRead(memory); \
112
+ checkBounds(memory, off, sizeof(type)); \
113
+ memcpy(&tmp, memory->address + off, sizeof(tmp)); \
114
+ return fromNative(VAL(tmp, swap)); \
115
+ } \
116
+ static VALUE memory_get_##name(VALUE self, VALUE offset); \
117
+ static VALUE \
118
+ memory_get_##name(VALUE self, VALUE offset) \
119
+ { \
120
+ AbstractMemory* memory; \
121
+ Data_Get_Struct(self, AbstractMemory, memory); \
122
+ return memory_op_get_##name(memory, NUM2LONG(offset)); \
123
+ } \
124
+ static VALUE memory_read_##name(VALUE self); \
125
+ static VALUE \
126
+ memory_read_##name(VALUE self) \
127
+ { \
128
+ AbstractMemory* memory; \
129
+ Data_Get_Struct(self, AbstractMemory, memory); \
130
+ return memory_op_get_##name(memory, 0); \
131
+ } \
132
+ static MemoryOp memory_op_##name = { memory_op_get_##name, memory_op_put_##name }; \
133
+ \
134
+ static VALUE memory_put_array_of_##name(VALUE self, VALUE offset, VALUE ary); \
135
+ static VALUE \
136
+ memory_put_array_of_##name(VALUE self, VALUE offset, VALUE ary) \
137
+ { \
138
+ long count = RARRAY_LEN(ary); \
139
+ long off = NUM2LONG(offset); \
140
+ AbstractMemory* memory = MEMORY(self); \
141
+ long i; \
142
+ checkWrite(memory); \
143
+ checkBounds(memory, off, count * sizeof(type)); \
144
+ for (i = 0; i < count; i++) { \
145
+ type tmp = (type) VAL(toNative(RARRAY_PTR(ary)[i]), swap); \
146
+ memcpy(memory->address + off + (i * sizeof(type)), &tmp, sizeof(tmp)); \
147
+ } \
148
+ return self; \
149
+ } \
150
+ static VALUE memory_write_array_of_##name(VALUE self, VALUE ary); \
151
+ static VALUE \
152
+ memory_write_array_of_##name(VALUE self, VALUE ary) \
153
+ { \
154
+ return memory_put_array_of_##name(self, INT2FIX(0), ary); \
155
+ } \
156
+ static VALUE memory_get_array_of_##name(VALUE self, VALUE offset, VALUE length); \
157
+ static VALUE \
158
+ memory_get_array_of_##name(VALUE self, VALUE offset, VALUE length) \
159
+ { \
160
+ long count = NUM2LONG(length); \
161
+ long off = NUM2LONG(offset); \
162
+ AbstractMemory* memory = MEMORY(self); \
163
+ VALUE retVal = rb_ary_new2(count); \
164
+ long i; \
165
+ checkRead(memory); \
166
+ checkBounds(memory, off, count * sizeof(type)); \
167
+ for (i = 0; i < count; ++i) { \
168
+ type tmp; \
169
+ memcpy(&tmp, memory->address + off + (i * sizeof(type)), sizeof(tmp)); \
170
+ rb_ary_push(retVal, fromNative(VAL(tmp, swap))); \
171
+ } \
172
+ return retVal; \
173
+ } \
174
+ static VALUE memory_read_array_of_##name(VALUE self, VALUE length); \
175
+ static VALUE \
176
+ memory_read_array_of_##name(VALUE self, VALUE length) \
177
+ { \
178
+ return memory_get_array_of_##name(self, INT2FIX(0), length); \
179
+ }
180
+
181
+ #define NOSWAP(x) (x)
182
+ #define bswap16(x) (((x) >> 8) & 0xff) | (((x) << 8) & 0xff00);
183
+ static inline int16_t
184
+ SWAPS16(int16_t x)
185
+ {
186
+ return bswap16(x);
187
+ }
188
+
189
+ static inline uint16_t
190
+ SWAPU16(uint16_t x)
191
+ {
192
+ return bswap16(x);
193
+ }
194
+
195
+ #if !defined(__GNUC__) || (__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
196
+ #define bswap32(x) \
197
+ (((x << 24) & 0xff000000) | \
198
+ ((x << 8) & 0x00ff0000) | \
199
+ ((x >> 8) & 0x0000ff00) | \
200
+ ((x >> 24) & 0x000000ff))
201
+
202
+ #define bswap64(x) \
203
+ (((x << 56) & 0xff00000000000000ULL) | \
204
+ ((x << 40) & 0x00ff000000000000ULL) | \
205
+ ((x << 24) & 0x0000ff0000000000ULL) | \
206
+ ((x << 8) & 0x000000ff00000000ULL) | \
207
+ ((x >> 8) & 0x00000000ff000000ULL) | \
208
+ ((x >> 24) & 0x0000000000ff0000ULL) | \
209
+ ((x >> 40) & 0x000000000000ff00ULL) | \
210
+ ((x >> 56) & 0x00000000000000ffULL))
211
+
212
+ static inline int32_t
213
+ SWAPS32(int32_t x)
214
+ {
215
+ return bswap32(x);
216
+ }
217
+
218
+ static inline uint32_t
219
+ SWAPU32(uint32_t x)
220
+ {
221
+ return bswap32(x);
222
+ }
223
+
224
+ static inline int64_t
225
+ SWAPS64(int64_t x)
226
+ {
227
+ return bswap64(x);
228
+ }
229
+
230
+ static inline uint64_t
231
+ SWAPU64(uint64_t x)
232
+ {
233
+ return bswap64(x);
234
+ }
235
+
236
+ #else
237
+ # define SWAPS32(x) ((int32_t) __builtin_bswap32(x))
238
+ # define SWAPU32(x) ((uint32_t) __builtin_bswap32(x))
239
+ # define SWAPS64(x) ((int64_t) __builtin_bswap64(x))
240
+ # define SWAPU64(x) ((uint64_t) __builtin_bswap64(x))
241
+ #endif
242
+
243
+ #if LONG_MAX > INT_MAX
244
+ # define SWAPSLONG SWAPS64
245
+ # define SWAPULONG SWAPU64
246
+ #else
247
+ # define SWAPSLONG SWAPS32
248
+ # define SWAPULONG SWAPU32
249
+ #endif
250
+
251
+ NUM_OP(int8, int8_t, NUM2INT, INT2NUM, NOSWAP);
252
+ NUM_OP(uint8, uint8_t, NUM2UINT, UINT2NUM, NOSWAP);
253
+ NUM_OP(int16, int16_t, NUM2INT, INT2NUM, SWAPS16);
254
+ NUM_OP(uint16, uint16_t, NUM2UINT, UINT2NUM, SWAPU16);
255
+ NUM_OP(int32, int32_t, NUM2INT, INT2NUM, SWAPS32);
256
+ NUM_OP(uint32, uint32_t, NUM2UINT, UINT2NUM, SWAPU32);
257
+ NUM_OP(int64, int64_t, NUM2LL, LL2NUM, SWAPS64);
258
+ NUM_OP(uint64, uint64_t, NUM2ULL, ULL2NUM, SWAPU64);
259
+ NUM_OP(long, long, NUM2LONG, LONG2NUM, SWAPSLONG);
260
+ NUM_OP(ulong, unsigned long, NUM2ULONG, ULONG2NUM, SWAPULONG);
261
+ NUM_OP(float32, float, NUM2DBL, rb_float_new, NOSWAP);
262
+ NUM_OP(float64, double, NUM2DBL, rb_float_new, NOSWAP);
263
+ NUM_OP(longdouble, long double, rbffi_num2longdouble, rbffi_longdouble_new, NOSWAP);
264
+
265
+ static inline void*
266
+ get_pointer_value(VALUE value)
267
+ {
268
+ const int type = TYPE(value);
269
+ if (type == T_DATA && rb_obj_is_kind_of(value, rbffi_PointerClass)) {
270
+ return memory_address(value);
271
+ } else if (type == T_NIL) {
272
+ return NULL;
273
+ } else if (type == T_FIXNUM) {
274
+ return (void *) (uintptr_t) FIX2ULONG(value);
275
+ } else if (type == T_BIGNUM) {
276
+ return (void *) (uintptr_t) NUM2ULL(value);
277
+ } else if (rb_respond_to(value, id_to_ptr)) {
278
+ return MEMORY_PTR(rb_funcall2(value, id_to_ptr, 0, NULL));
279
+ } else {
280
+ rb_raise(rb_eArgError, "value is not a pointer");
281
+ return NULL;
282
+ }
283
+ }
284
+
285
+ NUM_OP(pointer, void *, get_pointer_value, rbffi_Pointer_NewInstance, NOSWAP);
286
+
287
+ static inline uint8_t
288
+ rbffi_bool_value(VALUE value)
289
+ {
290
+ return RTEST(value);
291
+ }
292
+
293
+ static inline VALUE
294
+ rbffi_bool_new(uint8_t value)
295
+ {
296
+ return (value & 1) != 0 ? Qtrue : Qfalse;
297
+ }
298
+
299
+ NUM_OP(bool, unsigned char, rbffi_bool_value, rbffi_bool_new, NOSWAP);
300
+
301
+
302
+ /*
303
+ * call-seq: memory.clear
304
+ * Set the memory to all-zero.
305
+ * @return [self]
306
+ */
307
+ static VALUE
308
+ memory_clear(VALUE self)
309
+ {
310
+ AbstractMemory* ptr = MEMORY(self);
311
+ memset(ptr->address, 0, ptr->size);
312
+ return self;
313
+ }
314
+
315
+ /*
316
+ * call-seq: memory.size
317
+ * Return memory size in bytes (alias: #total)
318
+ * @return [Numeric]
319
+ */
320
+ static VALUE
321
+ memory_size(VALUE self)
322
+ {
323
+ AbstractMemory* ptr;
324
+
325
+ Data_Get_Struct(self, AbstractMemory, ptr);
326
+
327
+ return LONG2NUM(ptr->size);
328
+ }
329
+
330
+ /*
331
+ * call-seq: memory.get(type, offset)
332
+ * Return data of given type contained in memory.
333
+ * @param [Symbol, Type] type_name type of data to get
334
+ * @param [Numeric] offset point in buffer to start from
335
+ * @return [Object]
336
+ * @raise {ArgumentError} if type is not supported
337
+ */
338
+ static VALUE
339
+ memory_get(VALUE self, VALUE type_name, VALUE offset)
340
+ {
341
+ AbstractMemory* ptr;
342
+ VALUE nType;
343
+ Type *type;
344
+
345
+ nType = rbffi_Type_Lookup(type_name);
346
+ if(NIL_P(nType)) goto undefined_type;
347
+
348
+ Data_Get_Struct(self, AbstractMemory, ptr);
349
+ Data_Get_Struct(nType, Type, type);
350
+
351
+ MemoryOp *op = get_memory_op(type);
352
+ if(op == NULL) goto undefined_type;
353
+
354
+ return op->get(ptr, NUM2LONG(offset));
355
+
356
+ undefined_type: {
357
+ VALUE msg = rb_sprintf("undefined type '%" PRIsVALUE "'", type_name);
358
+ rb_exc_raise(rb_exc_new3(rb_eArgError, msg));
359
+ return Qnil;
360
+ }
361
+ }
362
+
363
+ /*
364
+ * call-seq: memory.put(type, offset, value)
365
+ * @param [Symbol, Type] type_name type of data to put
366
+ * @param [Numeric] offset point in buffer to start from
367
+ * @return [nil]
368
+ * @raise {ArgumentError} if type is not supported
369
+ */
370
+ static VALUE
371
+ memory_put(VALUE self, VALUE type_name, VALUE offset, VALUE value)
372
+ {
373
+ AbstractMemory* ptr;
374
+ VALUE nType;
375
+ Type *type;
376
+
377
+ nType = rbffi_Type_Lookup(type_name);
378
+ if(NIL_P(nType)) goto undefined_type;
379
+
380
+ Data_Get_Struct(self, AbstractMemory, ptr);
381
+ Data_Get_Struct(nType, Type, type);
382
+
383
+ MemoryOp *op = get_memory_op(type);
384
+ if(op == NULL) goto undefined_type;
385
+
386
+ op->put(ptr, NUM2LONG(offset), value);
387
+ return Qnil;
388
+
389
+ undefined_type: {
390
+ VALUE msg = rb_sprintf("unsupported type '%" PRIsVALUE "'", type_name);
391
+ rb_exc_raise(rb_exc_new3(rb_eArgError, msg));
392
+ return Qnil;
393
+ }
394
+ }
395
+
396
+ /*
397
+ * call-seq: memory.get_string(offset, length=nil)
398
+ * Return string contained in memory.
399
+ * @param [Numeric] offset point in buffer to start from
400
+ * @param [Numeric] length string's length in bytes. If nil, a (memory size - offset) length string is returned).
401
+ * @return [String]
402
+ * @raise {IndexError} if +length+ is too great
403
+ * @raise {NullPointerError} if memory not initialized
404
+ */
405
+ static VALUE
406
+ memory_get_string(int argc, VALUE* argv, VALUE self)
407
+ {
408
+ VALUE length = Qnil, offset = Qnil;
409
+ AbstractMemory* ptr = MEMORY(self);
410
+ long off, len;
411
+ char* end;
412
+ int nargs = rb_scan_args(argc, argv, "11", &offset, &length);
413
+
414
+ off = NUM2LONG(offset);
415
+ len = nargs > 1 && length != Qnil ? NUM2LONG(length) : (ptr->size - off);
416
+ checkRead(ptr);
417
+ checkBounds(ptr, off, len);
418
+
419
+ end = memchr(ptr->address + off, 0, len);
420
+ return rb_tainted_str_new((char *) ptr->address + off,
421
+ (end != NULL ? end - ptr->address - off : len));
422
+ }
423
+
424
+ /*
425
+ * call-seq: memory.get_array_of_string(offset, count=nil)
426
+ * Return an array of strings contained in memory.
427
+ * @param [Numeric] offset point in memory to start from
428
+ * @param [Numeric] count number of strings to get. If nil, return all strings
429
+ * @return [Array<String>]
430
+ * @raise {IndexError} if +offset+ is too great
431
+ * @raise {NullPointerError} if memory not initialized
432
+ */
433
+ static VALUE
434
+ memory_get_array_of_string(int argc, VALUE* argv, VALUE self)
435
+ {
436
+ VALUE offset = Qnil, countnum = Qnil, retVal = Qnil;
437
+ AbstractMemory* ptr;
438
+ long off;
439
+ int count;
440
+
441
+ rb_scan_args(argc, argv, "11", &offset, &countnum);
442
+ off = NUM2LONG(offset);
443
+ count = (countnum == Qnil ? 0 : NUM2INT(countnum));
444
+ retVal = rb_ary_new2(count);
445
+
446
+ Data_Get_Struct(self, AbstractMemory, ptr);
447
+ checkRead(ptr);
448
+
449
+ if (countnum != Qnil) {
450
+ int i;
451
+
452
+ checkBounds(ptr, off, count * sizeof (char*));
453
+
454
+ for (i = 0; i < count; ++i) {
455
+ const char* strptr = *((const char**) (ptr->address + off) + i);
456
+ rb_ary_push(retVal, (strptr == NULL ? Qnil : rb_tainted_str_new2(strptr)));
457
+ }
458
+
459
+ } else {
460
+ checkBounds(ptr, off, sizeof (char*));
461
+ for ( ; off < ptr->size - (long) sizeof (void *); off += (long) sizeof (void *)) {
462
+ const char* strptr = *(const char**) (ptr->address + off);
463
+ if (strptr == NULL) {
464
+ break;
465
+ }
466
+ rb_ary_push(retVal, rb_tainted_str_new2(strptr));
467
+ }
468
+ }
469
+
470
+ return retVal;
471
+ }
472
+
473
+ /*
474
+ * call-seq: memory.read_array_of_string(count=nil)
475
+ * Return an array of strings contained in memory. Same as:
476
+ * memory.get_array_of_string(0, count)
477
+ * @param [Numeric] count number of strings to get. If nil, return all strings
478
+ * @return [Array<String>]
479
+ */
480
+ static VALUE
481
+ memory_read_array_of_string(int argc, VALUE* argv, VALUE self)
482
+ {
483
+ VALUE* rargv = ALLOCA_N(VALUE, argc + 1);
484
+ int i;
485
+
486
+ rargv[0] = INT2FIX(0);
487
+ for (i = 0; i < argc; i++) {
488
+ rargv[i + 1] = argv[i];
489
+ }
490
+
491
+ return memory_get_array_of_string(argc + 1, rargv, self);
492
+ }
493
+
494
+
495
+ /*
496
+ * call-seq: memory.put_string(offset, str)
497
+ * @param [Numeric] offset
498
+ * @param [String] str
499
+ * @return [self]
500
+ * @raise {SecurityError} when writing unsafe string to memory
501
+ * @raise {IndexError} if +offset+ is too great
502
+ * @raise {NullPointerError} if memory not initialized
503
+ * Put a string in memory.
504
+ */
505
+ static VALUE
506
+ memory_put_string(VALUE self, VALUE offset, VALUE str)
507
+ {
508
+ AbstractMemory* ptr = MEMORY(self);
509
+ long off, len;
510
+
511
+ Check_Type(str, T_STRING);
512
+ off = NUM2LONG(offset);
513
+ len = RSTRING_LEN(str);
514
+
515
+ checkWrite(ptr);
516
+ checkBounds(ptr, off, len + 1);
517
+
518
+ memcpy(ptr->address + off, RSTRING_PTR(str), len);
519
+ *((char *) ptr->address + off + len) = '\0';
520
+
521
+ return self;
522
+ }
523
+
524
+ /*
525
+ * call-seq: memory.get_bytes(offset, length)
526
+ * Return string contained in memory.
527
+ * @param [Numeric] offset point in buffer to start from
528
+ * @param [Numeric] length string's length in bytes.
529
+ * @return [String]
530
+ * @raise {IndexError} if +length+ is too great
531
+ * @raise {NullPointerError} if memory not initialized
532
+ */
533
+ static VALUE
534
+ memory_get_bytes(VALUE self, VALUE offset, VALUE length)
535
+ {
536
+ AbstractMemory* ptr = MEMORY(self);
537
+ long off, len;
538
+
539
+ off = NUM2LONG(offset);
540
+ len = NUM2LONG(length);
541
+
542
+ checkRead(ptr);
543
+ checkBounds(ptr, off, len);
544
+
545
+ return rb_tainted_str_new((char *) ptr->address + off, len);
546
+ }
547
+
548
+ /*
549
+ * call-seq: memory.put_bytes(offset, str, index=0, length=nil)
550
+ * Put a string in memory.
551
+ * @param [Numeric] offset point in buffer to start from
552
+ * @param [String] str string to put to memory
553
+ * @param [Numeric] index
554
+ * @param [Numeric] length string's length in bytes. If nil, a (memory size - offset) length string is returned).
555
+ * @return [self]
556
+ * @raise {IndexError} if +length+ is too great
557
+ * @raise {NullPointerError} if memory not initialized
558
+ * @raise {RangeError} if +index+ is negative, or if index+length is greater than size of string
559
+ * @raise {SecurityError} when writing unsafe string to memory
560
+ */
561
+ static VALUE
562
+ memory_put_bytes(int argc, VALUE* argv, VALUE self)
563
+ {
564
+ AbstractMemory* ptr = MEMORY(self);
565
+ VALUE offset = Qnil, str = Qnil, rbIndex = Qnil, rbLength = Qnil;
566
+ long off, len, idx;
567
+ int nargs = rb_scan_args(argc, argv, "22", &offset, &str, &rbIndex, &rbLength);
568
+
569
+ Check_Type(str, T_STRING);
570
+
571
+ off = NUM2LONG(offset);
572
+ idx = nargs > 2 ? NUM2LONG(rbIndex) : 0;
573
+ if (idx < 0) {
574
+ rb_raise(rb_eRangeError, "index cannot be less than zero");
575
+ return Qnil;
576
+ }
577
+ len = nargs > 3 ? NUM2LONG(rbLength) : (RSTRING_LEN(str) - idx);
578
+ if ((idx + len) > RSTRING_LEN(str)) {
579
+ rb_raise(rb_eRangeError, "index+length is greater than size of string");
580
+ return Qnil;
581
+ }
582
+
583
+ checkWrite(ptr);
584
+ checkBounds(ptr, off, len);
585
+
586
+ if (rb_safe_level() >= 1 && OBJ_TAINTED(str)) {
587
+ rb_raise(rb_eSecurityError, "Writing unsafe string to memory");
588
+ return Qnil;
589
+ }
590
+ memcpy(ptr->address + off, RSTRING_PTR(str) + idx, len);
591
+
592
+ return self;
593
+ }
594
+
595
+ /*
596
+ * call-seq: memory.read_bytes(length)
597
+ * @param [Numeric] length of string to return
598
+ * @return [String]
599
+ * equivalent to :
600
+ * memory.get_bytes(0, length)
601
+ */
602
+ static VALUE
603
+ memory_read_bytes(VALUE self, VALUE length)
604
+ {
605
+ return memory_get_bytes(self, INT2FIX(0), length);
606
+ }
607
+
608
+ /*
609
+ * call-seq: memory.write_bytes(str, index=0, length=nil)
610
+ * @param [String] str string to put to memory
611
+ * @param [Numeric] index
612
+ * @param [Numeric] length string's length in bytes. If nil, a (memory size - offset) length string is returned).
613
+ * @return [self]
614
+ * equivalent to :
615
+ * memory.put_bytes(0, str, index, length)
616
+ */
617
+ static VALUE
618
+ memory_write_bytes(int argc, VALUE* argv, VALUE self)
619
+ {
620
+ VALUE* wargv = ALLOCA_N(VALUE, argc + 1);
621
+ int i;
622
+
623
+ wargv[0] = INT2FIX(0);
624
+ for (i = 0; i < argc; i++) {
625
+ wargv[i + 1] = argv[i];
626
+ }
627
+
628
+ return memory_put_bytes(argc + 1, wargv, self);
629
+ }
630
+
631
+ /*
632
+ * call-seq: memory.type_size
633
+ * @return [Numeric] type size in bytes
634
+ * Get the memory's type size.
635
+ */
636
+ static VALUE
637
+ memory_type_size(VALUE self)
638
+ {
639
+ AbstractMemory* ptr;
640
+
641
+ Data_Get_Struct(self, AbstractMemory, ptr);
642
+
643
+ return INT2NUM(ptr->typeSize);
644
+ }
645
+
646
+ /*
647
+ * Document-method: []
648
+ * call-seq: memory[idx]
649
+ * @param [Numeric] idx index to access in memory
650
+ * @return
651
+ * Memory read accessor.
652
+ */
653
+ static VALUE
654
+ memory_aref(VALUE self, VALUE idx)
655
+ {
656
+ AbstractMemory* ptr;
657
+ VALUE rbOffset = Qnil;
658
+
659
+ Data_Get_Struct(self, AbstractMemory, ptr);
660
+
661
+ rbOffset = ULONG2NUM(NUM2ULONG(idx) * ptr->typeSize);
662
+
663
+ return rb_funcall2(self, id_plus, 1, &rbOffset);
664
+ }
665
+
666
+ static inline char*
667
+ memory_address(VALUE obj)
668
+ {
669
+ return ((AbstractMemory *) DATA_PTR(obj))->address;
670
+ }
671
+
672
+ static VALUE
673
+ memory_copy_from(VALUE self, VALUE rbsrc, VALUE rblen)
674
+ {
675
+ AbstractMemory* dst;
676
+
677
+ Data_Get_Struct(self, AbstractMemory, dst);
678
+
679
+ memcpy(dst->address, rbffi_AbstractMemory_Cast(rbsrc, rbffi_AbstractMemoryClass)->address, NUM2INT(rblen));
680
+
681
+ return self;
682
+ }
683
+
684
+ AbstractMemory*
685
+ rbffi_AbstractMemory_Cast(VALUE obj, VALUE klass)
686
+ {
687
+ if (rb_obj_is_kind_of(obj, klass)) {
688
+ AbstractMemory* memory;
689
+ Data_Get_Struct(obj, AbstractMemory, memory);
690
+ return memory;
691
+ }
692
+
693
+ rb_raise(rb_eArgError, "Invalid Memory object");
694
+ return NULL;
695
+ }
696
+
697
+ void
698
+ rbffi_AbstractMemory_Error(AbstractMemory *mem, int op)
699
+ {
700
+ VALUE rbErrorClass = mem->address == NULL ? NullPointerErrorClass : rb_eRuntimeError;
701
+ if (op == MEM_RD) {
702
+ rb_raise(rbErrorClass, "invalid memory read at address=%p", mem->address);
703
+ } else if (op == MEM_WR) {
704
+ rb_raise(rbErrorClass, "invalid memory write at address=%p", mem->address);
705
+ } else {
706
+ rb_raise(rbErrorClass, "invalid memory access at address=%p", mem->address);
707
+ }
708
+ }
709
+
710
+ static VALUE
711
+ memory_op_get_strptr(AbstractMemory* ptr, long offset)
712
+ {
713
+ void* tmp = NULL;
714
+
715
+ if (ptr != NULL && ptr->address != NULL) {
716
+ checkRead(ptr);
717
+ checkBounds(ptr, offset, sizeof(tmp));
718
+ memcpy(&tmp, ptr->address + offset, sizeof(tmp));
719
+ }
720
+
721
+ return tmp != NULL ? rb_tainted_str_new2(tmp) : Qnil;
722
+ }
723
+
724
+ static void
725
+ memory_op_put_strptr(AbstractMemory* ptr, long offset, VALUE value)
726
+ {
727
+ rb_raise(rb_eArgError, "Cannot set :string fields");
728
+ }
729
+
730
+ static MemoryOp memory_op_strptr = { memory_op_get_strptr, memory_op_put_strptr };
731
+
732
+
733
+ MemoryOps rbffi_AbstractMemoryOps = {
734
+ &memory_op_int8, /*.int8 */
735
+ &memory_op_uint8, /* .uint8 */
736
+ &memory_op_int16, /* .int16 */
737
+ &memory_op_uint16, /* .uint16 */
738
+ &memory_op_int32, /* .int32 */
739
+ &memory_op_uint32, /* .uint32 */
740
+ &memory_op_int64, /* .int64 */
741
+ &memory_op_uint64, /* .uint64 */
742
+ &memory_op_long, /* .slong */
743
+ &memory_op_ulong, /* .uslong */
744
+ &memory_op_float32, /* .float32 */
745
+ &memory_op_float64, /* .float64 */
746
+ &memory_op_longdouble, /* .longdouble */
747
+ &memory_op_pointer, /* .pointer */
748
+ &memory_op_strptr, /* .strptr */
749
+ &memory_op_bool /* .boolOp */
750
+ };
751
+
752
+ void
753
+ rbffi_AbstractMemory_Init(VALUE moduleFFI)
754
+ {
755
+ /*
756
+ * Document-class: FFI::AbstractMemory
757
+ *
758
+ * {AbstractMemory} is the base class for many memory management classes such as {Buffer}.
759
+ *
760
+ * This class has a lot of methods to work with integers :
761
+ * * put_int<i>size</i>(offset, value)
762
+ * * get_int<i>size</i>(offset)
763
+ * * put_uint<i>size</i>(offset, value)
764
+ * * get_uint<i>size</i>(offset)
765
+ * * writeuint<i>size</i>(value)
766
+ * * read_int<i>size</i>
767
+ * * write_uint<i>size</i>(value)
768
+ * * read_uint<i>size</i>
769
+ * * put_array_of_int<i>size</i>(offset, ary)
770
+ * * get_array_of_int<i>size</i>(offset, length)
771
+ * * put_array_of_uint<i>size</i>(offset, ary)
772
+ * * get_array_of_uint<i>size</i>(offset, length)
773
+ * * write_array_of_int<i>size</i>(ary)
774
+ * * read_array_of_int<i>size</i>(length)
775
+ * * write_array_of_uint<i>size</i>(ary)
776
+ * * read_array_of_uint<i>size</i>(length)
777
+ * where _size_ is 8, 16, 32 or 64. Same methods exist for long type.
778
+ *
779
+ * Aliases exist : _char_ for _int8_, _short_ for _int16_, _int_ for _int32_ and <i>long_long</i> for _int64_.
780
+ *
781
+ * Others methods are listed below.
782
+ */
783
+ VALUE classMemory = rb_define_class_under(moduleFFI, "AbstractMemory", rb_cObject);
784
+ rbffi_AbstractMemoryClass = classMemory;
785
+ /*
786
+ * Document-variable: FFI::AbstractMemory
787
+ */
788
+ rb_global_variable(&rbffi_AbstractMemoryClass);
789
+ rb_define_alloc_func(classMemory, memory_allocate);
790
+
791
+ NullPointerErrorClass = rb_define_class_under(moduleFFI, "NullPointerError", rb_eRuntimeError);
792
+ /* Document-variable: NullPointerError */
793
+ rb_global_variable(&NullPointerErrorClass);
794
+
795
+
796
+ #undef INT
797
+ #define INT(type) \
798
+ rb_define_method(classMemory, "put_" #type, memory_put_##type, 2); \
799
+ rb_define_method(classMemory, "get_" #type, memory_get_##type, 1); \
800
+ rb_define_method(classMemory, "put_u" #type, memory_put_u##type, 2); \
801
+ rb_define_method(classMemory, "get_u" #type, memory_get_u##type, 1); \
802
+ rb_define_method(classMemory, "write_" #type, memory_write_##type, 1); \
803
+ rb_define_method(classMemory, "read_" #type, memory_read_##type, 0); \
804
+ rb_define_method(classMemory, "write_u" #type, memory_write_u##type, 1); \
805
+ rb_define_method(classMemory, "read_u" #type, memory_read_u##type, 0); \
806
+ rb_define_method(classMemory, "put_array_of_" #type, memory_put_array_of_##type, 2); \
807
+ rb_define_method(classMemory, "get_array_of_" #type, memory_get_array_of_##type, 2); \
808
+ rb_define_method(classMemory, "put_array_of_u" #type, memory_put_array_of_u##type, 2); \
809
+ rb_define_method(classMemory, "get_array_of_u" #type, memory_get_array_of_u##type, 2); \
810
+ rb_define_method(classMemory, "write_array_of_" #type, memory_write_array_of_##type, 1); \
811
+ rb_define_method(classMemory, "read_array_of_" #type, memory_read_array_of_##type, 1); \
812
+ rb_define_method(classMemory, "write_array_of_u" #type, memory_write_array_of_u##type, 1); \
813
+ rb_define_method(classMemory, "read_array_of_u" #type, memory_read_array_of_u##type, 1);
814
+
815
+ INT(int8);
816
+ INT(int16);
817
+ INT(int32);
818
+ INT(int64);
819
+ INT(long);
820
+
821
+ #define ALIAS(name, old) \
822
+ rb_define_alias(classMemory, "put_" #name, "put_" #old); \
823
+ rb_define_alias(classMemory, "get_" #name, "get_" #old); \
824
+ rb_define_alias(classMemory, "put_u" #name, "put_u" #old); \
825
+ rb_define_alias(classMemory, "get_u" #name, "get_u" #old); \
826
+ rb_define_alias(classMemory, "write_" #name, "write_" #old); \
827
+ rb_define_alias(classMemory, "read_" #name, "read_" #old); \
828
+ rb_define_alias(classMemory, "write_u" #name, "write_u" #old); \
829
+ rb_define_alias(classMemory, "read_u" #name, "read_u" #old); \
830
+ rb_define_alias(classMemory, "put_array_of_" #name, "put_array_of_" #old); \
831
+ rb_define_alias(classMemory, "get_array_of_" #name, "get_array_of_" #old); \
832
+ rb_define_alias(classMemory, "put_array_of_u" #name, "put_array_of_u" #old); \
833
+ rb_define_alias(classMemory, "get_array_of_u" #name, "get_array_of_u" #old); \
834
+ rb_define_alias(classMemory, "write_array_of_" #name, "write_array_of_" #old); \
835
+ rb_define_alias(classMemory, "read_array_of_" #name, "read_array_of_" #old); \
836
+ rb_define_alias(classMemory, "write_array_of_u" #name, "write_array_of_u" #old); \
837
+ rb_define_alias(classMemory, "read_array_of_u" #name, "read_array_of_u" #old);
838
+
839
+ ALIAS(char, int8);
840
+ ALIAS(short, int16);
841
+ ALIAS(int, int32);
842
+ ALIAS(long_long, int64);
843
+
844
+ /*
845
+ * Document-method: put_float32
846
+ * call-seq: memory.put_float32offset, value)
847
+ * @param [Numeric] offset
848
+ * @param [Numeric] value
849
+ * @return [self]
850
+ * Put +value+ as a 32-bit float in memory at offset +offset+ (alias: #put_float).
851
+ */
852
+ rb_define_method(classMemory, "put_float32", memory_put_float32, 2);
853
+ /*
854
+ * Document-method: get_float32
855
+ * call-seq: memory.get_float32(offset)
856
+ * @param [Numeric] offset
857
+ * @return [Float]
858
+ * Get a 32-bit float from memory at offset +offset+ (alias: #get_float).
859
+ */
860
+ rb_define_method(classMemory, "get_float32", memory_get_float32, 1);
861
+ rb_define_alias(classMemory, "put_float", "put_float32");
862
+ rb_define_alias(classMemory, "get_float", "get_float32");
863
+ /*
864
+ * Document-method: write_float
865
+ * call-seq: memory.write_float(value)
866
+ * @param [Numeric] value
867
+ * @return [self]
868
+ * Write +value+ as a 32-bit float in memory.
869
+ *
870
+ * Same as:
871
+ * memory.put_float(0, value)
872
+ */
873
+ rb_define_method(classMemory, "write_float", memory_write_float32, 1);
874
+ /*
875
+ * Document-method: read_float
876
+ * call-seq: memory.read_float
877
+ * @return [Float]
878
+ * Read a 32-bit float from memory.
879
+ *
880
+ * Same as:
881
+ * memory.get_float(0)
882
+ */
883
+ rb_define_method(classMemory, "read_float", memory_read_float32, 0);
884
+ /*
885
+ * Document-method: put_array_of_float32
886
+ * call-seq: memory.put_array_of_float32(offset, ary)
887
+ * @param [Numeric] offset
888
+ * @param [Array<Numeric>] ary
889
+ * @return [self]
890
+ * Put values from +ary+ as 32-bit floats in memory from offset +offset+ (alias: #put_array_of_float).
891
+ */
892
+ rb_define_method(classMemory, "put_array_of_float32", memory_put_array_of_float32, 2);
893
+ /*
894
+ * Document-method: get_array_of_float32
895
+ * call-seq: memory.get_array_of_float32(offset, length)
896
+ * @param [Numeric] offset
897
+ * @param [Numeric] length number of Float to get
898
+ * @return [Array<Float>]
899
+ * Get 32-bit floats in memory from offset +offset+ (alias: #get_array_of_float).
900
+ */
901
+ rb_define_method(classMemory, "get_array_of_float32", memory_get_array_of_float32, 2);
902
+ /*
903
+ * Document-method: write_array_of_float
904
+ * call-seq: memory.write_array_of_float(ary)
905
+ * @param [Array<Numeric>] ary
906
+ * @return [self]
907
+ * Write values from +ary+ as 32-bit floats in memory.
908
+ *
909
+ * Same as:
910
+ * memory.put_array_of_float(0, ary)
911
+ */
912
+ rb_define_method(classMemory, "write_array_of_float", memory_write_array_of_float32, 1);
913
+ /*
914
+ * Document-method: read_array_of_float
915
+ * call-seq: memory.read_array_of_float(length)
916
+ * @param [Numeric] length number of Float to read
917
+ * @return [Array<Float>]
918
+ * Read 32-bit floats from memory.
919
+ *
920
+ * Same as:
921
+ * memory.get_array_of_float(0, ary)
922
+ */
923
+ rb_define_method(classMemory, "read_array_of_float", memory_read_array_of_float32, 1);
924
+ rb_define_alias(classMemory, "put_array_of_float", "put_array_of_float32");
925
+ rb_define_alias(classMemory, "get_array_of_float", "get_array_of_float32");
926
+ /*
927
+ * Document-method: put_float64
928
+ * call-seq: memory.put_float64(offset, value)
929
+ * @param [Numeric] offset
930
+ * @param [Numeric] value
931
+ * @return [self]
932
+ * Put +value+ as a 64-bit float (double) in memory at offset +offset+ (alias: #put_double).
933
+ */
934
+ rb_define_method(classMemory, "put_float64", memory_put_float64, 2);
935
+ /*
936
+ * Document-method: get_float64
937
+ * call-seq: memory.get_float64(offset)
938
+ * @param [Numeric] offset
939
+ * @return [Float]
940
+ * Get a 64-bit float (double) from memory at offset +offset+ (alias: #get_double).
941
+ */
942
+ rb_define_method(classMemory, "get_float64", memory_get_float64, 1);
943
+ rb_define_alias(classMemory, "put_double", "put_float64");
944
+ rb_define_alias(classMemory, "get_double", "get_float64");
945
+ /*
946
+ * Document-method: write_double
947
+ * call-seq: memory.write_double(value)
948
+ * @param [Numeric] value
949
+ * @return [self]
950
+ * Write +value+ as a 64-bit float (double) in memory.
951
+ *
952
+ * Same as:
953
+ * memory.put_double(0, value)
954
+ */
955
+ rb_define_method(classMemory, "write_double", memory_write_float64, 1);
956
+ /*
957
+ * Document-method: read_double
958
+ * call-seq: memory.read_double
959
+ * @return [Float]
960
+ * Read a 64-bit float (double) from memory.
961
+ *
962
+ * Same as:
963
+ * memory.get_double(0)
964
+ */
965
+ rb_define_method(classMemory, "read_double", memory_read_float64, 0);
966
+ /*
967
+ * Document-method: put_array_of_float64
968
+ * call-seq: memory.put_array_of_float64(offset, ary)
969
+ * @param [Numeric] offset
970
+ * @param [Array<Numeric>] ary
971
+ * @return [self]
972
+ * Put values from +ary+ as 64-bit floats (doubles) in memory from offset +offset+ (alias: #put_array_of_double).
973
+ */
974
+ rb_define_method(classMemory, "put_array_of_float64", memory_put_array_of_float64, 2);
975
+ /*
976
+ * Document-method: get_array_of_float64
977
+ * call-seq: memory.get_array_of_float64(offset, length)
978
+ * @param [Numeric] offset
979
+ * @param [Numeric] length number of Float to get
980
+ * @return [Array<Float>]
981
+ * Get 64-bit floats (doubles) in memory from offset +offset+ (alias: #get_array_of_double).
982
+ */
983
+ rb_define_method(classMemory, "get_array_of_float64", memory_get_array_of_float64, 2);
984
+ /*
985
+ * Document-method: write_array_of_double
986
+ * call-seq: memory.write_array_of_double(ary)
987
+ * @param [Array<Numeric>] ary
988
+ * @return [self]
989
+ * Write values from +ary+ as 64-bit floats (doubles) in memory.
990
+ *
991
+ * Same as:
992
+ * memory.put_array_of_double(0, ary)
993
+ */
994
+ rb_define_method(classMemory, "write_array_of_double", memory_write_array_of_float64, 1);
995
+ /*
996
+ * Document-method: read_array_of_double
997
+ * call-seq: memory.read_array_of_double(length)
998
+ * @param [Numeric] length number of Float to read
999
+ * @return [Array<Float>]
1000
+ * Read 64-bit floats (doubles) from memory.
1001
+ *
1002
+ * Same as:
1003
+ * memory.get_array_of_double(0, ary)
1004
+ */
1005
+ rb_define_method(classMemory, "read_array_of_double", memory_read_array_of_float64, 1);
1006
+ rb_define_alias(classMemory, "put_array_of_double", "put_array_of_float64");
1007
+ rb_define_alias(classMemory, "get_array_of_double", "get_array_of_float64");
1008
+ /*
1009
+ * Document-method: put_pointer
1010
+ * call-seq: memory.put_pointer(offset, value)
1011
+ * @param [Numeric] offset
1012
+ * @param [nil,Pointer, Integer, #to_ptr] value
1013
+ * @return [self]
1014
+ * Put +value+ in memory from +offset+..
1015
+ */
1016
+ rb_define_method(classMemory, "put_pointer", memory_put_pointer, 2);
1017
+ /*
1018
+ * Document-method: get_pointer
1019
+ * call-seq: memory.get_pointer(offset)
1020
+ * @param [Numeric] offset
1021
+ * @return [Pointer]
1022
+ * Get a {Pointer} to the memory from +offset+.
1023
+ */
1024
+ rb_define_method(classMemory, "get_pointer", memory_get_pointer, 1);
1025
+ /*
1026
+ * Document-method: write_pointer
1027
+ * call-seq: memory.write_pointer(value)
1028
+ * @param [nil,Pointer, Integer, #to_ptr] value
1029
+ * @return [self]
1030
+ * Write +value+ in memory.
1031
+ *
1032
+ * Equivalent to:
1033
+ * memory.put_pointer(0, value)
1034
+ */
1035
+ rb_define_method(classMemory, "write_pointer", memory_write_pointer, 1);
1036
+ /*
1037
+ * Document-method: read_pointer
1038
+ * call-seq: memory.read_pointer
1039
+ * @return [Pointer]
1040
+ * Get a {Pointer} to the memory from base address.
1041
+ *
1042
+ * Equivalent to:
1043
+ * memory.get_pointer(0)
1044
+ */
1045
+ rb_define_method(classMemory, "read_pointer", memory_read_pointer, 0);
1046
+ /*
1047
+ * Document-method: put_array_of_pointer
1048
+ * call-seq: memory.put_array_of_pointer(offset, ary)
1049
+ * @param [Numeric] offset
1050
+ * @param [Array<#to_ptr>] ary
1051
+ * @return [self]
1052
+ * Put an array of {Pointer} into memory from +offset+.
1053
+ */
1054
+ rb_define_method(classMemory, "put_array_of_pointer", memory_put_array_of_pointer, 2);
1055
+ /*
1056
+ * Document-method: get_array_of_pointer
1057
+ * call-seq: memory.get_array_of_pointer(offset, length)
1058
+ * @param [Numeric] offset
1059
+ * @param [Numeric] length
1060
+ * @return [Array<Pointer>]
1061
+ * Get an array of {Pointer} of length +length+ from +offset+.
1062
+ */
1063
+ rb_define_method(classMemory, "get_array_of_pointer", memory_get_array_of_pointer, 2);
1064
+ /*
1065
+ * Document-method: write_array_of_pointer
1066
+ * call-seq: memory.write_array_of_pointer(ary)
1067
+ * @param [Array<#to_ptr>] ary
1068
+ * @return [self]
1069
+ * Write an array of {Pointer} into memory from +offset+.
1070
+ *
1071
+ * Same as :
1072
+ * memory.put_array_of_pointer(0, ary)
1073
+ */
1074
+ rb_define_method(classMemory, "write_array_of_pointer", memory_write_array_of_pointer, 1);
1075
+ /*
1076
+ * Document-method: read_array_of_pointer
1077
+ * call-seq: memory.read_array_of_pointer(length)
1078
+ * @param [Numeric] length
1079
+ * @return [Array<Pointer>]
1080
+ * Read an array of {Pointer} of length +length+.
1081
+ *
1082
+ * Same as:
1083
+ * memory.get_array_of_pointer(0, length)
1084
+ */
1085
+ rb_define_method(classMemory, "read_array_of_pointer", memory_read_array_of_pointer, 1);
1086
+
1087
+ rb_define_method(classMemory, "get_string", memory_get_string, -1);
1088
+ rb_define_method(classMemory, "put_string", memory_put_string, 2);
1089
+ rb_define_method(classMemory, "get_bytes", memory_get_bytes, 2);
1090
+ rb_define_method(classMemory, "put_bytes", memory_put_bytes, -1);
1091
+ rb_define_method(classMemory, "read_bytes", memory_read_bytes, 1);
1092
+ rb_define_method(classMemory, "write_bytes", memory_write_bytes, -1);
1093
+ rb_define_method(classMemory, "get_array_of_string", memory_get_array_of_string, -1);
1094
+
1095
+ rb_define_method(classMemory, "get", memory_get, 2);
1096
+ rb_define_method(classMemory, "put", memory_put, 3);
1097
+
1098
+ rb_define_method(classMemory, "clear", memory_clear, 0);
1099
+ rb_define_method(classMemory, "total", memory_size, 0);
1100
+ rb_define_alias(classMemory, "size", "total");
1101
+ rb_define_method(classMemory, "type_size", memory_type_size, 0);
1102
+ rb_define_method(classMemory, "[]", memory_aref, 1);
1103
+ rb_define_method(classMemory, "__copy_from__", memory_copy_from, 2);
1104
+
1105
+ id_to_ptr = rb_intern("to_ptr");
1106
+ id_call = rb_intern("call");
1107
+ id_plus = rb_intern("+");
1108
+ }
1109
+