[pypy-svn] r32394 - pypy/branch/kill-keepalives/pypy/rpython/rctypes

arigo at codespeak.net arigo at codespeak.net
Sat Sep 16 20:04:46 CEST 2006


Author: arigo
Date: Sat Sep 16 20:04:44 2006
New Revision: 32394

Modified:
   pypy/branch/kill-keepalives/pypy/rpython/rctypes/rarray.py
   pypy/branch/kill-keepalives/pypy/rpython/rctypes/rchar_p.py
   pypy/branch/kill-keepalives/pypy/rpython/rctypes/rmodel.py
   pypy/branch/kill-keepalives/pypy/rpython/rctypes/rprimitive.py
   pypy/branch/kill-keepalives/pypy/rpython/rctypes/rstringbuf.py
   pypy/branch/kill-keepalives/pypy/rpython/rctypes/rstruct.py
Log:
Wack wack wack.  Puf puf.  Argh.  Some progress, only a few failures left when
the currently-buggy malloc-removal is disabled.


Modified: pypy/branch/kill-keepalives/pypy/rpython/rctypes/rarray.py
==============================================================================
--- pypy/branch/kill-keepalives/pypy/rpython/rctypes/rarray.py	(original)
+++ pypy/branch/kill-keepalives/pypy/rpython/rctypes/rarray.py	Sat Sep 16 20:04:44 2006
@@ -127,12 +127,14 @@
             # example:  a[0].contents = ...  to change the first pointer of
             # an array of pointers.
             v_c_data = r_array.get_item_value(hop.llops, v_array, v_index)
+            v_item = r_array.r_item.return_value(hop.llops, v_c_data, v_owner)
         else:
             # ByRef case
             v_c_data = r_array.get_c_data_of_item(hop.llops, v_array, v_index)
-        v_item = r_array.r_item.return_c_data(hop.llops, v_c_data, v_owner)
+            v_item = r_array.r_item.return_c_data(hop.llops, v_c_data, v_owner)
         # copy the keepalive information too
-        if hasattr(r_array.r_item.lowleveltype.TO, 'keepalive'):
+        if (v_item.concretetype == r_array.r_item.lowleveltype
+            and hasattr(r_array.r_item.lowleveltype.TO, 'keepalive')):
             copykeepalive(hop.llops, r_array.r_item.lowleveltype.TO.keepalive,
                           v_array, (v_index,), v_item, ())
         return v_item

Modified: pypy/branch/kill-keepalives/pypy/rpython/rctypes/rchar_p.py
==============================================================================
--- pypy/branch/kill-keepalives/pypy/rpython/rctypes/rchar_p.py	(original)
+++ pypy/branch/kill-keepalives/pypy/rpython/rctypes/rchar_p.py	Sat Sep 16 20:04:44 2006
@@ -4,8 +4,10 @@
 from pypy.rpython.lltypesystem.rstr import string_repr
 from pypy.rpython.rctypes.rmodel import CTypesValueRepr, C_ZERO
 from pypy.rpython.rctypes.rarray import ArrayRepr
-from pypy.rpython.rctypes.rstringbuf import StringBufRepr
+from pypy.rpython.rctypes.rstringbuf import StringBufRepr, ll_stringbufnew
+from pypy.rpython.rctypes.astringbuf import StringBufferType
 from pypy.annotation.pairtype import pairtype
+from pypy.annotation import model as annmodel
 
 from ctypes import c_char, c_char_p, cast
 
@@ -25,14 +27,19 @@
         return llops.gendirectcall(ll_charp2str, v_value)
 
     def get_content_keepalive_type(self):
-        "An extra keepalive used for the raw copy the RPython string."
-        return lltype.Ptr(CHARSCOPY)  # raw_malloc'ed, automatically raw_free'd
+        """An extra keepalive used for the box that holds the raw copy
+        of the RPython string."""
+        s_buf = annmodel.SomeCTypesObject(StringBufferType, ownsmemory=True)
+        r_buf = self.rtyper.getrepr(s_buf)
+        return r_buf.lowleveltype
 
     def getstring(self, llops, v_box):
         return llops.gendirectcall(ll_getstring, v_box)
 
     def setstring(self, llops, v_box, v_str):
-        llops.gendirectcall(ll_setstring, v_box, v_str)
+        RCBOX_STRBUF = self.lowleveltype.TO.keepalive.TO
+        c_r_buf = inputconst(lltype.Void, RCBOX_STRBUF)
+        llops.gendirectcall(ll_setstring, c_r_buf, v_box, v_str)
 
     def convert_const(self, value):
         if value is not None and not isinstance(value, (str, c_char_p)):
@@ -44,8 +51,9 @@
     def initialize_const(self, p, string):
         if isinstance(string, c_char_p):
             string = string.value
+        RCBOX_STRBUF = self.lowleveltype.TO.keepalive.TO
         llstring = string_repr.convert_const(string)
-        ll_setstring(p, llstring)
+        ll_setstring(RCBOX_STRBUF, p, llstring)
 
     def rtype_getattr(self, hop):
         s_attr = hop.args_s[1]
@@ -133,29 +141,17 @@
     else:
         return lltype.nullptr(string_repr.lowleveltype.TO)
 
-def ll_setstring(box, string):
+def ll_setstring(RCBOX_STRBUF, box, string):
     if not string:
         new_c_data = lltype.nullptr(CCHARP.TO)
-        charscopy = lltype.nullptr(CHARSCOPY)
+        new_buf = lltype.nullptr(RCBOX_STRBUF)
     else:
         # XXX the copying of the string is often not needed, but it is
         # very hard to avoid in the general case of a moving GC that can
         # move 'string' around unpredictably.
-        n = len(string.chars)
-        buffer = llmemory.raw_malloc(NULL_HEADER + SIZEOF_CHAR * (n+1))
-        charscopy = llmemory.cast_adr_to_ptr(buffer, PCHARSCOPY)
-        for i in range(n):
-            charscopy[i] = string.chars[i]
-        charscopy[n] = '\x00'
-        new_c_data = lltype.direct_arrayitems(charscopy)
+        new_buf = ll_stringbufnew(RCBOX_STRBUF, string)
+        new_c_data = lltype.direct_arrayitems(new_buf.c_data)
     # store a 'char *' pointer in the box.c_data
     box.c_data[0] = new_c_data
     # keep the ownership of the buffer in the box.keepalive field
-    prev = box.keepalive
-    box.keepalive = charscopy
-    llmemory.raw_free(llmemory.cast_ptr_to_adr(prev))
-
-CHARSCOPY = lltype.Array(lltype.Char, hints={'nolength': True})
-PCHARSCOPY = lltype.Ptr(CHARSCOPY)
-NULL_HEADER = llmemory.itemoffsetof(CHARSCOPY)
-SIZEOF_CHAR = llmemory.sizeof(lltype.Char)
+    box.keepalive = new_buf

Modified: pypy/branch/kill-keepalives/pypy/rpython/rctypes/rmodel.py
==============================================================================
--- pypy/branch/kill-keepalives/pypy/rpython/rctypes/rmodel.py	(original)
+++ pypy/branch/kill-keepalives/pypy/rpython/rctypes/rmodel.py	Sat Sep 16 20:04:44 2006
@@ -174,6 +174,7 @@
             return result
 
     def get_c_data(self, llops, v_box):
+        assert v_box.concretetype == self.lowleveltype
         inputargs = [v_box, inputconst(lltype.Void, "c_data")]
         return llops.genop('getfield', inputargs,
                            lltype.Ptr(self.c_data_type))
@@ -248,6 +249,7 @@
         Used when the data is returned from an operation or C function call.
         Special-cased in PrimitiveRepr.
         """
+        assert v_c_data.concretetype.TO == self.c_data_type
         return self.allocate_instance_ref(llops, v_c_data, v_c_data_owner)
 
 
@@ -289,6 +291,7 @@
         return lltype.FixedSizeArray(ll_type, 1)
 
     def getvalue_from_c_data(self, llops, v_c_data):
+        assert v_c_data.concretetype.TO == self.c_data_type
         return llops.genop('getarrayitem', [v_c_data, C_ZERO],
                 resulttype=self.ll_type)
 

Modified: pypy/branch/kill-keepalives/pypy/rpython/rctypes/rprimitive.py
==============================================================================
--- pypy/branch/kill-keepalives/pypy/rpython/rctypes/rprimitive.py	(original)
+++ pypy/branch/kill-keepalives/pypy/rpython/rctypes/rprimitive.py	Sat Sep 16 20:04:44 2006
@@ -22,6 +22,7 @@
         """Read out the atomic data from a raw C pointer.
         Used when the data is returned from an operation or C function call.
         """
+        assert v_c_data.concretetype.TO == self.c_data_type
         v_value = self.getvalue_from_c_data(llops, v_c_data)
         return self.return_value(llops, v_value)
 

Modified: pypy/branch/kill-keepalives/pypy/rpython/rctypes/rstringbuf.py
==============================================================================
--- pypy/branch/kill-keepalives/pypy/rpython/rctypes/rstringbuf.py	(original)
+++ pypy/branch/kill-keepalives/pypy/rpython/rctypes/rstringbuf.py	Sat Sep 16 20:04:44 2006
@@ -1,4 +1,4 @@
-from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.annotation.pairtype import pairtype
 from pypy.rpython.rmodel import IntegerRepr, inputconst
 from pypy.rpython.rctypes.rmodel import CTypesRefRepr
@@ -128,4 +128,23 @@
         newstr.chars[i] = p[i]
     return newstr
 
+def ll_stringbufnew(RCBOX_STRBUF, s):
+    n = len(s.chars) + 1
+    box = lltype.malloc(RCBOX_STRBUF)
+    box.header.rtti = lltype.getRuntimeTypeInfo(RCBOX_STRBUF)
+    box.c_data = lltype.nullptr(STRBUFTYPE)
+    # no flavored_malloc_varsize operation so far :-(
+    a = llmemory.raw_malloc(FIRST_CHAR + SIZEOF_CHAR * n)
+    # puf puf
+    (a + LENGTH_POS).signed[0] = n
+    p = llmemory.cast_adr_to_ptr(a, PSTRBUFTYPE)
+    box.c_data = p
+    for i in range(n):
+        p[i] = s.chars[i]
+    return box
+
 STRBUFTYPE = lltype.Array(lltype.Char)
+PSTRBUFTYPE = lltype.Ptr(STRBUFTYPE)
+LENGTH_POS = llmemory.ArrayLengthOffset(STRBUFTYPE)
+FIRST_CHAR = llmemory.itemoffsetof(STRBUFTYPE)
+SIZEOF_CHAR = llmemory.sizeof(lltype.Char)

Modified: pypy/branch/kill-keepalives/pypy/rpython/rctypes/rstruct.py
==============================================================================
--- pypy/branch/kill-keepalives/pypy/rpython/rctypes/rstruct.py	(original)
+++ pypy/branch/kill-keepalives/pypy/rpython/rctypes/rstruct.py	Sat Sep 16 20:04:44 2006
@@ -104,7 +104,8 @@
             v_c_data = self.get_c_data_of_field(hop.llops, v_struct, name)
             v_item = r_field.return_c_data(hop.llops, v_c_data, v_owner)
         # copy the keepalive information too
-        if hasattr(r_field.lowleveltype.TO, 'keepalive'):
+        if (v_item.concretetype == r_field.lowleveltype
+            and hasattr(r_field.lowleveltype.TO, 'keepalive')):
             c_name = inputconst(lltype.Void, cmangle(name))
             copykeepalive(hop.llops, r_field.lowleveltype.TO.keepalive,
                           v_struct, (c_name,), v_item, ())



More information about the Pypy-commit mailing list