ffi 1.11.1-java → 1.13.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +3 -0
- data/.appveyor.yml +30 -0
- data/.github/workflows/ci.yml +64 -0
- data/.gitignore +25 -0
- data/.gitmodules +4 -0
- data/.travis.yml +58 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +80 -0
- data/Gemfile +17 -0
- data/LICENSE.SPECS +22 -0
- data/README.md +10 -1
- data/Rakefile +25 -43
- data/ffi.gemspec +43 -0
- data/lib/ffi.rb +28 -0
- data/lib/ffi/autopointer.rb +203 -0
- data/lib/ffi/buffer.rb +4 -0
- data/lib/ffi/callback.rb +4 -0
- data/lib/ffi/data_converter.rb +67 -0
- data/lib/ffi/enum.rb +296 -0
- data/lib/ffi/errno.rb +43 -0
- data/lib/ffi/ffi.rb +46 -0
- data/lib/ffi/io.rb +62 -0
- data/lib/ffi/library.rb +592 -0
- data/lib/ffi/managedstruct.rb +84 -0
- data/lib/ffi/memorypointer.rb +1 -0
- data/lib/ffi/platform.rb +175 -0
- data/lib/ffi/platform/aarch64-freebsd/types.conf +128 -0
- data/lib/ffi/platform/aarch64-freebsd12/types.conf +128 -0
- data/lib/ffi/platform/aarch64-linux/types.conf +104 -0
- data/lib/ffi/platform/arm-freebsd/types.conf +152 -0
- data/lib/ffi/platform/arm-freebsd12/types.conf +152 -0
- data/lib/ffi/platform/arm-linux/types.conf +132 -0
- data/lib/ffi/platform/i386-cygwin/types.conf +3 -0
- data/lib/ffi/platform/i386-darwin/types.conf +100 -0
- data/lib/ffi/platform/i386-freebsd/types.conf +152 -0
- data/lib/ffi/platform/i386-freebsd12/types.conf +152 -0
- data/lib/ffi/platform/i386-gnu/types.conf +107 -0
- data/lib/ffi/platform/i386-linux/types.conf +103 -0
- data/lib/ffi/platform/i386-netbsd/types.conf +126 -0
- data/lib/ffi/platform/i386-openbsd/types.conf +128 -0
- data/lib/ffi/platform/i386-solaris/types.conf +122 -0
- data/lib/ffi/platform/i386-windows/types.conf +52 -0
- data/lib/ffi/platform/ia64-linux/types.conf +104 -0
- data/lib/ffi/platform/mips-linux/types.conf +102 -0
- data/lib/ffi/platform/mips64-linux/types.conf +104 -0
- data/lib/ffi/platform/mips64el-linux/types.conf +104 -0
- data/lib/ffi/platform/mipsel-linux/types.conf +102 -0
- data/lib/ffi/platform/mipsisa32r6-linux/types.conf +102 -0
- data/lib/ffi/platform/mipsisa32r6el-linux/types.conf +102 -0
- data/lib/ffi/platform/mipsisa64r6-linux/types.conf +104 -0
- data/lib/ffi/platform/mipsisa64r6el-linux/types.conf +104 -0
- data/lib/ffi/platform/powerpc-aix/types.conf +180 -0
- data/lib/ffi/platform/powerpc-darwin/types.conf +100 -0
- data/lib/ffi/platform/powerpc-linux/types.conf +130 -0
- data/lib/ffi/platform/powerpc-openbsd/types.conf +156 -0
- data/lib/ffi/platform/powerpc64-linux/types.conf +104 -0
- data/lib/ffi/platform/s390-linux/types.conf +102 -0
- data/lib/ffi/platform/s390x-linux/types.conf +102 -0
- data/lib/ffi/platform/sparc-linux/types.conf +102 -0
- data/lib/ffi/platform/sparc-solaris/types.conf +128 -0
- data/lib/ffi/platform/sparc64-linux/types.conf +102 -0
- data/lib/ffi/platform/sparcv9-openbsd/types.conf +156 -0
- data/lib/ffi/platform/sparcv9-solaris/types.conf +128 -0
- data/lib/ffi/platform/x86_64-cygwin/types.conf +3 -0
- data/lib/ffi/platform/x86_64-darwin/types.conf +130 -0
- data/lib/ffi/platform/x86_64-dragonflybsd/types.conf +148 -0
- data/lib/ffi/platform/x86_64-freebsd/types.conf +128 -0
- data/lib/ffi/platform/x86_64-freebsd12/types.conf +158 -0
- data/lib/ffi/platform/x86_64-linux/types.conf +132 -0
- data/lib/ffi/platform/x86_64-netbsd/types.conf +128 -0
- data/lib/ffi/platform/x86_64-openbsd/types.conf +134 -0
- data/lib/ffi/platform/x86_64-solaris/types.conf +122 -0
- data/lib/ffi/platform/x86_64-windows/types.conf +52 -0
- data/lib/ffi/pointer.rb +167 -0
- data/lib/ffi/struct.rb +316 -0
- data/lib/ffi/struct_by_reference.rb +72 -0
- data/lib/ffi/struct_layout.rb +96 -0
- data/lib/ffi/struct_layout_builder.rb +227 -0
- data/lib/ffi/tools/const_generator.rb +230 -0
- data/lib/ffi/tools/generator.rb +105 -0
- data/lib/ffi/tools/generator_task.rb +32 -0
- data/lib/ffi/tools/struct_generator.rb +194 -0
- data/lib/ffi/tools/types_generator.rb +137 -0
- data/lib/ffi/types.rb +194 -0
- data/lib/ffi/union.rb +43 -0
- data/lib/ffi/variadic.rb +78 -0
- data/lib/ffi/version.rb +3 -0
- data/samples/getlogin.rb +8 -0
- data/samples/getpid.rb +8 -0
- data/samples/gettimeofday.rb +18 -0
- data/samples/hello.rb +8 -0
- data/samples/inotify.rb +60 -0
- data/samples/pty.rb +75 -0
- data/samples/qsort.rb +20 -0
- metadata +198 -9
- metadata.gz.sig +0 -0
data/lib/ffi/struct.rb
ADDED
@@ -0,0 +1,316 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2008-2010 Wayne Meissner
|
3
|
+
# Copyright (C) 2008, 2009 Andrea Fazzi
|
4
|
+
# Copyright (C) 2008, 2009 Luc Heinrich
|
5
|
+
#
|
6
|
+
# This file is part of ruby-ffi.
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice, this
|
14
|
+
# list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the Ruby FFI project nor the names of its contributors
|
19
|
+
# may be used to endorse or promote products derived from this software
|
20
|
+
# without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
25
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
26
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
27
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
28
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
29
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
30
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
31
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
#
|
33
|
+
|
34
|
+
require 'ffi/platform'
|
35
|
+
require 'ffi/struct_layout'
|
36
|
+
require 'ffi/struct_layout_builder'
|
37
|
+
require 'ffi/struct_by_reference'
|
38
|
+
|
39
|
+
module FFI
|
40
|
+
|
41
|
+
class Struct
|
42
|
+
|
43
|
+
# Get struct size
|
44
|
+
# @return [Numeric]
|
45
|
+
def size
|
46
|
+
self.class.size
|
47
|
+
end
|
48
|
+
|
49
|
+
# @return [Fixnum] Struct alignment
|
50
|
+
def alignment
|
51
|
+
self.class.alignment
|
52
|
+
end
|
53
|
+
alias_method :align, :alignment
|
54
|
+
|
55
|
+
# (see FFI::StructLayout#offset_of)
|
56
|
+
def offset_of(name)
|
57
|
+
self.class.offset_of(name)
|
58
|
+
end
|
59
|
+
|
60
|
+
# (see FFI::StructLayout#members)
|
61
|
+
def members
|
62
|
+
self.class.members
|
63
|
+
end
|
64
|
+
|
65
|
+
# @return [Array]
|
66
|
+
# Get array of values from Struct fields.
|
67
|
+
def values
|
68
|
+
members.map { |m| self[m] }
|
69
|
+
end
|
70
|
+
|
71
|
+
# (see FFI::StructLayout#offsets)
|
72
|
+
def offsets
|
73
|
+
self.class.offsets
|
74
|
+
end
|
75
|
+
|
76
|
+
# Clear the struct content.
|
77
|
+
# @return [self]
|
78
|
+
def clear
|
79
|
+
pointer.clear
|
80
|
+
self
|
81
|
+
end
|
82
|
+
|
83
|
+
# Get {Pointer} to struct content.
|
84
|
+
# @return [AbstractMemory]
|
85
|
+
def to_ptr
|
86
|
+
pointer
|
87
|
+
end
|
88
|
+
|
89
|
+
# Get struct size
|
90
|
+
# @return [Numeric]
|
91
|
+
def self.size
|
92
|
+
defined?(@layout) ? @layout.size : defined?(@size) ? @size : 0
|
93
|
+
end
|
94
|
+
|
95
|
+
# set struct size
|
96
|
+
# @param [Numeric] size
|
97
|
+
# @return [size]
|
98
|
+
def self.size=(size)
|
99
|
+
raise ArgumentError, "Size already set" if defined?(@size) || defined?(@layout)
|
100
|
+
@size = size
|
101
|
+
end
|
102
|
+
|
103
|
+
# @return (see Struct#alignment)
|
104
|
+
def self.alignment
|
105
|
+
@layout.alignment
|
106
|
+
end
|
107
|
+
|
108
|
+
# (see FFI::Type#members)
|
109
|
+
def self.members
|
110
|
+
@layout.members
|
111
|
+
end
|
112
|
+
|
113
|
+
# (see FFI::StructLayout#offsets)
|
114
|
+
def self.offsets
|
115
|
+
@layout.offsets
|
116
|
+
end
|
117
|
+
|
118
|
+
# (see FFI::StructLayout#offset_of)
|
119
|
+
def self.offset_of(name)
|
120
|
+
@layout.offset_of(name)
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.in
|
124
|
+
ptr(:in)
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.out
|
128
|
+
ptr(:out)
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.ptr(flags = :inout)
|
132
|
+
@ref_data_type ||= Type::Mapped.new(StructByReference.new(self))
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.val
|
136
|
+
@val_data_type ||= StructByValue.new(self)
|
137
|
+
end
|
138
|
+
|
139
|
+
def self.by_value
|
140
|
+
self.val
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.by_ref(flags = :inout)
|
144
|
+
self.ptr(flags)
|
145
|
+
end
|
146
|
+
|
147
|
+
class ManagedStructConverter < StructByReference
|
148
|
+
|
149
|
+
# @param [Struct] struct_class
|
150
|
+
def initialize(struct_class)
|
151
|
+
super(struct_class)
|
152
|
+
|
153
|
+
raise NoMethodError, "release() not implemented for class #{struct_class}" unless struct_class.respond_to? :release
|
154
|
+
@method = struct_class.method(:release)
|
155
|
+
end
|
156
|
+
|
157
|
+
# @param [Pointer] ptr
|
158
|
+
# @param [nil] ctx
|
159
|
+
# @return [Struct]
|
160
|
+
def from_native(ptr, ctx)
|
161
|
+
struct_class.new(AutoPointer.new(ptr, @method))
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def self.auto_ptr
|
166
|
+
@managed_type ||= Type::Mapped.new(ManagedStructConverter.new(self))
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
class << self
|
171
|
+
public
|
172
|
+
|
173
|
+
# @return [StructLayout]
|
174
|
+
# @overload layout
|
175
|
+
# @return [StructLayout]
|
176
|
+
# Get struct layout.
|
177
|
+
# @overload layout(*spec)
|
178
|
+
# @param [Array<Symbol, Integer>,Array(Hash)] spec
|
179
|
+
# @return [StructLayout]
|
180
|
+
# Create struct layout from +spec+.
|
181
|
+
# @example Creating a layout from an array +spec+
|
182
|
+
# class MyStruct < Struct
|
183
|
+
# layout :field1, :int,
|
184
|
+
# :field2, :pointer,
|
185
|
+
# :field3, :string
|
186
|
+
# end
|
187
|
+
# @example Creating a layout from an array +spec+ with offset
|
188
|
+
# class MyStructWithOffset < Struct
|
189
|
+
# layout :field1, :int,
|
190
|
+
# :field2, :pointer, 6, # set offset to 6 for this field
|
191
|
+
# :field3, :string
|
192
|
+
# end
|
193
|
+
# @example Creating a layout from a hash +spec+
|
194
|
+
# class MyStructFromHash < Struct
|
195
|
+
# layout :field1 => :int,
|
196
|
+
# :field2 => :pointer,
|
197
|
+
# :field3 => :string
|
198
|
+
# end
|
199
|
+
# @example Creating a layout with pointers to functions
|
200
|
+
# class MyFunctionTable < Struct
|
201
|
+
# layout :function1, callback([:int, :int], :int),
|
202
|
+
# :function2, callback([:pointer], :void),
|
203
|
+
# :field3, :string
|
204
|
+
# end
|
205
|
+
def layout(*spec)
|
206
|
+
warn "[DEPRECATION] Struct layout is already defined for class #{self.inspect}. Redefinition as in #{caller[0]} will be disallowed in ffi-2.0." if defined?(@layout)
|
207
|
+
return @layout if spec.size == 0
|
208
|
+
|
209
|
+
builder = StructLayoutBuilder.new
|
210
|
+
builder.union = self < Union
|
211
|
+
builder.packed = @packed if defined?(@packed)
|
212
|
+
builder.alignment = @min_alignment if defined?(@min_alignment)
|
213
|
+
|
214
|
+
if spec[0].kind_of?(Hash)
|
215
|
+
hash_layout(builder, spec)
|
216
|
+
else
|
217
|
+
array_layout(builder, spec)
|
218
|
+
end
|
219
|
+
builder.size = @size if defined?(@size) && @size > builder.size
|
220
|
+
cspec = builder.build
|
221
|
+
@layout = cspec unless self == Struct
|
222
|
+
@size = cspec.size
|
223
|
+
return cspec
|
224
|
+
end
|
225
|
+
|
226
|
+
|
227
|
+
protected
|
228
|
+
|
229
|
+
def callback(params, ret)
|
230
|
+
mod = enclosing_module
|
231
|
+
ret_type = find_type(ret, mod)
|
232
|
+
if ret_type == Type::STRING
|
233
|
+
raise TypeError, ":string is not allowed as return type of callbacks"
|
234
|
+
end
|
235
|
+
FFI::CallbackInfo.new(ret_type, params.map { |e| find_type(e, mod) })
|
236
|
+
end
|
237
|
+
|
238
|
+
def packed(packed = 1)
|
239
|
+
@packed = packed
|
240
|
+
end
|
241
|
+
alias :pack :packed
|
242
|
+
|
243
|
+
def aligned(alignment = 1)
|
244
|
+
@min_alignment = alignment
|
245
|
+
end
|
246
|
+
alias :align :aligned
|
247
|
+
|
248
|
+
def enclosing_module
|
249
|
+
begin
|
250
|
+
mod = self.name.split("::")[0..-2].inject(Object) { |obj, c| obj.const_get(c) }
|
251
|
+
if mod.respond_to?(:find_type) && (mod.is_a?(FFI::Library) || mod < FFI::Struct)
|
252
|
+
mod
|
253
|
+
end
|
254
|
+
rescue Exception
|
255
|
+
nil
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
|
260
|
+
def find_field_type(type, mod = enclosing_module)
|
261
|
+
if type.kind_of?(Class) && type < Struct
|
262
|
+
FFI::Type::Struct.new(type)
|
263
|
+
|
264
|
+
elsif type.kind_of?(Class) && type < FFI::StructLayout::Field
|
265
|
+
type
|
266
|
+
|
267
|
+
elsif type.kind_of?(::Array)
|
268
|
+
FFI::Type::Array.new(find_field_type(type[0]), type[1])
|
269
|
+
|
270
|
+
else
|
271
|
+
find_type(type, mod)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
def find_type(type, mod = enclosing_module)
|
276
|
+
if mod
|
277
|
+
mod.find_type(type)
|
278
|
+
end || FFI.find_type(type)
|
279
|
+
end
|
280
|
+
|
281
|
+
private
|
282
|
+
|
283
|
+
# @param [StructLayoutBuilder] builder
|
284
|
+
# @param [Hash] spec
|
285
|
+
# @return [builder]
|
286
|
+
# Add hash +spec+ to +builder+.
|
287
|
+
def hash_layout(builder, spec)
|
288
|
+
spec[0].each do |name, type|
|
289
|
+
builder.add name, find_field_type(type), nil
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
# @param [StructLayoutBuilder] builder
|
294
|
+
# @param [Array<Symbol, Integer>] spec
|
295
|
+
# @return [builder]
|
296
|
+
# Add array +spec+ to +builder+.
|
297
|
+
def array_layout(builder, spec)
|
298
|
+
i = 0
|
299
|
+
while i < spec.size
|
300
|
+
name, type = spec[i, 2]
|
301
|
+
i += 2
|
302
|
+
|
303
|
+
# If the next param is a Integer, it specifies the offset
|
304
|
+
if spec[i].kind_of?(Integer)
|
305
|
+
offset = spec[i]
|
306
|
+
i += 1
|
307
|
+
else
|
308
|
+
offset = nil
|
309
|
+
end
|
310
|
+
|
311
|
+
builder.add name, find_field_type(type), offset
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 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
|
+
# This class includes the {FFI::DataConverter} module.
|
33
|
+
class StructByReference
|
34
|
+
include DataConverter
|
35
|
+
|
36
|
+
attr_reader :struct_class
|
37
|
+
|
38
|
+
# @param [Struct] struct_class
|
39
|
+
def initialize(struct_class)
|
40
|
+
unless Class === struct_class and struct_class < FFI::Struct
|
41
|
+
raise TypeError, 'wrong type (expected subclass of FFI::Struct)'
|
42
|
+
end
|
43
|
+
@struct_class = struct_class
|
44
|
+
end
|
45
|
+
|
46
|
+
# Always get {FFI::Type}::POINTER.
|
47
|
+
def native_type
|
48
|
+
FFI::Type::POINTER
|
49
|
+
end
|
50
|
+
|
51
|
+
# @param [nil, Struct] value
|
52
|
+
# @param [nil] ctx
|
53
|
+
# @return [AbstractMemory] Pointer on +value+.
|
54
|
+
def to_native(value, ctx)
|
55
|
+
return Pointer::NULL if value.nil?
|
56
|
+
|
57
|
+
unless @struct_class === value
|
58
|
+
raise TypeError, "wrong argument type #{value.class} (expected #{@struct_class})"
|
59
|
+
end
|
60
|
+
|
61
|
+
value.pointer
|
62
|
+
end
|
63
|
+
|
64
|
+
# @param [AbstractMemory] value
|
65
|
+
# @param [nil] ctx
|
66
|
+
# @return [Struct]
|
67
|
+
# Create a struct from content of memory +value+.
|
68
|
+
def from_native(value, ctx)
|
69
|
+
@struct_class.new(value)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2008-2010 Wayne Meissner
|
3
|
+
# Copyright (C) 2008, 2009 Andrea Fazzi
|
4
|
+
# Copyright (C) 2008, 2009 Luc Heinrich
|
5
|
+
#
|
6
|
+
# This file is part of ruby-ffi.
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice, this
|
14
|
+
# list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the Ruby FFI project nor the names of its contributors
|
19
|
+
# may be used to endorse or promote products derived from this software
|
20
|
+
# without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
25
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
26
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
27
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
28
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
29
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
30
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
31
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
#
|
33
|
+
|
34
|
+
module FFI
|
35
|
+
|
36
|
+
class StructLayout
|
37
|
+
|
38
|
+
# @return [Array<Array(Symbol, Numeric)>
|
39
|
+
# Get an array of tuples (field name, offset of the field).
|
40
|
+
def offsets
|
41
|
+
members.map { |m| [ m, self[m].offset ] }
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [Numeric]
|
45
|
+
# Get the offset of a field.
|
46
|
+
def offset_of(field_name)
|
47
|
+
self[field_name].offset
|
48
|
+
end
|
49
|
+
|
50
|
+
# An enum {Field} in a {StructLayout}.
|
51
|
+
class Enum < Field
|
52
|
+
|
53
|
+
# @param [AbstractMemory] ptr pointer on a {Struct}
|
54
|
+
# @return [Object]
|
55
|
+
# Get an object of type {#type} from memory pointed by +ptr+.
|
56
|
+
def get(ptr)
|
57
|
+
type.find(ptr.get_int(offset))
|
58
|
+
end
|
59
|
+
|
60
|
+
# @param [AbstractMemory] ptr pointer on a {Struct}
|
61
|
+
# @param value
|
62
|
+
# @return [nil]
|
63
|
+
# Set +value+ into memory pointed by +ptr+.
|
64
|
+
def put(ptr, value)
|
65
|
+
ptr.put_int(offset, type.find(value))
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
class InnerStruct < Field
|
71
|
+
def get(ptr)
|
72
|
+
type.struct_class.new(ptr.slice(self.offset, self.size))
|
73
|
+
end
|
74
|
+
|
75
|
+
def put(ptr, value)
|
76
|
+
raise TypeError, "wrong value type (expected #{type.struct_class})" unless value.is_a?(type.struct_class)
|
77
|
+
ptr.slice(self.offset, self.size).__copy_from__(value.pointer, self.size)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class Mapped < Field
|
82
|
+
def initialize(name, offset, type, orig_field)
|
83
|
+
super(name, offset, type)
|
84
|
+
@orig_field = orig_field
|
85
|
+
end
|
86
|
+
|
87
|
+
def get(ptr)
|
88
|
+
type.from_native(@orig_field.get(ptr), nil)
|
89
|
+
end
|
90
|
+
|
91
|
+
def put(ptr, value)
|
92
|
+
@orig_field.put(ptr, type.to_native(value, nil))
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|