diff options
author | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-27 01:38:41 +0000 |
---|---|---|
committer | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-27 01:38:41 +0000 |
commit | ab2a7a9346038c4d47dcac030611834cde06a62d (patch) | |
tree | e588c360d963553a45f01146e1720b77c1df4ebf /ext/fiddle/fiddle.c | |
parent | 357d42569fc99c9eb6482484dab8a72cd35c57da (diff) | |
download | ruby-ab2a7a9346038c4d47dcac030611834cde06a62d.tar.gz |
* ext/fiddle/handle.c: Make Fiddle independent of DL, copy DL::Handle
to Fiddle::Handle.
* ext/fiddle/pointer.c: Make Fiddle independent of DL, copy
DL::Pointer to Fiddle::Pointer.
* test/fiddle/test_func.rb: relevent tests
* test/fiddle/test_handle.rb: ditto
* test/fiddle/test_pointer.rb: ditto
* ext/dl/lib/dl/struct.rb: use Fiddle::Pointer if available
* ext/fiddle/extconf.rb: check for dlfcn.h
* ext/fiddle/fiddle.c: add constants for sizeof() things
* ext/fiddle/fiddle.h: include dlfcn.h
* ext/fiddle/function.c: expose a C function for creating new
Fiddle::Function objects.
* ext/fiddle/lib/fiddle.rb: include constants for dl backwards compat
* ext/fiddle/lib/fiddle/function.rb: read the pointer from the
function for dl backwards compat.
* test/dl/test_callback.rb: check the addresses of the pointers rather
than their types.
* test/fiddle/helper.rb: remove dependency on dl
* test/fiddle/test_closure.rb: ditto
* test/fiddle/test_fiddle.rb: ditto
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37881 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/fiddle/fiddle.c')
-rw-r--r-- | ext/fiddle/fiddle.c | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/ext/fiddle/fiddle.c b/ext/fiddle/fiddle.c index 3006bbeb77..85a362cb04 100644 --- a/ext/fiddle/fiddle.c +++ b/ext/fiddle/fiddle.c @@ -1,6 +1,7 @@ #include <fiddle.h> VALUE mFiddle; +VALUE rb_eFiddleError; #ifndef TYPE_SSIZE_T # if SIZEOF_SIZE_T == SIZEOF_INT @@ -34,6 +35,100 @@ VALUE mFiddle; #endif #define TYPE_UINTPTR_T (-TYPE_INTPTR_T) +void Init_fiddle_pointer(void); + +/* + * call-seq: Fiddle.malloc(size) + * + * Allocate +size+ bytes of memory and return the integer memory address + * for the allocated memory. + */ +static VALUE +rb_fiddle_malloc(VALUE self, VALUE size) +{ + void *ptr; + + rb_secure(4); + ptr = (void*)ruby_xmalloc(NUM2INT(size)); + return PTR2NUM(ptr); +} + +/* + * call-seq: Fiddle.realloc(addr, size) + * + * Change the size of the memory allocated at the memory location +addr+ to + * +size+ bytes. Returns the memory address of the reallocated memory, which + * may be different than the address passed in. + */ +static VALUE +rb_fiddle_realloc(VALUE self, VALUE addr, VALUE size) +{ + void *ptr = NUM2PTR(addr); + + rb_secure(4); + ptr = (void*)ruby_xrealloc(ptr, NUM2INT(size)); + return PTR2NUM(ptr); +} + +/* + * call-seq: Fiddle.free(addr) + * + * Free the memory at address +addr+ + */ +VALUE +rb_fiddle_free(VALUE self, VALUE addr) +{ + void *ptr = NUM2PTR(addr); + + rb_secure(4); + ruby_xfree(ptr); + return Qnil; +} + +/* + * call-seq: Fiddle.dlunwrap(addr) + * + * Returns the hexadecimal representation of a memory pointer address +addr+ + * + * Example: + * + * lib = Fiddle.dlopen('/lib64/libc-2.15.so') + * => #<Fiddle::Handle:0x00000001342460> + * + * lib['strcpy'].to_s(16) + * => "7f59de6dd240" + * + * Fiddle.dlunwrap(Fiddle.dlwrap(lib['strcpy'].to_s(16))) + * => "7f59de6dd240" + */ +VALUE +rb_fiddle_ptr2value(VALUE self, VALUE addr) +{ + rb_secure(4); + return (VALUE)NUM2PTR(addr); +} + +/* + * call-seq: Fiddle.dlwrap(val) + * + * Returns a memory pointer of a function's hexadecimal address location +val+ + * + * Example: + * + * lib = Fiddle.dlopen('/lib64/libc-2.15.so') + * => #<Fiddle::Handle:0x00000001342460> + * + * Fiddle.dlwrap(lib['strcpy'].to_s(16)) + * => 25522520 + */ +static VALUE +rb_fiddle_value2ptr(VALUE self, VALUE val) +{ + return PTR2NUM((void*)val); +} + +void Init_fiddle_handle(void); + void Init_fiddle(void) { @@ -47,6 +142,13 @@ Init_fiddle(void) */ mFiddle = rb_define_module("Fiddle"); + /* + * Document-class: Fiddle::DLError + * + * standard dynamic load exception + */ + rb_eFiddleError = rb_define_class_under(mFiddle, "DLError", rb_eStandardError); + /* Document-const: TYPE_VOID * * C type - void @@ -143,7 +245,103 @@ Init_fiddle(void) rb_define_const(mFiddle, "WINDOWS", Qfalse); #endif + /* Document-const: SIZEOF_VOIDP + * + * size of a void* + */ + rb_define_const(mFiddle, "SIZEOF_VOIDP", INT2NUM(sizeof(void*))); + + /* Document-const: SIZEOF_CHAR + * + * size of a char + */ + rb_define_const(mFiddle, "SIZEOF_CHAR", INT2NUM(sizeof(char))); + + /* Document-const: SIZEOF_SHORT + * + * size of a short + */ + rb_define_const(mFiddle, "SIZEOF_SHORT", INT2NUM(sizeof(short))); + + /* Document-const: SIZEOF_INT + * + * size of an int + */ + rb_define_const(mFiddle, "SIZEOF_INT", INT2NUM(sizeof(int))); + + /* Document-const: SIZEOF_LONG + * + * size of a long + */ + rb_define_const(mFiddle, "SIZEOF_LONG", INT2NUM(sizeof(long))); + +#if HAVE_LONG_LONG + /* Document-const: SIZEOF_LONG_LONG + * + * size of a long long + */ + rb_define_const(mFiddle, "SIZEOF_LONG_LONG", INT2NUM(sizeof(LONG_LONG))); +#endif + + /* Document-const: SIZEOF_FLOAT + * + * size of a float + */ + rb_define_const(mFiddle, "SIZEOF_FLOAT", INT2NUM(sizeof(float))); + + /* Document-const: SIZEOF_DOUBLE + * + * size of a double + */ + rb_define_const(mFiddle, "SIZEOF_DOUBLE",INT2NUM(sizeof(double))); + + /* Document-const: SIZEOF_SIZE_T + * + * size of a size_t + */ + rb_define_const(mFiddle, "SIZEOF_SIZE_T", INT2NUM(sizeof(size_t))); + + /* Document-const: SIZEOF_SSIZE_T + * + * size of a ssize_t + */ + rb_define_const(mFiddle, "SIZEOF_SSIZE_T", INT2NUM(sizeof(size_t))); /* same as size_t */ + + /* Document-const: SIZEOF_PTRDIFF_T + * + * size of a ptrdiff_t + */ + rb_define_const(mFiddle, "SIZEOF_PTRDIFF_T", INT2NUM(sizeof(ptrdiff_t))); + + /* Document-const: SIZEOF_INTPTR_T + * + * size of a intptr_t + */ + rb_define_const(mFiddle, "SIZEOF_INTPTR_T", INT2NUM(sizeof(intptr_t))); + + /* Document-const: SIZEOF_UINTPTR_T + * + * size of a uintptr_t + */ + rb_define_const(mFiddle, "SIZEOF_UINTPTR_T", INT2NUM(sizeof(uintptr_t))); + + rb_define_module_function(mFiddle, "dlwrap", rb_fiddle_value2ptr, 1); + + /* Document-const: RUBY_FREE + * + * Address of the ruby_xfree() function + */ + rb_define_const(mFiddle, "RUBY_FREE", PTR2NUM(ruby_xfree)); + + rb_define_module_function(mFiddle, "dlwrap", rb_fiddle_value2ptr, 1); + rb_define_module_function(mFiddle, "dlunwrap", rb_fiddle_ptr2value, 1); + rb_define_module_function(mFiddle, "malloc", rb_fiddle_malloc, 1); + rb_define_module_function(mFiddle, "realloc", rb_fiddle_realloc, 2); + rb_define_module_function(mFiddle, "free", rb_fiddle_free, 1); + Init_fiddle_function(); Init_fiddle_closure(); + Init_fiddle_handle(); + Init_fiddle_pointer(); } /* vim: set noet sws=4 sw=4: */ |