mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/dl/cfunc.c (dlcfunc_data_type): typed.
* ext/dl/cptr.c (dlptr_data_type): ditto. * ext/dl/handle.c (dlhandle_data_type): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24821 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
098d8d11e1
commit
c754c0b237
4 changed files with 91 additions and 47 deletions
|
@ -1,3 +1,11 @@
|
|||
Wed Sep 9 21:19:27 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* ext/dl/cfunc.c (dlcfunc_data_type): typed.
|
||||
|
||||
* ext/dl/cptr.c (dlptr_data_type): ditto.
|
||||
|
||||
* ext/dl/handle.c (dlhandle_data_type): ditto.
|
||||
|
||||
Wed Sep 9 17:17:31 2009 NARUSE, Yui <naruse@ruby-lang.org>
|
||||
|
||||
* re.c (parser_regx_options): only one kcode should effect
|
||||
|
|
|
@ -42,7 +42,7 @@ rb_dl_set_win32_last_error(VALUE self, VALUE val)
|
|||
#endif
|
||||
|
||||
|
||||
void
|
||||
static void
|
||||
dlcfunc_free(void *ptr)
|
||||
{
|
||||
struct cfunc_data *data = ptr;
|
||||
|
@ -52,6 +52,25 @@ dlcfunc_free(void *ptr)
|
|||
xfree(data);
|
||||
}
|
||||
|
||||
static size_t
|
||||
dlcfunc_memsize(const void *ptr)
|
||||
{
|
||||
const struct cfunc_data *data = ptr;
|
||||
size_t size = 0;
|
||||
if( data ){
|
||||
size += sizeof(*data);
|
||||
if( data->name ){
|
||||
size += strlen(data->name) + 1;
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
const rb_data_type_t dlcfunc_data_type = {
|
||||
"dl/cfunc",
|
||||
0, dlcfunc_free, dlcfunc_memsize,
|
||||
};
|
||||
|
||||
VALUE
|
||||
rb_dlcfunc_new(void (*func)(), int type, const char *name, ID calltype)
|
||||
{
|
||||
|
@ -60,7 +79,7 @@ rb_dlcfunc_new(void (*func)(), int type, const char *name, ID calltype)
|
|||
|
||||
rb_secure(4);
|
||||
if( func ){
|
||||
val = Data_Make_Struct(rb_cDLCFunc, struct cfunc_data, 0, dlcfunc_free, data);
|
||||
val = TypedData_Make_Struct(rb_cDLCFunc, struct cfunc_data, &dlcfunc_data_type, data);
|
||||
data->ptr = func;
|
||||
data->name = name ? strdup(name) : NULL;
|
||||
data->type = type;
|
||||
|
@ -79,8 +98,8 @@ rb_dlcfunc2ptr(VALUE val)
|
|||
struct cfunc_data *data;
|
||||
void * func;
|
||||
|
||||
if( rb_obj_is_kind_of(val, rb_cDLCFunc) ){
|
||||
Data_Get_Struct(val, struct cfunc_data, data);
|
||||
if( rb_typeddata_is_kind_of(val, &dlcfunc_data_type) ){
|
||||
data = DATA_PTR(val);
|
||||
func = data->ptr;
|
||||
}
|
||||
else if( val == Qnil ){
|
||||
|
@ -99,7 +118,7 @@ rb_dlcfunc_s_allocate(VALUE klass)
|
|||
VALUE obj;
|
||||
struct cfunc_data *data;
|
||||
|
||||
obj = Data_Make_Struct(klass, struct cfunc_data, 0, dlcfunc_free, data);
|
||||
obj = TypedData_Make_Struct(klass, struct cfunc_data, &dlcfunc_data_type, data);
|
||||
data->ptr = 0;
|
||||
data->name = 0;
|
||||
data->type = 0;
|
||||
|
@ -111,8 +130,7 @@ rb_dlcfunc_s_allocate(VALUE klass)
|
|||
int
|
||||
rb_dlcfunc_kind_p(VALUE func)
|
||||
{
|
||||
if (TYPE(func) == T_DATA) return 0;
|
||||
return RDATA(func)->dfree == dlcfunc_free;
|
||||
return rb_typeddata_is_kind_of(func, &dlcfunc_data_type);
|
||||
}
|
||||
|
||||
VALUE
|
||||
|
@ -128,7 +146,7 @@ rb_dlcfunc_initialize(int argc, VALUE argv[], VALUE self)
|
|||
saddr = (void*)(NUM2PTR(rb_Integer(addr)));
|
||||
sname = NIL_P(name) ? NULL : StringValuePtr(name);
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, data);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, data);
|
||||
if( data->name ) xfree(data->name);
|
||||
data->ptr = saddr;
|
||||
data->name = sname ? strdup(sname) : 0;
|
||||
|
@ -143,7 +161,7 @@ rb_dlcfunc_name(VALUE self)
|
|||
{
|
||||
struct cfunc_data *cfunc;
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, cfunc);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, cfunc);
|
||||
return cfunc->name ? rb_tainted_str_new2(cfunc->name) : Qnil;
|
||||
}
|
||||
|
||||
|
@ -152,7 +170,7 @@ rb_dlcfunc_ctype(VALUE self)
|
|||
{
|
||||
struct cfunc_data *cfunc;
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, cfunc);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, cfunc);
|
||||
return INT2NUM(cfunc->type);
|
||||
}
|
||||
|
||||
|
@ -161,7 +179,7 @@ rb_dlcfunc_set_ctype(VALUE self, VALUE ctype)
|
|||
{
|
||||
struct cfunc_data *cfunc;
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, cfunc);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, cfunc);
|
||||
cfunc->type = NUM2INT(ctype);
|
||||
return ctype;
|
||||
}
|
||||
|
@ -171,7 +189,7 @@ rb_dlcfunc_calltype(VALUE self)
|
|||
{
|
||||
struct cfunc_data *cfunc;
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, cfunc);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, cfunc);
|
||||
return ID2SYM(cfunc->calltype);
|
||||
}
|
||||
|
||||
|
@ -180,7 +198,7 @@ rb_dlcfunc_set_calltype(VALUE self, VALUE sym)
|
|||
{
|
||||
struct cfunc_data *cfunc;
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, cfunc);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, cfunc);
|
||||
cfunc->calltype = SYM2ID(sym);
|
||||
return sym;
|
||||
}
|
||||
|
@ -191,7 +209,7 @@ rb_dlcfunc_ptr(VALUE self)
|
|||
{
|
||||
struct cfunc_data *cfunc;
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, cfunc);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, cfunc);
|
||||
return PTR2NUM(cfunc->ptr);
|
||||
}
|
||||
|
||||
|
@ -200,7 +218,7 @@ rb_dlcfunc_set_ptr(VALUE self, VALUE addr)
|
|||
{
|
||||
struct cfunc_data *cfunc;
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, cfunc);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, cfunc);
|
||||
cfunc->ptr = NUM2PTR(addr);
|
||||
|
||||
return Qnil;
|
||||
|
@ -214,7 +232,7 @@ rb_dlcfunc_inspect(VALUE self)
|
|||
int str_size;
|
||||
struct cfunc_data *cfunc;
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, cfunc);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, cfunc);
|
||||
|
||||
str_size = (cfunc->name ? strlen(cfunc->name) : 0) + 100;
|
||||
str = ruby_xmalloc(str_size);
|
||||
|
@ -259,7 +277,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
|
|||
memset(stack, 0, sizeof(DLSTACK_TYPE) * DLSTACK_SIZE);
|
||||
Check_Type(ary, T_ARRAY);
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, cfunc);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, cfunc);
|
||||
|
||||
if( cfunc->ptr == 0 ){
|
||||
rb_raise(rb_eDLError, "can't call null-function");
|
||||
|
@ -499,7 +517,7 @@ rb_dlcfunc_to_i(VALUE self)
|
|||
{
|
||||
struct cfunc_data *cfunc;
|
||||
|
||||
Data_Get_Struct(self, struct cfunc_data, cfunc);
|
||||
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, cfunc);
|
||||
return PTR2NUM(cfunc->ptr);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,9 @@ get_freefunc(VALUE func)
|
|||
static ID id_to_ptr;
|
||||
|
||||
static void
|
||||
dlptr_free(struct ptr_data *data)
|
||||
dlptr_free(void *ptr)
|
||||
{
|
||||
struct ptr_data *data = ptr;
|
||||
if (data->ptr) {
|
||||
if (data->free) {
|
||||
(*(data->free))(data->ptr);
|
||||
|
@ -33,17 +34,24 @@ dlptr_free(struct ptr_data *data)
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
dlptr_mark(struct ptr_data *data)
|
||||
static size_t
|
||||
dlptr_memsize(const void *ptr)
|
||||
{
|
||||
const struct ptr_data *data = ptr;
|
||||
return data ? sizeof(*data) + data->size : 0;
|
||||
}
|
||||
|
||||
static const rb_data_type_t dlptr_data_type = {
|
||||
"dl/ptr",
|
||||
0, dlptr_free, dlptr_memsize,
|
||||
};
|
||||
|
||||
void
|
||||
dlptr_init(VALUE val)
|
||||
{
|
||||
struct ptr_data *data;
|
||||
|
||||
Data_Get_Struct(val, struct ptr_data, data);
|
||||
TypedData_Get_Struct(val, struct ptr_data, &dlptr_data_type, data);
|
||||
OBJ_TAINT(val);
|
||||
}
|
||||
|
||||
|
@ -54,8 +62,7 @@ rb_dlptr_new2(VALUE klass, void *ptr, long size, freefunc_t func)
|
|||
VALUE val;
|
||||
|
||||
rb_secure(4);
|
||||
val = Data_Make_Struct(klass, struct ptr_data,
|
||||
0, dlptr_free, data);
|
||||
val = TypedData_Make_Struct(klass, struct ptr_data, &dlptr_data_type, data);
|
||||
data->ptr = ptr;
|
||||
data->free = func;
|
||||
data->size = size;
|
||||
|
@ -88,7 +95,7 @@ rb_dlptr2cptr(VALUE val)
|
|||
void *ptr;
|
||||
|
||||
if (rb_obj_is_kind_of(val, rb_cDLCPtr)) {
|
||||
Data_Get_Struct(val, struct ptr_data, data);
|
||||
TypedData_Get_Struct(val, struct ptr_data, &dlptr_data_type, data);
|
||||
ptr = data->ptr;
|
||||
}
|
||||
else if (val == Qnil) {
|
||||
|
@ -108,7 +115,7 @@ rb_dlptr_s_allocate(VALUE klass)
|
|||
struct ptr_data *data;
|
||||
|
||||
rb_secure(4);
|
||||
obj = Data_Make_Struct(klass, struct ptr_data, dlptr_mark, dlptr_free, data);
|
||||
obj = TypedData_Make_Struct(klass, struct ptr_data, &dlptr_data_type, data);
|
||||
data->ptr = 0;
|
||||
data->size = 0;
|
||||
data->free = 0;
|
||||
|
@ -143,7 +150,7 @@ rb_dlptr_initialize(int argc, VALUE argv[], VALUE self)
|
|||
}
|
||||
|
||||
if (p) {
|
||||
Data_Get_Struct(self, struct ptr_data, data);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||
if (data->ptr && data->free) {
|
||||
/* Free previous memory. Use of inappropriate initialize may cause SEGV. */
|
||||
(*(data->free))(data->ptr);
|
||||
|
@ -186,7 +193,7 @@ rb_dlptr_to_i(VALUE self)
|
|||
{
|
||||
struct ptr_data *data;
|
||||
|
||||
Data_Get_Struct(self, struct ptr_data, data);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||
return PTR2NUM(data->ptr);
|
||||
}
|
||||
|
||||
|
@ -194,7 +201,7 @@ VALUE
|
|||
rb_dlptr_to_value(VALUE self)
|
||||
{
|
||||
struct ptr_data *data;
|
||||
Data_Get_Struct(self, struct ptr_data, data);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||
return (VALUE)(data->ptr);
|
||||
}
|
||||
|
||||
|
@ -203,7 +210,7 @@ rb_dlptr_ptr(VALUE self)
|
|||
{
|
||||
struct ptr_data *data;
|
||||
|
||||
Data_Get_Struct(self, struct ptr_data, data);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||
return rb_dlptr_new(*((void**)(data->ptr)),0,0);
|
||||
}
|
||||
|
||||
|
@ -212,7 +219,7 @@ rb_dlptr_ref(VALUE self)
|
|||
{
|
||||
struct ptr_data *data;
|
||||
|
||||
Data_Get_Struct(self, struct ptr_data, data);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||
return rb_dlptr_new(&(data->ptr),0,0);
|
||||
}
|
||||
|
||||
|
@ -221,7 +228,7 @@ rb_dlptr_null_p(VALUE self)
|
|||
{
|
||||
struct ptr_data *data;
|
||||
|
||||
Data_Get_Struct(self, struct ptr_data, data);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||
return data->ptr ? Qfalse : Qtrue;
|
||||
}
|
||||
|
||||
|
@ -230,7 +237,7 @@ rb_dlptr_free_set(VALUE self, VALUE val)
|
|||
{
|
||||
struct ptr_data *data;
|
||||
|
||||
Data_Get_Struct(self, struct ptr_data, data);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||
data->free = get_freefunc(val);
|
||||
|
||||
return Qnil;
|
||||
|
@ -241,7 +248,7 @@ rb_dlptr_free_get(VALUE self)
|
|||
{
|
||||
struct ptr_data *pdata;
|
||||
|
||||
Data_Get_Struct(self, struct ptr_data, pdata);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, pdata);
|
||||
|
||||
return rb_dlcfunc_new(pdata->free, DLTYPE_VOID, "free<anonymous>", CFUNC_CDECL);
|
||||
}
|
||||
|
@ -253,7 +260,7 @@ rb_dlptr_to_s(int argc, VALUE argv[], VALUE self)
|
|||
VALUE arg1, val;
|
||||
int len;
|
||||
|
||||
Data_Get_Struct(self, struct ptr_data, data);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||
switch (rb_scan_args(argc, argv, "01", &arg1)) {
|
||||
case 0:
|
||||
val = rb_tainted_str_new2((char*)(data->ptr));
|
||||
|
@ -276,7 +283,7 @@ rb_dlptr_to_str(int argc, VALUE argv[], VALUE self)
|
|||
VALUE arg1, val;
|
||||
int len;
|
||||
|
||||
Data_Get_Struct(self, struct ptr_data, data);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||
switch (rb_scan_args(argc, argv, "01", &arg1)) {
|
||||
case 0:
|
||||
val = rb_tainted_str_new((char*)(data->ptr),data->size);
|
||||
|
@ -298,7 +305,7 @@ rb_dlptr_inspect(VALUE self)
|
|||
struct ptr_data *data;
|
||||
char str[1024];
|
||||
|
||||
Data_Get_Struct(self, struct ptr_data, data);
|
||||
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
||||
snprintf(str, 1023, "#<%s:%p ptr=%p size=%ld free=%p>",
|
||||
rb_class2name(CLASS_OF(self)), data, data->ptr, data->size, data->free);
|
||||
return rb_str_new2(str);
|
||||
|
|
|
@ -31,20 +31,32 @@ w32_dlclose(void *ptr)
|
|||
#define dlclose(ptr) w32_dlclose(ptr)
|
||||
#endif
|
||||
|
||||
void
|
||||
dlhandle_free(struct dl_handle *dlhandle)
|
||||
static void
|
||||
dlhandle_free(void *ptr)
|
||||
{
|
||||
struct dl_handle *dlhandle = ptr;
|
||||
if( dlhandle->ptr && dlhandle->open && dlhandle->enable_close ){
|
||||
dlclose(dlhandle->ptr);
|
||||
}
|
||||
}
|
||||
|
||||
static size_t
|
||||
dlhandle_memsize(const void *ptr)
|
||||
{
|
||||
return ptr ? sizeof(struct dl_handle) : 0;
|
||||
}
|
||||
|
||||
static const rb_data_type_t dlhandle_data_type = {
|
||||
"dl/handle",
|
||||
0, dlhandle_free, dlhandle_memsize,
|
||||
};
|
||||
|
||||
VALUE
|
||||
rb_dlhandle_close(VALUE self)
|
||||
{
|
||||
struct dl_handle *dlhandle;
|
||||
|
||||
Data_Get_Struct(self, struct dl_handle, dlhandle);
|
||||
TypedData_Get_Struct(self, struct dl_handle, &dlhandle_data_type, dlhandle);
|
||||
dlhandle->open = 0;
|
||||
return INT2NUM(dlclose(dlhandle->ptr));
|
||||
}
|
||||
|
@ -55,8 +67,7 @@ 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);
|
||||
obj = TypedData_Make_Struct(rb_cDLHandle, struct dl_handle, &dlhandle_data_type, dlhandle);
|
||||
dlhandle->ptr = 0;
|
||||
dlhandle->open = 0;
|
||||
dlhandle->enable_close = 0;
|
||||
|
@ -133,7 +144,7 @@ rb_dlhandle_initialize(int argc, VALUE argv[], VALUE self)
|
|||
rb_raise(rb_eDLError, "%s", err);
|
||||
}
|
||||
#endif
|
||||
Data_Get_Struct(self, struct dl_handle, dlhandle);
|
||||
TypedData_Get_Struct(self, struct dl_handle, &dlhandle_data_type, dlhandle);
|
||||
if( dlhandle->ptr && dlhandle->open && dlhandle->enable_close ){
|
||||
dlclose(dlhandle->ptr);
|
||||
}
|
||||
|
@ -153,7 +164,7 @@ rb_dlhandle_enable_close(VALUE self)
|
|||
{
|
||||
struct dl_handle *dlhandle;
|
||||
|
||||
Data_Get_Struct(self, struct dl_handle, dlhandle);
|
||||
TypedData_Get_Struct(self, struct dl_handle, &dlhandle_data_type, dlhandle);
|
||||
dlhandle->enable_close = 1;
|
||||
return Qnil;
|
||||
}
|
||||
|
@ -163,7 +174,7 @@ rb_dlhandle_disable_close(VALUE self)
|
|||
{
|
||||
struct dl_handle *dlhandle;
|
||||
|
||||
Data_Get_Struct(self, struct dl_handle, dlhandle);
|
||||
TypedData_Get_Struct(self, struct dl_handle, &dlhandle_data_type, dlhandle);
|
||||
dlhandle->enable_close = 0;
|
||||
return Qnil;
|
||||
}
|
||||
|
@ -173,7 +184,7 @@ rb_dlhandle_to_i(VALUE self)
|
|||
{
|
||||
struct dl_handle *dlhandle;
|
||||
|
||||
Data_Get_Struct(self, struct dl_handle, dlhandle);
|
||||
TypedData_Get_Struct(self, struct dl_handle, &dlhandle_data_type, dlhandle);
|
||||
return PTR2NUM(dlhandle);
|
||||
}
|
||||
|
||||
|
@ -189,7 +200,7 @@ rb_dlhandle_sym(VALUE self, VALUE sym)
|
|||
|
||||
name = StringValuePtr(sym);
|
||||
|
||||
Data_Get_Struct(self, struct dl_handle, dlhandle);
|
||||
TypedData_Get_Struct(self, struct dl_handle, &dlhandle_data_type, dlhandle);
|
||||
if( ! dlhandle->open ){
|
||||
rb_raise(rb_eDLError, "closed handle");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue