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

Conform to the allocation framework.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2686 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ttate 2002-08-04 13:49:13 +00:00
parent 14859cdf94
commit 45a08a4296
5 changed files with 83 additions and 49 deletions

View file

@ -538,7 +538,7 @@ rb_io_to_ptr(VALUE self)
VALUE
rb_dl_dlopen(int argc, VALUE argv[], VALUE self)
{
return rb_dlhandle_s_new(argc, argv, rb_cDLHandle);
return rb_class_new_instance(argc, argv, rb_cDLHandle);
}
VALUE

View file

@ -18,8 +18,6 @@ rb_cDLHandle
rb_dlhandle_close
rb_dlhandle_disable_close
rb_dlhandle_enable_close
rb_dlhandle_init
rb_dlhandle_s_new
rb_dlhandle_sym
Init_dlptr
rb_cDLPtrData
@ -54,10 +52,8 @@ rb_cDLSymbol
rb_dlsym2csym
rb_dlsym_call
rb_dlsym_cproto
rb_dlsym_initialize
rb_dlsym_inspect
rb_dlsym_name
rb_dlsym_new
rb_dlsym_proto
rb_dlsym_s_new
rb_dlsym_to_ptr

View file

@ -26,10 +26,24 @@ rb_dlhandle_close(VALUE self)
}
VALUE
rb_dlhandle_s_new(int argc, VALUE argv[], VALUE self)
rb_dlhandle_s_allocate(VALUE klass)
{
VALUE obj;
struct dl_handle *dlhandle;
obj = Data_Make_Struct(rb_cDLHandle, struct dl_handle, 0,
dlhandle_free, dlhandle);
dlhandle->ptr = 0;
dlhandle->open = 0;
dlhandle->enable_close = 0;
return obj;
}
VALUE
rb_dlhandle_initialize(int argc, VALUE argv[], VALUE self)
{
void *ptr;
VALUE val;
struct dl_handle *dlhandle;
VALUE lib, flag;
char *clib;
@ -60,24 +74,15 @@ rb_dlhandle_s_new(int argc, VALUE argv[], VALUE self)
rb_raise(rb_eRuntimeError, err);
};
#endif
val = Data_Make_Struct(rb_cDLHandle, struct dl_handle, 0,
dlhandle_free, dlhandle);
Data_Get_Struct(self, struct dl_handle, dlhandle);
dlhandle->ptr = ptr;
dlhandle->open = 1;
dlhandle->enable_close = 0;
rb_obj_call_init(val, argc, argv);
if( rb_block_given_p() ){
rb_ensure(rb_yield, val, rb_dlhandle_close, val);
rb_ensure(rb_yield, self, rb_dlhandle_close, self);
};
return val;
}
VALUE
rb_dlhandle_init(int argc, VALUE argv[], VALUE self)
{
return Qnil;
}
@ -153,6 +158,9 @@ rb_dlhandle_sym(int argc, VALUE argv[], VALUE self)
Data_Get_Struct(self, struct dl_handle, dlhandle);
if( ! dlhandle->open ){
rb_raise(rb_eRuntimeError, "Closed handle.");
}
handle = dlhandle->ptr;
func = dlsym(handle, name);
@ -192,9 +200,9 @@ rb_dlhandle_sym(int argc, VALUE argv[], VALUE self)
void
Init_dlhandle()
{
rb_cDLHandle = rb_define_class_under(rb_mDL, "Handle", rb_cData);
rb_define_singleton_method(rb_cDLHandle, "new", rb_dlhandle_s_new, -1);
rb_define_method(rb_cDLHandle, "initialize", rb_dlhandle_init, -1);
rb_cDLHandle = rb_define_class_under(rb_mDL, "Handle", rb_cObject);
rb_define_singleton_method(rb_cDLHandle, "allocate", rb_dlhandle_s_allocate, 0);
rb_define_method(rb_cDLHandle, "initialize", rb_dlhandle_initialize, -1);
rb_define_method(rb_cDLHandle, "to_i", rb_dlhandle_to_i, 0);
rb_define_method(rb_cDLHandle, "to_ptr", rb_dlhandle_to_ptr, 0);
rb_define_method(rb_cDLHandle, "close", rb_dlhandle_close, 0);

View file

@ -155,9 +155,30 @@ rb_dlptr2cptr(VALUE val)
}
static VALUE
rb_dlptr_s_new(int argc, VALUE argv[], VALUE klass)
rb_dlptr_s_allocate(VALUE klass)
{
VALUE obj;
struct ptr_data *data;
obj = Data_Make_Struct(klass, struct ptr_data, 0, dlptr_free, data);
data->ptr = 0;
data->free = 0;
data->ctype = DLPTR_CTYPE_UNKNOWN;
data->stype = NULL;
data->ssize = NULL;
data->slen = 0;
data->size = 0;
data->ids = NULL;
data->ids_num = 0;
return obj;
}
static VALUE
rb_dlptr_initialize(int argc, VALUE argv[], VALUE self)
{
VALUE ptr, sym, obj, size;
struct ptr_data *data;
void *p = NULL;
freefunc_t f = NULL;
long s = 0;
@ -179,11 +200,14 @@ rb_dlptr_s_new(int argc, VALUE argv[], VALUE klass)
rb_bug("rb_dlptr_s_new");
};
obj = rb_dlptr_new(p,s,f);
if( p ){
Data_Get_Struct(self, struct ptr_data, data);
data->ptr = p;
data->size = s;
data->free = f;
}
rb_obj_call_init(obj, argc, argv);
return obj;
return Qnil;
}
static VALUE
@ -210,12 +234,6 @@ rb_dlptr_s_malloc(int argc, VALUE argv[], VALUE klass)
return obj;
}
static VALUE
rb_dlptr_init(int argc, VALUE argv[], VALUE self)
{
return Qnil;
}
VALUE
rb_dlptr_to_i(VALUE self)
{
@ -1002,10 +1020,10 @@ rb_dlmem_each(VALUE self)
void
Init_dlptr()
{
rb_cDLPtrData = rb_define_class_under(rb_mDL, "PtrData", rb_cData);
rb_define_singleton_method(rb_cDLPtrData, "new", rb_dlptr_s_new, -1);
rb_cDLPtrData = rb_define_class_under(rb_mDL, "PtrData", rb_cObject);
rb_define_singleton_method(rb_cDLPtrData, "allocate", rb_dlptr_s_allocate, 0);
rb_define_singleton_method(rb_cDLPtrData, "malloc", rb_dlptr_s_malloc, -1);
rb_define_method(rb_cDLPtrData, "initialize", rb_dlptr_init, -1);
rb_define_method(rb_cDLPtrData, "initialize", rb_dlptr_initialize, -1);
rb_define_method(rb_cDLPtrData, "free=", rb_dlptr_free_set, 1);
rb_define_method(rb_cDLPtrData, "free", rb_dlptr_free_get, 0);
rb_define_method(rb_cDLPtrData, "to_i", rb_dlptr_to_i, 0);

View file

@ -127,10 +127,26 @@ rb_dlsym2csym(VALUE val)
}
VALUE
rb_dlsym_s_new(int argc, VALUE argv[], VALUE self)
rb_dlsym_s_allocate(VALUE klass)
{
VALUE obj;
struct sym_data *data;
obj = Data_Make_Struct(klass, struct sym_data, 0, dlsym_free, data);
data->func = 0;
data->name = 0;
data->type = 0;
data->len = 0;
return obj;
}
VALUE
rb_dlsym_initialize(int argc, VALUE argv[], VALUE self)
{
VALUE addr, name, type;
VALUE val;
struct sym_data *data;
void *saddr;
const char *sname, *stype;
@ -140,18 +156,14 @@ rb_dlsym_s_new(int argc, VALUE argv[], VALUE self)
sname = NIL_P(name) ? NULL : StringValuePtr(name);
stype = NIL_P(type) ? NULL : StringValuePtr(type);
val = rb_dlsym_new((void (*)())saddr, sname, stype);
if( saddr ){
Data_Get_Struct(self, struct sym_data, data);
data->func = saddr;
data->name = sname ? strdup(sname) : 0;
data->type = stype ? strdup(stype) : 0;
data->len = stype ? strlen(stype) : 0;
}
if( val != Qnil ){
rb_obj_call_init(val, argc, argv);
};
return val;
}
VALUE
rb_dlsym_initialize(int argc, VALUE argv[], VALUE self)
{
return Qnil;
}
@ -807,8 +819,8 @@ rb_dlsym_to_ptr(VALUE self)
void
Init_dlsym()
{
rb_cDLSymbol = rb_define_class_under(rb_mDL, "Symbol", rb_cData);
rb_define_singleton_method(rb_cDLSymbol, "new", rb_dlsym_s_new, -1);
rb_cDLSymbol = rb_define_class_under(rb_mDL, "Symbol", rb_cObject);
rb_define_singleton_method(rb_cDLSymbol, "allocate", rb_dlsym_s_allocate, 0);
rb_define_singleton_method(rb_cDLSymbol, "char2type", rb_s_dlsym_char2type, 1);
rb_define_method(rb_cDLSymbol, "initialize", rb_dlsym_initialize, -1);
rb_define_method(rb_cDLSymbol, "call", rb_dlsym_call, -1);