Author: Maciej Fijalkowski <[email protected]>
Branch: gc-minimark-pinning
Changeset: r54371:7deff2c04f6f
Date: 2012-04-15 14:44 +0200
http://bitbucket.org/pypy/pypy/changeset/7deff2c04f6f/

Log:    implement malloc_and_pin operation

diff --git a/pypy/jit/tl/jittest.py b/pypy/jit/tl/jittest.py
--- a/pypy/jit/tl/jittest.py
+++ b/pypy/jit/tl/jittest.py
@@ -5,7 +5,6 @@
 """
 
 from pypy.conftest import option
-from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.llinterp import LLInterpreter
 from pypy.rpython.annlowlevel import llstr
 from pypy.jit.metainterp import warmspot
@@ -19,10 +18,6 @@
     graph = driver.translator.graphs[0]
     interp = LLInterpreter(driver.translator.rtyper, malloc_check=False)
 
-    def returns_null(T, *args, **kwds):
-        return lltype.nullptr(T)
-    interp.heap.malloc_nonmovable = returns_null     # XXX
-
     get_policy = driver.extra['jitpolicy']
     jitpolicy = get_policy(driver)
 
diff --git a/pypy/jit/tl/pypyjit_child.py b/pypy/jit/tl/pypyjit_child.py
--- a/pypy/jit/tl/pypyjit_child.py
+++ b/pypy/jit/tl/pypyjit_child.py
@@ -1,5 +1,4 @@
 from pypy.conftest import option
-from pypy.rpython.lltypesystem import lltype
 from pypy.jit.metainterp import warmspot
 from pypy.module.pypyjit.policy import PyPyJitPolicy
 
@@ -10,10 +9,6 @@
     graph = loc['graph']
     interp.malloc_check = False
 
-    def returns_null(T, *args, **kwds):
-        return lltype.nullptr(T)
-    interp.heap.malloc_nonmovable = returns_null     # XXX
-
     from pypy.jit.backend.llgraph.runner import LLtypeCPU
     #LLtypeCPU.supports_floats = False    # for now
     apply_jit(interp, graph, LLtypeCPU)
diff --git a/pypy/rlib/rgc.py b/pypy/rlib/rgc.py
--- a/pypy/rlib/rgc.py
+++ b/pypy/rlib/rgc.py
@@ -116,19 +116,18 @@
         return annmodel.SomePtr(lltype.Ptr(ARRAY_TYPEID_MAP))
 
     def specialize_call(self, hop):
-        from pypy.rpython.memory.gc.base import ARRAY_TYPEID_MAP
         hop.exception_is_here()
         return hop.genop('gc_heap_stats', [], resulttype=hop.r_result)
 
-def malloc_nonmovable(TP, n=None, zero=False):
-    """ Allocate a non-moving buffer or return nullptr.
-    When running directly, will pretend that gc is always
-    moving (might be configurable in a future)
+def malloc_and_pin(TP, n=None, zero=False):
+    """ Allocate a buffer and immediately pin it. If the GC does not support
+    pinning or cannot fulfill the request for some other reason, null ptr
+    is returned. Make sure you unpin this thing when you're done
     """
-    return lltype.nullptr(TP)
+    return lltype.malloc(TP, n, zero=zero)
 
-class MallocNonMovingEntry(ExtRegistryEntry):
-    _about_ = malloc_nonmovable
+class MallocAndPinEntry(ExtRegistryEntry):
+    _about_ = malloc_and_pin
 
     def compute_result_annotation(self, s_TP, s_n=None, s_zero=None):
         # basically return the same as malloc
@@ -136,10 +135,9 @@
         return malloc(s_TP, s_n, s_zero=s_zero)
 
     def specialize_call(self, hop, i_zero=None):
-        # XXX assume flavor and zero to be None by now
         assert hop.args_s[0].is_constant()
         vlist = [hop.inputarg(lltype.Void, arg=0)]
-        opname = 'malloc_nonmovable'
+        opname = 'malloc_and_pin'
         flags = {'flavor': 'gc'}
         if i_zero is not None:
             flags['zero'] = hop.args_s[i_zero].const
@@ -150,7 +148,7 @@
 
         if nb_args == 2:
             vlist.append(hop.inputarg(lltype.Signed, arg=1))
-            opname += '_varsize'
+            opname = 'malloc_varsize_and_pin'
 
         hop.exception_cannot_occur()
         return hop.genop(opname, vlist, resulttype = hop.r_result.lowleveltype)
diff --git a/pypy/rpython/llinterp.py b/pypy/rpython/llinterp.py
--- a/pypy/rpython/llinterp.py
+++ b/pypy/rpython/llinterp.py
@@ -702,17 +702,17 @@
         except MemoryError:
             self.make_llexception()
 
-    def op_malloc_nonmovable(self, TYPE, flags):
+    def op_malloc_and_pin(self, TYPE, flags):
         flavor = flags['flavor']
         assert flavor == 'gc'
         zero = flags.get('zero', False)
-        return self.heap.malloc_nonmovable(TYPE, zero=zero)
+        return self.heap.malloc_and_pin(TYPE, zero=zero)
 
-    def op_malloc_nonmovable_varsize(self, TYPE, flags, size):
+    def op_malloc_varsize_and_pin(self, TYPE, flags, size):
         flavor = flags['flavor']
         assert flavor == 'gc'
         zero = flags.get('zero', False)
-        return self.heap.malloc_nonmovable(TYPE, size, zero=zero)
+        return self.heap.malloc_varsize_and_pin(TYPE, size, zero=zero)
 
     def op_free(self, obj, flags):
         assert flags['flavor'] == 'raw'
diff --git a/pypy/rpython/lltypesystem/llheap.py 
b/pypy/rpython/lltypesystem/llheap.py
--- a/pypy/rpython/lltypesystem/llheap.py
+++ b/pypy/rpython/lltypesystem/llheap.py
@@ -18,7 +18,7 @@
 def weakref_create_getlazy(objgetter):
     return weakref_create(objgetter())
 
-malloc_nonmovable = malloc
+malloc_and_pin = malloc
 
 def shrink_array(p, smallersize):
     return False
diff --git a/pypy/rpython/lltypesystem/lloperation.py 
b/pypy/rpython/lltypesystem/lloperation.py
--- a/pypy/rpython/lltypesystem/lloperation.py
+++ b/pypy/rpython/lltypesystem/lloperation.py
@@ -357,8 +357,8 @@
 
     'malloc':               LLOp(canmallocgc=True),
     'malloc_varsize':       LLOp(canmallocgc=True),
-    'malloc_nonmovable':    LLOp(canmallocgc=True),
-    'malloc_nonmovable_varsize':LLOp(canmallocgc=True),
+    'malloc_and_pin':    LLOp(canmallocgc=True),
+    'malloc_varsize_and_pin':LLOp(canmallocgc=True),
     'shrink_array':         LLOp(canrun=True),
     'zero_gc_pointers_inside': LLOp(),
     'free':                 LLOp(),
diff --git a/pypy/rpython/lltypesystem/rclass.py 
b/pypy/rpython/lltypesystem/rclass.py
--- a/pypy/rpython/lltypesystem/rclass.py
+++ b/pypy/rpython/lltypesystem/rclass.py
@@ -510,13 +510,7 @@
         ctype = inputconst(Void, self.object_type)
         cflags = inputconst(Void, flags)
         vlist = [ctype, cflags]
-        cnonmovable = self.classdef.classdesc.read_attribute(
-            '_alloc_nonmovable_', Constant(False))
-        if cnonmovable.value:
-            opname = 'malloc_nonmovable'
-        else:
-            opname = 'malloc'
-        vptr = llops.genop(opname, vlist,
+        vptr = llops.genop('malloc', vlist,
                            resulttype = Ptr(self.object_type))
         ctypeptr = inputconst(CLASSTYPE, self.rclass.getvtable())
         self.setfield(vptr, '__class__', ctypeptr, llops)
diff --git a/pypy/rpython/lltypesystem/rffi.py 
b/pypy/rpython/lltypesystem/rffi.py
--- a/pypy/rpython/lltypesystem/rffi.py
+++ b/pypy/rpython/lltypesystem/rffi.py
@@ -759,6 +759,9 @@
         safe high-level string. When the garbage collector cooperates, this
         allows for the process to be performed without an extra copy.
         Make sure to call keep_buffer_alive_until_here on the returned values.
+
+        Right now this is a version optimized for minimark which can pin values
+        in the nursery.
         """
         raw_buf = lltype.malloc(TYPEP.TO, count, flavor='raw')
         return raw_buf, lltype.nullptr(STRTYPE)
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
@@ -49,9 +49,6 @@
     def _teardown(self):
         pass
 
-    def can_malloc_nonmovable(self):
-        return not self.moving_gc
-
     def can_optimize_clean_setarrayitems(self):
         return True     # False in case of card marking
 
@@ -169,8 +166,12 @@
         # lots of cast and reverse-cast around...
         return llmemory.cast_ptr_to_adr(ref)
 
-    def malloc_nonmovable(self, typeid, length=0, zero=False):
-        return self.malloc(typeid, length, zero)
+    def malloc_fixedsize_and_pin(self, typeid, size):
+        return lltype.nullptr(llmemory.GCREF.TO)
+
+    def malloc_varsize_and_pin(self, typeid, length, size, itemsize,
+                               offset_to_length):
+        return lltype.nullptr(llmemory.GCREF.TO)
 
     def id(self, ptr):
         return lltype.cast_ptr_to_int(ptr)
diff --git a/pypy/rpython/memory/gc/hybrid.py b/pypy/rpython/memory/gc/hybrid.py
--- a/pypy/rpython/memory/gc/hybrid.py
+++ b/pypy/rpython/memory/gc/hybrid.py
@@ -197,17 +197,6 @@
 
     malloc_varsize_slowpath._dont_inline_ = True
 
-    def malloc_varsize_nonmovable(self, typeid, length):
-        return self.malloc_varsize_slowpath(typeid, length, True)
-
-    def malloc_nonmovable(self, typeid, length, zero):
-        # helper for testing, same as GCBase.malloc
-        if self.is_varsize(typeid):
-            gcref = self.malloc_varsize_slowpath(typeid, length, True)
-        else:
-            raise NotImplementedError("Not supported")
-        return llmemory.cast_ptr_to_adr(gcref)
-
     def can_move(self, addr):
         tid = self.header(addr).tid
         return not (tid & GCFLAG_EXTERNAL)
@@ -554,6 +543,3 @@
                   "gen3: unexpected GCFLAG_UNVISITED")
         ll_assert((tid & GCFLAG_AGE_MASK) == GCFLAG_AGE_MAX,
                   "gen3: wrong age field")
-
-    def can_malloc_nonmovable(self):
-        return True
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
@@ -574,6 +574,17 @@
         #
         return llmemory.cast_adr_to_ptr(obj, llmemory.GCREF)
 
+    def malloc_fixedsize_and_pin(self, typeid, size):
+        r = self.malloc_fixedsize_clear(typeid, size)
+        self.pin(llmemory.cast_ptr_to_adr(r))
+        return r
+        
+    def malloc_varsize_and_pin(self, typeid, length, size, itemsize,
+                               offset_to_length):
+        r = self.malloc_varsize_clear(typeid, length, size, itemsize,
+                                      offset_to_length)
+        self.pin(llmemory.cast_ptr_to_adr(r))
+        return r        
 
     def collect(self, gen=1):
         """Do a minor (gen=0) or major (gen>0) collection."""
@@ -775,9 +786,6 @@
             # that one will occur very soon
             self.nursery_free = self.nursery_top
 
-    def can_malloc_nonmovable(self):
-        return True
-
     def can_optimize_clean_setarrayitems(self):
         if self.card_page_indices > 0:
             return False
@@ -821,20 +829,6 @@
         (obj + offset_to_length).signed[0] = smallerlength
         return True
 
-
-    def malloc_fixedsize_nonmovable(self, typeid):
-        obj = self.external_malloc(typeid, 0)
-        return llmemory.cast_adr_to_ptr(obj, llmemory.GCREF)
-
-    def malloc_varsize_nonmovable(self, typeid, length):
-        obj = self.external_malloc(typeid, length)
-        return llmemory.cast_adr_to_ptr(obj, llmemory.GCREF)
-
-    def malloc_nonmovable(self, typeid, length, zero):
-        # helper for testing, same as GCBase.malloc
-        return self.external_malloc(typeid, length or 0)    # None -> 0
-
-
     # ----------
     # Simple helpers
 
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
@@ -1,15 +1,10 @@
 from pypy.rpython.memory.gctransform.transform import GCTransformer
-from pypy.rpython.memory.gctransform.support import find_gc_ptrs_in_type, \
-     get_rtti, ll_call_destructor, type_contains_pyobjs, var_ispyobj
+from pypy.rpython.memory.gctransform.support import get_rtti,\
+     ll_call_destructor, type_contains_pyobjs, var_ispyobj
 from pypy.rpython.lltypesystem import lltype, llmemory, rffi, llgroup
 from pypy.rpython import rmodel
 from pypy.rpython.memory import gctypelayout
-from pypy.rpython.memory.gc import marksweep
-from pypy.rpython.memory.gcheader import GCHeaderBuilder
-from pypy.rlib.rarithmetic import ovfcheck
 from pypy.rlib import rgc
-from pypy.rlib.debug import ll_assert
-from pypy.rlib.objectmodel import we_are_translated
 from pypy.translator.backendopt import graphanalyze
 from pypy.translator.backendopt.support import var_needsgc
 from pypy.translator.backendopt.finalizer import FinalizerAnalyzer
@@ -17,11 +12,11 @@
 from pypy.rpython import annlowlevel
 from pypy.rpython.rbuiltin import gen_cast
 from pypy.rpython.memory.gctypelayout import ll_weakref_deref, WEAKREF
-from pypy.rpython.memory.gctypelayout import convert_weakref_to, WEAKREFPTR
+from pypy.rpython.memory.gctypelayout import WEAKREFPTR
 from pypy.rpython.memory.gctransform.log import log
 from pypy.tool.sourcetools import func_with_new_name
-from pypy.rpython.lltypesystem.lloperation import llop, LL_OPERATIONS
-import sys, types
+from pypy.rpython.lltypesystem.lloperation import LL_OPERATIONS
+import types
 
 
 TYPE_ID = llgroup.HALFWORD
@@ -288,6 +283,14 @@
         self.can_move_ptr = getfn(GCClass.can_move.im_func,
                                   [s_gc, annmodel.SomeAddress()],
                                   annmodel.SomeBool())
+        self.malloc_fixedsize_and_pin_ptr = getfn(
+            GCClass.malloc_fixedsize_and_pin,
+            [s_gc, s_typeid16, annmodel.SomeInteger(nonneg=True)],
+            s_gcref)
+        self.malloc_varsize_and_pin_ptr = getfn(
+            GCClass.malloc_varsize_and_pin,
+            [s_gc, s_typeid16] + [annmodel.SomeInteger(nonneg=True)] * 4,
+            s_gcref)
 
         if hasattr(GCClass, 'shrink_array'):
             self.shrink_array_ptr = getfn(
@@ -365,17 +368,6 @@
         else:
             self.malloc_varsize_clear_fast_ptr = None
 
-        if getattr(GCClass, 'malloc_varsize_nonmovable', False):
-            malloc_nonmovable = func_with_new_name(
-                GCClass.malloc_varsize_nonmovable.im_func,
-                "malloc_varsize_nonmovable")
-            self.malloc_varsize_nonmovable_ptr = getfn(
-                malloc_nonmovable,
-                [s_gc, s_typeid16,
-                 annmodel.SomeInteger(nonneg=True)], s_gcref)
-        else:
-            self.malloc_varsize_nonmovable_ptr = None
-
         if getattr(GCClass, 'raw_malloc_memory_pressure', False):
             def raw_malloc_memory_pressure_varsize(length, itemsize):
                 totalmem = length * itemsize
@@ -690,10 +682,12 @@
         c_has_light_finalizer = rmodel.inputconst(lltype.Bool,
                                                   has_light_finalizer)
 
-        if not op.opname.endswith('_varsize') and not flags.get('varsize'):
+        if not 'varsize' in op.opname and not flags.get('varsize'):
             #malloc_ptr = self.malloc_fixedsize_ptr
             zero = flags.get('zero', False)
-            if (self.malloc_fast_ptr is not None and
+            if op.opname == 'malloc_and_pin':
+                malloc_ptr = self.malloc_fixedsize_and_pin_ptr
+            elif (self.malloc_fast_ptr is not None and
                 not c_has_finalizer.value and
                 (self.malloc_fast_is_clearing or not zero)):
                 malloc_ptr = self.malloc_fast_ptr
@@ -712,18 +706,14 @@
                                               info_varsize.ofstolength)
             c_varitemsize = rmodel.inputconst(lltype.Signed,
                                               info_varsize.varitemsize)
-            if flags.get('nonmovable') and self.malloc_varsize_nonmovable_ptr:
-                # we don't have tests for such cases, let's fail
-                # explicitely
-                malloc_ptr = self.malloc_varsize_nonmovable_ptr
-                args = [self.c_const_gc, c_type_id, v_length]
+            if op.opname == 'malloc_varsize_and_pin':
+                malloc_ptr = self.malloc_varsize_and_pin_ptr
+            elif self.malloc_varsize_clear_fast_ptr is not None:
+                malloc_ptr = self.malloc_varsize_clear_fast_ptr
             else:
-                if self.malloc_varsize_clear_fast_ptr is not None:
-                    malloc_ptr = self.malloc_varsize_clear_fast_ptr
-                else:
-                    malloc_ptr = self.malloc_varsize_clear_ptr
-                args = [self.c_const_gc, c_type_id, v_length, c_size,
-                        c_varitemsize, c_ofstolength]
+                malloc_ptr = self.malloc_varsize_clear_ptr
+            args = [self.c_const_gc, c_type_id, v_length, c_size,
+                    c_varitemsize, c_ofstolength]
         livevars = self.push_roots(hop)
         v_result = hop.genop("direct_call", [malloc_ptr] + args,
                              resulttype=llmemory.GCREF)
@@ -1109,19 +1099,15 @@
                   resultvar=hop.spaceop.result)
         self.pop_roots(hop, livevars)
 
-    def gct_malloc_nonmovable_varsize(self, hop):
+    def gct_malloc_and_pin(self, hop):
         TYPE = hop.spaceop.result.concretetype
-        if self.gcdata.gc.can_malloc_nonmovable():
-            return self.gct_malloc_varsize(hop, {'nonmovable':True})
-        c = rmodel.inputconst(TYPE, lltype.nullptr(TYPE.TO))
-        return hop.cast_result(c)
+        v_raw = self.gct_fv_gc_malloc(hop, {'flavor': 'gc'}, TYPE.TO)
+        hop.cast_result(v_raw)
 
-    def gct_malloc_nonmovable(self, hop):
+    def gct_malloc_varisze_and_pin(self, hop):
         TYPE = hop.spaceop.result.concretetype
-        if self.gcdata.gc.can_malloc_nonmovable():
-            return self.gct_malloc(hop, {'nonmovable':True})
-        c = rmodel.inputconst(TYPE, lltype.nullptr(TYPE.TO))
-        return hop.cast_result(c)
+        v_raw = self.gct_fv_gc_malloc(hop, {'flavor': 'gc'}, TYPE.TO)
+        hop.cast_result(v_raw)
 
     def _set_into_gc_array_part(self, op):
         if op.opname == 'setarrayitem':
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
@@ -548,14 +548,14 @@
         flavor = flags['flavor']
         assert flavor != 'cpy', "cannot malloc CPython objects directly"
         meth = getattr(self, 'gct_fv_%s_malloc_varsize' % flavor, None)
-        assert meth, "%s has no support for malloc_varsize with flavor %r" % 
(self, flavor) 
+        assert meth, "%s has no support for malloc_varsize with flavor %r" % 
(self, flavor)
         return self.varsize_malloc_helper(hop, flags, meth, [])
 
-    def gct_malloc_nonmovable(self, *args, **kwds):
+    def gct_malloc_and_pin(self, *args, **kwds):
         return self.gct_malloc(*args, **kwds)
 
-    def gct_malloc_nonmovable_varsize(self, *args, **kwds):
-        return self.gct_malloc_varsize(*args, **kwds)
+    def gct_malloc_varsize_and_pin(self, hop, *args, **kwds):
+        return self.gct_malloc_varsize(hop, *args, **kwds)
 
     def gct_gc_add_memory_pressure(self, hop):
         if hasattr(self, 'raw_malloc_memory_pressure_ptr'):
diff --git a/pypy/rpython/memory/gcwrapper.py b/pypy/rpython/memory/gcwrapper.py
--- a/pypy/rpython/memory/gcwrapper.py
+++ b/pypy/rpython/memory/gcwrapper.py
@@ -56,15 +56,24 @@
             return lltype.malloc(TYPE, n, flavor=flavor, zero=zero,
                                  track_allocation=track_allocation)
 
-    def malloc_nonmovable(self, TYPE, n=None, zero=False):
+    def malloc_and_pin(self, TYPE, n=None, zero=False):
         typeid = self.get_type_id(TYPE)
-        if not self.gc.can_malloc_nonmovable():
-            return lltype.nullptr(TYPE)
-        addr = self.gc.malloc_nonmovable(typeid, n, zero=zero)
-        result = llmemory.cast_adr_to_ptr(addr, lltype.Ptr(TYPE))
-        if not self.gc.malloc_zero_filled:
-            gctypelayout.zero_gc_pointers(result)
-        return result
+        size = self.gc.fixed_size(typeid)
+        result = self.gc.malloc_fixedsize_and_pin(typeid, size)
+        if result:
+            assert self.gc.malloc_zero_filled
+        return lltype.cast_opaque_ptr(lltype.Ptr(TYPE), result)
+
+    def malloc_varsize_and_pin(self, TYPE, n=None, zero=False):
+        typeid = self.get_type_id(TYPE)
+        size = self.gc.fixed_size(typeid)
+        itemsize = self.gc.varsize_item_sizes(typeid)
+        offset_to_length = self.gc.varsize_offset_to_length(typeid)
+        result = self.gc.malloc_varsize_and_pin(typeid, n, size, itemsize,
+                                              offset_to_length)
+        if result:
+            assert self.gc.malloc_zero_filled
+        return lltype.cast_opaque_ptr(lltype.Ptr(TYPE), result)
 
     def add_memory_pressure(self, size):
         if hasattr(self.gc, 'raw_malloc_memory_pressure'):
diff --git a/pypy/rpython/memory/test/test_gc.py 
b/pypy/rpython/memory/test/test_gc.py
--- a/pypy/rpython/memory/test/test_gc.py
+++ b/pypy/rpython/memory/test/test_gc.py
@@ -24,7 +24,7 @@
 class GCTest(object):
     GC_PARAMS = {}
     GC_CAN_MOVE = False
-    GC_CAN_MALLOC_NONMOVABLE = True
+    GC_CAN_MALLOC_AND_PIN = False
     GC_CAN_SHRINK_ARRAY = False
     GC_CAN_SHRINK_BIG_ARRAY = False
     BUT_HOW_BIG_IS_A_BIG_STRING = 3*WORD
@@ -592,24 +592,23 @@
         assert self.interpret(func, []) == self.GC_CAN_MOVE
 
 
-    def test_malloc_nonmovable(self):
+    def test_malloc_and_pin(self):
         TP = lltype.GcArray(lltype.Char)
         def func():
-            a = rgc.malloc_nonmovable(TP, 3)
+            a = rgc.malloc_and_pin(TP, 3)
             if a:
                 assert not rgc.can_move(a)
                 return 1
             return 0
 
-        assert self.interpret(func, []) == int(self.GC_CAN_MALLOC_NONMOVABLE)
+        assert self.interpret(func, []) == int(self.GC_CAN_MALLOC_AND_PIN)
 
-    def test_malloc_nonmovable_fixsize(self):
+    def test_malloc_and_pin_fixsize(self):
         S = lltype.GcStruct('S', ('x', lltype.Float))
         TP = lltype.GcStruct('T', ('s', lltype.Ptr(S)))
         def func():
             try:
-                a = rgc.malloc_nonmovable(TP)
-                rgc.collect()
+                a = rgc.malloc_and_pin(TP)
                 if a:
                     assert not rgc.can_move(a)
                     return 1
@@ -617,7 +616,7 @@
             except Exception:
                 return 2
 
-        assert self.interpret(func, []) == int(self.GC_CAN_MALLOC_NONMOVABLE)
+        assert self.interpret(func, []) == int(self.GC_CAN_MALLOC_AND_PIN)
 
     def test_shrink_array(self):
         from pypy.rpython.lltypesystem.rstr import STR
@@ -793,7 +792,6 @@
 class TestSemiSpaceGC(GCTest, snippet.SemiSpaceGCTests):
     from pypy.rpython.memory.gc.semispace import SemiSpaceGC as GCClass
     GC_CAN_MOVE = True
-    GC_CAN_MALLOC_NONMOVABLE = False
     GC_CAN_SHRINK_ARRAY = True
     GC_CAN_SHRINK_BIG_ARRAY = True
 
@@ -814,7 +812,6 @@
 
 class TestHybridGC(TestGenerationalGC):
     from pypy.rpython.memory.gc.hybrid import HybridGC as GCClass
-    GC_CAN_MALLOC_NONMOVABLE = True
     GC_CAN_SHRINK_BIG_ARRAY = False
 
     def test_ref_from_rawmalloced_to_regular(self):
@@ -878,14 +875,10 @@
         res = self.interpret(f, [15])
         assert res == 16
 
-    def test_malloc_nonmovable_fixsize(self):
-        py.test.skip("Not supported")
-
 class TestHybridGCSmallHeap(GCTest):
     from pypy.rpython.memory.gc.hybrid import HybridGC as GCClass
     GC_CAN_MOVE = False # with this size of heap, stuff gets allocated
                         # in 3rd gen.
-    GC_CAN_MALLOC_NONMOVABLE = True
     GC_PARAMS = {'space_size': 48*WORD,
                  'min_nursery_size': 12*WORD,
                  'nursery_size': 12*WORD,
@@ -927,14 +920,11 @@
         res = self.interpret(f, [200])
         assert res == 401
 
-    def test_malloc_nonmovable_fixsize(self):
-        py.test.skip("Not supported")
-
 
 class TestMiniMarkGC(TestSemiSpaceGC):
     from pypy.rpython.memory.gc.minimark import MiniMarkGC as GCClass
     GC_CAN_SHRINK_BIG_ARRAY = False
-    GC_CAN_MALLOC_NONMOVABLE = True
+    GC_CAN_MALLOC_AND_PIN = True
     BUT_HOW_BIG_IS_A_BIG_STRING = 11*WORD
 
     # those tests are here because they'll be messy and useless
diff --git a/pypy/rpython/memory/test/test_transformed_gc.py 
b/pypy/rpython/memory/test/test_transformed_gc.py
--- a/pypy/rpython/memory/test/test_transformed_gc.py
+++ b/pypy/rpython/memory/test/test_transformed_gc.py
@@ -42,7 +42,7 @@
 class GCTest(object):
     gcpolicy = None
     GC_CAN_MOVE = False
-    GC_CAN_MALLOC_NONMOVABLE = True
+    GC_CAN_MALLOC_AND_PIN = False
     GC_CAN_ALWAYS_PIN = False
     taggedpointers = False
 
@@ -643,44 +643,41 @@
         res = run([])
         assert res == self.GC_CAN_MOVE
 
-    def define_malloc_nonmovable(cls):
+    def define_malloc_and_pin(cls):
         TP = lltype.GcArray(lltype.Char)
         def func():
-            #try:
-            a = rgc.malloc_nonmovable(TP, 3, zero=True)
-            rgc.collect()
+            a = rgc.malloc_and_pin(TP, 3, zero=True)
             if a:
                 assert not rgc.can_move(a)
+                rgc.unpin(a)
                 return 1
             return 0
-            #except Exception, e:
-            #    return 2
 
         return func
 
-    def test_malloc_nonmovable(self):
-        run = self.runner("malloc_nonmovable")
-        assert int(self.GC_CAN_MALLOC_NONMOVABLE) == run([])
+    def test_malloc_and_pin(self):
+        run = self.runner("malloc_and_pin")
+        assert int(self.GC_CAN_MALLOC_AND_PIN) == run([])
 
-    def define_malloc_nonmovable_fixsize(cls):
+    def define_malloc_and_pin_fixsize(cls):
         S = lltype.GcStruct('S', ('x', lltype.Float))
         TP = lltype.GcStruct('T', ('s', lltype.Ptr(S)))
         def func():
             try:
-                a = rgc.malloc_nonmovable(TP)
-                rgc.collect()
+                a = rgc.malloc_and_pin(TP)
                 if a:
                     assert not rgc.can_move(a)
+                    rgc.unpin(a)
                     return 1
                 return 0
-            except Exception, e:
+            except Exception:
                 return 2
 
         return func
 
-    def test_malloc_nonmovable_fixsize(self):
-        run = self.runner("malloc_nonmovable_fixsize")
-        assert run([]) == int(self.GC_CAN_MALLOC_NONMOVABLE)
+    def test_malloc_and_pin_fixsize(self):
+        run = self.runner("malloc_and_pin_fixsize")
+        assert run([]) == int(self.GC_CAN_MALLOC_AND_PIN)
 
     def define_shrink_array(cls):
         from pypy.rpython.lltypesystem.rstr import STR
@@ -751,7 +748,6 @@
 
 class GenericMovingGCTests(GenericGCTests):
     GC_CAN_MOVE = True
-    GC_CAN_MALLOC_NONMOVABLE = False
     GC_CAN_TEST_ID = False
 
     def define_many_ids(cls):
@@ -1226,7 +1222,7 @@
 
 class TestHybridGC(TestGenerationGC):
     gcname = "hybrid"
-    GC_CAN_MALLOC_NONMOVABLE = True
+    GC_CAN_MALLOC_AND_PIN = True
 
     class gcpolicy(gc.FrameworkGcPolicy):
         class transformerclass(framework.FrameworkGCTransformer):
@@ -1289,14 +1285,12 @@
         res = run([])
         assert res == 42
 
-    def test_malloc_nonmovable_fixsize(self):
-        py.test.skip("not supported")
-
 
 class TestMiniMarkGC(TestHybridGC):
     gcname = "minimark"
     GC_CAN_TEST_ID = True
     GC_CAN_ALWAYS_PIN = True
+    GC_CAN_MALLOC_AND_PIN = True
 
     class gcpolicy(gc.FrameworkGcPolicy):
         class transformerclass(framework.FrameworkGCTransformer):
diff --git a/pypy/rpython/test/test_rclass.py b/pypy/rpython/test/test_rclass.py
--- a/pypy/rpython/test/test_rclass.py
+++ b/pypy/rpython/test/test_rclass.py
@@ -1131,18 +1131,6 @@
             assert sorted([u]) == [6]                    # 32-bit types
             assert sorted([i, r, d, l]) == [2, 3, 4, 5]  # 64-bit types
 
-    def test_nonmovable(self):
-        for (nonmovable, opname) in [(True, 'malloc_nonmovable'),
-                                     (False, 'malloc')]:
-            class A(object):
-                _alloc_nonmovable_ = nonmovable
-            def f():
-                return A()
-            t, typer, graph = self.gengraph(f, [])
-            assert summary(graph) == {opname: 1,
-                                      'cast_pointer': 1,
-                                      'setfield': 1}
-
 
 class TestOOtype(BaseTestRclass, OORtypeMixin):
 
diff --git a/pypy/translator/c/test/test_boehm.py 
b/pypy/translator/c/test/test_boehm.py
--- a/pypy/translator/c/test/test_boehm.py
+++ b/pypy/translator/c/test/test_boehm.py
@@ -391,23 +391,6 @@
         c_fn = self.getcompiled(fn, [])
         assert not c_fn()
 
-    def test_malloc_nonmovable(self):
-        TP = lltype.GcArray(lltype.Char)
-        def func():
-            try:
-                from pypy.rlib import rgc
-                a = rgc.malloc_nonmovable(TP, 3)
-                rgc.collect()
-                if a:
-                    assert not rgc.can_move(a)
-                    return 0
-                return 1
-            except Exception, e:
-                return 2
-
-        run = self.getcompiled(func)
-        assert run() == 0
-
     def test_shrink_array(self):
         from pypy.rpython.lltypesystem.rstr import STR
         from pypy.rlib import rgc
diff --git a/pypy/translator/c/test/test_newgc.py 
b/pypy/translator/c/test/test_newgc.py
--- a/pypy/translator/c/test/test_newgc.py
+++ b/pypy/translator/c/test/test_newgc.py
@@ -19,7 +19,7 @@
     removetypeptr = False
     taggedpointers = False
     GC_CAN_MOVE = False
-    GC_CAN_MALLOC_NONMOVABLE = True
+    GC_CAN_MALLOC_AND_PIN = False
     GC_CAN_SHRINK_ARRAY = False
 
     _isolated_func = None
@@ -723,24 +723,24 @@
     def test_can_move(self):
         assert self.run('can_move') == self.GC_CAN_MOVE
 
-    def define_malloc_nonmovable(cls):
+    def define_malloc_and_pin(cls):
         TP = lltype.GcArray(lltype.Char)
         def func():
             try:
-                a = rgc.malloc_nonmovable(TP, 3)
-                rgc.collect()
+                a = rgc.malloc_and_pin(TP, 3)
                 if a:
                     assert not rgc.can_move(a)
+                    rgc.unpin(a)
                     return 1
                 return 0
-            except Exception, e:
+            except Exception:
                 return 2
 
         return func
 
-    def test_malloc_nonmovable(self):
-        res = self.run('malloc_nonmovable')
-        assert res == self.GC_CAN_MALLOC_NONMOVABLE
+    def test_malloc_and_pin(self):
+        res = self.run('malloc_and_pin')
+        assert res == self.GC_CAN_MALLOC_AND_PIN
 
     def define_resizable_buffer(cls):
         from pypy.rpython.lltypesystem.rstr import STR
@@ -1189,7 +1189,6 @@
     gcpolicy = "semispace"
     should_be_moving = True
     GC_CAN_MOVE = True
-    GC_CAN_MALLOC_NONMOVABLE = False
     GC_CAN_SHRINK_ARRAY = True
 
     # for snippets
@@ -1366,7 +1365,6 @@
 class TestHybridGC(TestGenerationalGC):
     gcpolicy = "hybrid"
     should_be_moving = True
-    GC_CAN_MALLOC_NONMOVABLE = True
 
     def test_gc_set_max_heap_size(self):
         py.test.skip("not implemented")
@@ -1440,7 +1438,7 @@
 class TestMiniMarkGC(TestSemiSpaceGC):
     gcpolicy = "minimark"
     should_be_moving = True
-    GC_CAN_MALLOC_NONMOVABLE = True
+    GC_CAN_MALLOC_AND_PIN = True
     GC_CAN_SHRINK_ARRAY = True
 
     def test_gc_heap_stats(self):
diff --git a/pypy/translator/exceptiontransform.py 
b/pypy/translator/exceptiontransform.py
--- a/pypy/translator/exceptiontransform.py
+++ b/pypy/translator/exceptiontransform.py
@@ -256,7 +256,8 @@
               (block.exits[0].args[0].concretetype is lltype.Void or
                block.exits[0].args[0] is block.operations[-1].result) and
               block.operations[-1].opname not in ('malloc',     # special cases
-                                                  'malloc_nonmovable')):
+                                                  'malloc_and_pin',
+                                                  'malloc_varsize_and_pin')):
             last_operation -= 1
         lastblock = block
         for i in range(last_operation, -1, -1):
@@ -437,11 +438,11 @@
             flavor = spaceop.args[1].value['flavor']
             if flavor == 'gc':
                 insert_zeroing_op = True
-        elif spaceop.opname == 'malloc_nonmovable':
+        elif spaceop.opname in ['malloc_and_pin', 'malloc_varsize_and_pin']:
             # xxx we cannot insert zero_gc_pointers_inside after
-            # malloc_nonmovable, because it can return null.  For now
+            # malloc_and_pin, because it can return null.  For now
             # we simply always force the zero=True flag on
-            # malloc_nonmovable.
+            # malloc_and_pin.
             c_flags = spaceop.args[1]
             c_flags.value = c_flags.value.copy()
             spaceop.args[1].value['zero'] = True
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to