fixed the alignment problems discussed in [ruby-dev:28667].

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10440 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ttate 2006-06-30 18:05:40 +00:00
parent abbe974267
commit e587cf2730
4 changed files with 50 additions and 34 deletions

View File

@ -223,10 +223,12 @@ rb_dlcfunc_inspect(VALUE self)
} }
#if defined(__GNUC__) #if defined(__GNUC__)
# define DECL_FUNC(f,ret,args,calltype) ret (__attribute__((calltype)) *f)(args) # define DECL_FUNC_CDECL(f,ret,args) FUNC_CDECL(ret (*f)(args))
# define DECL_FUNC_STDCALL(f,ret,args) FUNC_STDCALL(ret (*f)(args))
/* # define DECL_FUNC(f,ret,args,calltype) ret (*f)(args) */ /* # define DECL_FUNC(f,ret,args,calltype) ret (*f)(args) */
#elif defined(_MSC_VER) || defined(__BORLANDC__) #elif defined(_MSC_VER) || defined(__BORLANDC__)
# define DECL_FUNC(f,ret,args,calltype) ret (__##calltype *f)(args) # define DECL_FUNC_CDECL(f,ret,args) ret (__cdecl *f)(args)
# define DECL_FUNC_STDCALL(f,ret,args) ret (__stdcall *f)(args)
#else #else
# error "unsupported compiler." # error "unsupported compiler."
#endif #endif
@ -273,7 +275,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
switch( cfunc->type ){ switch( cfunc->type ){
case DLTYPE_VOID: case DLTYPE_VOID:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,void,DLSTACK_PROTO##n,cdecl) = cfunc->ptr; \ DECL_FUNC_CDECL(f,void,DLSTACK_PROTO##n) = cfunc->ptr; \
f(DLSTACK_ARGS##n(stack)); \ f(DLSTACK_ARGS##n(stack)); \
result = Qnil; \ result = Qnil; \
} }
@ -282,7 +284,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_VOIDP: case DLTYPE_VOIDP:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,void*,DLSTACK_PROTO##n,cdecl) = cfunc->ptr; \ DECL_FUNC_CDECL(f,void*,DLSTACK_PROTO##n) = cfunc->ptr; \
void * ret; \ void * ret; \
ret = f(DLSTACK_ARGS##n(stack)); \ ret = f(DLSTACK_ARGS##n(stack)); \
result = PTR2NUM(ret); \ result = PTR2NUM(ret); \
@ -292,7 +294,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_CHAR: case DLTYPE_CHAR:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,char,DLSTACK_PROTO##n,cdecl) = cfunc->ptr; \ DECL_FUNC_CDECL(f,char,DLSTACK_PROTO##n) = cfunc->ptr; \
char ret; \ char ret; \
ret = f(DLSTACK_ARGS##n(stack)); \ ret = f(DLSTACK_ARGS##n(stack)); \
result = CHR2FIX(ret); \ result = CHR2FIX(ret); \
@ -302,7 +304,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_SHORT: case DLTYPE_SHORT:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,short,DLSTACK_PROTO##n,cdecl) = cfunc->ptr; \ DECL_FUNC_CDECL(f,short,DLSTACK_PROTO##n) = cfunc->ptr; \
short ret; \ short ret; \
ret = f(DLSTACK_ARGS##n(stack)); \ ret = f(DLSTACK_ARGS##n(stack)); \
result = INT2NUM((int)ret); \ result = INT2NUM((int)ret); \
@ -312,7 +314,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_INT: case DLTYPE_INT:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,int,DLSTACK_PROTO##n,cdecl) = cfunc->ptr; \ DECL_FUNC_CDECL(f,int,DLSTACK_PROTO##n) = cfunc->ptr; \
int ret; \ int ret; \
ret = f(DLSTACK_ARGS##n(stack)); \ ret = f(DLSTACK_ARGS##n(stack)); \
result = INT2NUM(ret); \ result = INT2NUM(ret); \
@ -322,7 +324,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_LONG: case DLTYPE_LONG:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,long,DLSTACK_PROTO##n,cdecl) = cfunc->ptr; \ DECL_FUNC_CDECL(f,long,DLSTACK_PROTO##n) = cfunc->ptr; \
long ret; \ long ret; \
ret = f(DLSTACK_ARGS##n(stack)); \ ret = f(DLSTACK_ARGS##n(stack)); \
result = LONG2NUM(ret); \ result = LONG2NUM(ret); \
@ -333,7 +335,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
#if HAVE_LONG_LONG /* used in ruby.h */ #if HAVE_LONG_LONG /* used in ruby.h */
case DLTYPE_LONG_LONG: case DLTYPE_LONG_LONG:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,LONG_LONG,DLSTACK_PROTO,cdecl) = cfunc->ptr; \ DECL_FUNC_CDECL(f,LONG_LONG,DLSTACK_PROTO) = cfunc->ptr; \
LONG_LONG ret; \ LONG_LONG ret; \
ret = f(DLSTACK_ARGS(stack)); \ ret = f(DLSTACK_ARGS(stack)); \
result = LL2NUM(ret); \ result = LL2NUM(ret); \
@ -344,7 +346,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
#endif #endif
case DLTYPE_FLOAT: case DLTYPE_FLOAT:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,float,DLSTACK_PROTO,cdecl) = cfunc->ptr; \ DECL_FUNC_CDECL(f,float,DLSTACK_PROTO) = cfunc->ptr; \
float ret; \ float ret; \
ret = f(DLSTACK_ARGS(stack)); \ ret = f(DLSTACK_ARGS(stack)); \
result = rb_float_new(ret); \ result = rb_float_new(ret); \
@ -354,7 +356,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_DOUBLE: case DLTYPE_DOUBLE:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,double,DLSTACK_PROTO,cdecl) = cfunc->ptr; \ DECL_FUNC_CDECL(f,double,DLSTACK_PROTO) = cfunc->ptr; \
double ret; \ double ret; \
ret = f(DLSTACK_ARGS(stack)); \ ret = f(DLSTACK_ARGS(stack)); \
result = rb_float_new(ret); \ result = rb_float_new(ret); \
@ -371,7 +373,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
switch( cfunc->type ){ switch( cfunc->type ){
case DLTYPE_VOID: case DLTYPE_VOID:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,void,DLSTACK_PROTO##n,stdcall) = cfunc->ptr; \ DECL_FUNC_STDCALL(f,void,DLSTACK_PROTO##n) = cfunc->ptr; \
f(DLSTACK_ARGS##n(stack)); \ f(DLSTACK_ARGS##n(stack)); \
result = Qnil; \ result = Qnil; \
} }
@ -380,7 +382,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_VOIDP: case DLTYPE_VOIDP:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,void*,DLSTACK_PROTO##n,stdcall) = cfunc->ptr; \ DECL_FUNC_STDCALL(f,void*,DLSTACK_PROTO##n) = cfunc->ptr; \
void * ret; \ void * ret; \
ret = f(DLSTACK_ARGS##n(stack)); \ ret = f(DLSTACK_ARGS##n(stack)); \
result = PTR2NUM(ret); \ result = PTR2NUM(ret); \
@ -390,7 +392,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_CHAR: case DLTYPE_CHAR:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,char,DLSTACK_PROTO##n,stdcall) = cfunc->ptr; \ DECL_FUNC_STDCALL(f,char,DLSTACK_PROTO##n) = cfunc->ptr; \
char ret; \ char ret; \
ret = f(DLSTACK_ARGS##n(stack)); \ ret = f(DLSTACK_ARGS##n(stack)); \
result = CHR2FIX(ret); \ result = CHR2FIX(ret); \
@ -400,7 +402,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_SHORT: case DLTYPE_SHORT:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,short,DLSTACK_PROTO##n,stdcall) = cfunc->ptr; \ DECL_FUNC_STDCALL(f,short,DLSTACK_PROTO##n) = cfunc->ptr; \
short ret; \ short ret; \
ret = f(DLSTACK_ARGS##n(stack)); \ ret = f(DLSTACK_ARGS##n(stack)); \
result = INT2NUM((int)ret); \ result = INT2NUM((int)ret); \
@ -410,7 +412,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_INT: case DLTYPE_INT:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,int,DLSTACK_PROTO##n,stdcall) = cfunc->ptr; \ DECL_FUNC_STDCALL(f,int,DLSTACK_PROTO##n) = cfunc->ptr; \
int ret; \ int ret; \
ret = f(DLSTACK_ARGS##n(stack)); \ ret = f(DLSTACK_ARGS##n(stack)); \
result = INT2NUM(ret); \ result = INT2NUM(ret); \
@ -420,7 +422,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_LONG: case DLTYPE_LONG:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,long,DLSTACK_PROTO##n,stdcall) = cfunc->ptr; \ DECL_FUNC_STDCALL(f,long,DLSTACK_PROTO##n) = cfunc->ptr; \
long ret; \ long ret; \
ret = f(DLSTACK_ARGS##n(stack)); \ ret = f(DLSTACK_ARGS##n(stack)); \
result = LONG2NUM(ret); \ result = LONG2NUM(ret); \
@ -431,7 +433,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
#if HAVE_LONG_LONG /* used in ruby.h */ #if HAVE_LONG_LONG /* used in ruby.h */
case DLTYPE_LONG_LONG: case DLTYPE_LONG_LONG:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,LONG_LONG,DLSTACK_PROTO,stdcall) = cfunc->ptr; \ DECL_FUNC_STDCALL(f,LONG_LONG,DLSTACK_PROTO) = cfunc->ptr; \
LONG_LONG ret; \ LONG_LONG ret; \
ret = f(DLSTACK_ARGS(stack)); \ ret = f(DLSTACK_ARGS(stack)); \
result = LL2NUM(ret); \ result = LL2NUM(ret); \
@ -442,7 +444,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
#endif #endif
case DLTYPE_FLOAT: case DLTYPE_FLOAT:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,float,DLSTACK_PROTO,stdcall) = cfunc->ptr; \ DECL_FUNC_STDCALL(f,float,DLSTACK_PROTO) = cfunc->ptr; \
float ret; \ float ret; \
ret = f(DLSTACK_ARGS(stack)); \ ret = f(DLSTACK_ARGS(stack)); \
result = rb_float_new(ret); \ result = rb_float_new(ret); \
@ -452,7 +454,7 @@ rb_dlcfunc_call(VALUE self, VALUE ary)
break; break;
case DLTYPE_DOUBLE: case DLTYPE_DOUBLE:
#define CASE(n) case n: { \ #define CASE(n) case n: { \
DECL_FUNC(f,double,DLSTACK_PROTO,stdcall) = cfunc->ptr; \ DECL_FUNC_STDCALL(f,double,DLSTACK_PROTO) = cfunc->ptr; \
double ret; \ double ret; \
ret = f(DLSTACK_ARGS(stack)); \ ret = f(DLSTACK_ARGS(stack)); \
result = rb_float_new(ret); \ result = rb_float_new(ret); \

View File

@ -54,19 +54,25 @@ module DL
def CStructEntity.size(types) def CStructEntity.size(types)
offset = 0 offset = 0
max_align = 0
types.each_with_index{|t,i| types.each_with_index{|t,i|
orig_offset = offset orig_offset = offset
if( t.is_a?(Array) ) if( t.is_a?(Array) )
offset = PackInfo.align(orig_offset, PackInfo::ALIGN_MAP[TYPE_VOIDP]) align = PackInfo::ALIGN_MAP[t[0]]
offset = PackInfo.align(orig_offset, align)
size = offset - orig_offset size = offset - orig_offset
offset += (PackInfo::SIZE_MAP[t[0]] * t[1]) offset += (PackInfo::SIZE_MAP[t[0]] * t[1])
else else
offset = PackInfo.align(orig_offset, PackInfo::ALIGN_MAP[t]) align = PackInfo::ALIGN_MAP[t]
offset = PackInfo.align(orig_offset, align)
size = offset - orig_offset size = offset - orig_offset
offset += PackInfo::SIZE_MAP[t] offset += PackInfo::SIZE_MAP[t]
end end
if (max_align < align)
max_align = align
end
} }
offset = PackInfo.align(offset, PackInfo::ALIGN_MAP[TYPE_VOIDP]) offset = PackInfo.align(offset, max_align)
offset offset
end end
@ -83,13 +89,15 @@ module DL
@ctypes = types @ctypes = types
@offset = [] @offset = []
offset = 0 offset = 0
max_align = 0
types.each_with_index{|t,i| types.each_with_index{|t,i|
orig_offset = offset orig_offset = offset
if( t.is_a?(Array) ) if( t.is_a?(Array) )
offset = align(orig_offset, ALIGN_MAP[TYPE_VOIDP]) align = ALIGN_MAP[t[0]]
else else
offset = align(orig_offset, ALIGN_MAP[t]) align = ALIGN_MAP[t]
end end
offset = PackInfo.align(orig_offset, align)
size = offset - orig_offset size = offset - orig_offset
@offset[i] = offset @offset[i] = offset
if( t.is_a?(Array) ) if( t.is_a?(Array) )
@ -97,8 +105,11 @@ module DL
else else
offset += SIZE_MAP[t] offset += SIZE_MAP[t]
end end
if (max_align < align)
max_align = align
end
} }
offset = align(offset, ALIGN_MAP[TYPE_VOIDP]) offset = PackInfo.align(offset, max_align)
@size = offset @size = offset
end end

View File

@ -111,8 +111,8 @@ for calltype in CALLTYPES
for n in 0..(MAX_CALLBACK-1) for n in 0..(MAX_CALLBACK-1)
$out << (<<-EOS) $out << (<<-EOS)
PRE_DECL_#{calltype.upcase} static #{DLTYPE[ty][:type]} MIDST_DECL_#{calltype.upcase} FUNC_#{calltype.upcase}(static #{DLTYPE[ty][:type]}
#{func_name(ty,argc,n,calltype)}(#{(0...argc).collect{|i| "DLSTACK_TYPE stack" + i.to_s}.join(", ")}) POST_DECL_#{calltype.upcase} #{func_name(ty,argc,n,calltype)}(#{(0...argc).collect{|i| "DLSTACK_TYPE stack" + i.to_s}.join(", ")}))
{ {
VALUE ret, cb#{argc > 0 ? ", args[#{argc}]" : ""}; VALUE ret, cb#{argc > 0 ? ", args[#{argc}]" : ""};
#{ #{

View File

@ -28,8 +28,9 @@ module DL
"int tz_dsttime", "int tz_dsttime",
] ]
MyStruct = struct [ MyStruct = struct [
"int num[10]", "short num[5]",
"unsigned char buff[8]", "char c",
"unsigned char buff[7]",
] ]
CallCallback = bind("void call_callback(void*, void*)"){|ptr1, ptr2| CallCallback = bind("void call_callback(void*, void*)"){|ptr1, ptr2|
@ -93,10 +94,12 @@ module DL
def test_struct() def test_struct()
s = LIBC::MyStruct.malloc() s = LIBC::MyStruct.malloc()
s.num = [0,1,2,3,4,5,6,7,8,9] s.num = [0,1,2,3,4]
s.buff = "0123456\377" s.c = ?a
assert_equal([0,1,2,3,4,5,6,7,8,9], s.num) s.buff = "012345\377"
assert_equal([?0,?1,?2,?3,?4,?5,?6,?\377], s.buff) assert_equal([0,1,2,3,4], s.num)
assert_equal(?a, s.c)
assert_equal([?0,?1,?2,?3,?4,?5,?\377], s.buff)
end end
def test_gettimeofday() def test_gettimeofday()