1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

added DL::Importer.sizeof().

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7897 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ttate 2005-02-06 06:54:12 +00:00
parent b393a2bc5f
commit 93711e5bd3
3 changed files with 50 additions and 0 deletions

View file

@ -10,6 +10,8 @@
VALUE rb_cDLCPtr;
static ID id_to_ptr;
static void
dlptr_free(struct ptr_data *data)
{
@ -422,6 +424,15 @@ rb_dlptr_s_to_ptr(VALUE self, VALUE val)
#endif
return rb_dlptr_new(fp, sizeof(FILE), NULL);
}
else if( rb_respond_to(val, id_to_ptr) ){
VALUE vptr = rb_funcall(val, id_to_ptr, 0);
if( rb_obj_is_kind_of(vptr, rb_cDLCPtr) ){
return vptr;
}
else{
rb_raise(rb_eDLError, "to_ptr should return a CPtr object.");
}
}
else{
return rb_dlptr_new(NUM2PTR(rb_Integer(val)), 0, NULL);
}
@ -430,6 +441,8 @@ rb_dlptr_s_to_ptr(VALUE self, VALUE val)
void
Init_dlptr()
{
id_to_ptr = rb_intern("to_ptr");
rb_cDLCPtr = rb_define_class_under(rb_mDL, "CPtr", rb_cObject);
rb_define_alloc_func(rb_cDLCPtr, rb_dlptr_s_allocate);
rb_define_singleton_method(rb_cDLCPtr, "malloc", rb_dlptr_s_malloc, -1);

View file

@ -62,6 +62,38 @@ module DL
@type_alias[alias_type] = orig_type
end
def sizeof(ty)
case ty
when String
ty = parse_ctype(ty, @type_alias).abs()
case ty
when TYPE_CHAR
return SIZEOF_CHAR
when TYPE_SHORT
return SIZEOF_SHORT
when TYPE_INT
return SIZEOF_INT
when TYPE_LONG
return SIZEOF_LONG
when TYPE_LONG_LONG
return SIZEOF_LONG_LON
when TYPE_FLOAT
return SIZEOF_FLOAT
when TYPE_DOUBLE
return SIZEOF_DOUBLE
when TYPE_VOIDP
return SIZEOF_VOIDP
else
raise(DLError, "unknown type: #{ty}")
end
when Class
if( ty.instance_methods().include?("to_ptr") )
return ty.size()
end
end
return CPtr[ty].size()
end
def parse_bind_options(opts)
h = {}
prekey = nil

View file

@ -40,6 +40,11 @@ module DL
end
class TestImport < TestBase
def test_sizeof()
assert_equal(DL::SIZEOF_VOIDP, LIBC.sizeof("FILE*"))
assert_equal(LIBC::MyStruct.size(), LIBC.sizeof(LIBC::MyStruct))
end
def test_unsigned_result()
d = (2 ** 31) + 1