From 74b4c8139695c52123e8db0761c78bb071a61271 Mon Sep 17 00:00:00 2001 From: ttate Date: Wed, 3 Apr 2002 11:59:01 +0000 Subject: [PATCH] Merge Nakada's patch and define StringValuePtr for ruby-1.6. CV ---------------------------------------------------------------------- git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2330 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++ ext/dl/depend | 21 +++---- ext/dl/dl.c | 64 ++++++++++---------- ext/dl/dl.h | 19 ++++-- ext/dl/extconf.rb | 11 ++-- ext/dl/handle.c | 30 +++++----- ext/dl/ptr.c | 143 ++++++++++++++++++++++---------------------- ext/dl/sym.c | 44 +++++++------- ext/dl/test/test.c | 5 +- ext/dl/test/test.rb | 4 +- 10 files changed, 180 insertions(+), 167 deletions(-) diff --git a/ChangeLog b/ChangeLog index af86402e16..5fb1e54db5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Wed Apr 3 20:42:34 2002 Takaaki Tateishi + + * ext/dl: Merge Nakada's patch. + + * ext/dl/dl.h: define StringValuePtr for ruby-1.6. + Wed Apr 3 01:54:10 2002 Nobuyoshi Nakada * ext/extmk.rb.in (enable_config): follow lib/mkmf.rb. diff --git a/ext/dl/depend b/ext/dl/depend index c66a7f0daf..552c67d0f5 100644 --- a/ext/dl/depend +++ b/ext/dl/depend @@ -1,16 +1,17 @@ CLEANFILES = test/test.o DISTCLEANFILES = call.func callback.func cbtable.func dlconfig.rb dlconfig.h \ test/libtest.so test/*~ *~ mkmf.log +LDSHARED_TEST = $(LDSHARED) $(LDFLAGS) test/test.o -o test/libtest.so $(LOCAL_LIBS) libtest.so: test/libtest.so test/libtest.so: test/test.o test/libtest.def $(RUBY) -rftools -e 'ARGV.each{|d|File.mkpath(File.dirname(d))}' $@ - `$(RUBY) -e 'print ARGV.join(" ").gsub(/dl\\.def/,"test/libtest.def")' $(LDSHARED)` $(LDFLAGS) test/test.o -o test/libtest.so + $(LDSHARED_TEST:dl.def=test/libtest.def) -test/test.o: test/test.c +test/test.o: $(srcdir)/test/test.c @$(RUBY) -rftools -e 'File.mkpath(*ARGV)' test - $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) $(CPPFLAGS) -c $(srcdir)/test/test.c -o $@ test:: dl.so libtest.so .force $(RUBY) -I. -I$(srcdir)/lib $(srcdir)/test/test.rb @@ -22,23 +23,23 @@ test:: dl.so libtest.so .force allclean: distclean @rm -f $(CLEANFILES) $(DISTCLEANFILES) -$(OBJS): dlconfig.h +$(OBJS): ./dlconfig.h sym.o: call.func dl.o: callback.func cbtable.func -call.func: mkcall.rb dlconfig.rb +call.func: $(srcdir)/mkcall.rb ./dlconfig.rb @echo "Generating call.func" - @$(RUBY) $< > $@ + @$(RUBY) $(srcdir)/mkcall.rb > $@ -callback.func: mkcallback.rb dlconfig.rb +callback.func: $(srcdir)/mkcallback.rb ./dlconfig.rb @echo "Generating callback.func" - @$(RUBY) $< > $@ + @$(RUBY) $(srcdir)/mkcallback.rb > $@ -cbtable.func: mkcbtable.rb dlconfig.rb +cbtable.func: $(srcdir)/mkcbtable.rb ./dlconfig.rb @echo "Generating cbtable.func" - @$(RUBY) $< > $@ + @$(RUBY) $(srcdir)/mkcbtable.rb > $@ debug: $(MAKE) CPPFLAGS="$(CPPFLAGS) -DDEBUG" diff --git a/ext/dl/dl.c b/ext/dl/dl.c index b8e7381f83..f9b5a145cb 100644 --- a/ext/dl/dl.c +++ b/ext/dl/dl.c @@ -19,7 +19,7 @@ static ID id_call; static void init_dl_func_table(){ #include "cbtable.func" -}; +} void * dlmalloc(size_t size) @@ -35,7 +35,7 @@ dlmalloc(size_t size) { return xmalloc(size); }); -}; +} void * dlrealloc(void *ptr, size_t size) @@ -44,7 +44,7 @@ dlrealloc(void *ptr, size_t size) printf("dlrealloc(0x%x,%d)\n",ptr,size); }); return xrealloc(ptr, size); -}; +} void dlfree(void *ptr) @@ -53,7 +53,7 @@ dlfree(void *ptr) printf("dlfree(0x%x)\n",ptr); }); xfree(ptr); -}; +} char* dlstrdup(const char *str) @@ -64,7 +64,7 @@ dlstrdup(const char *str) strcpy(newstr,str); return newstr; -}; +} size_t dlsizeof(const char *cstr) @@ -133,7 +133,7 @@ dlsizeof(const char *cstr) }; return size; -}; +} static float * c_farray(VALUE v, long *size) @@ -161,7 +161,7 @@ c_farray(VALUE v, long *size) }; return ary; -}; +} static double * c_darray(VALUE v, long *size) @@ -189,7 +189,7 @@ c_darray(VALUE v, long *size) }; return ary; -}; +} static long * c_larray(VALUE v, long *size) @@ -218,7 +218,7 @@ c_larray(VALUE v, long *size) }; return ary; -}; +} static int * c_iarray(VALUE v, long *size) @@ -247,7 +247,7 @@ c_iarray(VALUE v, long *size) }; return ary; -}; +} static short * c_harray(VALUE v, long *size) @@ -276,7 +276,7 @@ c_harray(VALUE v, long *size) }; return ary; -}; +} static char * c_carray(VALUE v, long *size) @@ -305,7 +305,7 @@ c_carray(VALUE v, long *size) }; return ary; -}; +} static void * c_parray(VALUE v, long *size) @@ -323,7 +323,7 @@ c_parray(VALUE v, long *size) case T_STRING: { char *str, *src; - src = STR2CSTR(e); + src = StringValuePtr(e); str = dlstrdup(src); ary[i] = (void*)str; }; @@ -348,7 +348,7 @@ c_parray(VALUE v, long *size) }; return ary; -}; +} void * rb_ary2cary(char t, VALUE v, long *size) @@ -403,7 +403,7 @@ rb_ary2cary(char t, VALUE v, long *size) default: rb_raise(rb_eDLTypeError, "unsupported type"); }; -}; +} VALUE rb_str_to_ptr(VALUE self) @@ -413,10 +413,10 @@ rb_str_to_ptr(VALUE self) len = RSTRING(self)->len; ptr = (char*)dlmalloc(len + 1); - memcpy(ptr, STR2CSTR(self), len); + memcpy(ptr, RSTRING(self)->ptr, len); ptr[len] = '\0'; return rb_dlptr_new((void*)ptr,len,dlfree); -}; +} VALUE rb_ary_to_ptr(int argc, VALUE argv[], VALUE self) @@ -427,14 +427,14 @@ rb_ary_to_ptr(int argc, VALUE argv[], VALUE self) switch( rb_scan_args(argc, argv, "01", &t) ){ case 1: - ptr = rb_ary2cary(STR2CSTR(t)[0], self, &size); + ptr = rb_ary2cary(StringValuePtr(t)[0], self, &size); break; case 0: ptr = rb_ary2cary(0, self, &size); break; }; return ptr ? rb_dlptr_new(ptr, size, dlfree) : Qnil; -}; +} VALUE rb_io_to_ptr(VALUE self) @@ -452,7 +452,7 @@ VALUE rb_dl_dlopen(int argc, VALUE argv[], VALUE self) { return rb_dlhandle_s_new(argc, argv, rb_cDLHandle); -}; +} VALUE rb_dl_malloc(VALUE self, VALUE size) @@ -464,7 +464,7 @@ rb_dl_malloc(VALUE self, VALUE size) ptr = dlmalloc((size_t)s); memset(ptr,0,(size_t)s); return rb_dlptr_new(ptr, s, dlfree); -}; +} VALUE rb_dl_strdup(VALUE self, VALUE str) @@ -472,14 +472,14 @@ rb_dl_strdup(VALUE self, VALUE str) void *p; str = rb_String(str); - return rb_dlptr_new(strdup(STR2CSTR(str)), RSTRING(str)->len, dlfree); -}; + return rb_dlptr_new(strdup(StringValuePtr(str)), RSTRING(str)->len, dlfree); +} static VALUE rb_dl_sizeof(VALUE self, VALUE str) { - return INT2NUM(dlsizeof(STR2CSTR(str))); -}; + return INT2NUM(dlsizeof(StringValuePtr(str))); +} static VALUE rb_dl_callback_type(VALUE str) @@ -490,7 +490,7 @@ rb_dl_callback_type(VALUE str) long ftype; ftype = 0; - type = STR2CSTR(str); + type = StringValuePtr(str); len = RSTRING(str)->len; if( len - 1 > MAX_CBARG ){ @@ -517,7 +517,7 @@ rb_dl_callback_type(VALUE str) rb_raise(rb_eDLError, "unsupported type `%c'", type[i]); break; }; - }; + } switch( type[0] ){ case '0': @@ -544,7 +544,7 @@ rb_dl_callback_type(VALUE str) }; return INT2NUM(ftype); -}; +} VALUE rb_dl_set_callback(int argc, VALUE argv[], VALUE self) @@ -578,12 +578,12 @@ rb_dl_set_callback(int argc, VALUE argv[], VALUE self) if( func ){ rb_hash_aset(entry, num, proc); snprintf(func_name, 1023, "rb_dl_func%d_%d", NUM2INT(key), NUM2INT(num)); - return rb_dlsym_new(func, func_name, STR2CSTR(types)); + return rb_dlsym_new(func, func_name, StringValuePtr(types)); } else{ return Qnil; }; -}; +} VALUE rb_dl_get_callback(VALUE self, VALUE types, VALUE num) @@ -597,7 +597,7 @@ rb_dl_get_callback(VALUE self, VALUE types, VALUE num) return Qnil; }; return rb_hash_aref(entry, num); -}; +} void Init_dl() @@ -652,4 +652,4 @@ Init_dl() rb_define_method(rb_cString, "to_ptr", rb_str_to_ptr, 0); rb_define_method(rb_cArray, "to_ptr", rb_ary_to_ptr, -1); rb_define_method(rb_cIO, "to_ptr", rb_io_to_ptr, 0); -}; +} diff --git a/ext/dl/dl.h b/ext/dl/dl.h index 27290fc12b..a88e95d66f 100644 --- a/ext/dl/dl.h +++ b/ext/dl/dl.h @@ -8,6 +8,11 @@ #include #include +#define DL_VERSION "1.1.0" +#define DL_MAJOR_VERSION 1 +#define DL_MINOR_VERSION 1 +#define DL_PATCH_VERSION 0 + #if defined(HAVE_DLFCN_H) # include #else @@ -23,6 +28,13 @@ # endif #endif +#if !defined(StringValue) +# define StringValue(v) if(TYPE(v) != T_STRING) v = rb_str_to_str(v) +#endif +#if !defined(StringValuePtr) +# define StringValuePtr(v) RSTRING((TYPE(v) == T_STRING) ? (v) : rb_str_to_str(v))->ptr +#endif + #ifdef DEBUG #define DEBUG_CODE(b) {printf("DEBUG:%d\n",__LINE__);b;} #define DEBUG_CODE2(b1,b2) {printf("DEBUG:%d\n",__LINE__);b1;} @@ -31,11 +43,6 @@ #define DEBUG_CODE2(b1,b2) b2 #endif -#define DL_VERSION "1.1.0" -#define DL_MAJOR_VERSION 1 -#define DL_MINOR_VERSION 1 -#define DL_PATCH_VERSION 0 - #define VOID_DLTYPE 0x00 #define CHAR_DLTYPE 0x01 #define SHORT_DLTYPE 0x02 @@ -203,7 +210,7 @@ typedef struct { char c; double x; } s_double; #define DOUBLE_ALIGN ALIGN_DOUBLE #define DLALIGN(ptr,offset,align) {\ - while( (((unsigned long)(ptr + offset)) % align) != 0 ) offset++;\ + while( (((unsigned long)((char *)ptr + offset)) % align) != 0 ) offset++;\ } typedef void (*freefunc_t)(void *); diff --git a/ext/dl/extconf.rb b/ext/dl/extconf.rb index 372f1b1384..ea4ebfdf9a 100644 --- a/ext/dl/extconf.rb +++ b/ext/dl/extconf.rb @@ -20,8 +20,7 @@ end ($CPPFLAGS || $CFLAGS) << " -I." -case RUBY_PLATFORM # from Win32API -when /cygwin/,/mingw/ +if (Config::CONFIG['CC'] =~ /gcc/) # from Win32API $CFLAGS << " -fno-defer-pop -fno-omit-frame-pointer" end @@ -56,11 +55,11 @@ $with_type_voidp &= DLTYPE[VOIDP][:sym] $with_cbtype_voidp = DLTYPE[VOIDP][:cb] -$with_type_char = with_config("type-char") ? true : false -$with_type_short = with_config("type-short") ? true : false -$with_type_float = with_config("type-float") ? true : false +$with_type_char = enable_config("type-char", $with_type_char) +$with_type_short = enable_config("type-short", $with_type_short) +$with_type_float = enable_config("type-float", $with_type_float) -$with_asm = with_config("asm") ? true : $with_asm +$with_asm = enable_config("asm", $with_asm) args = with_config("args") max_arg = max_cbarg = max_cbent = nil diff --git a/ext/dl/handle.c b/ext/dl/handle.c index d75cb11a8c..6debf5e0c5 100644 --- a/ext/dl/handle.c +++ b/ext/dl/handle.c @@ -13,7 +13,7 @@ dlhandle_free(struct dl_handle *dlhandle) if( dlhandle->ptr && dlhandle->open && dlhandle->enable_close ){ dlclose(dlhandle->ptr); }; -}; +} VALUE rb_dlhandle_close(VALUE self) @@ -23,7 +23,7 @@ rb_dlhandle_close(VALUE self) Data_Get_Struct(self, struct dl_handle, dlhandle); dlhandle->open = 0; return INT2NUM(dlclose(dlhandle->ptr)); -}; +} VALUE rb_dlhandle_s_new(int argc, VALUE argv[], VALUE self) @@ -38,11 +38,11 @@ rb_dlhandle_s_new(int argc, VALUE argv[], VALUE self) switch( rb_scan_args(argc, argv, "11", &lib, &flag) ){ case 1: - clib = STR2CSTR(lib); + clib = StringValuePtr(lib); cflag = RTLD_LAZY | RTLD_GLOBAL; break; case 2: - clib = STR2CSTR(lib); + clib = StringValuePtr(lib); cflag = NUM2INT(flag); break; default: @@ -73,13 +73,13 @@ rb_dlhandle_s_new(int argc, VALUE argv[], VALUE self) }; return val; -}; +} VALUE rb_dlhandle_init(int argc, VALUE argv[], VALUE self) { return Qnil; -}; +} VALUE rb_dlhandle_enable_close(VALUE self) @@ -89,7 +89,7 @@ rb_dlhandle_enable_close(VALUE self) Data_Get_Struct(self, struct dl_handle, dlhandle); dlhandle->enable_close = 1; return Qnil; -}; +} VALUE rb_dlhandle_disable_close(VALUE self) @@ -99,7 +99,7 @@ rb_dlhandle_disable_close(VALUE self) Data_Get_Struct(self, struct dl_handle, dlhandle); dlhandle->enable_close = 0; return Qnil; -}; +} VALUE rb_dlhandle_to_i(VALUE self) @@ -108,7 +108,7 @@ rb_dlhandle_to_i(VALUE self) Data_Get_Struct(self, struct dl_handle, dlhandle); return DLLONG2NUM(dlhandle); -}; +} VALUE rb_dlhandle_to_ptr(VALUE self) @@ -117,7 +117,7 @@ rb_dlhandle_to_ptr(VALUE self) Data_Get_Struct(self, struct dl_handle, dlhandle); return rb_dlptr_new(dlhandle, sizeof(dlhandle), 0); -}; +} VALUE rb_dlhandle_sym(int argc, VALUE argv[], VALUE self) @@ -134,8 +134,7 @@ rb_dlhandle_sym(int argc, VALUE argv[], VALUE self) const char *err; if( rb_scan_args(argc, argv, "11", &sym, &type) == 2 ){ - Check_Type(type, T_STRING); - stype = STR2CSTR(type); + stype = StringValuePtr(type); } else{ stype = NULL; @@ -149,8 +148,7 @@ rb_dlhandle_sym(int argc, VALUE argv[], VALUE self) #endif } else{ - Check_Type(sym, T_STRING); - name = STR2CSTR(sym); + name = StringValuePtr(sym); }; @@ -189,7 +187,7 @@ rb_dlhandle_sym(int argc, VALUE argv[], VALUE self) val = rb_dlsym_new(func, name, stype); return val; -}; +} void Init_dlhandle() @@ -204,4 +202,4 @@ Init_dlhandle() rb_define_method(rb_cDLHandle, "[]", rb_dlhandle_sym, -1); rb_define_method(rb_cDLHandle, "disable_close", rb_dlhandle_disable_close, 0); rb_define_method(rb_cDLHandle, "enable_close", rb_dlhandle_enable_close, 0); -}; +} diff --git a/ext/dl/ptr.c b/ext/dl/ptr.c index 6ac214bc46..7a4d0c8e62 100644 --- a/ext/dl/ptr.c +++ b/ext/dl/ptr.c @@ -26,7 +26,7 @@ static void rb_dlmem_delete(void *ptr) { rb_hash_delete(DLMemoryTable, DLLONG2NUM(ptr)); -}; +} static void rb_dlmem_aset(void *ptr, VALUE obj) @@ -37,7 +37,7 @@ rb_dlmem_aset(void *ptr, VALUE obj) else{ rb_hash_aset(DLMemoryTable, DLLONG2NUM(ptr), DLLONG2NUM(obj)); }; -}; +} static VALUE rb_dlmem_aref(void *ptr) @@ -46,7 +46,7 @@ rb_dlmem_aref(void *ptr) val = rb_hash_aref(DLMemoryTable, DLLONG2NUM(ptr)); return val == Qnil ? Qnil : (VALUE)DLNUM2LONG(val); -}; +} void dlptr_free(struct ptr_data *data) @@ -67,7 +67,7 @@ dlptr_free(struct ptr_data *data) if( data->stype ) dlfree(data->stype); if( data->ssize ) dlfree(data->ssize); if( data->ids ) dlfree(data->ids); -}; +} void dlptr_init(VALUE val) @@ -80,7 +80,7 @@ dlptr_init(VALUE val) data->ptr); }); rb_dlmem_aset(data->ptr, val); -}; +} VALUE rb_dlptr_new(void *ptr, long size, freefunc_t func) @@ -116,13 +116,13 @@ rb_dlptr_new(void *ptr, long size, freefunc_t func) }; return val; -}; +} VALUE rb_dlptr_alloc(long size, freefunc_t func) { return rb_dlptr_new(dlmalloc((size_t)size), size, func); -}; +} void * rb_dlptr2cptr(VALUE val) @@ -142,14 +142,14 @@ rb_dlptr2cptr(VALUE val) }; return ptr; -}; +} static VALUE rb_dlptr_s_new(int argc, VALUE argv[], VALUE klass) { VALUE ptr, sym, obj, size; void *p = NULL; - void (*f)() = NULL; + freefunc_t f = NULL; long s = 0; switch( rb_scan_args(argc, argv, "12", &ptr, &size, &sym) ){ @@ -174,14 +174,14 @@ rb_dlptr_s_new(int argc, VALUE argv[], VALUE klass) rb_obj_call_init(obj, argc, argv); return obj; -}; +} static VALUE rb_dlptr_s_alloc(int argc, VALUE argv[], VALUE klass) { VALUE size, sym, obj; int s; - void (*f)() = NULL; + freefunc_t f = NULL; switch( rb_scan_args(argc, argv, "11", &size, &sym) ){ case 1: @@ -200,13 +200,13 @@ rb_dlptr_s_alloc(int argc, VALUE argv[], VALUE klass) rb_obj_call_init(obj, argc, argv); return obj; -}; +} static VALUE rb_dlptr_init(int argc, VALUE argv[], VALUE self) { return Qnil; -}; +} static VALUE rb_dlptr_cast(int argc, VALUE argv[], VALUE self) @@ -244,7 +244,7 @@ rb_dlptr_cast(int argc, VALUE argv[], VALUE self) RDATA(val)->dfree = 0; return Qnil; -}; +} VALUE rb_dlptr_to_i(VALUE self) @@ -253,7 +253,7 @@ rb_dlptr_to_i(VALUE self) Data_Get_Struct(self, struct ptr_data, data); return DLLONG2NUM(data->ptr); -}; +} VALUE rb_dlptr_ptr(VALUE self) @@ -262,7 +262,7 @@ rb_dlptr_ptr(VALUE self) Data_Get_Struct(self, struct ptr_data, data); return rb_dlptr_new(*((void**)(data->ptr)),0,0); -}; +} VALUE rb_dlptr_ref(VALUE self) @@ -271,7 +271,7 @@ rb_dlptr_ref(VALUE self) Data_Get_Struct(self, struct ptr_data, data); return rb_dlptr_new(&(data->ptr),0,0); -}; +} VALUE rb_dlptr_null_p(VALUE self) @@ -280,7 +280,7 @@ rb_dlptr_null_p(VALUE self) Data_Get_Struct(self, struct ptr_data, data); return data->ptr ? Qfalse : Qtrue; -}; +} VALUE rb_dlptr_free_set(VALUE self, VALUE val) @@ -293,7 +293,7 @@ rb_dlptr_free_set(VALUE self, VALUE val) data->free = DLFREEFUNC(rb_dlsym2csym(val)); return Qnil; -}; +} VALUE rb_dlptr_free_get(VALUE self) @@ -303,7 +303,7 @@ rb_dlptr_free_get(VALUE self) Data_Get_Struct(self, struct ptr_data, pdata); return rb_dlsym_new(pdata->free,"(free)","0P"); -}; +} VALUE rb_dlptr_to_array(int argc, VALUE argv[], VALUE self) @@ -319,11 +319,11 @@ rb_dlptr_to_array(int argc, VALUE argv[], VALUE self) switch( rb_scan_args(argc, argv, "11", &type, &size) ){ case 2: - t = STR2CSTR(type)[0]; + t = StringValuePtr(type)[0]; n = NUM2INT(size); break; case 1: - t = STR2CSTR(type)[0]; + t = StringValuePtr(type)[0]; switch( t ){ case 'C': n = data->size; @@ -416,7 +416,7 @@ rb_dlptr_to_array(int argc, VALUE argv[], VALUE self) }; return ary; -}; +} VALUE @@ -440,7 +440,7 @@ rb_dlptr_to_s(int argc, VALUE argv[], VALUE self) }; return val; -}; +} VALUE rb_dlptr_to_str(int argc, VALUE argv[], VALUE self) @@ -463,7 +463,7 @@ rb_dlptr_to_str(int argc, VALUE argv[], VALUE self) }; return val; -}; +} VALUE rb_dlptr_inspect(VALUE self) @@ -476,7 +476,7 @@ rb_dlptr_inspect(VALUE self) snprintf(str, 1023, "#<%s:0x%x ptr=0x%x size=%ld free=0x%x>", rb_class2name(CLASS_OF(self)), data, data->ptr, data->size, data->free); return rb_str_new2(str); -}; +} VALUE rb_dlptr_eql(VALUE self, VALUE other) @@ -486,7 +486,7 @@ rb_dlptr_eql(VALUE self, VALUE other) ptr2 = rb_dlptr2cptr(other); return ptr1 == ptr2 ? Qtrue : Qfalse; -}; +} VALUE rb_dlptr_cmp(VALUE self, VALUE other) @@ -495,7 +495,7 @@ rb_dlptr_cmp(VALUE self, VALUE other) ptr1 = rb_dlptr2cptr(self); ptr2 = rb_dlptr2cptr(other); return DLLONG2NUM((long)ptr1 - (long)ptr2); -}; +} VALUE rb_dlptr_plus(VALUE self, VALUE other) @@ -506,8 +506,8 @@ rb_dlptr_plus(VALUE self, VALUE other) ptr = rb_dlptr2cptr(self); size = RDLPTR(self)->size; num = DLNUM2LONG(other); - return rb_dlptr_new((void*)(ptr + num), size - num, 0); -}; + return rb_dlptr_new((char *)ptr + num, size - num, 0); +} VALUE rb_dlptr_minus(VALUE self, VALUE other) @@ -518,8 +518,8 @@ rb_dlptr_minus(VALUE self, VALUE other) ptr = rb_dlptr2cptr(self); size = RDLPTR(self)->size; num = DLNUM2LONG(other); - return rb_dlptr_new((void*)(ptr - num), size + num, 0); -}; + return rb_dlptr_new((char *)ptr - num, size + num, 0); +} VALUE rb_dlptr_define_data_type(int argc, VALUE argv[], VALUE self) @@ -553,10 +553,9 @@ rb_dlptr_define_data_type(int argc, VALUE argv[], VALUE self) }; }; - Check_Type(data_type, T_FIXNUM); - Check_Type(type, T_STRING); + t = NUM2INT(data_type); + StringValue(type); Check_Type(rest, T_ARRAY); - t = FIX2INT(data_type); num = RARRAY(rest)->len; for( i=0; iids ) dlfree(data->ids); data->ids = (ID*)dlmalloc(sizeof(ID) * data->ids_num); - ctype = STR2CSTR(type); + ctype = StringValuePtr(type); for( i=0; iids[i] = rb_to_id(vid); @@ -600,10 +599,10 @@ rb_dlptr_define_data_type(int argc, VALUE argv[], VALUE self) }; if( !data->size ) - data->size = dlsizeof(STR2CSTR(type)); + data->size = dlsizeof(RSTRING(type)->ptr); return Qnil; -}; +} VALUE rb_dlptr_define_struct(int argc, VALUE argv[], VALUE self) @@ -618,7 +617,7 @@ rb_dlptr_define_struct(int argc, VALUE argv[], VALUE self) pass_argv[i] = argv[i-1]; }; return rb_dlptr_define_data_type(pass_argc, pass_argv, self); -}; +} VALUE rb_dlptr_define_union(int argc, VALUE argv[], VALUE self) @@ -633,7 +632,7 @@ rb_dlptr_define_union(int argc, VALUE argv[], VALUE self) pass_argv[i] = argv[i-1]; }; return rb_dlptr_define_data_type(pass_argc, pass_argv, self); -}; +} VALUE rb_dlptr_get_data_type(VALUE self) @@ -646,7 +645,7 @@ rb_dlptr_get_data_type(VALUE self) rb_tainted_str_new(data->stype, data->slen)); else return rb_assoc_new(INT2FIX(data->ctype), Qnil); -}; +} static VALUE cary2ary(void *ptr, char t, int len) @@ -662,31 +661,31 @@ cary2ary(void *ptr, char t, int len) switch( t ){ case 'I': case 'i': elem = INT2NUM(*((int*)ptr)); - ptr += sizeof(int); + ptr = (char *)ptr + sizeof(int); break; case 'L': case 'l': elem = DLLONG2NUM(*((long*)ptr)); - ptr += sizeof(long); + ptr = (char *)ptr + sizeof(long); break; case 'P': case 'p': elem = rb_dlptr_new(*((void**)ptr),0, 0); - ptr += sizeof(void*); + ptr = (char *)ptr + sizeof(void*); break; case 'F': case 'f': elem = rb_float_new(*((float*)ptr)); - ptr += sizeof(float); + ptr = (char *)ptr + sizeof(float); break; case 'D': case 'd': elem = rb_float_new(*((float*)ptr)); - ptr += sizeof(double); + ptr = (char *)ptr + sizeof(double); break; case 'C': case 'c': elem = INT2NUM(*((char*)ptr)); - ptr += sizeof(char); + ptr = (char *)ptr + sizeof(char); break; case 'H': case 'h': elem = INT2NUM(*((short*)ptr)); - ptr += sizeof(short); + ptr = (char *)ptr + sizeof(short); default: raise(rb_eDLTypeError, "unsupported type '%c'", t); }; @@ -698,31 +697,31 @@ cary2ary(void *ptr, char t, int len) switch( t ){ case 'I': case 'i': elem = INT2NUM(*((int*)ptr)); - ptr += sizeof(int); + ptr = (char *)ptr + sizeof(int); break; case 'L': case 'l': elem = DLLONG2NUM(*((long*)ptr)); - ptr += sizeof(long); + ptr = (char *)ptr + sizeof(long); break; case 'P': case 'p': elem = rb_dlptr_new(*((void**)ptr), 0, 0); - ptr += sizeof(void*); + ptr = (char *)ptr + sizeof(void*); break; case 'F': case 'f': elem = rb_float_new(*((float*)ptr)); - ptr += sizeof(float); + ptr = (char *)ptr + sizeof(float); break; case 'D': case 'd': elem = rb_float_new(*((float*)ptr)); - ptr += sizeof(double); + ptr = (char *)ptr + sizeof(double); break; case 'C': case 'c': elem = INT2NUM(*((char*)ptr)); - ptr += sizeof(char); + ptr = (char *)ptr + sizeof(char); break; case 'H': case 'h': elem = INT2NUM(*((short*)ptr)); - ptr += sizeof(short); + ptr = (char *)ptr + sizeof(short); default: raise(rb_eDLTypeError, "unsupported type '%c'", t); }; @@ -730,7 +729,7 @@ cary2ary(void *ptr, char t, int len) }; return ary; -}; +} VALUE rb_dlptr_aref(int argc, VALUE argv[], VALUE self) @@ -757,9 +756,9 @@ rb_dlptr_aref(int argc, VALUE argv[], VALUE self) id = rb_to_id(key); Data_Get_Struct(self, struct ptr_data, data); + offset = 0; switch( data->ctype ){ case DLPTR_CTYPE_STRUCT: - offset = 0; for( i=0; i < data->ids_num; i++ ){ if( data->ids[i] == id ){ switch( data->stype[i] ){ @@ -785,7 +784,7 @@ rb_dlptr_aref(int argc, VALUE argv[], VALUE self) default: raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]); }; - return cary2ary(data->ptr + offset, data->stype[i], data->ssize[i]); + return cary2ary((char *)data->ptr + offset, data->stype[i], data->ssize[i]); }; switch( data->stype[i] ){ case 'I': @@ -824,7 +823,7 @@ rb_dlptr_aref(int argc, VALUE argv[], VALUE self) case DLPTR_CTYPE_UNION: for( i=0; i < data->ids_num; i++ ){ if( data->ids[i] == id ){ - return cary2ary(data->ptr + offset, data->stype[i], data->ssize[i]); + return cary2ary((char *)data->ptr + offset, data->stype[i], data->ssize[i]); }; }; break; @@ -834,7 +833,7 @@ rb_dlptr_aref(int argc, VALUE argv[], VALUE self) rb_id2name(id), rb_class2name(CLASS_OF(self))); return Qnil; -}; +} static void * ary2cary(char t, VALUE val, long *size) @@ -848,7 +847,7 @@ ary2cary(char t, VALUE val, long *size) ptr = rb_ary2cary(t, rb_ary_new3(1, val), size); }; return ptr; -}; +} VALUE rb_dlptr_aset(int argc, VALUE argv[], VALUE self) @@ -872,10 +871,10 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self) void *dst, *src; int len; - Check_Type(val, T_STRING); + StringValue(val); Data_Get_Struct(self, struct ptr_data, data); dst = (void*)((long)(data->ptr) + DLNUM2LONG(key)); - src = STR2CSTR(val); + src = RSTRING(val)->ptr; if( num == Qnil ){ len = RSTRING(val)->len; } @@ -922,7 +921,7 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self) raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]); }; memimg = ary2cary(data->stype[i], val, &memsize); - memcpy(data->ptr + offset, memimg, memsize); + memcpy((char *)data->ptr + offset, memimg, memsize); return val; }; switch( data->stype[i] ){ @@ -1000,21 +999,21 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self) rb_id2name(id), rb_class2name(CLASS_OF(self))); return Qnil; -}; +} VALUE rb_dlptr_size(int argc, VALUE argv[], VALUE self) { VALUE size; - switch( rb_scan_args(argc, argv, "01", &size) ){ - case 0: + if( rb_scan_args(argc, argv, "01", &size) == 0){ return DLLONG2NUM(RDLPTR(self)->size); - case 1: + } + else{ RDLPTR(self)->size = DLNUM2LONG(size); return size; }; -}; +} static VALUE dlmem_each_i(VALUE assoc, void *data) @@ -1024,14 +1023,14 @@ dlmem_each_i(VALUE assoc, void *data) val = rb_ary_entry(assoc, 1); rb_yield(rb_assoc_new(key,(VALUE)DLNUM2LONG(val))); return Qnil; -}; +} VALUE rb_dlmem_each(VALUE self) { rb_iterate(rb_each, DLMemoryTable, dlmem_each_i, 0); return Qnil; -}; +} void Init_dlptr() @@ -1072,4 +1071,4 @@ Init_dlptr() DLMemoryTable = rb_hash_new(); rb_define_const(rb_mDLMemorySpace, "MemoryTable", DLMemoryTable); rb_define_module_function(rb_mDLMemorySpace, "each", rb_dlmem_each, 0); -}; +} diff --git a/ext/dl/sym.c b/ext/dl/sym.c index 683092ae7b..f383d1e786 100644 --- a/ext/dl/sym.c +++ b/ext/dl/sym.c @@ -59,7 +59,7 @@ char2type(int ch) return "[]"; /* ?? */ }; return NULL; -}; +} void dlsym_free(struct sym_data *data) @@ -76,7 +76,7 @@ dlsym_free(struct sym_data *data) }); free(data->type); }; -}; +} VALUE rb_dlsym_new(void (*func)(), const char *name, const char *type) @@ -112,13 +112,13 @@ rb_dlsym_new(void (*func)(), const char *name, const char *type) }; return val; -}; +} freefunc_t rb_dlsym2csym(VALUE val) { struct sym_data *data; - void (*func)(); + freefunc_t func; if( rb_obj_is_kind_of(val, rb_cDLSymbol) ){ Data_Get_Struct(val, struct sym_data, data); @@ -132,7 +132,7 @@ rb_dlsym2csym(VALUE val) }; return func; -}; +} VALUE rb_dlsym_s_new(int argc, VALUE argv[], VALUE self) @@ -154,8 +154,8 @@ rb_dlsym_s_new(int argc, VALUE argv[], VALUE self) }; saddr = (void*)(DLNUM2LONG(rb_Integer(addr))); - sname = (name == Qnil) ? NULL : STR2CSTR(name); - stype = (type == Qnil) ? NULL : STR2CSTR(type); + sname = NIL_P(name) ? NULL : StringValuePtr(name); + stype = NIL_P(type) ? NULL : StringValuePtr(type); val = rb_dlsym_new(saddr, sname, stype); @@ -164,26 +164,26 @@ rb_dlsym_s_new(int argc, VALUE argv[], VALUE self) }; return val; -}; +} VALUE rb_dlsym_initialize(int argc, VALUE argv[], VALUE self) { return Qnil; -}; +} VALUE rb_s_dlsym_char2type(VALUE self, VALUE ch) { const char *type; - type = char2type(STR2CSTR(ch)[0]); + type = char2type(StringValuePtr(ch)[0]); if (type == NULL) return Qnil; else return rb_str_new2(type); -}; +} VALUE rb_dlsym_name(VALUE self) @@ -192,7 +192,7 @@ rb_dlsym_name(VALUE self) Data_Get_Struct(self, struct sym_data, sym); return sym->name ? rb_tainted_str_new2(sym->name) : Qnil; -}; +} VALUE rb_dlsym_proto(VALUE self) @@ -201,7 +201,7 @@ rb_dlsym_proto(VALUE self) Data_Get_Struct(self, struct sym_data, sym); return sym->type ? rb_tainted_str_new2(sym->type) : Qnil; -}; +} VALUE rb_dlsym_cproto(VALUE self) @@ -252,7 +252,7 @@ rb_dlsym_cproto(VALUE self) }; return val; -}; +} VALUE rb_dlsym_inspect(VALUE self) @@ -270,12 +270,12 @@ rb_dlsym_inspect(VALUE self) str = dlmalloc(str_size); snprintf(str, str_size - 1, "#", - sym, sym->func, STR2CSTR(proto)); + sym, sym->func, StringValuePtr(proto)); val = rb_tainted_str_new2(str); dlfree(str); return val; -}; +} VALUE @@ -418,7 +418,7 @@ rb_dlsym_call(int argc, VALUE argv[], VALUE self) ANY2S(args[i]) = DLSTR(0); } else{ - ANY2S(args[i]) = DLSTR(STR2CSTR(argv[i])); + ANY2S(args[i]) = DLSTR(StringValuePtr(argv[i])); }; PUSH_P(ftype); break; @@ -427,7 +427,7 @@ rb_dlsym_call(int argc, VALUE argv[], VALUE self) raise(rb_eDLError, "#%d must be a string",i); }; ANY2S(args[i]) = DLSTR(dlmalloc(RSTRING(argv[i])->len + 1)); - memcpy((char*)(ANY2S(args[i])), STR2CSTR(argv[i]), RSTRING(argv[i])->len + 1); + memcpy((char*)(ANY2S(args[i])), StringValuePtr(argv[i]), RSTRING(argv[i])->len + 1); dtypes[i] = 's'; PUSH_P(ftype); break; @@ -732,7 +732,7 @@ rb_dlsym_call(int argc, VALUE argv[], VALUE self) #undef FREE_ARGS return rb_assoc_new(val,dvals); -}; +} VALUE rb_dlsym_to_i(VALUE self) @@ -741,7 +741,7 @@ rb_dlsym_to_i(VALUE self) Data_Get_Struct(self, struct sym_data, sym); return DLLONG2NUM(sym); -}; +} VALUE rb_dlsym_to_ptr(VALUE self) @@ -750,7 +750,7 @@ rb_dlsym_to_ptr(VALUE self) Data_Get_Struct(self, struct sym_data, sym); return rb_dlptr_new(sym->func, sizeof(freefunc_t), 0); -}; +} void Init_dlsym() @@ -768,4 +768,4 @@ Init_dlsym() rb_define_method(rb_cDLSymbol, "to_s", rb_dlsym_cproto, 0); rb_define_method(rb_cDLSymbol, "to_ptr", rb_dlsym_to_ptr, 0); rb_define_method(rb_cDLSymbol, "to_i", rb_dlsym_to_i, 0); -}; +} diff --git a/ext/dl/test/test.c b/ext/dl/test/test.c index c5ead79813..ed42b4d1a0 100644 --- a/ext/dl/test/test.c +++ b/ext/dl/test/test.c @@ -221,7 +221,8 @@ test_data_add(struct test_data *list, const char *name) struct test_data *data; data = (struct test_data *)malloc(sizeof(struct test_data)); - strcpy(data->name, name); + memset(data->name, 0, 1024); + strncpy(data->name, name, 1024); data->next = list->next; list->next = data; }; @@ -236,7 +237,7 @@ test_data_print(struct test_data *list) }; }; -struct data * +struct test_data * test_data_aref(struct test_data *list, int i) { struct test_data *data; diff --git a/ext/dl/test/test.rb b/ext/dl/test/test.rb index de9a9cc533..91ae1f6730 100644 --- a/ext/dl/test/test.rb +++ b/ext/dl/test/test.rb @@ -184,10 +184,12 @@ if( fp ) r,rs = sym_gets[" " * 256, 256, fp] debug r,rs assert("open,gets", :must, rs[0] == line) + ObjectSpace.define_finalizer(fp) {File.unlink("tmp.txt")} + fp = nil else assert("open,gets", :must, line == nil) + File.unlink("tmp.txt") end -File.unlink("tmp.txt") callback1 = h["test_callback1"]