[pypy-commit] pypy ffistruct: copy the logic to cope with field alignment from _rawffi, the failing test now passes

antocuni noreply at buildbot.pypy.org
Wed Nov 9 13:51:06 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: ffistruct
Changeset: r48996:fd6938fabf7b
Date: 2011-11-08 13:00 +0100
http://bitbucket.org/pypy/pypy/changeset/fd6938fabf7b/

Log:	copy the logic to cope with field alignment from _rawffi, the
	failing test now passes

diff --git a/pypy/module/_ffi/interp_ffitype.py b/pypy/module/_ffi/interp_ffitype.py
--- a/pypy/module/_ffi/interp_ffitype.py
+++ b/pypy/module/_ffi/interp_ffitype.py
@@ -28,6 +28,9 @@
     def sizeof(self):
         return intmask(self.ffitype.c_size)
 
+    def get_alignment(self):
+        return intmask(self.ffitype.c_alignment)
+
     def repr(self, space):
         return space.wrap(self.__repr__())
 
diff --git a/pypy/module/_ffi/interp_struct.py b/pypy/module/_ffi/interp_struct.py
--- a/pypy/module/_ffi/interp_struct.py
+++ b/pypy/module/_ffi/interp_struct.py
@@ -63,27 +63,35 @@
 @unwrap_spec(name=str)
 def descr_new_structdescr(space, w_type, name, w_fields):
     fields_w = space.fixedview(w_fields)
-    # note that the fields_w returned by compute_size_and_alignemnt has a
+    # note that the fields_w returned by compute_size_and_alignement has a
     # different annotation than the original: list(W_Root) vs list(W_Field)
-    size, alignment, fields_w = compute_size_and_alignemnt(space, fields_w)
+    size, alignment, fields_w = compute_size_and_alignement(space, fields_w)
     field_types = [] # clibffi's types
     for w_field in fields_w:
         field_types.append(w_field.w_ffitype.ffitype)
     ffistruct = clibffi.make_struct_ffitype_e(size, alignment, field_types)
     return W__StructDescr(space, name, fields_w, ffistruct)
 
-def compute_size_and_alignemnt(space, fields_w):
+def round_up(size, alignment):
+    return (size + alignment - 1) & -alignment
+
+def compute_size_and_alignement(space, fields_w):
     size = 0
-    alignment = 0 # XXX
+    alignment = 1
     fields_w2 = []
     for w_field in fields_w:
         w_field = space.interp_w(W_Field, w_field)
-        w_field.offset = size # XXX: alignment!
-        size += w_field.w_ffitype.sizeof()
+        fieldsize = w_field.w_ffitype.sizeof()
+        fieldalignment = w_field.w_ffitype.get_alignment()
+        alignment = max(alignment, fieldalignment)
+        size = round_up(size, fieldalignment)
+        w_field.offset = size
+        size += fieldsize
         fields_w2.append(w_field)
     return size, alignment, fields_w2
 
 
+
 W__StructDescr.typedef = TypeDef(
     '_StructDescr',
     __new__ = interp2app(descr_new_structdescr),
diff --git a/pypy/module/_ffi/test/test_struct.py b/pypy/module/_ffi/test/test_struct.py
--- a/pypy/module/_ffi/test/test_struct.py
+++ b/pypy/module/_ffi/test/test_struct.py
@@ -1,4 +1,5 @@
 from pypy.module._ffi.test.test_funcptr import BaseAppTestFFI
+    
 
 class AppTestStruct(BaseAppTestFFI):
 


More information about the pypy-commit mailing list