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

docgok at codespeak.net docgok at codespeak.net
Wed Apr 16 02:48:26 CEST 2008


Author: docgok
Date: Wed Apr 16 02:48:24 2008
New Revision: 53815

Modified:
   pypy/branch/io-improvements/pypy/rpython/lltypesystem/rffi.py
   pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_rffi.py
   pypy/branch/io-improvements/pypy/rpython/module/ll_os.py
Log:
Still doesn't work, but now I don't know why.



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 02:48:24 2008
@@ -16,6 +16,7 @@
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 from pypy.translator.backendopt.canraise import RaiseAnalyzer
 from pypy.rpython.annlowlevel import llhelper, llstr, hlstr
+from pypy.rpython.lltypesystem.rstr import STR, mallocstr
 import os
 
 class UnhandledRPythonException(Exception):
@@ -501,53 +502,49 @@
     else:
         keepalive_until_here(data)
 
+
+str_chars_offset = offsetof(STR, 'chars') + itemoffsetof(STR.chars, 0)
+
 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
+    gc_buf = rgc.malloc_nonmovable(STR, count)
+    if gc_buf:
+        realbuf = cast_ptr_to_adr(gc_buf) + str_chars_offset
+        raw_buf = cast(CCHARP.TO, realbuf)
+        return raw_buf, gc_buf
     else:
         raw_buf = lltype.malloc(CCHARP.TO, count, flavor='raw')
         return raw_buf, lltype.nullptr(STR)
 
-def hlstr_from_buffer(buf, allocated_size, needed_size):
-    from pypy.rpython.lltypesystem.rstr import STR, mallocstr
-    offset = offsetof(STR, 'chars') + itemoffsetof(STR.chars, 0)
-    if buf:
+def hlstr_from_buffer(raw_buf, gc_buf, allocated_size, needed_size):
+    if gc_buf:
         if allocated_size != needed_size:
             new_buf = lltype.nullptr(STR)
             try:
                 new_buf = mallocstr(needed_size)
-                dest = cast_ptr_to_adr(new_buf) + offset
-                realbuf = cast_ptr_to_adr(buf) + offset
+                dest = cast_ptr_to_adr(new_buf) + str_chars_offset
                 ## FIXME: This is bad, because dest could potentially move
                 ## if there are threads involved.
-                raw_memcopy(realbuf, dest, sizeof(lltype.Char) * needed_size)
+                raw_memcopy(raw_buf, dest, sizeof(lltype.Char) * needed_size)
                 return hlstr(new_buf)
             finally:
                 keepalive_until_here(new_buf)
-        return hlstr(buf)
+        return hlstr(gc_buf)
     else:
         new_buf = lltype.nullptr(STR)
         try:
             new_buf = mallocstr(needed_size)
-            source = cast_ptr_to_adr(buf) + \
-                     itemoffsetof(CCHARP.TO, 0)
-            dest = cast_ptr_to_adr(new_buf) + offset
+            dest = cast_ptr_to_adr(new_buf) + str_chars_offset
             ## FIXME: see above
-            raw_memcopy(source, dest, sizeof(lltype.Char) * needed_size)
+            raw_memcopy(raw_buf, 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):
-    if buf:
-        keepalive_until_here(buf)
+def keep_buffer_for_hlstr_alive_until_here(raw_buf, gc_buf):
+    if gc_buf:
+        keepalive_until_here(gc_buf)
     else:
-        lltype.free(buf, flavor='raw')
+        lltype.free(raw_buf, flavor='raw')
 
 # char* -> str, with an upper bound on the length in case there is no \x00
 def charp2strn(cp, maxlen):

Modified: pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_rffi.py
==============================================================================
--- pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_rffi.py	(original)
+++ pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_rffi.py	Wed Apr 16 02:48:24 2008
@@ -6,6 +6,7 @@
 from pypy.translator.c.test.test_genc import compile as compile_c
 from pypy.translator.llvm.test.runtest import compile_function as compile_llvm
 from pypy.rpython.lltypesystem.lltype import Signed, Ptr, Char, malloc
+from pypy.rpython.lltypesystem.rstr import STR
 from pypy.rpython.lltypesystem import lltype
 from pypy.tool.udir import udir
 from pypy.rpython.test.test_llinterp import interpret, MallocMismatch
@@ -430,6 +431,22 @@
         unregister_keepalive(pos, TP)
         assert res == 8
 
+    def test_nonmoving_hlstr(self):
+        d = 'non-moving data stuff'
+        def f():
+            gc_buf = lltype.nullptr(STR)
+            raw_buf = lltype.nullptr(CCHARP.TO)
+            try:
+                raw_buf, gc_buf = alloc_buffer_for_hlstr(len(d))
+                for i in range(len(d)):
+                    raw_buf[i] = d[i]
+                return hlstr_from_buffer(raw_buf, gc_buf, len(d), len(d)-1)
+            finally:
+                keep_buffer_for_hlstr_alive_until_here(raw_buf, gc_buf)
+        fn = self.compile(f, [], gcpolicy='ref')
+        assert fn() == d[:-1]
+    
+
     def test_nonmovingbuffer(self):
         d = 'some cool data that should not move'
         def f():

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 02:48:24 2008
@@ -487,15 +487,17 @@
         def os_read_llimpl(fd, count):
             if count < 0:
                 raise OSError(errno.EINVAL, None)
-            buf = lltype.nullptr(STR)
+            raw_buf = lltype.nullptr(rffi.CCHARP.TO)    
+            gc_buf = lltype.nullptr(STR)
             try:
-                c_buf, buf = rffi.alloc_buffer_for_hlstr(count)
-                got = rffi.cast(lltype.Signed, os_read(fd, c_buf, count))
+                raw_buf, gc_buf = rffi.alloc_buffer_for_hlstr(count)
+                void_buf = rffi.cast(rffi.VOIDP, raw_buf)
+                got = rffi.cast(lltype.Signed, os_read(fd, void_buf, count))
                 if got < 0:
                     raise OSError(rposix.get_errno(), "os_read failed")
-                return rffi.hlstr_from_buffer(buf, count, got)
+                return rffi.hlstr_from_buffer(raw_buf, gc_buf, count, got)
             finally:
-                rffi.keep_buffer_for_hlstr_alive_until_here(buf)
+                rffi.keep_buffer_for_hlstr_alive_until_here(raw_buf, gc_buf)
             
         def os_read_oofakeimpl(fd, count):
             return OOSupport.to_rstr(os.read(fd, count))



More information about the Pypy-commit mailing list