[pypy-svn] r16139 - pypy/dist/pypy/rpython/memory

cfbolz at codespeak.net cfbolz at codespeak.net
Thu Aug 18 16:05:47 CEST 2005


Author: cfbolz
Date: Thu Aug 18 16:05:46 2005
New Revision: 16139

Modified:
   pypy/dist/pypy/rpython/memory/gcwrapper.py
   pypy/dist/pypy/rpython/memory/lltypesimulation.py
Log:
started cleaning up of all the different places that currently contain
knowledge about the memory layout the lltypesimulation uses.


Modified: pypy/dist/pypy/rpython/memory/gcwrapper.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gcwrapper.py	(original)
+++ pypy/dist/pypy/rpython/memory/gcwrapper.py	Thu Aug 18 16:05:46 2005
@@ -27,6 +27,15 @@
         typeid = self.type_to_typeid[TYPE]
         return typeid
 
+    def get_contained_pointers(self, addr, typeid):
+        TYPE = self.types[typeid]
+        ptr = lltypesimulation.simulatorptr(lltype.Ptr(TYPE), addr)
+        ll = AddressLinkedList()
+        offsets = ptr.get_offsets_of_contained_pointers()
+        for offset in offsets:
+            ll.append(addr + offset)
+        return ll
+
     def get_roots(self):
         print "getting roots"
         if self.pseudo_root_pointers != NULL:
@@ -43,50 +52,6 @@
             ll.append(self.pseudo_root_pointers + INT_SIZE * i)
         return ll
 
-    def get_contained_pointers(self, addr, typeid):
-        TYPE = self.types[typeid]
-        if isinstance(TYPE, lltype.Struct):
-            offsets = self.get_contained_pointers_struct(addr, TYPE)
-        elif isinstance(TYPE, lltype.Array):
-            offsets = self.get_contained_pointers_array(addr, TYPE)
-        ll = AddressLinkedList()
-        for offset in offsets:
-            ll.append(addr + offset)
-        print "for the TYPE %s if found the follwing offsets: %s" % (TYPE, offsets)
-        return ll
-
-    def get_contained_pointers_struct(self, addr, TYPE, offset=0):
-        offsets = []
-        substructures = [(TYPE, offset)]
-        while len(substructures):
-            TYPE, offset = substructures.pop()
-            layout = lltypesimulation.get_layout(TYPE)
-            for name in TYPE._names:
-                FIELD = getattr(TYPE, name)
-                if isinstance(FIELD, lltype.Ptr) and FIELD._needsgc():
-                    offsets.append(offset + layout[name])
-                elif isinstance(FIELD, lltype.Struct):
-                    substructures.append((FIELD, layout[name] + offset))
-                elif isinstance(FIELD, lltype.Array):
-                    assert offset == 0 #can only inline into outermost struct
-                    baseaddr = addr + layout[name]
-                    offsets += self.get_contained_pointers_array(
-                        baseaddr, FIELD, layout[name])
-        return offsets
-
-    def get_contained_pointers_array(self, addr, TYPE, offset=0):
-        offsets = []
-        length = addr.signed[0]
-        itemsize = lltypesimulation.get_variable_size(TYPE)
-        if isinstance(TYPE.OF, lltype.Ptr) and TYPE.OF._needsgc():
-            for i in range(length):
-                offsets.append(offset + INT_SIZE + i * itemsize)
-        elif isinstance(TYPE.OF, lltype.GcStruct):
-            for i in range(length):
-                item_offset = INT_SIZE + i * itemsize
-                offsets += self.get_contained_pointers_array(
-                    TYPE.OF, addr + item_offset, offset + item_offset)
-        return offsets
 
 class GcWrapper(object):
     def __init__(self, llinterp, gc):

Modified: pypy/dist/pypy/rpython/memory/lltypesimulation.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/lltypesimulation.py	(original)
+++ pypy/dist/pypy/rpython/memory/lltypesimulation.py	Thu Aug 18 16:05:46 2005
@@ -13,6 +13,12 @@
                     lltype.Bool:            "B",
                     }
 
+INT_SIZE = struct.calcsize("i")
+
+#___________________________________________________________________________
+# Utility functions that know about the memory layout of the lltypes
+# in the simulation
+
 #returns some sort of layout information that is useful for the simulatorptr
 def get_layout(TYPE):
     layout = {}
@@ -84,7 +90,7 @@
         return fixedsize
     else:
         return fixedsize + i * varsize
-    
+
 
 def _expose(T, address):
     """XXX A nice docstring here"""
@@ -104,8 +110,10 @@
         assert 0, "not implemented yet"
 
 
+#_____________________________________________________________________________
 # this class is intended to replace the _ptr class in lltype
 # using the memory simulator
+
 class simulatorptr(object):
     def __init__(self, TYPE, address):
         self.__dict__['_TYPE'] = TYPE
@@ -239,6 +247,38 @@
     def __repr__(self):
         return '<simulatorptr %s to %s>' % (self._TYPE.TO, self._address)
 
+    def get_offsets_of_contained_pointers(self):
+        if isinstance(self._TYPE.TO, lltype.Struct):
+            offsets = self._get_offsets_of_contained_pointers_struct()
+        elif isinstance(self._TYPE.TO, lltype.Array):
+            offsets = self._get_offsets_of_contained_pointers_array()
+        return offsets
+
+    def _get_offsets_of_contained_pointers_struct(self):
+        offsets = []
+        for name in self._TYPE.TO._names:
+            FIELD = getattr(self._TYPE.TO, name)
+            if isinstance(FIELD, lltype.Ptr) and FIELD._needsgc():
+                offsets.append(self._layout[name])
+            elif isinstance(FIELD, (lltype.Struct, lltype.Array)):
+                embedded_ptr = getattr(self, name)
+                suboffsets = embedded_ptr.get_offsets_of_contained_pointers()
+                offsets += [s + self._layout[name] for s in suboffsets]
+        return offsets
+
+    def _get_offsets_of_contained_pointers_array(self):
+        offsets = []
+        if (isinstance(self._TYPE.TO.OF, lltype.Ptr) and
+            self._TYPE.TO.OF._needsgc()):
+            for i in range(len(self)):
+                offsets.append(self._layout[0] + i * self._layout[1])
+        elif isinstance(self._TYPE.TO.OF, lltype.GcStruct):
+            for i in range(len(self)):
+                suboffsets += self[i].get_offsets_of_contained_pointers()
+                offsets += [s + self._layout[0] + i * self._layout[1]
+                               for s in suboffsets]
+        return offsets
+
 
 def cast_pointer(PTRTYPE, ptr):
     if not isinstance(ptr, simulatorptr) or not isinstance(PTRTYPE, lltype.Ptr):



More information about the Pypy-commit mailing list