ffi 0.6.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ffi might be problematic. Click here for more details.

Files changed (92) hide show
  1. data/History.txt +7 -0
  2. data/LICENSE +10 -21
  3. data/README.rdoc +70 -0
  4. data/Rakefile +56 -84
  5. data/ext/ffi_c/AbstractMemory.c +56 -38
  6. data/ext/ffi_c/AbstractMemory.h +15 -22
  7. data/ext/ffi_c/Buffer.c +61 -22
  8. data/ext/ffi_c/Call.c +52 -540
  9. data/ext/ffi_c/Call.h +1 -1
  10. data/ext/ffi_c/DataConverter.c +62 -0
  11. data/ext/ffi_c/DynamicLibrary.c +21 -1
  12. data/ext/ffi_c/Function.c +252 -30
  13. data/ext/ffi_c/MappedType.c +146 -0
  14. data/{libtest/FunctionTest.c → ext/ffi_c/MappedType.h} +32 -25
  15. data/ext/ffi_c/MemoryPointer.c +12 -33
  16. data/ext/ffi_c/Platform.c +2 -0
  17. data/ext/ffi_c/Pointer.c +66 -28
  18. data/ext/ffi_c/Struct.c +19 -306
  19. data/ext/ffi_c/Struct.h +6 -0
  20. data/ext/ffi_c/StructByReference.c +150 -0
  21. data/{libtest/LastErrorTest.c → ext/ffi_c/StructByReference.h} +30 -21
  22. data/ext/ffi_c/StructLayout.c +26 -16
  23. data/ext/ffi_c/Type.c +39 -68
  24. data/ext/ffi_c/Type.h +12 -22
  25. data/ext/ffi_c/Types.c +20 -5
  26. data/ext/ffi_c/Types.h +7 -7
  27. data/ext/ffi_c/Variadic.c +21 -17
  28. data/ext/ffi_c/extconf.rb +4 -0
  29. data/ext/ffi_c/ffi.c +8 -2
  30. data/ext/ffi_c/rbffi.h +1 -0
  31. data/lib/ffi/autopointer.rb +23 -22
  32. data/lib/ffi/enum.rb +36 -21
  33. data/lib/ffi/errno.rb +20 -0
  34. data/lib/ffi/ffi.rb +13 -80
  35. data/lib/ffi/io.rb +12 -20
  36. data/lib/ffi/library.rb +109 -92
  37. data/lib/ffi/managedstruct.rb +1 -1
  38. data/lib/ffi/memorypointer.rb +15 -21
  39. data/lib/ffi/platform.rb +27 -33
  40. data/lib/ffi/pointer.rb +14 -21
  41. data/lib/ffi/struct.rb +98 -49
  42. data/lib/ffi/struct_layout_builder.rb +158 -0
  43. data/lib/ffi/types.rb +99 -128
  44. data/lib/ffi/union.rb +20 -0
  45. data/lib/ffi/variadic.rb +33 -22
  46. data/spec/ffi/async_callback_spec.rb +23 -0
  47. data/spec/ffi/callback_spec.rb +62 -0
  48. data/spec/ffi/custom_param_type.rb +31 -0
  49. data/spec/ffi/custom_type_spec.rb +73 -0
  50. data/spec/ffi/enum_spec.rb +19 -0
  51. data/spec/ffi/ffi_spec.rb +24 -0
  52. data/spec/ffi/pointer_spec.rb +15 -0
  53. data/spec/ffi/rbx/memory_pointer_spec.rb +7 -1
  54. data/spec/ffi/strptr_spec.rb +36 -0
  55. data/spec/ffi/struct_packed_spec.rb +46 -0
  56. data/spec/ffi/struct_spec.rb +19 -5
  57. data/spec/ffi/typedef_spec.rb +14 -0
  58. data/tasks/ann.rake +80 -0
  59. data/tasks/extension.rake +25 -0
  60. data/tasks/gem.rake +200 -0
  61. data/tasks/git.rake +41 -0
  62. data/tasks/notes.rake +27 -0
  63. data/tasks/post_load.rake +34 -0
  64. data/tasks/rdoc.rake +50 -0
  65. data/tasks/rubyforge.rake +55 -0
  66. data/tasks/setup.rb +301 -0
  67. data/tasks/spec.rake +54 -0
  68. data/tasks/svn.rake +47 -0
  69. data/tasks/test.rake +40 -0
  70. metadata +139 -131
  71. data/README.md +0 -109
  72. data/ext/ffi_c/AutoPointer.c +0 -60
  73. data/ext/ffi_c/AutoPointer.h +0 -18
  74. data/ext/ffi_c/Ffi_c.iml +0 -12
  75. data/ffi.gemspec +0 -18
  76. data/gen/log +0 -1
  77. data/lib/Lib.iml +0 -21
  78. data/libtest/Benchmark.c +0 -73
  79. data/libtest/BoolTest.c +0 -52
  80. data/libtest/BufferTest.c +0 -52
  81. data/libtest/ClosureTest.c +0 -173
  82. data/libtest/EnumTest.c +0 -55
  83. data/libtest/GNUmakefile +0 -141
  84. data/libtest/GlobalVariable.c +0 -56
  85. data/libtest/NumberTest.c +0 -145
  86. data/libtest/PointerTest.c +0 -84
  87. data/libtest/ReferenceTest.c +0 -44
  88. data/libtest/StringTest.c +0 -55
  89. data/libtest/StructTest.c +0 -247
  90. data/libtest/UnionTest.c +0 -64
  91. data/libtest/VariadicTest.c +0 -57
  92. data/spec/ffi/Ffi.iml +0 -12
data/lib/ffi/io.rb CHANGED
@@ -3,28 +3,20 @@
3
3
  #
4
4
  # All rights reserved.
5
5
  #
6
- # Redistribution and use in source and binary forms, with or without
7
- # modification, are permitted provided that the following conditions are met:
6
+ # This file is part of ruby-ffi.
8
7
  #
9
- # * Redistributions of source code must retain the above copyright notice, this
10
- # list of conditions and the following disclaimer.
11
- # * Redistributions in binary form must reproduce the above copyright notice
12
- # this list of conditions and the following disclaimer in the documentation
13
- # and/or other materials provided with the distribution.
14
- # * Neither the name of the Evan Phoenix nor the names of its contributors
15
- # may be used to endorse or promote products derived from this software
16
- # without specific prior written permission.
8
+ # This code is free software: you can redistribute it and/or modify it under
9
+ # the terms of the GNU Lesser General Public License version 3 only, as
10
+ # published by the Free Software Foundation.
11
+ #
12
+ # This code is distributed in the hope that it will be useful, but WITHOUT
13
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15
+ # version 3 for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
17
19
  #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
20
 
29
21
  module FFI
30
22
  module IO
data/lib/ffi/library.rb CHANGED
@@ -1,35 +1,46 @@
1
1
  #
2
- # Copyright (C) 2008, 2009 Wayne Meissner
2
+ # Copyright (C) 2008-2010 Wayne Meissner
3
3
  # Copyright (C) 2008 Luc Heinrich <luc@honk-honk.com>
4
- # Copyright (c) 2007, 2008 Evan Phoenix
4
+ #
5
5
  # All rights reserved.
6
6
  #
7
- # Redistribution and use in source and binary forms, with or without
8
- # modification, are permitted provided that the following conditions are met:
7
+ # This file is part of ruby-ffi.
8
+ #
9
+ # This code is free software: you can redistribute it and/or modify it under
10
+ # the terms of the GNU Lesser General Public License version 3 only, as
11
+ # published by the Free Software Foundation.
9
12
  #
10
- # * Redistributions of source code must retain the above copyright notice, this
11
- # list of conditions and the following disclaimer.
12
- # * Redistributions in binary form must reproduce the above copyright notice
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- # * Neither the name of the Evan Phoenix nor the names of its contributors
16
- # may be used to endorse or promote products derived from this software
17
- # without specific prior written permission.
13
+ # This code is distributed in the hope that it will be useful, but WITHOUT
14
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16
+ # version 3 for more details.
18
17
  #
19
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
23
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18
+ # You should have received a copy of the GNU Lesser General Public License
19
+ # version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
29
20
 
30
21
  module FFI
31
22
  CURRENT_PROCESS = USE_THIS_PROCESS_AS_LIBRARY = Object.new
32
23
 
24
+ def self.map_library_name(lib)
25
+ # Mangle the library name to reflect the native library naming conventions
26
+ lib = lib.to_s unless lib.kind_of?(String)
27
+ lib = Library::LIBC if lib == 'c'
28
+
29
+ if lib && File.basename(lib) == lib
30
+ lib = Platform::LIBPREFIX + lib unless lib =~ /^#{Platform::LIBPREFIX}/
31
+ r = Platform::IS_LINUX ? "\\.so($|\\.[1234567890]+)" : "\\.#{Platform::LIBSUFFIX}$"
32
+ lib += ".#{Platform::LIBSUFFIX}" unless lib =~ /#{r}/
33
+ end
34
+
35
+ lib
36
+ end
37
+
38
+ class NotFoundError < LoadError
39
+ def initialize(function, *libraries)
40
+ super("Function '#{function}' not found in [#{libraries[0].nil? ? 'current process' : libraries.join(", ")}]")
41
+ end
42
+ end
43
+
33
44
  module Library
34
45
  CURRENT_PROCESS = FFI::CURRENT_PROCESS
35
46
  LIBC = FFI::Platform::LIBC
@@ -39,10 +50,12 @@ module FFI
39
50
  end
40
51
 
41
52
  def ffi_lib(*names)
42
-
53
+ lib_flags = defined?(@ffi_lib_flags) ? @ffi_lib_flags : FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL
43
54
  ffi_libs = names.map do |name|
55
+
44
56
  if name == FFI::CURRENT_PROCESS
45
57
  FFI::DynamicLibrary.open(nil, FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL)
58
+
46
59
  else
47
60
  libnames = (name.is_a?(::Array) ? name : [ name ]).map { |n| [ n, FFI.map_library_name(n) ].uniq }.flatten.compact
48
61
  lib = nil
@@ -50,8 +63,9 @@ module FFI
50
63
 
51
64
  libnames.each do |libname|
52
65
  begin
53
- lib = FFI::DynamicLibrary.open(libname, FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL)
66
+ lib = FFI::DynamicLibrary.open(libname, lib_flags)
54
67
  break if lib
68
+
55
69
  rescue Exception => ex
56
70
  errors[libname] = ex
57
71
  end
@@ -80,66 +94,63 @@ module FFI
80
94
  @ffi_libs
81
95
  end
82
96
 
97
+ FlagsMap = {
98
+ :global => DynamicLibrary::RTLD_GLOBAL,
99
+ :local => DynamicLibrary::RTLD_LOCAL,
100
+ :lazy => DynamicLibrary::RTLD_LAZY,
101
+ :now => DynamicLibrary::RTLD_NOW
102
+ }
103
+
104
+ def ffi_lib_flags(*flags)
105
+ @ffi_lib_flags = flags.inject(0) { |result, f| result | FlagsMap[f] }
106
+ end
107
+
108
+
83
109
  ##
84
- # Attach C function +name+ to this module.
85
- #
86
- # If you want to provide an alternate name for the module function, supply
87
- # it after the +name+, otherwise the C function name will be used.#
88
- #
89
- # After the +name+, the C function argument types are provided as an Array.
110
+ # Attach C function to this module.
90
111
  #
91
- # The C function return type is provided last.
92
-
93
- def attach_function(mname, a3, a4, a5=nil)
94
- cname, arg_types, ret_type = a5 ? [ a3, a4, a5 ] : [ mname.to_s, a3, a4 ]
112
+ def attach_function(mname, a2, a3, a4=nil, a5 = nil)
113
+ 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 ]
95
114
 
96
115
  # Convert :foo to the native type
97
116
  arg_types.map! { |e| find_type(e) }
98
- has_callback = arg_types.any? {|t| t.kind_of?(FFI::CallbackInfo)}
99
- options = Hash.new
100
- options[:convention] = defined?(@ffi_convention) ? @ffi_convention : :default
101
- options[:type_map] = @ffi_typedefs if defined?(@ffi_typedefs)
102
- options[:enums] = @ffi_enums if defined?(@ffi_enums)
117
+ options = {
118
+ :convention => defined?(@ffi_convention) ? @ffi_convention : :default,
119
+ :type_map => defined?(@ffi_typedefs) ? @ffi_typedefs : nil,
120
+ :blocking => defined?(@blocking) && @blocking,
121
+ :enums => defined?(@ffi_enums) ? @ffi_enums : nil,
122
+ }
123
+
124
+ @blocking = false
125
+ options.merge!(opts) if opts && opts.is_a?(Hash)
103
126
 
104
127
  # Try to locate the function in any of the libraries
105
128
  invokers = []
106
129
  ffi_libraries.each do |lib|
107
- begin
108
- invokers << FFI.create_invoker(lib, cname.to_s, arg_types, find_type(ret_type), options)
109
- rescue LoadError => ex
110
- end if invokers.empty?
111
- end
112
- invoker = invokers.compact.shift
113
- raise FFI::NotFoundError.new(cname.to_s, ffi_libraries.map { |lib| lib.name }) unless invoker
130
+ if invokers.empty?
131
+ begin
132
+ function = lib.find_function(cname.to_s)
133
+ raise LoadError unless function
114
134
 
115
- # Setup the parameter list for the module function as (a1, a2)
116
- arity = arg_types.length
117
- params = (1..arity).map {|i| "a#{i}" }.join(",")
135
+ invokers << if arg_types.length > 0 && arg_types[arg_types.length - 1] == FFI::NativeType::VARARGS
136
+ VariadicInvoker.new(function, arg_types, find_type(ret_type), options)
118
137
 
119
- # Always use rest args for functions with callback parameters
120
- if has_callback || invoker.kind_of?(FFI::VariadicInvoker)
121
- params = "*args, &block"
122
- end
123
- call = arity <= 3 && !has_callback && !invoker.kind_of?(FFI::VariadicInvoker)? "call#{arity}" : "call"
138
+ else
139
+ Function.new(find_type(ret_type), arg_types, function, options)
140
+ end
124
141
 
125
- #
126
- # Attach the invoker to this module as 'mname'.
127
- #
128
- if !has_callback && !invoker.kind_of?(FFI::VariadicInvoker)
129
- invoker.attach(self, mname.to_s)
130
- else
131
- self.module_eval <<-code
132
- @@#{mname} = invoker
133
- def self.#{mname}(#{params})
134
- @@#{mname}.#{call}(#{params})
142
+ rescue LoadError => ex
135
143
  end
136
- def #{mname}(#{params})
137
- @@#{mname}.#{call}(#{params})
138
- end
139
- code
144
+ end
140
145
  end
146
+ invoker = invokers.compact.shift
147
+ raise FFI::NotFoundError.new(cname.to_s, ffi_libraries.map { |lib| lib.name }) unless invoker
148
+
149
+ invoker.attach(self, mname.to_s)
141
150
  invoker
142
151
  end
152
+
153
+
143
154
  def attach_variable(mname, a1, a2 = nil)
144
155
  cname, type = a2 ? [ a1, a2 ] : [ mname.to_s, a1 ]
145
156
  address = nil
@@ -199,28 +210,34 @@ module FFI
199
210
 
200
211
  # Add to the symbol -> type map (unless there was no name)
201
212
  unless name.nil?
202
- @ffi_callbacks = Hash.new unless defined?(@ffi_callbacks)
203
- @ffi_callbacks[name] = cb
213
+ typedef cb, name
204
214
  end
205
215
 
206
216
  cb
207
217
  end
208
218
 
209
- def typedef(current, add, info=nil)
219
+ def typedef(old, add, info=nil)
210
220
  @ffi_typedefs = Hash.new unless defined?(@ffi_typedefs)
211
- code = if current.kind_of?(FFI::Type)
212
- current
213
- elsif current == :enum
221
+
222
+ @ffi_typedefs[add] = if old.kind_of?(FFI::Type)
223
+ old
224
+
225
+ elsif @ffi_typedefs.has_key?(old)
226
+ @ffi_typedefs[old]
227
+
228
+ elsif old.is_a?(DataConverter)
229
+ FFI::Type::Mapped.new(old)
230
+
231
+ elsif old == :enum
214
232
  if add.kind_of?(Array)
215
233
  self.enum(add)
216
234
  else
217
235
  self.enum(info, add)
218
236
  end
237
+
219
238
  else
220
- @ffi_typedefs[current] || FFI.find_type(current)
239
+ FFI.find_type(old)
221
240
  end
222
-
223
- @ffi_typedefs[add] = code
224
241
  end
225
242
 
226
243
  def enum(*args)
@@ -253,21 +270,21 @@ module FFI
253
270
  @ffi_enums.__map_symbol(symbol)
254
271
  end
255
272
 
256
- def find_type(name)
257
- code = if defined?(@ffi_typedefs) && @ffi_typedefs.has_key?(name)
258
- @ffi_typedefs[name]
259
- elsif defined?(@ffi_callbacks) && @ffi_callbacks.has_key?(name)
260
- @ffi_callbacks[name]
261
- elsif name.is_a?(Class) && name < FFI::Struct
262
- FFI::NativeType::POINTER
263
- elsif name.kind_of?(FFI::Type)
264
- name
265
- end
266
- if code.nil? || code.kind_of?(Symbol)
267
- FFI.find_type(name)
268
- else
269
- code
270
- end
273
+ def find_type(t)
274
+ if t.kind_of?(Type)
275
+ t
276
+
277
+ elsif defined?(@ffi_typedefs) && @ffi_typedefs.has_key?(t)
278
+ @ffi_typedefs[t]
279
+
280
+ elsif t.is_a?(Class) && t < Struct
281
+ Type::POINTER
282
+
283
+ elsif t.is_a?(DataConverter)
284
+ # Add a typedef so next time the converter is used, it hits the cache
285
+ typedef Type::Mapped.new(t), t
286
+
287
+ end || FFI.find_type(t)
271
288
  end
272
289
  end
273
290
  end
@@ -48,7 +48,7 @@ module FFI
48
48
  def initialize(pointer=nil)
49
49
  raise NoMethodError, "release() not implemented for class #{self}" unless self.class.respond_to? :release
50
50
  raise ArgumentError, "Must supply a pointer to memory for the Struct" unless pointer
51
- super FFI::AutoPointer.new(pointer, self.class.method(:release))
51
+ super AutoPointer.new(pointer, self.class.method(:release))
52
52
  end
53
53
 
54
54
  end
@@ -2,35 +2,29 @@
2
2
  # Copyright (C) 2008, 2009 Wayne Meissner
3
3
  # All rights reserved.
4
4
  #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are met:
5
+ # All rights reserved.
6
+ #
7
+ # This file is part of ruby-ffi.
8
+ #
9
+ # This code is free software: you can redistribute it and/or modify it under
10
+ # the terms of the GNU Lesser General Public License version 3 only, as
11
+ # published by the Free Software Foundation.
12
+ #
13
+ # This code is distributed in the hope that it will be useful, but WITHOUT
14
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16
+ # version 3 for more details.
7
17
  #
8
- # * Redistributions of source code must retain the above copyright notice, this
9
- # list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above copyright notice
11
- # this list of conditions and the following disclaimer in the documentation
12
- # and/or other materials provided with the distribution.
13
- # * Neither the name of the Evan Phoenix nor the names of its contributors
14
- # may be used to endorse or promote products derived from this software
15
- # without specific prior written permission.
18
+ # You should have received a copy of the GNU Lesser General Public License
19
+ # version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
16
20
  #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
21
 
28
22
  require 'ffi/pointer'
29
23
  module FFI
30
24
  class MemoryPointer
31
25
 
32
26
  def self.from_string(s)
33
- ptr = self.new(s.length + 1, 1, false)
27
+ ptr = self.new(s.bytesize + 1, 1, false)
34
28
  ptr.put_string(0, s)
35
29
  ptr
36
30
  end
data/lib/ffi/platform.rb CHANGED
@@ -2,37 +2,29 @@
2
2
  # Copyright (C) 2008, 2009 Wayne Meissner
3
3
  # All rights reserved.
4
4
  #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are met:
5
+ # This file is part of ruby-ffi.
7
6
  #
8
- # * Redistributions of source code must retain the above copyright notice, this
9
- # list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above copyright notice
11
- # this list of conditions and the following disclaimer in the documentation
12
- # and/or other materials provided with the distribution.
13
- # * Neither the name of the Evan Phoenix nor the names of its contributors
14
- # may be used to endorse or promote products derived from this software
15
- # without specific prior written permission.
7
+ # All rights reserved.
8
+ #
9
+ # This code is free software: you can redistribute it and/or modify it under
10
+ # the terms of the GNU Lesser General Public License version 3 only, as
11
+ # published by the Free Software Foundation.
12
+ #
13
+ # This code is distributed in the hope that it will be useful, but WITHOUT
14
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16
+ # version 3 for more details.
17
+ #
18
+ # You should have received a copy of the GNU Lesser General Public License
19
+ # version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
16
20
  #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
21
 
28
- require 'ffi_c'
29
22
  require 'rbconfig'
30
-
31
23
  module FFI
32
- class PlatformError < FFI::NativeError; end
24
+ class PlatformError < LoadError; end
33
25
 
34
26
  module Platform
35
- OS = case RbConfig::CONFIG['host_os'].downcase
27
+ OS = case Config::CONFIG['host_os'].downcase
36
28
  when /linux/
37
29
  "linux"
38
30
  when /darwin/
@@ -46,7 +38,7 @@ module FFI
46
38
  when /win|mingw/
47
39
  "windows"
48
40
  else
49
- RbConfig::CONFIG['host_os'].downcase
41
+ Config::CONFIG['host_os'].downcase
50
42
  end
51
43
 
52
44
  ARCH = case CPU.downcase
@@ -57,7 +49,7 @@ module FFI
57
49
  when /ppc|powerpc/
58
50
  "powerpc"
59
51
  else
60
- RbConfig::CONFIG['host_cpu']
52
+ Config::CONFIG['host_cpu']
61
53
  end
62
54
 
63
55
  private
@@ -75,13 +67,7 @@ module FFI
75
67
  CONF_DIR = File.dirname(__FILE__)
76
68
  public
77
69
 
78
- LIBC = if IS_WINDOWS
79
- "msvcrt"
80
- elsif IS_LINUX
81
- "libc.so.6"
82
- else
83
- "c"
84
- end
70
+
85
71
 
86
72
  LIBPREFIX = IS_WINDOWS ? '' : 'lib'
87
73
 
@@ -97,6 +83,14 @@ module FFI
97
83
  'so'
98
84
  end
99
85
 
86
+ LIBC = if IS_WINDOWS
87
+ "msvcrt.dll"
88
+ elsif IS_LINUX
89
+ "libc.so.6"
90
+ else
91
+ "#{LIBPREFIX}c.#{LIBSUFFIX}"
92
+ end
93
+
100
94
  def self.bsd?
101
95
  IS_BSD
102
96
  end