[pypy-svn] r53674 - in pypy/branch/io-improvements/pypy: rpython/lltypesystem rpython/module translator/goal

docgok at codespeak.net docgok at codespeak.net
Thu Apr 10 21:12:52 CEST 2008


Author: docgok
Date: Thu Apr 10 21:12:50 2008
New Revision: 53674

Modified:
   pypy/branch/io-improvements/pypy/rpython/lltypesystem/rffi.py
   pypy/branch/io-improvements/pypy/rpython/module/ll_os.py
   pypy/branch/io-improvements/pypy/translator/goal/targetsimplewrite.py
Log:
New convenience functions for making buffers non-moving.



Modified: pypy/branch/io-improvements/pypy/rpython/lltypesystem/rffi.py
==============================================================================
--- pypy/branch/io-improvements/pypy/rpython/lltypesystem/rffi.py	(original)
+++ pypy/branch/io-improvements/pypy/rpython/lltypesystem/rffi.py	Thu Apr 10 21:12:50 2008
@@ -2,17 +2,20 @@
 from pypy.annotation import model as annmodel
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.lltypesystem import ll2ctypes
+from pypy.rpython.lltypesystem.llmemory import cast_adr_to_ptr, cast_ptr_to_adr
+from pypy.rpython.lltypesystem.llmemory import itemoffsetof, offsetof
 from pypy.annotation.model import lltype_to_annotation
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.rlib.objectmodel import Symbolic, CDefinedIntSymbolic
-from pypy.rlib import rarithmetic
+from pypy.rlib.objectmodel import keepalive_until_here
+from pypy.rlib import rarithmetic, rgc
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rlib.unroll import unrolling_iterable
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.rpython.tool.rfficache import platform
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 from pypy.translator.backendopt.canraise import RaiseAnalyzer
-from pypy.rpython.annlowlevel import llhelper
+from pypy.rpython.annlowlevel import llhelper, llstr
 import os
 
 class UnhandledRPythonException(Exception):
@@ -467,7 +470,37 @@
         l.append(cp[i])
         i += 1
     return "".join(l)
+    
+# str -> char*
+def get_nonmovingbuffer(data):
+    """
+    Either returns a non-moving copy or performs neccessary pointer arithmetic
+    to return a pointer to the characters of a string if the string is already
+    nonmovable.
+    Must be followed by a free_nonmovingbuffer call.
+    """
+    from pypy.rpython.lltypesystem import rstr
+    if rgc.can_move(data):
+        count = len(data)
+        buf = lltype.malloc(CCHARP.TO, count, flavor='raw')
+        for i in range(count):
+            buf[i] = data[i]
+        return buf
+    else:
+        data_start = cast_ptr_to_adr(llstr(data)) + \
+            offsetof(rstr.STR, 'chars') + itemoffsetof(rstr.STR.chars, 0)
+        return cast(VOIDP, data_start)
 
+# (str, char*) -> None
+def free_nonmovingbuffer(data, buf):
+    """
+    Either free a non-moving buffer or keep the original storage alive.
+    """
+    if rgc.can_move(data):
+        lltype.free(buf, flavor='raw')
+    else:
+        keepalive_until_here(data)
+    
 # char* -> str, with an upper bound on the length in case there is no \x00
 def charp2strn(cp, maxlen):
     l = []

Modified: pypy/branch/io-improvements/pypy/rpython/module/ll_os.py
==============================================================================
--- pypy/branch/io-improvements/pypy/rpython/module/ll_os.py	(original)
+++ pypy/branch/io-improvements/pypy/rpython/module/ll_os.py	Thu Apr 10 21:12:50 2008
@@ -17,8 +17,6 @@
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.tool import rffi_platform as platform
 from pypy.rlib import rposix
-from pypy.rlib import rgc
-from pypy.rlib.objectmodel import keepalive_until_here
 from pypy.tool.udir import udir
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 from pypy.rpython.lltypesystem.rstr import mallocstr
@@ -513,29 +511,17 @@
                                    rffi.SIZE_T)
 
         def os_write_llimpl(fd, data):
-            count = len(data)
-            if rgc.can_move(data):
-                outbuf = lltype.malloc(rffi.CCHARP.TO, count, flavor='raw')
-                try:
-                    for i in range(count):
-                        outbuf[i] = data[i]
-                    written = rffi.cast(lltype.Signed, os_write(
-                        rffi.cast(rffi.INT, fd),
-                        outbuf, rffi.cast(rffi.SIZE_T, count)))
-                    if written < 0:
-                        raise OSError(rposix.get_errno(), "os_write failed")
-                finally:
-                    lltype.free(outbuf, flavor='raw')
-            else:
-                data_start = cast_ptr_to_adr(llstr(data)) + \
-                    offsetof(STR, 'chars') + itemoffsetof(STR.chars, 0)
-                outbuf = rffi.cast(rffi.VOIDP, data_start)
+            buf = lltype.nullptr(rffi.CCHARP.TO)
+            try:
+                count = len(data)
+                buf = rffi.get_nonmovingbuffer(data)
                 written = rffi.cast(lltype.Signed, os_write(
                     rffi.cast(rffi.INT, fd),
-                    outbuf, rffi.cast(rffi.SIZE_T, count)))
+                    buf, rffi.cast(rffi.SIZE_T, count)))
                 if written < 0:
                     raise OSError(rposix.get_errno(), "os_write failed")
-                keepalive_until_here(data)
+            finally:
+                rffi.free_nonmovingbuffer(data, buf)
             return written
 
         def os_write_oofakeimpl(fd, data):

Modified: pypy/branch/io-improvements/pypy/translator/goal/targetsimplewrite.py
==============================================================================
--- pypy/branch/io-improvements/pypy/translator/goal/targetsimplewrite.py	(original)
+++ pypy/branch/io-improvements/pypy/translator/goal/targetsimplewrite.py	Thu Apr 10 21:12:50 2008
@@ -2,7 +2,7 @@
 
 def main(iterations=1):
     dest = os.open('/dev/null', os.O_RDWR, 0777)
-    payload = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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