Commits

Maciej Fijalkowski  committed 3db72c7

(fijal, arigo) aligned arrays support for ll2ctypes

  • Participants
  • Parent commits 52478d8
  • Branches backend-vector-ops

Comments (0)

Files changed (3)

File pypy/rpython/lltypesystem/ll2ctypes.py

                         ('items',  max_n * ctypes_item)]
         else:
             _fields_ = [('items',  max_n * ctypes_item)]
-
+            if A._hints.get('memory_position_alignment'):
+                # This pack means the same as #pragma pack in MSVC and *not*
+                # in gcc
+                _pack_ = A._hints.get('memory_position_alignment')
+            
         @classmethod
         def _malloc(cls, n=None):
             if not isinstance(n, int):

File pypy/rpython/lltypesystem/lltype.py

         raise TypeError, "%r cannot be inlined in structure" % self
 
     def _install_extras(self, adtmeths={}, hints={}):
+        if not isinstance(self, Array):
+            assert not 'memory_position_alignment' in hints
         self._adtmeths = frozendict(adtmeths)
         self._hints = frozendict(hints)
 
                             % (self.OF._gckind,))
         self.OF._inline_is_varsize(False)
 
+        if 'memory_position_alignment' in kwds.get('_hints', {}):
+            assert kwds['_hints']['nolength'], 'alignment only for raw non-lenght arrays'
+            assert self._gckind == 'raw', 'alignment only for raw non-lenght arrays'
+            assert kwds['_hints']['memory_position_alignment'] in (1, 2, 4, 8, 16)
         self._install_extras(**kwds)
 
     def _inline_is_varsize(self, last):

File pypy/rpython/lltypesystem/test/test_ll2ctypes.py

         a2 = ctypes2lltype(lltype.Ptr(A), lltype2ctypes(a))
         assert a2._obj.getitem(0)._obj._parentstructure() is a2._obj
 
+    def test_aligned_alloc(Self):
+        A = lltype.Array(lltype.Signed,
+                         hints={'memory_position_alignment': 16,
+                                'nolength': True})
+        l = []
+        for i in range(10):
+            a = lltype.malloc(A, 5, flavor='raw')
+            ca = lltype2ctypes(a)
+            assert ctypes.cast(ca, ctypes.c_void_p).value % 16 == 0
+            l.append(a)
+        for i in range(10):
+            lltype.free(l[i], 'raw')
+
 class TestPlatform(object):
     def test_lib_on_libpaths(self):
         from pypy.translator.platform import platform