RubyInlineAcceleration 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/GPL.txt +342 -0
  2. data/lib/inline_acceleration.rb +493 -0
  3. metadata +55 -0
data/GPL.txt ADDED
@@ -0,0 +1,342 @@
1
+ The GNU General Public License (GPL)
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5
+
6
+ 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7
+
8
+ Everyone is permitted to copy and distribute verbatim copies
9
+ of this license document, but changing it is not allowed.
10
+
11
+ Preamble
12
+
13
+ The licenses for most software are designed to take away your
14
+ freedom to share and change it. By contrast, the GNU General Public
15
+ License is intended to guarantee your freedom to share and change free
16
+ software--to make sure the software is free for all its users. This
17
+ General Public License applies to most of the Free Software
18
+ Foundation's software and to any other program whose authors commit to
19
+ using it. (Some other Free Software Foundation software is covered by
20
+ the GNU Library General Public License instead.) You can apply it to
21
+ your programs, too.
22
+
23
+ When we speak of free software, we are referring to freedom, not
24
+ price. Our General Public Licenses are designed to make sure that you
25
+ have the freedom to distribute copies of free software (and charge for
26
+ this service if you wish), that you receive source code or can get it
27
+ if you want it, that you can change the software or use pieces of it
28
+ in new free programs; and that you know you can do these things.
29
+
30
+ To protect your rights, we need to make restrictions that forbid
31
+ anyone to deny you these rights or to ask you to surrender the rights.
32
+ These restrictions translate to certain responsibilities for you if you
33
+ distribute copies of the software, or if you modify it.
34
+
35
+ For example, if you distribute copies of such a program, whether
36
+ gratis or for a fee, you must give the recipients all the rights that
37
+ you have. You must make sure that they, too, receive or can get the
38
+ source code. And you must show them these terms so they know their
39
+ rights.
40
+
41
+ We protect your rights with two steps: (1) copyright the software, and
42
+ (2) offer you this license which gives you legal permission to copy,
43
+ distribute and/or modify the software.
44
+
45
+ Also, for each author's protection and ours, we want to make certain
46
+ that everyone understands that there is no warranty for this free
47
+ software. If the software is modified by someone else and passed on, we
48
+ want its recipients to know that what they have is not the original, so
49
+ that any problems introduced by others will not reflect on the original
50
+ authors' reputations.
51
+
52
+ Finally, any free program is threatened constantly by software
53
+ patents. We wish to avoid the danger that redistributors of a free
54
+ program will individually obtain patent licenses, in effect making the
55
+ program proprietary. To prevent this, we have made it clear that any
56
+ patent must be licensed for everyone's free use or not licensed at all.
57
+
58
+ The precise terms and conditions for copying, distribution and
59
+ modification follow.
60
+
61
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
62
+
63
+ 0. This License applies to any program or other work which contains
64
+ a notice placed by the copyright holder saying it may be distributed
65
+ under the terms of this General Public License. The "Program", below,
66
+ refers to any such program or work, and a "work based on the Program"
67
+ means either the Program or any derivative work under copyright law:
68
+ that is to say, a work containing the Program or a portion of it,
69
+ either verbatim or with modifications and/or translated into another
70
+ language. (Hereinafter, translation is included without limitation in
71
+ the term "modification".) Each licensee is addressed as "you".
72
+
73
+ Activities other than copying, distribution and modification are not
74
+ covered by this License; they are outside its scope. The act of
75
+ running the Program is not restricted, and the output from the Program
76
+ is covered only if its contents constitute a work based on the
77
+ Program (independent of having been made by running the Program).
78
+ Whether that is true depends on what the Program does.
79
+
80
+ 1. You may copy and distribute verbatim copies of the Program's
81
+ source code as you receive it, in any medium, provided that you
82
+ conspicuously and appropriately publish on each copy an appropriate
83
+ copyright notice and disclaimer of warranty; keep intact all the
84
+ notices that refer to this License and to the absence of any warranty;
85
+ and give any other recipients of the Program a copy of this License
86
+ along with the Program.
87
+
88
+ You may charge a fee for the physical act of transferring a copy, and
89
+ you may at your option offer warranty protection in exchange for a fee.
90
+
91
+ 2. You may modify your copy or copies of the Program or any portion
92
+ of it, thus forming a work based on the Program, and copy and
93
+ distribute such modifications or work under the terms of Section 1
94
+ above, provided that you also meet all of these conditions:
95
+
96
+ a) You must cause the modified files to carry prominent notices
97
+ stating that you changed the files and the date of any change.
98
+
99
+ b) You must cause any work that you distribute or publish, that in
100
+ whole or in part contains or is derived from the Program or any
101
+ part thereof, to be licensed as a whole at no charge to all third
102
+ parties under the terms of this License.
103
+
104
+ c) If the modified program normally reads commands interactively
105
+ when run, you must cause it, when started running for such
106
+ interactive use in the most ordinary way, to print or display an
107
+ announcement including an appropriate copyright notice and a
108
+ notice that there is no warranty (or else, saying that you provide
109
+ a warranty) and that users may redistribute the program under
110
+ these conditions, and telling the user how to view a copy of this
111
+ License. (Exception: if the Program itself is interactive but
112
+ does not normally print such an announcement, your work based on
113
+ the Program is not required to print an announcement.)
114
+
115
+ These requirements apply to the modified work as a whole. If
116
+ identifiable sections of that work are not derived from the Program,
117
+ and can be reasonably considered independent and separate works in
118
+ themselves, then this License, and its terms, do not apply to those
119
+ sections when you distribute them as separate works. But when you
120
+ distribute the same sections as part of a whole which is a work based
121
+ on the Program, the distribution of the whole must be on the terms of
122
+ this License, whose permissions for other licensees extend to the
123
+ entire whole, and thus to each and every part regardless of who wrote it.
124
+
125
+ Thus, it is not the intent of this section to claim rights or contest
126
+ your rights to work written entirely by you; rather, the intent is to
127
+ exercise the right to control the distribution of derivative or
128
+ collective works based on the Program.
129
+
130
+ In addition, mere aggregation of another work not based on the Program
131
+ with the Program (or with a work based on the Program) on a volume of
132
+ a storage or distribution medium does not bring the other work under
133
+ the scope of this License.
134
+
135
+ 3. You may copy and distribute the Program (or a work based on it,
136
+ under Section 2) in object code or executable form under the terms of
137
+ Sections 1 and 2 above provided that you also do one of the following:
138
+
139
+ a) Accompany it with the complete corresponding machine-readable
140
+ source code, which must be distributed under the terms of Sections
141
+ 1 and 2 above on a medium customarily used for software interchange; or,
142
+
143
+ b) Accompany it with a written offer, valid for at least three
144
+ years, to give any third party, for a charge no more than your
145
+ cost of physically performing source distribution, a complete
146
+ machine-readable copy of the corresponding source code, to be
147
+ distributed under the terms of Sections 1 and 2 above on a medium
148
+ customarily used for software interchange; or,
149
+
150
+ c) Accompany it with the information you received as to the offer
151
+ to distribute corresponding source code. (This alternative is
152
+ allowed only for noncommercial distribution and only if you
153
+ received the program in object code or executable form with such
154
+ an offer, in accord with Subsection b above.)
155
+
156
+ The source code for a work means the preferred form of the work for
157
+ making modifications to it. For an executable work, complete source
158
+ code means all the source code for all modules it contains, plus any
159
+ associated interface definition files, plus the scripts used to
160
+ control compilation and installation of the executable. However, as a
161
+ special exception, the source code distributed need not include
162
+ anything that is normally distributed (in either source or binary
163
+ form) with the major components (compiler, kernel, and so on) of the
164
+ operating system on which the executable runs, unless that component
165
+ itself accompanies the executable.
166
+
167
+ If distribution of executable or object code is made by offering
168
+ access to copy from a designated place, then offering equivalent
169
+ access to copy the source code from the same place counts as
170
+ distribution of the source code, even though third parties are not
171
+ compelled to copy the source along with the object code.
172
+
173
+ 4. You may not copy, modify, sublicense, or distribute the Program
174
+ except as expressly provided under this License. Any attempt
175
+ otherwise to copy, modify, sublicense or distribute the Program is
176
+ void, and will automatically terminate your rights under this License.
177
+ However, parties who have received copies, or rights, from you under
178
+ this License will not have their licenses terminated so long as such
179
+ parties remain in full compliance.
180
+
181
+ 5. You are not required to accept this License, since you have not
182
+ signed it. However, nothing else grants you permission to modify or
183
+ distribute the Program or its derivative works. These actions are
184
+ prohibited by law if you do not accept this License. Therefore, by
185
+ modifying or distributing the Program (or any work based on the
186
+ Program), you indicate your acceptance of this License to do so, and
187
+ all its terms and conditions for copying, distributing or modifying
188
+ the Program or works based on it.
189
+
190
+ 6. Each time you redistribute the Program (or any work based on the
191
+ Program), the recipient automatically receives a license from the
192
+ original licensor to copy, distribute or modify the Program subject to
193
+ these terms and conditions. You may not impose any further
194
+ restrictions on the recipients' exercise of the rights granted herein.
195
+ You are not responsible for enforcing compliance by third parties to
196
+ this License.
197
+
198
+ 7. If, as a consequence of a court judgment or allegation of patent
199
+ infringement or for any other reason (not limited to patent issues),
200
+ conditions are imposed on you (whether by court order, agreement or
201
+ otherwise) that contradict the conditions of this License, they do not
202
+ excuse you from the conditions of this License. If you cannot
203
+ distribute so as to satisfy simultaneously your obligations under this
204
+ License and any other pertinent obligations, then as a consequence you
205
+ may not distribute the Program at all. For example, if a patent
206
+ license would not permit royalty-free redistribution of the Program by
207
+ all those who receive copies directly or indirectly through you, then
208
+ the only way you could satisfy both it and this License would be to
209
+ refrain entirely from distribution of the Program.
210
+
211
+ If any portion of this section is held invalid or unenforceable under
212
+ any particular circumstance, the balance of the section is intended to
213
+ apply and the section as a whole is intended to apply in other
214
+ circumstances.
215
+
216
+ It is not the purpose of this section to induce you to infringe any
217
+ patents or other property right claims or to contest validity of any
218
+ such claims; this section has the sole purpose of protecting the
219
+ integrity of the free software distribution system, which is
220
+ implemented by public license practices. Many people have made
221
+ generous contributions to the wide range of software distributed
222
+ through that system in reliance on consistent application of that
223
+ system; it is up to the author/donor to decide if he or she is willing
224
+ to distribute software through any other system and a licensee cannot
225
+ impose that choice.
226
+
227
+ This section is intended to make thoroughly clear what is believed to
228
+ be a consequence of the rest of this License.
229
+
230
+ 8. If the distribution and/or use of the Program is restricted in
231
+ certain countries either by patents or by copyrighted interfaces, the
232
+ original copyright holder who places the Program under this License
233
+ may add an explicit geographical distribution limitation excluding
234
+ those countries, so that distribution is permitted only in or among
235
+ countries not thus excluded. In such case, this License incorporates
236
+ the limitation as if written in the body of this License.
237
+
238
+ 9. The Free Software Foundation may publish revised and/or new versions
239
+ of the General Public License from time to time. Such new versions will
240
+ be similar in spirit to the present version, but may differ in detail to
241
+ address new problems or concerns.
242
+
243
+ Each version is given a distinguishing version number. If the Program
244
+ specifies a version number of this License which applies to it and "any
245
+ later version", you have the option of following the terms and conditions
246
+ either of that version or of any later version published by the Free
247
+ Software Foundation. If the Program does not specify a version number of
248
+ this License, you may choose any version ever published by the Free Software
249
+ Foundation.
250
+
251
+ 10. If you wish to incorporate parts of the Program into other free
252
+ programs whose distribution conditions are different, write to the author
253
+ to ask for permission. For software which is copyrighted by the Free
254
+ Software Foundation, write to the Free Software Foundation; we sometimes
255
+ make exceptions for this. Our decision will be guided by the two goals
256
+ of preserving the free status of all derivatives of our free software and
257
+ of promoting the sharing and reuse of software generally.
258
+
259
+ NO WARRANTY
260
+
261
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
262
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
263
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
264
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
265
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
266
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
267
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
268
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
269
+ REPAIR OR CORRECTION.
270
+
271
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
272
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
273
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
274
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
275
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
276
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
277
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
278
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
279
+ POSSIBILITY OF SUCH DAMAGES.
280
+
281
+ END OF TERMS AND CONDITIONS
282
+
283
+ How to Apply These Terms to Your New Programs
284
+
285
+ If you develop a new program, and you want it to be of the greatest
286
+ possible use to the public, the best way to achieve this is to make it
287
+ free software which everyone can redistribute and change under these terms.
288
+
289
+ To do so, attach the following notices to the program. It is safest
290
+ to attach them to the start of each source file to most effectively
291
+ convey the exclusion of warranty; and each file should have at least
292
+ the "copyright" line and a pointer to where the full notice is found.
293
+
294
+ One line to give the program's name and a brief idea of what it does.
295
+
296
+ Copyright (C) <year> <name of author>
297
+
298
+ This program is free software; you can redistribute it and/or modify
299
+ it under the terms of the GNU General Public License as published by
300
+ the Free Software Foundation; either version 2 of the License, or
301
+ (at your option) any later version.
302
+
303
+ This program is distributed in the hope that it will be useful,
304
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
305
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
306
+ GNU General Public License for more details.
307
+
308
+ You should have received a copy of the GNU General Public License
309
+ along with this program; if not, write to the Free Software
310
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
311
+
312
+ Also add information on how to contact you by electronic and paper mail.
313
+
314
+ If the program is interactive, make it output a short notice like this
315
+ when it starts in an interactive mode:
316
+
317
+ Gnomovision version 69, Copyright (C) year name of author
318
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
319
+ This is free software, and you are welcome to redistribute it
320
+ under certain conditions; type `show c' for details.
321
+
322
+ The hypothetical commands `show w' and `show c' should show the appropriate
323
+ parts of the General Public License. Of course, the commands you use may
324
+ be called something other than `show w' and `show c'; they could even be
325
+ mouse-clicks or menu items--whatever suits your program.
326
+
327
+ You should also get your employer (if you work as a programmer) or your
328
+ school, if any, to sign a "copyright disclaimer" for the program, if
329
+ necessary. Here is a sample; alter the names:
330
+
331
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
332
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
333
+
334
+ signature of Ty Coon, 1 April 1989
335
+
336
+ Ty Coon, President of Vice
337
+
338
+ This General Public License does not permit incorporating your program into
339
+ proprietary programs. If your program is a subroutine library, you may
340
+ consider it more useful to permit linking proprietary applications with the
341
+ library. If this is what you want to do, use the GNU Library General
342
+ Public License instead of this License.
@@ -0,0 +1,493 @@
1
+ # Copyright (C) 2007 Richard Musiol
2
+ #
3
+ # This program is free software; you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation; either version 2 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program; if not, write to the Free Software
15
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
+
17
+ require 'inline'
18
+
19
+ class String
20
+ def to_class
21
+ names = self.split('::')
22
+ klass = Object
23
+ names.each do |name|
24
+ sym = name.to_sym
25
+ return nil if !klass.const_defined?(sym)
26
+ klass = klass.const_get(sym)
27
+ end
28
+ klass
29
+ end
30
+ end
31
+
32
+ class Module
33
+ def parent_module
34
+ self.name.split('::')[0..-2].join('::').to_class
35
+ end
36
+
37
+ def lazy_attr_reader(name, &block)
38
+ attr_name = name.to_s.gsub('?', '')
39
+ define_method("lazy_attr_initialization_#{attr_name}".to_sym, block)
40
+ class_eval <<-CODE_END
41
+ def #{name.to_s}
42
+ @#{attr_name} = lazy_attr_initialization_#{attr_name} if @#{attr_name}.nil?
43
+ @#{attr_name}
44
+ end
45
+ CODE_END
46
+ end
47
+ end
48
+
49
+ class Array
50
+ def all_indices
51
+ (0...self.size).to_a
52
+ end
53
+
54
+ def assigned_indices
55
+ all_indices.reject { |i| self[i].nil? }
56
+ end
57
+
58
+ def prefix(str)
59
+ self.map { |item| "#{str}#{item}" }
60
+ end
61
+
62
+ def prefix!(str)
63
+ self.map! { |item| "#{str}#{item}" }
64
+ end
65
+
66
+ def postfix(str)
67
+ self.map { |item| "#{item}#{str}" }
68
+ end
69
+
70
+ def postfix!(str)
71
+ self.map! { |item| "#{item}#{str}" }
72
+ end
73
+
74
+ def map_with_indices(target = [])
75
+ self.each_index do |index|
76
+ target[index] = yield(self[index], index)
77
+ end
78
+ target
79
+ end
80
+
81
+ def map_with_indices!
82
+ self.map_with_indices(self)
83
+ end
84
+
85
+ def map_method_results(method_name)
86
+ self.map { |item| item.send(method_name) }
87
+ end
88
+
89
+ def map_method_results!(method_name)
90
+ self.map! { |item| item.send(method_name) }
91
+ end
92
+
93
+ alias_method :throw_method_missing, :method_missing
94
+ def method_missing(name, *args, &block)
95
+ if name.to_s[-1..-1] == 's'
96
+ method_name = name.to_s[0..-2].to_sym
97
+ if size == 0 || self[0].respond_to?(method_name)
98
+ return map_method_results(method_name)
99
+ end
100
+ end
101
+ throw_method_missing(name, args, &block)
102
+ end
103
+
104
+ def reject_nils
105
+ self.reject { |item| item.nil? }
106
+ end
107
+
108
+ def reject_nils!
109
+ self.reject! { |item| item.nil? }
110
+ end
111
+ end
112
+
113
+ module Inline
114
+ class C
115
+ # TRANSFER_TYPE = $TESTING ? 'unsigned long' : 'VALUE'
116
+ TRANSFER_TYPE = 'long'
117
+
118
+ @@internal_c_functions = []
119
+ @@internal_c_functions_module = nil
120
+
121
+ def self.internal_c_functions
122
+ @@internal_c_functions
123
+ end
124
+
125
+ def self.internal_c_functions_module
126
+ @@internal_c_functions_module
127
+ end
128
+
129
+ def self.type_map
130
+ @@type_map
131
+ end
132
+
133
+ alias_method :initialize_orig, :initialize
134
+ def initialize(mod)
135
+ initialize_orig(mod)
136
+ if @@internal_c_functions_module.nil?
137
+ @@internal_c_functions.each { |function| self.c_singleton(function) }
138
+ @@internal_c_functions_module = mod
139
+ end
140
+ @eval_on_load = ''
141
+ end
142
+
143
+ alias_method :load_orig, :load
144
+ def load
145
+ load_orig
146
+ @mod.class_eval(@eval_on_load)
147
+ end
148
+
149
+ def link_function(c_return_type, function_name, parameter_types)
150
+ delegate_function_name = "C_delegate_#{function_name}"
151
+ return_type = Type.new(c_return_type, link_type(c_return_type))
152
+ parameters = parameter_types.map_with_indices { |type, index| Parameter.new(type, link_type(type), index) }
153
+
154
+ #build c delegate
155
+ c_code = <<-CODE_END
156
+ #{return_type.c_transfer_type} #{delegate_function_name}(#{parameters.c_method_parameters.join(', ')}) {
157
+ #{return_type.void? ? '' : "return (#{return_type.c_transfer_type})"} #{function_name}(#{parameters.c_casted_call_args.join(', ')});
158
+ }
159
+ CODE_END
160
+ # puts c_code
161
+ self.c_singleton c_code
162
+
163
+ #build ruby delegate
164
+ line = __LINE__; ruby_code = <<-CODE_END
165
+ def self.#{function_name}(#{parameters.ruby_method_parameters.join(', ')})
166
+ #{parameters.ruby_ref_assigns.reject_nils.join('; ')}
167
+ return_values = []
168
+ #{return_type.void? ? '' : ('return_values << ' + return_type.ruby_type_conversion)}(#{delegate_function_name}(#{parameters.ruby_transformed_call_args.join(', ')}))
169
+ #{parameters.ruby_converted_return_values.reject_nils.prefix('return_values << ').join('; ')}
170
+ case return_values.size
171
+ when 0: nil
172
+ when 1: return_values[0]
173
+ else return_values
174
+ end
175
+ end
176
+ def #{function_name}(#{parameters.ruby_method_parameters.join(', ')})
177
+ self.class.#{function_name}(#{parameters.arg_names.join(', ')})
178
+ end
179
+ CODE_END
180
+ # puts ruby_code
181
+ @mod.class_eval ruby_code, __FILE__, line + 1
182
+ end
183
+
184
+ def link_fields(struct_name, field_hash)
185
+ field_hash.each do |field_name, c_type|
186
+ delegate_function_name = "#{struct_name.gsub(' ', '_')}_#{field_name}"
187
+ type = Type.new(c_type, link_type(c_type))
188
+
189
+ #build c delegate
190
+ c_code = <<-CODE_END
191
+ #{type.c_transfer_type} #{delegate_function_name}(#{TRANSFER_TYPE} pointer) {
192
+ #{type.c_transfer_type} result = (#{type.c_transfer_type}) ((#{struct_name}*) pointer)->#{field_name};
193
+ #{type.is_simple_type? ? 'return result;' : "if(result) { return result; } else { return(Qfalse); }"}
194
+ }
195
+ CODE_END
196
+ # puts c_code
197
+ self.c_singleton c_code
198
+
199
+ #build ruby delegate
200
+ line = __LINE__; ruby_code = <<-CODE_END
201
+ def #{field_name}
202
+ result = (#{@mod.name}.#{delegate_function_name}(memory_address))
203
+ #{type.is_pointer_type? ? 'return nil if !result' : ''}
204
+ #{type.ruby_type_conversion}(result)
205
+ end
206
+ CODE_END
207
+ # puts ruby_code
208
+ link_type("#{struct_name}*").class_eval ruby_code, __FILE__, line + 1
209
+ end
210
+ end
211
+
212
+ def link_type(c_type, reallocate_pointers=false, &block)
213
+ is_simple_type = @@type_map.has_key?(c_type)
214
+ is_pointer = c_type[-1..-1] == '*'
215
+ return nil if is_simple_type
216
+ return Value if !is_pointer
217
+
218
+ c_inner_type = c_type[0..-2].strip
219
+ class_name = c_inner_type.gsub(' ', '_').capitalize
220
+ klass = "#{@mod.name}::#{class_name}".to_class
221
+ if !klass
222
+ inner_type = Type.new(c_inner_type, nil)
223
+ is_simple_type = @@type_map.has_key?(c_inner_type)
224
+
225
+ klass = Class.new()
226
+ @mod.const_set(class_name.to_sym, klass)
227
+
228
+ line = __LINE__; ruby_code = <<-CODE_END
229
+ include Inline::C::Pointer
230
+
231
+ def dereference
232
+ #{link_type('void *').name}.new(Inline::C.internal_c_functions_module.C_Pointer_dereference_to_pointer(memory_address))
233
+ end
234
+
235
+ alias_method :throw_method_missing, :method_missing
236
+ def method_missing(name, *args, &block)
237
+ if name.to_s[0..2] == 'to_'
238
+ type = name.to_s[3..-1]
239
+ klass = ("#{@mod.name}::" + type.capitalize).to_class
240
+ return klass.new(memory_address, ruby_managed)
241
+ end
242
+ throw_method_missing(name, *args, &block)
243
+ end
244
+ CODE_END
245
+ klass.class_eval ruby_code, __FILE__, line + 1
246
+
247
+ if !inner_type.void?
248
+ create_pointer_from_data_function_name = "#{class_name}_create_pointer_from_data"
249
+ create_pointer_from_pointer_function_name = "#{class_name}_create_pointer_from_pointer"
250
+ get_array_entry_function_name = "#{class_name}_get_array_entry"
251
+
252
+ #create pointer from data
253
+ c_code = <<-CODE_END
254
+ #{TRANSFER_TYPE} #{create_pointer_from_data_function_name}(#{is_simple_type ? c_inner_type : TRANSFER_TYPE} source) {
255
+ #{c_type} pointer = (#{c_type}) malloc(sizeof(#{c_inner_type}));
256
+ memcpy(pointer, &source, sizeof(#{c_inner_type}));
257
+ return (#{TRANSFER_TYPE}) pointer;
258
+ }
259
+ CODE_END
260
+ self.c_singleton c_code
261
+
262
+ #create pointer from pointer
263
+ c_code = <<-CODE_END
264
+ #{TRANSFER_TYPE} #{create_pointer_from_pointer_function_name}(#{TRANSFER_TYPE} source) {
265
+ #{c_type} pointer = (#{c_type}) malloc(sizeof(#{c_inner_type}));
266
+ *pointer = *((#{c_type}) source);
267
+ return (#{TRANSFER_TYPE}) pointer;
268
+ }
269
+ CODE_END
270
+ self.c_singleton c_code
271
+
272
+ #array delegate
273
+ c_code = <<-CODE_END
274
+ #{TRANSFER_TYPE} #{get_array_entry_function_name}(#{TRANSFER_TYPE} source, long index) {
275
+ return (#{TRANSFER_TYPE}) &((#{c_type}) source)[index];
276
+ }
277
+ CODE_END
278
+ self.c_singleton c_code
279
+
280
+ line = __LINE__; ruby_code = <<-CODE_END
281
+ @@reallocate_pointers = #{reallocate_pointers}
282
+ def self.reallocate_pointers?
283
+ @@reallocate_pointers
284
+ end
285
+
286
+ def self.create(source=0)
287
+ if source.is_a? Inline::C::Pointer
288
+ if !@@reallocate_pointers
289
+ source
290
+ else
291
+ self.new(#{@mod.name}.#{create_pointer_from_pointer_function_name}(source.memory_address), true)
292
+ end
293
+ else
294
+ self.new(#{@mod.name}.#{create_pointer_from_data_function_name}(Inline::C::Value.get_data(source)), true)
295
+ end
296
+ end
297
+
298
+ def [](index)
299
+ self.class.new(#{@mod.name}.#{get_array_entry_function_name}(self.memory_address, index), self.ruby_managed)
300
+ end
301
+ CODE_END
302
+ klass.class_eval ruby_code, __FILE__, line + 1
303
+ end
304
+ end
305
+
306
+ klass.class_eval(&block) if block
307
+ klass
308
+ end
309
+
310
+ def link_constants(type, names)
311
+ names.each do |name|
312
+ get_constant_method = "get_constant_#{name}"
313
+
314
+ self.c_singleton <<-CODE_END
315
+ #{type} #{get_constant_method}(void) {
316
+ return #{name};
317
+ }
318
+ CODE_END
319
+
320
+ @eval_on_load << <<-CODE_END
321
+ #{@mod.name}.const_set(:#{name}, #{get_constant_method})
322
+ CODE_END
323
+ end
324
+ end
325
+
326
+ class Type
327
+ def initialize(c_type, ruby_type_class)
328
+ @c_type = c_type
329
+ @ruby_type_class = ruby_type_class
330
+ end
331
+
332
+ attr_reader :c_type, :ruby_type_class
333
+
334
+ lazy_attr_reader :is_simple_type? do
335
+ void? || C.type_map.has_key?(c_type)
336
+ end
337
+
338
+ lazy_attr_reader :is_pointer_type? do
339
+ !is_simple_type? && c_type[-1..-1] == '*'
340
+ end
341
+
342
+ def void?
343
+ c_type == 'void'
344
+ end
345
+
346
+ def c_transfer_type
347
+ is_simple_type? ? c_type : TRANSFER_TYPE
348
+ end
349
+
350
+ def ruby_type_conversion
351
+ is_simple_type? ? '' : "#{ruby_type_class.name}.new"
352
+ end
353
+ end
354
+
355
+ class Parameter < Type
356
+ alias_method :type_initialize, :initialize
357
+ def initialize(c_type, ruby_type_class, index)
358
+ type_initialize(c_type, ruby_type_class)
359
+ @index = index
360
+ end
361
+
362
+ attr_reader :index
363
+
364
+ def arg_name
365
+ "arg#{index}"
366
+ end
367
+
368
+ def ref_name
369
+ "ref#{index}"
370
+ end
371
+
372
+ def c_method_parameter
373
+ "#{c_transfer_type} #{arg_name}"
374
+ end
375
+
376
+ def c_casted_call_arg
377
+ "(#{c_type}) #{arg_name}"
378
+ end
379
+
380
+ def ruby_method_parameter
381
+ "#{arg_name}=" + (is_pointer_type? ? 'nil' : '0')
382
+ end
383
+
384
+ def ruby_ref_assign
385
+ return nil if !is_pointer_type?
386
+ "#{ref_name} = #{ruby_type_class.name}.create(#{arg_name})"
387
+ end
388
+
389
+ def ruby_transformed_call_arg
390
+ is_pointer_type? ? "#{ref_name}.memory_address" : "Inline::C::Value.get_data(#{arg_name})"
391
+ end
392
+
393
+ def ruby_converted_return_value
394
+ return nil if !is_pointer_type?
395
+ (is_simple_type? ? "#{ref_name}.to_#{c_type[0..-2].strip.gsub(' ', '_')}" : ref_name + (ruby_type_class.reallocate_pointers? ? '' : " if #{arg_name}.nil?"))
396
+ end
397
+ end
398
+
399
+ class Value
400
+ def initialize(data)
401
+ @data = ValueSafe.new()
402
+ @data.value = data
403
+ end
404
+
405
+ def data
406
+ @data.value
407
+ end
408
+
409
+ def self.get_data(source)
410
+ if source.is_a?(self)
411
+ source.data
412
+ elsif source.nil?
413
+ 0
414
+ else
415
+ source
416
+ end
417
+ end
418
+
419
+ ['char', 'double', 'int', 'long', 'unsigned int', 'unsigned long'].each do |type|
420
+ Inline::C.internal_c_functions << "#{type} C_Value_convert_to_#{type.gsub(' ', '_')}(#{Inline::C::TRANSFER_TYPE} data) { return (#{type}) data; }"
421
+
422
+ #actual conversion is done by RubyInline
423
+ self.class_eval <<-CODE_END
424
+ def to_#{type.gsub(' ', '_')}
425
+ return Inline::C.internal_c_functions_module.C_Value_convert_to_#{type.gsub(' ', '_')}(data)
426
+ end
427
+ CODE_END
428
+ end
429
+ end
430
+
431
+ module Pointer
432
+ @@registered_pointers = {}
433
+
434
+ attr_reader :ruby_managed
435
+
436
+ def initialize(memory_address, ruby_managed=false)
437
+ @memory_address = ValueSafe.new()
438
+ @memory_address.value = memory_address
439
+ @ruby_managed = ruby_managed
440
+
441
+ if ruby_managed
442
+ @@registered_pointers[self.object_id] = memory_address
443
+ ObjectSpace.define_finalizer(self, lambda { |id|
444
+ memory_address = @@registered_pointers.delete(id)
445
+ if !@@registered_pointers.has_value?(memory_address)
446
+ Inline::C.internal_c_functions_module.C_Pointer_free_memory(memory_address)
447
+ end
448
+ })
449
+ end
450
+ end
451
+
452
+ def memory_address
453
+ @memory_address.value
454
+ end
455
+
456
+ def ==(other)
457
+ (other.is_a? Pointer) && Inline::C.internal_c_functions_module.C_Pointer_equals(self.memory_address, other.memory_address)
458
+ end
459
+
460
+ Inline::C.internal_c_functions << "void C_Pointer_free_memory(#{Inline::C::TRANSFER_TYPE} pointer) { free((void*) pointer); }"
461
+ Inline::C.internal_c_functions << "#{Inline::C::TRANSFER_TYPE} C_Pointer_dereference_to_pointer(#{Inline::C::TRANSFER_TYPE} pointer) { return (#{Inline::C::TRANSFER_TYPE}) *((void**) pointer); }"
462
+ Inline::C.internal_c_functions << "VALUE C_Pointer_equals(#{Inline::C::TRANSFER_TYPE} pointer1, #{Inline::C::TRANSFER_TYPE} pointer2) { return (pointer1 == pointer2) ? Qtrue : Qfalse; }"
463
+
464
+ ['char', 'double', 'int', 'long', 'unsigned int', 'unsigned long'].each do |type|
465
+ Inline::C.internal_c_functions << "#{type} C_Pointer_dereference_to_#{type.gsub(' ', '_')}(#{Inline::C::TRANSFER_TYPE} pointer) { return *((#{type}*) pointer); }"
466
+
467
+ self.class_eval <<-CODE_END
468
+ def to_#{type.gsub(' ', '_')}
469
+ return Inline::C.internal_c_functions_module.C_Pointer_dereference_to_#{type.gsub(' ', '_')}(memory_address)
470
+ end
471
+ CODE_END
472
+ end
473
+ end
474
+ end
475
+
476
+ class ValueSafe
477
+ def value
478
+ @value
479
+ end
480
+
481
+ def value=(data)
482
+ @value = data
483
+ end
484
+
485
+ def to_s
486
+ $TESTING ? @value : '[enable $TESTING]'
487
+ end
488
+
489
+ def inspect
490
+ to_s
491
+ end
492
+ end
493
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.3
3
+ specification_version: 1
4
+ name: RubyInlineAcceleration
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-05-21 00:00:00 +02:00
8
+ summary: Wrapping extension to RubyInline
9
+ require_paths:
10
+ - lib
11
+ email: mail@richard-musiol.de
12
+ homepage: http://rubyforge.org/projects/rubyinlineaccel/
13
+ rubyforge_project:
14
+ description: RubyInlineAcceleration is an extension to RubyInline which provides an easy way to use C libraries in Ruby by directly wrapping methods, structures and fields.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Richard Musiol
31
+ files:
32
+ - GPL.txt
33
+ - lib/inline_acceleration.rb
34
+ test_files: []
35
+
36
+ rdoc_options: []
37
+
38
+ extra_rdoc_files: []
39
+
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ requirements: []
45
+
46
+ dependencies:
47
+ - !ruby/object:Gem::Dependency
48
+ name: RubyInline
49
+ version_requirement:
50
+ version_requirements: !ruby/object:Gem::Version::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.6.3
55
+ version: