[pypy-svn] r53812 - in pypy/branch/io-improvements/pypy/rpython: lltypesystem module

docgok at codespeak.net docgok at codespeak.net
Wed Apr 16 01:35:42 CEST 2008


Author: docgok
Date: Wed Apr 16 01:35:41 2008
New Revision: 53812

Modified:
   pypy/branch/io-improvements/pypy/rpython/lltypesystem/rffi.py
   pypy/branch/io-improvements/pypy/rpython/module/ll_os.py
Log:
Attempted refactorization of non-moving APIs.



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	Wed Apr 16 01:35:41 2008
@@ -3,7 +3,7 @@
 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.rpython.lltypesystem.llmemory import itemoffsetof, offsetof, raw_memcopy
 from pypy.annotation.model import lltype_to_annotation
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.rlib.objectmodel import Symbolic, CDefinedIntSymbolic
@@ -15,7 +15,7 @@
 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, llstr
+from pypy.rpython.annlowlevel import llhelper, llstr, hlstr
 import os
 
 class UnhandledRPythonException(Exception):
@@ -500,7 +500,56 @@
         lltype.free(buf, flavor='raw')
     else:
         keepalive_until_here(data)
-    
+
+def alloc_buffer_for_hlstr(count):
+    from pypy.rpython.lltypesystem.rstr import STR
+    buf = rgc.malloc_nonmovable(STR, count)
+    if buf:
+        offset = offsetof(STR, 'chars') + itemoffsetof(STR.chars, 0)
+        realbuf = cast_ptr_to_adr(buf) + offset
+        c_buf = cast(VOIDP, realbuf)
+        return c_buf, buf, True
+    else:
+        raw_buf = lltype.malloc(CCHARP.TO, count, flavor='raw')
+        return raw_buf, lltype.nullptr(STR), False
+
+def hlstr_from_buffer(buf, is_collected, allocated_size, needed_size):
+    from pypy.rpython.lltypesystem.rstr import STR, mallocstr
+    offset = offsetof(STR, 'chars') + itemoffsetof(STR.chars, 0)
+    if is_collected:
+        if allocated_size != needed_size:
+            new_buf = lltype.nullptr(STR)
+            try:
+                new_buf = mallocstr(needed_size)
+                dest = cast_ptr_to_adr(new_buf) + offset
+                source = cast_ptr_to_adr(buf) + \
+                         itemoffsetof(lltype.typeOf(buf).TO, 0)
+                ## FIXME: This is bad, because dest could potentially move
+                ## if there are threads involved.
+                raw_memcopy(source, dest, sizeof(lltype.Char) * needed_size)
+                return hlstr(new_buf)
+            finally:
+                keepalive_until_here(new_buf)
+        return hlstr(buf)
+    else:
+        new_buf = lltype.nullptr(STR)
+        try:
+            new_buf = mallocstr(needed_size)
+            source = cast_ptr_to_adr(buf) + \
+                     itemoffsetof(lltype.typeOf(buf).TO, 0)
+            dest = cast_ptr_to_adr(new_buf) + offset
+            ## FIXME: see above
+            raw_memcopy(source, dest, sizeof(lltype.Char) * needed_size)
+            return hlstr(new_buf)
+        finally:
+            keepalive_until_here(new_buf)
+        
+def keep_buffer_for_hlstr_alive_until_here(buf, is_collected):
+    if is_collected:
+        keepalive_until_here(buf)
+    else:
+        lltype.free(buf, flavor='raw')
+
 # 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	Wed Apr 16 01:35:41 2008
@@ -20,8 +20,8 @@
 from pypy.tool.udir import udir
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 from pypy.rpython.lltypesystem.rstr import mallocstr
-from pypy.rpython.annlowlevel import hlstr, llstr
-from pypy.rpython.lltypesystem.llmemory import raw_memcopy, sizeof,\
+from pypy.rpython.annlowlevel import llstr
+from pypy.rpython.lltypesystem.llmemory import sizeof,\
      itemoffsetof, cast_ptr_to_adr, cast_adr_to_ptr, offsetof
 from pypy.rpython.lltypesystem.rstr import STR
 from pypy.rpython.annlowlevel import llstr
@@ -487,38 +487,17 @@
         def os_read_llimpl(fd, count):
             if count < 0:
                 raise OSError(errno.EINVAL, None)
-            inbuf = rgc.malloc_nonmovable(STR, count)
-            if inbuf:
-                try:
-                    realbuf = cast_ptr_to_adr(inbuf) + offset
-                    c_buf = rffi.cast(rffi.VOIDP, realbuf)
-                    got = rffi.cast(lltype.Signed, os_read(fd, c_buf, count))
-                    if got < 0:
-                        raise OSError(rposix.get_errno(), "os_read failed")
-                    if got != count:
-                        # we need to realloc
-                        s = mallocstr(got)
-                        dest = cast_ptr_to_adr(s) + offset
-                        raw_memcopy(realbuf, dest, sizeof(lltype.Char) * got)
-                        return hlstr(s)
-                    return hlstr(inbuf)
-                finally:
-                    keepalive_until_here(inbuf)
-            else:
-                inbuf = lltype.malloc(rffi.CCHARP.TO, count, flavor='raw')
-                try:
-                    got = rffi.cast(lltype.Signed, os_read(fd, inbuf, count))
-                    if got < 0:
-                        raise OSError(rposix.get_errno(), "os_read failed")
-                    s = mallocstr(got)
-                    source = cast_ptr_to_adr(inbuf) + \
-                             itemoffsetof(lltype.typeOf(inbuf).TO, 0)
-                    dest = cast_ptr_to_adr(s) + offset
-                    raw_memcopy(source, dest, sizeof(lltype.Char) * got)
-                finally:
-                    lltype.free(inbuf, flavor='raw')
-                return hlstr(s)
-
+            buf = lltype.nullptr(STR)
+            is_collected = False
+            try:
+                c_buf, buf, is_collected = rffi.alloc_buffer_for_hlstr(count)
+                got = rffi.cast(lltype.Signed, os_read(fd, c_buf, count))
+                if got < 0:
+                    raise OSError(rposix.get_errno(), "os_read failed")
+                return rffi.hlstr_from_buffer(buf, is_collected, count, got)
+            finally:
+                rffi.keep_buffer_for_hlstr_alive_until_here(buf, is_collected)
+            
         def os_read_oofakeimpl(fd, count):
             return OOSupport.to_rstr(os.read(fd, count))
 



More information about the Pypy-commit mailing list