[pypy-svn] r36055 - in pypy/dist/pypy: rpython/lltypesystem rpython/lltypesystem/test translator/c/test

arigo at codespeak.net arigo at codespeak.net
Sat Dec 30 11:41:14 CET 2006


Author: arigo
Date: Sat Dec 30 11:41:11 2006
New Revision: 36055

Modified:
   pypy/dist/pypy/rpython/lltypesystem/llmemory.py
   pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py
   pypy/dist/pypy/translator/c/test/test_lltyped.py
Log:
An extension of "addr + offset" for the case where "addr" is the address
of a atructure that is actually inlined into a larger array - in which
case we can add an offset to access the sibling structures of the same
larger array.



Modified: pypy/dist/pypy/rpython/lltypesystem/llmemory.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/llmemory.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/llmemory.py	Sat Dec 30 11:41:11 2006
@@ -47,6 +47,14 @@
         return ItemOffset(self.TYPE, -self.repeat)
 
     def ref(self, firstitemref):
+        if isinstance(firstitemref, _obref):
+            parent, index = lltype.parentlink(firstitemref.ob._obj)
+            if parent is None:
+                raise TypeError("address + itemoffset: not the address"
+                                " of an array")
+            A = lltype.typeOf(parent)
+            assert isinstance(A, (lltype.Array, lltype.FixedSizeArray))
+            firstitemref = _arrayitemref(parent._as_ptr(), index)
         assert isinstance(firstitemref, _arrayitemref)
         array = firstitemref.array
         assert lltype.typeOf(array).TO.OF == self.TYPE

Modified: pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py	Sat Dec 30 11:41:11 2006
@@ -93,6 +93,16 @@
     res = interpret(f, [0])
     assert res == 1000
 
+def test_structarray_add():
+    S = lltype.Struct("S", ("x", lltype.Signed))
+    A = lltype.GcArray(S)
+    a = lltype.malloc(A, 5)
+    a[3].x = 42
+    adr_s = cast_ptr_to_adr(a[0])
+    adr_s += sizeof(S) * 3
+    s = cast_adr_to_ptr(adr_s, lltype.Ptr(S))
+    assert s.x == 42
+
 def test_cast_adr_to_ptr():
     from pypy.rpython.memory.test.test_llinterpsim import interpret
     S = lltype.GcStruct("S", ("x", lltype.Signed))

Modified: pypy/dist/pypy/translator/c/test/test_lltyped.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_lltyped.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_lltyped.py	Sat Dec 30 11:41:11 2006
@@ -160,6 +160,23 @@
             res = fn(4)
             assert res == 0 + 10 + 30 + 1000
 
+    def test_structarray_add(self):
+        from pypy.rpython.lltypesystem import llmemory
+        S = Struct("S", ("x", Signed))
+        PS = Ptr(S)
+        size = llmemory.sizeof(S)
+        A = GcArray(S)
+        def llf(n):
+            a = malloc(A, 5)
+            a[3].x = 42
+            adr_s = llmemory.cast_ptr_to_adr(a[0])
+            adr_s += size * n
+            s = llmemory.cast_adr_to_ptr(adr_s, PS)
+            return s.x
+        fn = self.getcompiled(llf, [int])
+        res = fn(3)
+        assert res == 42
+
     def test_direct_fieldptr(self):
         S = GcStruct('S', ('x', Signed), ('y', Signed))
         def llf(n):



More information about the Pypy-commit mailing list