なかだです。

At Tue, 2 Apr 2002 20:11:41 +0900,
Takaaki Tateishi wrote:
> ext/dlを追加しました.

-fno-defer-pop -fno-omit-frame-pointerは、Windowsだけではなくて
gccなら必要です。それにvoid*の演算はgcc拡張なので他の処理系では
コンパイルが通りません。

testのほうでは、DOSやWindowsではオープンしているファイルは削除
できないため、途中でエラーになります。また、dl.defを使ってるの
はWindows用だと思いますが、mswin32版というかnmakeでは``は使えま
せん。その他VPATHの指定が特殊だったり特殊マクロが暗黙のルールで
ないと使えなかったりと、nmakeで通そうと思うとかなり苦しいです。

# ext/extmk.rb.inから(というかlib/mkmf.rbも含めて)見直したほう
# が楽かも知れないくらい。


Index: ext/dl/depend =================================================================== RCS file: /cvs/ruby/src/ruby/ext/dl/depend,v retrieving revision 1.2 diff -u -2 -p -r1.2 depend --- ext/dl/depend 2002/04/02 12:06:23 1.2 +++ ext/dl/depend 2002/04/03 08:06:24 @@ -2,4 +2,5 @@ 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 @@ -7,9 +8,9 @@ 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 @@ -23,5 +24,5 @@ allclean: distclean @rm -f $(CLEANFILES) $(DISTCLEANFILES) -$(OBJS): dlconfig.h +$(OBJS): ./dlconfig.h sym.o: call.func @@ -29,15 +30,15 @@ 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: Index: ext/dl/dl.c =================================================================== RCS file: /cvs/ruby/src/ruby/ext/dl/dl.c,v retrieving revision 1.1 diff -u -2 -p -r1.1 dl.c --- ext/dl/dl.c 2002/04/02 10:56:13 1.1 +++ ext/dl/dl.c 2002/04/03 04:19:04 @@ -324,5 +324,5 @@ c_parray(VALUE v, long *size) { char *str, *src; - src = STR2CSTR(e); + src = StringValuePtr(e); str = dlstrdup(src); ary[i] = (void*)str; @@ -414,5 +414,5 @@ 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); @@ -428,5 +428,5 @@ rb_ary_to_ptr(int argc, VALUE argv[], VA 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: @@ -473,5 +473,5 @@ rb_dl_strdup(VALUE self, VALUE str) 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); }; @@ -479,5 +479,5 @@ static VALUE rb_dl_sizeof(VALUE self, VALUE str) { - return INT2NUM(dlsizeof(STR2CSTR(str))); + return INT2NUM(dlsizeof(StringValuePtr(str))); }; @@ -491,5 +491,5 @@ rb_dl_callback_type(VALUE str) ftype = 0; - type = STR2CSTR(str); + type = StringValuePtr(str); len = RSTRING(str)->len; @@ -579,5 +579,5 @@ rb_dl_set_callback(int argc, VALUE argv[ 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{ Index: ext/dl/dl.h =================================================================== RCS file: /cvs/ruby/src/ruby/ext/dl/dl.h,v retrieving revision 1.1 diff -u -2 -p -r1.1 dl.h --- ext/dl/dl.h 2002/04/02 10:56:13 1.1 +++ ext/dl/dl.h 2002/04/03 04:19:04 @@ -204,5 +204,5 @@ typedef struct { char c; double x; } s_d #define DLALIGN(ptr,offset,align) {\ - while( (((unsigned long)(ptr + offset)) % align) != 0 ) offset++;\ + while( (((unsigned long)((char *)ptr + offset)) % align) != 0 ) offset++;\ } Index: ext/dl/extconf.rb =================================================================== RCS file: /cvs/ruby/src/ruby/ext/dl/extconf.rb,v retrieving revision 1.1 diff -u -2 -p -r1.1 extconf.rb --- ext/dl/extconf.rb 2002/04/02 10:56:13 1.1 +++ ext/dl/extconf.rb 2002/04/02 17:11:16 @@ -21,6 +21,5 @@ 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 @@ -57,9 +56,9 @@ $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") Index: ext/dl/handle.c =================================================================== RCS file: /cvs/ruby/src/ruby/ext/dl/handle.c,v retrieving revision 1.1 diff -u -2 -p -r1.1 handle.c --- ext/dl/handle.c 2002/04/02 10:56:13 1.1 +++ ext/dl/handle.c 2002/04/03 04:19:04 @@ -39,9 +39,9 @@ rb_dlhandle_s_new(int argc, VALUE argv[] 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; @@ -135,6 +135,5 @@ rb_dlhandle_sym(int argc, VALUE argv[], if( rb_scan_args(argc, argv, "11", &sym, &type) == 2 ){ - Check_Type(type, T_STRING); - stype = STR2CSTR(type); + stype = StringValuePtr(type); } else{ @@ -150,6 +149,5 @@ rb_dlhandle_sym(int argc, VALUE argv[], } else{ - Check_Type(sym, T_STRING); - name = STR2CSTR(sym); + name = StringValuePtr(sym); }; Index: ext/dl/ptr.c =================================================================== RCS file: /cvs/ruby/src/ruby/ext/dl/ptr.c,v retrieving revision 1.1 diff -u -2 -p -r1.1 ptr.c --- ext/dl/ptr.c 2002/04/02 10:56:13 1.1 +++ ext/dl/ptr.c 2002/04/03 04:19:04 @@ -150,5 +150,5 @@ rb_dlptr_s_new(int argc, VALUE argv[], V VALUE ptr, sym, obj, size; void *p = NULL; - void (*f)() = NULL; + freefunc_t f = NULL; long s = 0; @@ -182,5 +182,5 @@ rb_dlptr_s_alloc(int argc, VALUE argv[], VALUE size, sym, obj; int s; - void (*f)() = NULL; + freefunc_t f = NULL; switch( rb_scan_args(argc, argv, "11", &size, &sym) ){ @@ -320,9 +320,9 @@ rb_dlptr_to_array(int argc, VALUE argv[] 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': @@ -507,5 +507,5 @@ rb_dlptr_plus(VALUE self, VALUE other) 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); }; @@ -519,5 +519,5 @@ rb_dlptr_minus(VALUE self, VALUE other) 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); }; @@ -554,8 +554,7 @@ rb_dlptr_define_data_type(int argc, VALU }; - 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; i<num; i++ ){ @@ -576,5 +575,5 @@ rb_dlptr_define_data_type(int argc, VALU data->ids = (ID*)dlmalloc(sizeof(ID) * data->ids_num); - ctype = STR2CSTR(type); + ctype = StringValuePtr(type); for( i=0; i<num; i++ ){ vid = rb_ary_entry(rest,i); @@ -601,5 +600,5 @@ rb_dlptr_define_data_type(int argc, VALU if( !data->size ) - data->size = dlsizeof(STR2CSTR(type)); + data->size = dlsizeof(RSTRING(type)->ptr); return Qnil; @@ -663,29 +662,29 @@ cary2ary(void *ptr, char t, int len) 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); @@ -699,29 +698,29 @@ cary2ary(void *ptr, char t, int len) 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); @@ -758,7 +757,7 @@ rb_dlptr_aref(int argc, VALUE argv[], VA 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 ){ @@ -786,5 +785,5 @@ rb_dlptr_aref(int argc, VALUE argv[], VA 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] ){ @@ -825,5 +824,5 @@ rb_dlptr_aref(int argc, VALUE argv[], VA 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]); }; }; @@ -873,8 +872,8 @@ rb_dlptr_aset(int argc, VALUE argv[], VA 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; @@ -923,5 +922,5 @@ rb_dlptr_aset(int argc, VALUE argv[], VA }; memimg = ary2cary(data->stype[i], val, &memsize); - memcpy(data->ptr + offset, memimg, memsize); + memcpy((char *)data->ptr + offset, memimg, memsize); return val; }; @@ -1008,8 +1007,8 @@ rb_dlptr_size(int argc, VALUE argv[], VA 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; Index: ext/dl/sym.c =================================================================== RCS file: /cvs/ruby/src/ruby/ext/dl/sym.c,v retrieving revision 1.1 diff -u -2 -p -r1.1 sym.c --- ext/dl/sym.c 2002/04/02 10:56:13 1.1 +++ ext/dl/sym.c 2002/04/03 04:19:04 @@ -119,5 +119,5 @@ rb_dlsym2csym(VALUE val) { struct sym_data *data; - void (*func)(); + freefunc_t func; if( rb_obj_is_kind_of(val, rb_cDLSymbol) ){ @@ -155,6 +155,6 @@ rb_dlsym_s_new(int argc, VALUE argv[], V 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); @@ -178,5 +178,5 @@ rb_s_dlsym_char2type(VALUE self, VALUE c const char *type; - type = char2type(STR2CSTR(ch)[0]); + type = char2type(StringValuePtr(ch)[0]); if (type == NULL) @@ -271,5 +271,5 @@ rb_dlsym_inspect(VALUE self) snprintf(str, str_size - 1, "#<DL::Symbol:0x%x func=0x%x '%s'>", - sym, sym->func, STR2CSTR(proto)); + sym, sym->func, StringValuePtr(proto)); val = rb_tainted_str_new2(str); dlfree(str); @@ -419,5 +419,5 @@ rb_dlsym_call(int argc, VALUE argv[], VA } else{ - ANY2S(args[i]) = DLSTR(STR2CSTR(argv[i])); + ANY2S(args[i]) = DLSTR(StringValuePtr(argv[i])); }; PUSH_P(ftype); @@ -428,5 +428,5 @@ rb_dlsym_call(int argc, VALUE argv[], VA }; 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); Index: ext/dl/test/test.c =================================================================== RCS file: /cvs/ruby/src/ruby/ext/dl/test/test.c,v retrieving revision 1.1 diff -u -2 -p -r1.1 test.c --- ext/dl/test/test.c 2002/04/02 10:56:13 1.1 +++ ext/dl/test/test.c 2002/04/03 04:19:27 @@ -222,5 +222,6 @@ test_data_add(struct test_data *list, co 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; @@ -237,5 +238,5 @@ test_data_print(struct test_data *list) }; -struct data * +struct test_data * test_data_aref(struct test_data *list, int i) { Index: ext/dl/test/test.rb =================================================================== RCS file: /cvs/ruby/src/ruby/ext/dl/test/test.rb,v retrieving revision 1.1 diff -u -2 -p -r1.1 test.rb --- ext/dl/test/test.rb 2002/04/02 10:56:13 1.1 +++ ext/dl/test/test.rb 2002/04/03 04:52:06 @@ -185,8 +185,10 @@ if( 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")
-- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦