[pypy-commit] pypy gc-minimark-pinning: use with more consistently and improve gctransform

fijal noreply at buildbot.pypy.org
Mon Apr 23 12:32:10 CEST 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: gc-minimark-pinning
Changeset: r54664:b99acab54074
Date: 2012-04-23 12:18 +0200
http://bitbucket.org/pypy/pypy/changeset/b99acab54074/

Log:	use with more consistently and improve gctransform

diff --git a/pypy/module/_multibytecodec/c_codecs.py b/pypy/module/_multibytecodec/c_codecs.py
--- a/pypy/module/_multibytecodec/c_codecs.py
+++ b/pypy/module/_multibytecodec/c_codecs.py
@@ -128,8 +128,7 @@
 def decodeex(decodebuf, stringdata, errors="strict", errorcb=None, namecb=None,
              ignore_error=0):
     inleft = len(stringdata)
-    inbuf = rffi.get_nonmovingbuffer(stringdata)
-    try:
+    with rffi.scoped_nonmovingbuffer(stringdata) as inbuf:
         if pypy_cjk_dec_init(decodebuf, inbuf, inleft) < 0:
             raise MemoryError
         while True:
@@ -141,9 +140,6 @@
         src = pypy_cjk_dec_outbuf(decodebuf)
         length = pypy_cjk_dec_outlen(decodebuf)
         return rffi.wcharpsize2unicode(src, length)
-    #
-    finally:
-        rffi.free_nonmovingbuffer(stringdata, inbuf)
 
 def multibytecodec_decerror(decodebuf, e, errors,
                             errorcb, namecb, stringdata):
@@ -172,11 +168,8 @@
         assert errorcb
         replace, end = errorcb(errors, namecb, reason,
                                stringdata, start, end)
-    inbuf = rffi.get_nonmoving_unicodebuffer(replace)
-    try:
+    with rffi.scoped_nonmoving_unicodebuffer(replace) as inbuf:
         r = pypy_cjk_dec_replace_on_error(decodebuf, inbuf, len(replace), end)
-    finally:
-        rffi.free_nonmoving_unicodebuffer(replace, inbuf)
     if r == MBERR_NOMEMORY:
         raise MemoryError
 
@@ -223,8 +216,7 @@
 def encodeex(encodebuf, unicodedata, errors="strict", errorcb=None,
              namecb=None, ignore_error=0):
     inleft = len(unicodedata)
-    inbuf = rffi.get_nonmoving_unicodebuffer(unicodedata)
-    try:
+    with rffi.scoped_nonmoving_unicodebuffer(unicodedata) as inbuf:
         if pypy_cjk_enc_init(encodebuf, inbuf, inleft) < 0:
             raise MemoryError
         if ignore_error == 0:
@@ -246,9 +238,6 @@
         src = pypy_cjk_enc_outbuf(encodebuf)
         length = pypy_cjk_enc_outlen(encodebuf)
         return rffi.charpsize2str(src, length)
-    #
-    finally:
-        rffi.free_nonmoving_unicodebuffer(unicodedata, inbuf)
 
 def multibytecodec_encerror(encodebuf, e, errors,
                             errorcb, namecb, unicodedata):
@@ -281,10 +270,7 @@
         assert errorcb
         replace, end = errorcb(errors, namecb, reason,
                                unicodedata, start, end)
-    inbuf = rffi.get_nonmovingbuffer(replace)
-    try:
+    with rffi.scoped_nonmovingbuffer(replace) as inbuf:
         r = pypy_cjk_enc_replace_on_error(encodebuf, inbuf, len(replace), end)
-    finally:
-        rffi.free_nonmovingbuffer(replace, inbuf)
     if r == MBERR_NOMEMORY:
         raise MemoryError
diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -431,11 +431,8 @@
 
     count = space.len_w(w_str)
     data = space.str_w(w_str)
-    buf = rffi.get_nonmovingbuffer(data)
-    try:
+    with rffi.scoped_nonmovingbuffer(data) as buf:
         fwrite(buf, 1, count, fp)
-    finally:
-        rffi.free_nonmovingbuffer(data, buf)
     return 0
 
 
diff --git a/pypy/rlib/rsocket.py b/pypy/rlib/rsocket.py
--- a/pypy/rlib/rsocket.py
+++ b/pypy/rlib/rsocket.py
@@ -998,19 +998,15 @@
         """Send a data string to the socket.  For the optional flags
         argument, see the Unix manual.  Return the number of bytes
         sent; this may be less than len(data) if the network is busy."""
-        dataptr = rffi.get_nonmovingbuffer(data)
-        try:
+        with rffi.scoped_nonmovingbuffer(data) as dataptr:
             return self.send_raw(dataptr, len(data), flags)
-        finally:
-            rffi.free_nonmovingbuffer(data, dataptr)
 
     def sendall(self, data, flags=0, signal_checker=None):
         """Send a data string to the socket.  For the optional flags
         argument, see the Unix manual.  This calls send() repeatedly
         until all data is sent.  If an error occurs, it's impossible
         to tell how much data has been sent."""
-        dataptr = rffi.get_nonmovingbuffer(data)
-        try:
+        with rffi.scoped_nonmovingbuffer(data) as dataptr:
             remaining = len(data)
             p = dataptr
             while remaining > 0:
@@ -1023,8 +1019,6 @@
                         raise
                 if signal_checker:
                     signal_checker.check()
-        finally:
-            rffi.free_nonmovingbuffer(data, dataptr)
 
     def sendto(self, data, flags, address):
         """Like send(data, flags) but allows specifying the destination
@@ -1408,8 +1402,7 @@
             raise RSocketError("unknown address family")
         if len(packed) != srcsize:
             raise ValueError("packed IP wrong length for inet_ntop")
-        srcbuf = rffi.get_nonmovingbuffer(packed)
-        try:
+        with rffi.scoped_nonmovingbuffer(packed) as srcbuf:
             dstbuf = mallocbuf(dstsize)
             try:
                 res = _c.inet_ntop(family, srcbuf, dstbuf, dstsize)
@@ -1418,8 +1411,6 @@
                 return rffi.charp2str(res)
             finally:
                 lltype.free(dstbuf, flavor='raw')
-        finally:
-            rffi.free_nonmovingbuffer(packed, srcbuf)
 
 def setdefaulttimeout(timeout):
     if timeout < 0.0:
diff --git a/pypy/rlib/runicode.py b/pypy/rlib/runicode.py
--- a/pypy/rlib/runicode.py
+++ b/pypy/rlib/runicode.py
@@ -1499,8 +1499,7 @@
     def unicode_encode_mbcs(p, size, errors, errorhandler=None):
         if size == 0:
             return ''
-        dataptr = rffi.get_nonmoving_unicodebuffer(p)
-        try:
+        with rffi.scoped_nonmoving_unicodebuffer(p) as dataptr:
             # first get the size of the result
             mbcssize = WideCharToMultiByte(CP_ACP, 0,
                                            dataptr, size, None, 0,
@@ -1519,5 +1518,3 @@
                 return rffi.str_from_buffer(raw_buf, gc_buf, mbcssize, mbcssize)
             finally:
                 rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
-        finally:
-            rffi.free_nonmoving_unicodebuffer(p, dataptr)
diff --git a/pypy/rlib/rzlib.py b/pypy/rlib/rzlib.py
--- a/pypy/rlib/rzlib.py
+++ b/pypy/rlib/rzlib.py
@@ -169,11 +169,8 @@
     Compute the CRC32 checksum of the string, possibly with the given
     start value, and return it as a unsigned 32 bit integer.
     """
-    bytes = rffi.get_nonmovingbuffer(string)
-    try:
+    with rffi.scoped_nonmovingbuffer(string) as bytes:
         checksum = _crc32(start, rffi.cast(Bytefp, bytes), len(string))
-    finally:
-        rffi.free_nonmovingbuffer(string, bytes)
     return checksum
 
 
@@ -184,11 +181,8 @@
     Compute the Adler-32 checksum of the string, possibly with the given
     start value, and return it as a unsigned 32 bit integer.
     """
-    bytes = rffi.get_nonmovingbuffer(string)
-    try:
+    with rffi.scoped_nonmovingbuffer(string) as bytes:
         checksum = _adler32(start, rffi.cast(Bytefp, bytes), len(string))
-    finally:
-        rffi.free_nonmovingbuffer(string, bytes)
     return checksum
 
 # ____________________________________________________________
diff --git a/pypy/rpython/lltypesystem/test/test_rffi.py b/pypy/rpython/lltypesystem/test/test_rffi.py
--- a/pypy/rpython/lltypesystem/test/test_rffi.py
+++ b/pypy/rpython/lltypesystem/test/test_rffi.py
@@ -521,7 +521,7 @@
                         counter += 1
                 return counter
             finally:
-                free_nonmovingbuffer(d, buf, is_pinned_is_raw)
+                free_nonmovingbuffer(d, buf, is_pinned, is_raw)
         assert f() == len(d)
         fn = self.compile(f, [], gcpolicy='ref')
         assert fn() == len(d)
diff --git a/pypy/rpython/memory/gc/base.py b/pypy/rpython/memory/gc/base.py
--- a/pypy/rpython/memory/gc/base.py
+++ b/pypy/rpython/memory/gc/base.py
@@ -180,7 +180,7 @@
         return False
 
     def pin(self, addr):
-        pass
+        return False
 
     def unpin(self, addr):
         pass
diff --git a/pypy/rpython/memory/gc/minimark.py b/pypy/rpython/memory/gc/minimark.py
--- a/pypy/rpython/memory/gc/minimark.py
+++ b/pypy/rpython/memory/gc/minimark.py
@@ -804,10 +804,15 @@
                 not self.header(obj).tid & GCFLAG_PINNED)
 
     def pin(self, obj):
+        if not self.is_in_nursery(obj):
+            return False # note that this should never happen since
+        # objects out of nursery should be guarded by can_move = False
+        # better check though.
         if self.pinned_objects_in_nursery >= self.max_number_of_pinned_objects:
-            return
+            return False
         self.pinned_objects_in_nursery += 1
         self.header(obj).tid |= GCFLAG_PINNED
+        return True
 
     def unpin(self, obj):
         if self.header(obj).tid & GCFLAG_PINNED:
diff --git a/pypy/rpython/memory/gctransform/framework.py b/pypy/rpython/memory/gctransform/framework.py
--- a/pypy/rpython/memory/gctransform/framework.py
+++ b/pypy/rpython/memory/gctransform/framework.py
@@ -439,7 +439,8 @@
                                             annmodel.SomeInteger(nonneg=True)],
                                            annmodel.s_None)
         self.pin_ptr = getfn(GCClass.pin,
-                             [s_gc, annmodel.SomeAddress()], annmodel.s_None)
+                             [s_gc, annmodel.SomeAddress()],
+                             annmodel.SomeBool())
         self.unpin_ptr = getfn(GCClass.unpin,
                                [s_gc, annmodel.SomeAddress()], annmodel.s_None)
 
@@ -743,7 +744,8 @@
 
     def gct_gc_pin(self, hop):
         op = hop.spaceop
-        hop.genop("direct_call", [self.pin_ptr, self.c_const_gc, op.args[0]])
+        hop.genop("direct_call", [self.pin_ptr, self.c_const_gc, op.args[0]],
+                  resultvar=op.result)
 
     def gct_gc_unpin(self, hop):
         op = hop.spaceop
diff --git a/pypy/rpython/memory/gctransform/transform.py b/pypy/rpython/memory/gctransform/transform.py
--- a/pypy/rpython/memory/gctransform/transform.py
+++ b/pypy/rpython/memory/gctransform/transform.py
@@ -378,7 +378,10 @@
         pass
 
     def gct_gc_pin(self, hop):
-        pass
+        op = hop.spaceop
+        hop.genop("same_as",
+                  [rmodel.inputconst(lltype.Bool, False)],
+                  resultvar=op.result)
 
     def gct_gc_unpin(self, hop):
         pass


More information about the pypy-commit mailing list