From 7bdb1f5de82cd56e3ec9d171600e34a9d12b9637 Mon Sep 17 00:00:00 2001 From: ttate Date: Thu, 25 May 2006 16:37:58 +0000 Subject: * fixed the problems described in [ruby-dev:28665]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10191 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/dl/ptr.c | 102 ++++++++++++++++++++++++++-------------------------- ext/dl/test/test.rb | 13 ++++++- 2 files changed, 64 insertions(+), 51 deletions(-) (limited to 'ext/dl') diff --git a/ext/dl/ptr.c b/ext/dl/ptr.c index 32f78c4de5..59de4d8c98 100644 --- a/ext/dl/ptr.c +++ b/ext/dl/ptr.c @@ -753,32 +753,32 @@ rb_dlptr_aref(int argc, VALUE argv[], VALUE self) switch (data->ctype) { case DLPTR_CTYPE_STRUCT: for (i=0; i < data->ids_num; i++) { + switch (data->stype[i]) { + case 'I': + DLALIGN(data->ptr,offset,INT_ALIGN); + break; + case 'L': + DLALIGN(data->ptr,offset,LONG_ALIGN); + break; + case 'P': + case 'S': + DLALIGN(data->ptr,offset,VOIDP_ALIGN); + break; + case 'F': + DLALIGN(data->ptr,offset,FLOAT_ALIGN); + break; + case 'D': + DLALIGN(data->ptr,offset,DOUBLE_ALIGN); + break; + case 'C': + break; + case 'H': + DLALIGN(data->ptr,offset,SHORT_ALIGN); + break; + default: + rb_raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]); + } if (data->ids[i] == id) { - switch (data->stype[i]) { - case 'I': - DLALIGN(data->ptr,offset,INT_ALIGN); - break; - case 'L': - DLALIGN(data->ptr,offset,LONG_ALIGN); - break; - case 'P': - case 'S': - DLALIGN(data->ptr,offset,VOIDP_ALIGN); - break; - case 'F': - DLALIGN(data->ptr,offset,FLOAT_ALIGN); - break; - case 'D': - DLALIGN(data->ptr,offset,DOUBLE_ALIGN); - break; - case 'C': - break; - case 'H': - DLALIGN(data->ptr,offset,SHORT_ALIGN); - break; - default: - rb_raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]); - } return cary2ary((char *)data->ptr + offset, data->stype[i], data->ssize[i]); } switch (data->stype[i]) { @@ -883,34 +883,35 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self) case DLPTR_CTYPE_STRUCT: offset = 0; for (i=0; i < data->ids_num; i++) { + switch (data->stype[i]) { + case 'I': + DLALIGN(data->ptr,offset,INT_ALIGN); + break; + case 'L': + DLALIGN(data->ptr,offset,LONG_ALIGN); + break; + case 'P': + case 'S': + DLALIGN(data->ptr,offset,VOIDP_ALIGN); + break; + case 'D': + DLALIGN(data->ptr,offset,DOUBLE_ALIGN); + break; + case 'F': + DLALIGN(data->ptr,offset,FLOAT_ALIGN); + break; + case 'C': + break; + case 'H': + DLALIGN(data->ptr,offset,SHORT_ALIGN); + break; + default: + rb_raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]); + } if (data->ids[i] == id) { - switch (data->stype[i]) { - case 'I': - DLALIGN(data->ptr,offset,INT_ALIGN); - break; - case 'L': - DLALIGN(data->ptr,offset,LONG_ALIGN); - break; - case 'P': - case 'S': - DLALIGN(data->ptr,offset,VOIDP_ALIGN); - break; - case 'D': - DLALIGN(data->ptr,offset,DOUBLE_ALIGN); - break; - case 'F': - DLALIGN(data->ptr,offset,FLOAT_ALIGN); - break; - case 'C': - break; - case 'H': - DLALIGN(data->ptr,offset,SHORT_ALIGN); - break; - default: - rb_raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]); - } memimg = ary2cary(data->stype[i], val, &memsize); memcpy((char *)data->ptr + offset, memimg, memsize); + dlfree(memimg); return val; } switch (data->stype[i]) { @@ -981,6 +982,7 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self) } memimg = ary2cary(data->stype[i], val, NULL); memcpy(data->ptr, memimg, memsize); + dlfree(memimg); } } return val; diff --git a/ext/dl/test/test.rb b/ext/dl/test/test.rb index 52be04699f..bf8dfc18e3 100644 --- a/ext/dl/test/test.rb +++ b/ext/dl/test/test.rb @@ -273,7 +273,7 @@ assert("data_aref", :must, GC.start -ptr = DL::malloc(1024) +ptr = DL::malloc(32) ptr.struct!("CHIL", "c", "h", "i", "l") ptr["c"] = 1 ptr["h"] = 2 @@ -291,5 +291,16 @@ ptr["n"] = 10 ptr["ptr"] = nil assert("struct!", :must, ptr["n"] == 10 && ptr["ptr"] == nil) +ptr = DL::malloc(16) +ptr.struct!("CICI", "c1", "i1", "c2", "i2") +ptr["c1"] = 0xf1 +ptr["c2"] = 0xf2 +c1 = [ptr["c1"]].pack("c").unpack("C")[0] +c2 = [ptr["c2"]].pack("c").unpack("C")[0] +assert("struct!", :must, + c1 == 0xf1 && + c2 == 0xf2) + + GC.start printf("fail/total = #{$FAIL}/#{$TOTAL}\n") -- cgit v1.2.3