fiddle 1.0.9 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/ext/fiddle/fiddle.c CHANGED
@@ -7,7 +7,7 @@ VALUE rb_eFiddleError;
7
7
  void Init_fiddle_pointer(void);
8
8
  void Init_fiddle_pinned(void);
9
9
 
10
- #ifdef FIDDLE_MEMORY_VIEW
10
+ #ifdef HAVE_RUBY_MEMORY_VIEW_H
11
11
  void Init_fiddle_memory_view(void);
12
12
  #endif
13
13
 
@@ -164,137 +164,193 @@ Init_fiddle(void)
164
164
  */
165
165
  rb_eFiddleDLError = rb_define_class_under(mFiddle, "DLError", rb_eFiddleError);
166
166
 
167
- /* Document-const: TYPE_VOID
167
+ VALUE mFiddleTypes = rb_define_module_under(mFiddle, "Types");
168
+
169
+ /* Document-const: Fiddle::Types::VOID
168
170
  *
169
171
  * C type - void
170
172
  */
171
- rb_define_const(mFiddle, "TYPE_VOID", INT2NUM(TYPE_VOID));
173
+ rb_define_const(mFiddleTypes, "VOID", INT2NUM(TYPE_VOID));
172
174
 
173
- /* Document-const: TYPE_VOIDP
175
+ /* Document-const: Fiddle::Types::VOIDP
174
176
  *
175
177
  * C type - void*
176
178
  */
177
- rb_define_const(mFiddle, "TYPE_VOIDP", INT2NUM(TYPE_VOIDP));
179
+ rb_define_const(mFiddleTypes, "VOIDP", INT2NUM(TYPE_VOIDP));
178
180
 
179
- /* Document-const: TYPE_CHAR
181
+ /* Document-const: Fiddle::Types::CHAR
180
182
  *
181
183
  * C type - char
182
184
  */
183
- rb_define_const(mFiddle, "TYPE_CHAR", INT2NUM(TYPE_CHAR));
185
+ rb_define_const(mFiddleTypes, "CHAR", INT2NUM(TYPE_CHAR));
184
186
 
185
- /* Document-const: TYPE_SHORT
187
+ /* Document-const: Fiddle::Types::UCHAR
188
+ *
189
+ * C type - unsigned char
190
+ */
191
+ rb_define_const(mFiddleTypes, "UCHAR", INT2NUM(TYPE_UCHAR));
192
+
193
+ /* Document-const: Fiddle::Types::SHORT
186
194
  *
187
195
  * C type - short
188
196
  */
189
- rb_define_const(mFiddle, "TYPE_SHORT", INT2NUM(TYPE_SHORT));
197
+ rb_define_const(mFiddleTypes, "SHORT", INT2NUM(TYPE_SHORT));
190
198
 
191
- /* Document-const: TYPE_INT
199
+ /* Document-const: Fiddle::Types::USHORT
200
+ *
201
+ * C type - unsigned short
202
+ */
203
+ rb_define_const(mFiddleTypes, "USHORT", INT2NUM(TYPE_USHORT));
204
+
205
+ /* Document-const: Fiddle::Types::INT
192
206
  *
193
207
  * C type - int
194
208
  */
195
- rb_define_const(mFiddle, "TYPE_INT", INT2NUM(TYPE_INT));
209
+ rb_define_const(mFiddleTypes, "INT", INT2NUM(TYPE_INT));
210
+
211
+ /* Document-const: Fiddle::Types::UINT
212
+ *
213
+ * C type - unsigned int
214
+ */
215
+ rb_define_const(mFiddleTypes, "UINT", INT2NUM(TYPE_UINT));
216
+
217
+ /* Document-const: Fiddle::Types::LONG
218
+ *
219
+ * C type - long
220
+ */
221
+ rb_define_const(mFiddleTypes, "LONG", INT2NUM(TYPE_LONG));
196
222
 
197
- /* Document-const: TYPE_LONG
223
+ /* Document-const: Fiddle::Types::ULONG
198
224
  *
199
225
  * C type - long
200
226
  */
201
- rb_define_const(mFiddle, "TYPE_LONG", INT2NUM(TYPE_LONG));
227
+ rb_define_const(mFiddleTypes, "ULONG", INT2NUM(TYPE_ULONG));
202
228
 
203
229
  #if HAVE_LONG_LONG
204
- /* Document-const: TYPE_LONG_LONG
230
+ /* Document-const: Fiddle::Types::LONG_LONG
231
+ *
232
+ * C type - long long
233
+ */
234
+ rb_define_const(mFiddleTypes, "LONG_LONG", INT2NUM(TYPE_LONG_LONG));
235
+
236
+ /* Document-const: Fiddle::Types::ULONG_LONG
205
237
  *
206
238
  * C type - long long
207
239
  */
208
- rb_define_const(mFiddle, "TYPE_LONG_LONG", INT2NUM(TYPE_LONG_LONG));
240
+ rb_define_const(mFiddleTypes, "ULONG_LONG", INT2NUM(TYPE_ULONG_LONG));
209
241
  #endif
210
242
 
211
243
  #ifdef TYPE_INT8_T
212
- /* Document-const: TYPE_INT8_T
244
+ /* Document-const: Fiddle::Types::INT8_T
213
245
  *
214
246
  * C type - int8_t
215
247
  */
216
- rb_define_const(mFiddle, "TYPE_INT8_T", INT2NUM(TYPE_INT8_T));
248
+ rb_define_const(mFiddleTypes, "INT8_T", INT2NUM(TYPE_INT8_T));
249
+
250
+ /* Document-const: Fiddle::Types::UINT8_T
251
+ *
252
+ * C type - uint8_t
253
+ */
254
+ rb_define_const(mFiddleTypes, "UINT8_T", INT2NUM(TYPE_UINT8_T));
217
255
  #endif
218
256
 
219
257
  #ifdef TYPE_INT16_T
220
- /* Document-const: TYPE_INT16_T
258
+ /* Document-const: Fiddle::Types::INT16_T
221
259
  *
222
260
  * C type - int16_t
223
261
  */
224
- rb_define_const(mFiddle, "TYPE_INT16_T", INT2NUM(TYPE_INT16_T));
262
+ rb_define_const(mFiddleTypes, "INT16_T", INT2NUM(TYPE_INT16_T));
263
+
264
+ /* Document-const: Fiddle::Types::UINT16_T
265
+ *
266
+ * C type - uint16_t
267
+ */
268
+ rb_define_const(mFiddleTypes, "UINT16_T", INT2NUM(TYPE_UINT16_T));
225
269
  #endif
226
270
 
227
271
  #ifdef TYPE_INT32_T
228
- /* Document-const: TYPE_INT32_T
272
+ /* Document-const: Fiddle::Types::INT32_T
229
273
  *
230
274
  * C type - int32_t
231
275
  */
232
- rb_define_const(mFiddle, "TYPE_INT32_T", INT2NUM(TYPE_INT32_T));
276
+ rb_define_const(mFiddleTypes, "INT32_T", INT2NUM(TYPE_INT32_T));
277
+
278
+ /* Document-const: Fiddle::Types::UINT32_T
279
+ *
280
+ * C type - uint32_t
281
+ */
282
+ rb_define_const(mFiddleTypes, "UINT32_T", INT2NUM(TYPE_UINT32_T));
233
283
  #endif
234
284
 
235
285
  #ifdef TYPE_INT64_T
236
- /* Document-const: TYPE_INT64_T
286
+ /* Document-const: Fiddle::Types::INT64_T
237
287
  *
238
288
  * C type - int64_t
239
289
  */
240
- rb_define_const(mFiddle, "TYPE_INT64_T", INT2NUM(TYPE_INT64_T));
290
+ rb_define_const(mFiddleTypes, "INT64_T", INT2NUM(TYPE_INT64_T));
291
+
292
+ /* Document-const: Fiddle::Types::UINT64_T
293
+ *
294
+ * C type - uint64_t
295
+ */
296
+ rb_define_const(mFiddleTypes, "UINT64_T", INT2NUM(TYPE_UINT64_T));
241
297
  #endif
242
298
 
243
- /* Document-const: TYPE_FLOAT
299
+ /* Document-const: Fiddle::Types::FLOAT
244
300
  *
245
301
  * C type - float
246
302
  */
247
- rb_define_const(mFiddle, "TYPE_FLOAT", INT2NUM(TYPE_FLOAT));
303
+ rb_define_const(mFiddleTypes, "FLOAT", INT2NUM(TYPE_FLOAT));
248
304
 
249
- /* Document-const: TYPE_DOUBLE
305
+ /* Document-const: Fiddle::Types::DOUBLE
250
306
  *
251
307
  * C type - double
252
308
  */
253
- rb_define_const(mFiddle, "TYPE_DOUBLE", INT2NUM(TYPE_DOUBLE));
309
+ rb_define_const(mFiddleTypes, "DOUBLE", INT2NUM(TYPE_DOUBLE));
254
310
 
255
311
  #ifdef HAVE_FFI_PREP_CIF_VAR
256
- /* Document-const: TYPE_VARIADIC
312
+ /* Document-const: Fiddle::Types::VARIADIC
257
313
  *
258
314
  * C type - ...
259
315
  */
260
- rb_define_const(mFiddle, "TYPE_VARIADIC", INT2NUM(TYPE_VARIADIC));
316
+ rb_define_const(mFiddleTypes, "VARIADIC", INT2NUM(TYPE_VARIADIC));
261
317
  #endif
262
318
 
263
- /* Document-const: TYPE_CONST_STRING
319
+ /* Document-const: Fiddle::Types::CONST_STRING
264
320
  *
265
321
  * C type - const char* ('\0' terminated const char*)
266
322
  */
267
- rb_define_const(mFiddle, "TYPE_CONST_STRING", INT2NUM(TYPE_CONST_STRING));
323
+ rb_define_const(mFiddleTypes, "CONST_STRING", INT2NUM(TYPE_CONST_STRING));
268
324
 
269
- /* Document-const: TYPE_SIZE_T
325
+ /* Document-const: Fiddle::Types::SIZE_T
270
326
  *
271
327
  * C type - size_t
272
328
  */
273
- rb_define_const(mFiddle, "TYPE_SIZE_T", INT2NUM(TYPE_SIZE_T));
329
+ rb_define_const(mFiddleTypes, "SIZE_T", INT2NUM(TYPE_SIZE_T));
274
330
 
275
- /* Document-const: TYPE_SSIZE_T
331
+ /* Document-const: Fiddle::Types::SSIZE_T
276
332
  *
277
333
  * C type - ssize_t
278
334
  */
279
- rb_define_const(mFiddle, "TYPE_SSIZE_T", INT2NUM(TYPE_SSIZE_T));
335
+ rb_define_const(mFiddleTypes, "SSIZE_T", INT2NUM(TYPE_SSIZE_T));
280
336
 
281
- /* Document-const: TYPE_PTRDIFF_T
337
+ /* Document-const: Fiddle::Types::PTRDIFF_T
282
338
  *
283
339
  * C type - ptrdiff_t
284
340
  */
285
- rb_define_const(mFiddle, "TYPE_PTRDIFF_T", INT2NUM(TYPE_PTRDIFF_T));
341
+ rb_define_const(mFiddleTypes, "PTRDIFF_T", INT2NUM(TYPE_PTRDIFF_T));
286
342
 
287
- /* Document-const: TYPE_INTPTR_T
343
+ /* Document-const: Fiddle::Types::INTPTR_T
288
344
  *
289
345
  * C type - intptr_t
290
346
  */
291
- rb_define_const(mFiddle, "TYPE_INTPTR_T", INT2NUM(TYPE_INTPTR_T));
347
+ rb_define_const(mFiddleTypes, "INTPTR_T", INT2NUM(TYPE_INTPTR_T));
292
348
 
293
- /* Document-const: TYPE_UINTPTR_T
349
+ /* Document-const: Fiddle::Types::UINTPTR_T
294
350
  *
295
351
  * C type - uintptr_t
296
352
  */
297
- rb_define_const(mFiddle, "TYPE_UINTPTR_T", INT2NUM(TYPE_UINTPTR_T));
353
+ rb_define_const(mFiddleTypes, "UINTPTR_T", INT2NUM(TYPE_UINTPTR_T));
298
354
 
299
355
  /* Document-const: ALIGN_VOIDP
300
356
  *
@@ -422,30 +478,60 @@ Init_fiddle(void)
422
478
  */
423
479
  rb_define_const(mFiddle, "SIZEOF_CHAR", INT2NUM(sizeof(char)));
424
480
 
481
+ /* Document-const: SIZEOF_UCHAR
482
+ *
483
+ * size of a unsigned char
484
+ */
485
+ rb_define_const(mFiddle, "SIZEOF_UCHAR", INT2NUM(sizeof(unsigned char)));
486
+
425
487
  /* Document-const: SIZEOF_SHORT
426
488
  *
427
489
  * size of a short
428
490
  */
429
491
  rb_define_const(mFiddle, "SIZEOF_SHORT", INT2NUM(sizeof(short)));
430
492
 
493
+ /* Document-const: SIZEOF_USHORT
494
+ *
495
+ * size of a unsigned short
496
+ */
497
+ rb_define_const(mFiddle, "SIZEOF_USHORT", INT2NUM(sizeof(unsigned short)));
498
+
431
499
  /* Document-const: SIZEOF_INT
432
500
  *
433
501
  * size of an int
434
502
  */
435
503
  rb_define_const(mFiddle, "SIZEOF_INT", INT2NUM(sizeof(int)));
436
504
 
505
+ /* Document-const: SIZEOF_UINT
506
+ *
507
+ * size of an unsigned int
508
+ */
509
+ rb_define_const(mFiddle, "SIZEOF_UINT", INT2NUM(sizeof(unsigned int)));
510
+
437
511
  /* Document-const: SIZEOF_LONG
438
512
  *
439
513
  * size of a long
440
514
  */
441
515
  rb_define_const(mFiddle, "SIZEOF_LONG", INT2NUM(sizeof(long)));
442
516
 
517
+ /* Document-const: SIZEOF_ULONG
518
+ *
519
+ * size of a unsigned long
520
+ */
521
+ rb_define_const(mFiddle, "SIZEOF_ULONG", INT2NUM(sizeof(unsigned long)));
522
+
443
523
  #if HAVE_LONG_LONG
444
524
  /* Document-const: SIZEOF_LONG_LONG
445
525
  *
446
526
  * size of a long long
447
527
  */
448
528
  rb_define_const(mFiddle, "SIZEOF_LONG_LONG", INT2NUM(sizeof(LONG_LONG)));
529
+
530
+ /* Document-const: SIZEOF_ULONG_LONG
531
+ *
532
+ * size of a unsigned long long
533
+ */
534
+ rb_define_const(mFiddle, "SIZEOF_ULONG_LONG", INT2NUM(sizeof(unsigned LONG_LONG)));
449
535
  #endif
450
536
 
451
537
  /* Document-const: SIZEOF_INT8_T
@@ -454,24 +540,48 @@ Init_fiddle(void)
454
540
  */
455
541
  rb_define_const(mFiddle, "SIZEOF_INT8_T", INT2NUM(sizeof(int8_t)));
456
542
 
543
+ /* Document-const: SIZEOF_UINT8_T
544
+ *
545
+ * size of a uint8_t
546
+ */
547
+ rb_define_const(mFiddle, "SIZEOF_UINT8_T", INT2NUM(sizeof(uint8_t)));
548
+
457
549
  /* Document-const: SIZEOF_INT16_T
458
550
  *
459
551
  * size of a int16_t
460
552
  */
461
553
  rb_define_const(mFiddle, "SIZEOF_INT16_T", INT2NUM(sizeof(int16_t)));
462
554
 
555
+ /* Document-const: SIZEOF_UINT16_T
556
+ *
557
+ * size of a uint16_t
558
+ */
559
+ rb_define_const(mFiddle, "SIZEOF_UINT16_T", INT2NUM(sizeof(uint16_t)));
560
+
463
561
  /* Document-const: SIZEOF_INT32_T
464
562
  *
465
563
  * size of a int32_t
466
564
  */
467
565
  rb_define_const(mFiddle, "SIZEOF_INT32_T", INT2NUM(sizeof(int32_t)));
468
566
 
567
+ /* Document-const: SIZEOF_UINT32_T
568
+ *
569
+ * size of a uint32_t
570
+ */
571
+ rb_define_const(mFiddle, "SIZEOF_UINT32_T", INT2NUM(sizeof(uint32_t)));
572
+
469
573
  /* Document-const: SIZEOF_INT64_T
470
574
  *
471
575
  * size of a int64_t
472
576
  */
473
577
  rb_define_const(mFiddle, "SIZEOF_INT64_T", INT2NUM(sizeof(int64_t)));
474
578
 
579
+ /* Document-const: SIZEOF_UINT64_T
580
+ *
581
+ * size of a uint64_t
582
+ */
583
+ rb_define_const(mFiddle, "SIZEOF_UINT64_T", INT2NUM(sizeof(uint64_t)));
584
+
475
585
  /* Document-const: SIZEOF_FLOAT
476
586
  *
477
587
  * size of a float
@@ -540,13 +650,37 @@ Init_fiddle(void)
540
650
  rb_define_module_function(mFiddle, "realloc", rb_fiddle_realloc, 2);
541
651
  rb_define_module_function(mFiddle, "free", rb_fiddle_free, 1);
542
652
 
653
+ /* Document-const: Qtrue
654
+ *
655
+ * The value of Qtrue
656
+ */
657
+ rb_define_const(mFiddle, "Qtrue", INT2NUM(Qtrue));
658
+
659
+ /* Document-const: Qfalse
660
+ *
661
+ * The value of Qfalse
662
+ */
663
+ rb_define_const(mFiddle, "Qfalse", INT2NUM(Qfalse));
664
+
665
+ /* Document-const: Qnil
666
+ *
667
+ * The value of Qnil
668
+ */
669
+ rb_define_const(mFiddle, "Qnil", INT2NUM(Qnil));
670
+
671
+ /* Document-const: Qundef
672
+ *
673
+ * The value of Qundef
674
+ */
675
+ rb_define_const(mFiddle, "Qundef", INT2NUM(Qundef));
676
+
543
677
  Init_fiddle_function();
544
678
  Init_fiddle_closure();
545
679
  Init_fiddle_handle();
546
680
  Init_fiddle_pointer();
547
681
  Init_fiddle_pinned();
548
682
 
549
- #ifdef FIDDLE_MEMORY_VIEW
683
+ #ifdef HAVE_RUBY_MEMORY_VIEW_H
550
684
  Init_fiddle_memory_view();
551
685
  #endif
552
686
  }
data/ext/fiddle/fiddle.h CHANGED
@@ -12,6 +12,10 @@
12
12
  #include <sys/mman.h>
13
13
  #endif
14
14
 
15
+ #if defined(HAVE_LINK_H)
16
+ # include <link.h>
17
+ #endif
18
+
15
19
  #if defined(HAVE_DLFCN_H)
16
20
  # include <dlfcn.h>
17
21
  # /* some stranger systems may not define all of these */
@@ -107,11 +111,16 @@
107
111
  #define TYPE_VOID 0
108
112
  #define TYPE_VOIDP 1
109
113
  #define TYPE_CHAR 2
114
+ #define TYPE_UCHAR -TYPE_CHAR
110
115
  #define TYPE_SHORT 3
116
+ #define TYPE_USHORT -TYPE_SHORT
111
117
  #define TYPE_INT 4
118
+ #define TYPE_UINT -TYPE_INT
112
119
  #define TYPE_LONG 5
120
+ #define TYPE_ULONG -TYPE_LONG
113
121
  #if HAVE_LONG_LONG
114
122
  #define TYPE_LONG_LONG 6
123
+ #define TYPE_ULONG_LONG -TYPE_LONG_LONG
115
124
  #endif
116
125
  #define TYPE_FLOAT 7
117
126
  #define TYPE_DOUBLE 8
@@ -119,11 +128,18 @@
119
128
  #define TYPE_CONST_STRING 10
120
129
 
121
130
  #define TYPE_INT8_T TYPE_CHAR
131
+ #define TYPE_UINT8_T -TYPE_INT8_T
132
+
122
133
  #if SIZEOF_SHORT == 2
123
134
  # define TYPE_INT16_T TYPE_SHORT
124
135
  #elif SIZEOF_INT == 2
125
136
  # define TYPE_INT16_T TYPE_INT
126
137
  #endif
138
+
139
+ #ifdef TYPE_INT16_T
140
+ # define TYPE_UINT16_T -TYPE_INT16_T
141
+ #endif
142
+
127
143
  #if SIZEOF_SHORT == 4
128
144
  # define TYPE_INT32_T TYPE_SHORT
129
145
  #elif SIZEOF_INT == 4
@@ -131,6 +147,11 @@
131
147
  #elif SIZEOF_LONG == 4
132
148
  # define TYPE_INT32_T TYPE_LONG
133
149
  #endif
150
+
151
+ #ifdef TYPE_INT32_T
152
+ #define TYPE_UINT32_T -TYPE_INT32_T
153
+ #endif
154
+
134
155
  #if SIZEOF_INT == 8
135
156
  # define TYPE_INT64_T TYPE_INT
136
157
  #elif SIZEOF_LONG == 8
@@ -139,6 +160,10 @@
139
160
  # define TYPE_INT64_T TYPE_LONG_LONG
140
161
  #endif
141
162
 
163
+ #ifdef TYPE_INT64_T
164
+ #define TYPE_UINT64_T -TYPE_INT64_T
165
+ #endif
166
+
142
167
  #ifndef TYPE_SSIZE_T
143
168
  # if SIZEOF_SIZE_T == SIZEOF_INT
144
169
  # define TYPE_SSIZE_T TYPE_INT
@@ -189,14 +214,13 @@
189
214
  #define ALIGN_INT32_T ALIGN_OF(int32_t)
190
215
  #define ALIGN_INT64_T ALIGN_OF(int64_t)
191
216
 
192
- #ifdef HAVE_TYPE_RB_MEMORY_VIEW_T
193
- # define FIDDLE_MEMORY_VIEW
194
- #endif
195
-
196
217
  extern VALUE mFiddle;
197
218
  extern VALUE rb_eFiddleDLError;
198
219
 
199
220
  VALUE rb_fiddle_new_function(VALUE address, VALUE arg_types, VALUE ret_type);
200
221
 
222
+ typedef void (*rb_fiddle_freefunc_t)(void*);
223
+ VALUE rb_fiddle_ptr_new_wrap(void *ptr, long size, rb_fiddle_freefunc_t func, VALUE wrap0, VALUE wrap1);
224
+
201
225
  #endif
202
226
  /* vim: set noet sws=4 sw=4: */
data/ext/fiddle/handle.c CHANGED
@@ -259,7 +259,21 @@ rb_fiddle_handle_to_i(VALUE self)
259
259
  struct dl_handle *fiddle_handle;
260
260
 
261
261
  TypedData_Get_Struct(self, struct dl_handle, &fiddle_handle_data_type, fiddle_handle);
262
- return PTR2NUM(fiddle_handle);
262
+ return PTR2NUM(fiddle_handle->ptr);
263
+ }
264
+
265
+ /*
266
+ * call-seq: to_ptr
267
+ *
268
+ * Returns the Fiddle::Pointer of this handle.
269
+ */
270
+ static VALUE
271
+ rb_fiddle_handle_to_ptr(VALUE self)
272
+ {
273
+ struct dl_handle *fiddle_handle;
274
+
275
+ TypedData_Get_Struct(self, struct dl_handle, &fiddle_handle_data_type, fiddle_handle);
276
+ return rb_fiddle_ptr_new_wrap(fiddle_handle->ptr, 0, 0, self, 0);
263
277
  }
264
278
 
265
279
  static VALUE fiddle_handle_sym(void *handle, VALUE symbol);
@@ -307,8 +321,10 @@ rb_fiddle_handle_s_sym(VALUE self, VALUE sym)
307
321
  return fiddle_handle_sym(RTLD_NEXT, sym);
308
322
  }
309
323
 
310
- static VALUE
311
- fiddle_handle_sym(void *handle, VALUE symbol)
324
+ typedef void (*fiddle_void_func)(void);
325
+
326
+ static fiddle_void_func
327
+ fiddle_handle_find_func(void *handle, VALUE symbol)
312
328
  {
313
329
  #if defined(HAVE_DLERROR)
314
330
  const char *err;
@@ -316,13 +332,13 @@ fiddle_handle_sym(void *handle, VALUE symbol)
316
332
  #else
317
333
  # define CHECK_DLERROR
318
334
  #endif
319
- void (*func)();
335
+ fiddle_void_func func;
320
336
  const char *name = StringValueCStr(symbol);
321
337
 
322
338
  #ifdef HAVE_DLERROR
323
339
  dlerror();
324
340
  #endif
325
- func = (void (*)())(VALUE)dlsym(handle, name);
341
+ func = (fiddle_void_func)(VALUE)dlsym(handle, name);
326
342
  CHECK_DLERROR;
327
343
  #if defined(FUNC_STDCALL)
328
344
  if( !func ){
@@ -365,6 +381,53 @@ fiddle_handle_sym(void *handle, VALUE symbol)
365
381
  xfree(name_n);
366
382
  }
367
383
  #endif
384
+
385
+ return func;
386
+ }
387
+
388
+ static VALUE
389
+ rb_fiddle_handle_s_sym_defined(VALUE self, VALUE sym)
390
+ {
391
+ fiddle_void_func func;
392
+
393
+ func = fiddle_handle_find_func(RTLD_NEXT, sym);
394
+
395
+ if( func ) {
396
+ return PTR2NUM(func);
397
+ }
398
+ else {
399
+ return Qnil;
400
+ }
401
+ }
402
+
403
+ static VALUE
404
+ rb_fiddle_handle_sym_defined(VALUE self, VALUE sym)
405
+ {
406
+ struct dl_handle *fiddle_handle;
407
+ fiddle_void_func func;
408
+
409
+ TypedData_Get_Struct(self, struct dl_handle, &fiddle_handle_data_type, fiddle_handle);
410
+ if( ! fiddle_handle->open ){
411
+ rb_raise(rb_eFiddleDLError, "closed handle");
412
+ }
413
+
414
+ func = fiddle_handle_find_func(fiddle_handle->ptr, sym);
415
+
416
+ if( func ) {
417
+ return PTR2NUM(func);
418
+ }
419
+ else {
420
+ return Qnil;
421
+ }
422
+ }
423
+
424
+ static VALUE
425
+ fiddle_handle_sym(void *handle, VALUE symbol)
426
+ {
427
+ fiddle_void_func func;
428
+
429
+ func = fiddle_handle_find_func(handle, symbol);
430
+
368
431
  if( !func ){
369
432
  rb_raise(rb_eFiddleDLError, "unknown symbol \"%"PRIsVALUE"\"", symbol);
370
433
  }
@@ -372,6 +435,48 @@ fiddle_handle_sym(void *handle, VALUE symbol)
372
435
  return PTR2NUM(func);
373
436
  }
374
437
 
438
+ /*
439
+ * call-seq: file_name
440
+ *
441
+ * Returns the file name of this handle.
442
+ */
443
+ static VALUE
444
+ rb_fiddle_handle_file_name(VALUE self)
445
+ {
446
+ struct dl_handle *fiddle_handle;
447
+
448
+ TypedData_Get_Struct(self, struct dl_handle, &fiddle_handle_data_type, fiddle_handle);
449
+
450
+ #if defined(HAVE_DLINFO) && defined(HAVE_CONST_RTLD_DI_LINKMAP)
451
+ {
452
+ struct link_map *lm = NULL;
453
+ int res = dlinfo(fiddle_handle->ptr, RTLD_DI_LINKMAP, &lm);
454
+ if (res == 0 && lm != NULL) {
455
+ return rb_str_new_cstr(lm->l_name);
456
+ }
457
+ else {
458
+ #if defined(HAVE_DLERROR)
459
+ rb_raise(rb_eFiddleDLError, "could not get handle file name: %s", dlerror());
460
+ #else
461
+ rb_raise(rb_eFiddleDLError, "could not get handle file name");
462
+ #endif
463
+ }
464
+ }
465
+ #elif defined(HAVE_GETMODULEFILENAME)
466
+ {
467
+ char filename[MAX_PATH];
468
+ DWORD res = GetModuleFileName(fiddle_handle->ptr, filename, MAX_PATH);
469
+ if (res == 0) {
470
+ rb_raise(rb_eFiddleDLError, "could not get handle file name: %s", dlerror());
471
+ }
472
+ return rb_str_new_cstr(filename);
473
+ }
474
+ #else
475
+ (void)fiddle_handle;
476
+ return Qnil;
477
+ #endif
478
+ }
479
+
375
480
  void
376
481
  Init_fiddle_handle(void)
377
482
  {
@@ -412,6 +517,7 @@ Init_fiddle_handle(void)
412
517
  rb_cHandle = rb_define_class_under(mFiddle, "Handle", rb_cObject);
413
518
  rb_define_alloc_func(rb_cHandle, rb_fiddle_handle_s_allocate);
414
519
  rb_define_singleton_method(rb_cHandle, "sym", rb_fiddle_handle_s_sym, 1);
520
+ rb_define_singleton_method(rb_cHandle, "sym_defined?", rb_fiddle_handle_s_sym_defined, 1);
415
521
  rb_define_singleton_method(rb_cHandle, "[]", rb_fiddle_handle_s_sym, 1);
416
522
 
417
523
  /* Document-const: NEXT
@@ -466,9 +572,12 @@ Init_fiddle_handle(void)
466
572
 
467
573
  rb_define_method(rb_cHandle, "initialize", rb_fiddle_handle_initialize, -1);
468
574
  rb_define_method(rb_cHandle, "to_i", rb_fiddle_handle_to_i, 0);
575
+ rb_define_method(rb_cHandle, "to_ptr", rb_fiddle_handle_to_ptr, 0);
469
576
  rb_define_method(rb_cHandle, "close", rb_fiddle_handle_close, 0);
470
577
  rb_define_method(rb_cHandle, "sym", rb_fiddle_handle_sym, 1);
471
578
  rb_define_method(rb_cHandle, "[]", rb_fiddle_handle_sym, 1);
579
+ rb_define_method(rb_cHandle, "sym_defined?", rb_fiddle_handle_sym_defined, 1);
580
+ rb_define_method(rb_cHandle, "file_name", rb_fiddle_handle_file_name, 0);
472
581
  rb_define_method(rb_cHandle, "disable_close", rb_fiddle_handle_disable_close, 0);
473
582
  rb_define_method(rb_cHandle, "enable_close", rb_fiddle_handle_enable_close, 0);
474
583
  rb_define_method(rb_cHandle, "close_enabled?", rb_fiddle_handle_close_enabled_p, 0);
@@ -1,10 +1,11 @@
1
+ #include <fiddle.h>
2
+
3
+ #ifdef HAVE_RUBY_MEMORY_VIEW_H
4
+
1
5
  #include <stdbool.h>
2
6
  #include <ruby/ruby.h>
3
7
  #include <ruby/encoding.h>
4
-
5
- #ifdef HAVE_RUBY_MEMORY_VIEW_H
6
- # include <ruby/memory_view.h>
7
- #endif
8
+ #include <ruby/memory_view.h>
8
9
 
9
10
  #if SIZEOF_INTPTR_T == SIZEOF_LONG_LONG
10
11
  # define INTPTR2NUM LL2NUM
@@ -17,9 +18,6 @@
17
18
  # define UINTPTR2NUM UINT2NUM
18
19
  #endif
19
20
 
20
- #include <fiddle.h>
21
-
22
- #ifdef FIDDLE_MEMORY_VIEW
23
21
  VALUE rb_cMemoryView = Qnil;
24
22
 
25
23
  struct memview_data {
@@ -320,4 +318,4 @@ Init_fiddle_memory_view(void)
320
318
  rb_define_method(rb_cMemoryView, "to_s", rb_fiddle_memview_to_s, 0);
321
319
  }
322
320
 
323
- #endif /* FIDDLE_MEMORY_VIEW */
321
+ #endif /* HAVE_RUBY_MEMORY_VIEW_H */