| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import sys, weakref, py |
| 8 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 9 |
from rpython.rlib.objectmodel import we_are_translated, specialize, not_rpython |
| 10 |
from rpython.rtyper.extregistry import ExtRegistryEntry |
| 11 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 12 |
from rpython.rlib import rgc |
| 13 |
|
| 14 |
|
| 15 |
REFCNT_FROM_PYPY = sys.maxint // 4 + 1 |
| 16 |
REFCNT_FROM_PYPY_LIGHT = REFCNT_FROM_PYPY + (sys.maxint // 2 + 1) |
| 17 |
|
| 18 |
RAWREFCOUNT_DEALLOC_TRIGGER = lltype.Ptr(lltype.FuncType([], lltype.Void)) |
| 19 |
|
| 20 |
|
| 21 |
def _build_pypy_link(p): |
| 22 |
res = len(_adr2pypy) |
| 23 |
_adr2pypy.append(p) |
| 24 |
return res |
| 25 |
|
| 26 |
|
| 27 |
@not_rpython |
| 28 |
def init(dealloc_trigger_callback=None): |
| 29 |
"""set up rawrefcount with the GC. This is only used |
| 30 |
for tests; it should not be called at all during translation. |
| 31 |
""" |
| 32 |
global _p_list, _o_list, _adr2pypy, _pypy2ob, _pypy2ob_rev |
| 33 |
global _d_list, _dealloc_trigger_callback |
| 34 |
_p_list = [] |
| 35 |
_o_list = [] |
| 36 |
_adr2pypy = [None] |
| 37 |
_pypy2ob = {} |
| 38 |
_pypy2ob_rev = {} |
| 39 |
_d_list = [] |
| 40 |
_d_marker = None |
| 41 |
_dealloc_trigger_callback = dealloc_trigger_callback |
| 42 |
|
| 43 |
@not_rpython |
| 44 |
def create_link_pypy(p, ob): |
| 45 |
"a link where the PyPy object contains some or all the data" |
| 46 |
|
| 47 |
assert p not in _pypy2ob |
| 48 |
assert ob._obj not in _pypy2ob_rev |
| 49 |
assert not ob.c_ob_pypy_link |
| 50 |
ob.c_ob_pypy_link = _build_pypy_link(p) |
| 51 |
_pypy2ob[p] = ob |
| 52 |
_pypy2ob_rev[ob._obj] = p |
| 53 |
_p_list.append(ob) |
| 54 |
|
| 55 |
@not_rpython |
| 56 |
def create_link_pyobj(p, ob): |
| 57 |
"""a link where the PyObject contains all the data. |
| 58 |
from_obj() will not work on this 'p'.""" |
| 59 |
|
| 60 |
assert p not in _pypy2ob |
| 61 |
assert ob._obj not in _pypy2ob_rev |
| 62 |
assert not ob.c_ob_pypy_link |
| 63 |
ob.c_ob_pypy_link = _build_pypy_link(p) |
| 64 |
_o_list.append(ob) |
| 65 |
|
| 66 |
@not_rpython |
| 67 |
def mark_deallocating(marker, ob): |
| 68 |
"""mark the PyObject as deallocating, by storing 'marker' |
| 69 |
inside its ob_pypy_link field""" |
| 70 |
assert ob._obj not in _pypy2ob_rev |
| 71 |
assert not ob.c_ob_pypy_link |
| 72 |
ob.c_ob_pypy_link = _build_pypy_link(marker) |
| 73 |
|
| 74 |
@not_rpython |
| 75 |
def from_obj(OB_PTR_TYPE, p): |
| 76 |
ob = _pypy2ob.get(p) |
| 77 |
if ob is None: |
| 78 |
return lltype.nullptr(OB_PTR_TYPE.TO) |
| 79 |
assert lltype.typeOf(ob) == OB_PTR_TYPE |
| 80 |
assert _pypy2ob_rev[ob._obj] is p |
| 81 |
return ob |
| 82 |
|
| 83 |
@not_rpython |
| 84 |
def to_obj(Class, ob): |
| 85 |
link = ob.c_ob_pypy_link |
| 86 |
if link == 0: |
| 87 |
return None |
| 88 |
p = _adr2pypy[link] |
| 89 |
assert isinstance(p, Class) |
| 90 |
return p |
| 91 |
|
| 92 |
@not_rpython |
| 93 |
def next_dead(OB_PTR_TYPE): |
| 94 |
"""When the GC runs, it finds some pyobjs to be dead |
| 95 |
but cannot immediately dispose of them (it doesn't know how to call |
| 96 |
e.g. tp_dealloc(), and anyway calling it immediately would cause all |
| 97 |
sorts of bugs). So instead, it stores them in an internal list, |
| 98 |
initially with refcnt == 1. This pops the next item off this list. |
| 99 |
""" |
| 100 |
if len(_d_list) == 0: |
| 101 |
return lltype.nullptr(OB_PTR_TYPE.TO) |
| 102 |
ob = _d_list.pop() |
| 103 |
assert lltype.typeOf(ob) == OB_PTR_TYPE |
| 104 |
return ob |
| 105 |
|
| 106 |
@not_rpython |
| 107 |
def _collect(track_allocation=True): |
| 108 |
"""for tests only. Emulates a GC collection. |
| 109 |
Will invoke dealloc_trigger_callback() once if there are objects |
| 110 |
whose _Py_Dealloc() should be called. |
| 111 |
""" |
| 112 |
def detach(ob, wr_list): |
| 113 |
assert ob.c_ob_refcnt >= REFCNT_FROM_PYPY |
| 114 |
assert ob.c_ob_pypy_link |
| 115 |
p = _adr2pypy[ob.c_ob_pypy_link] |
| 116 |
assert p is not None |
| 117 |
_adr2pypy[ob.c_ob_pypy_link] = None |
| 118 |
wr_list.append((ob, weakref.ref(p))) |
| 119 |
return p |
| 120 |
|
| 121 |
global _p_list, _o_list |
| 122 |
wr_p_list = [] |
| 123 |
new_p_list = [] |
| 124 |
for ob in reversed(_p_list): |
| 125 |
if ob.c_ob_refcnt not in (REFCNT_FROM_PYPY, REFCNT_FROM_PYPY_LIGHT): |
| 126 |
new_p_list.append(ob) |
| 127 |
else: |
| 128 |
p = detach(ob, wr_p_list) |
| 129 |
ob_test = _pypy2ob.pop(p) |
| 130 |
p_test = _pypy2ob_rev.pop(ob_test._obj) |
| 131 |
assert p_test is p |
| 132 |
del p, p_test |
| 133 |
ob = None |
| 134 |
_p_list = Ellipsis |
| 135 |
|
| 136 |
wr_o_list = [] |
| 137 |
for ob in reversed(_o_list): |
| 138 |
detach(ob, wr_o_list) |
| 139 |
_o_list = Ellipsis |
| 140 |
|
| 141 |
rgc.collect() |
| 142 |
rgc.collect() |
| 143 |
rgc.collect() |
| 144 |
|
| 145 |
def attach(ob, wr, final_list): |
| 146 |
assert ob.c_ob_refcnt >= REFCNT_FROM_PYPY |
| 147 |
p = wr() |
| 148 |
if p is not None: |
| 149 |
assert ob.c_ob_pypy_link |
| 150 |
_adr2pypy[ob.c_ob_pypy_link] = p |
| 151 |
final_list.append(ob) |
| 152 |
return p |
| 153 |
else: |
| 154 |
ob.c_ob_pypy_link = 0 |
| 155 |
if ob.c_ob_refcnt >= REFCNT_FROM_PYPY_LIGHT: |
| 156 |
ob.c_ob_refcnt -= REFCNT_FROM_PYPY_LIGHT |
| 157 |
ob.c_ob_pypy_link = 0 |
| 158 |
if ob.c_ob_refcnt == 0: |
| 159 |
lltype.free(ob, flavor='raw', |
| 160 |
track_allocation=track_allocation) |
| 161 |
else: |
| 162 |
assert ob.c_ob_refcnt >= REFCNT_FROM_PYPY |
| 163 |
assert ob.c_ob_refcnt < int(REFCNT_FROM_PYPY_LIGHT * 0.99) |
| 164 |
ob.c_ob_refcnt -= REFCNT_FROM_PYPY |
| 165 |
ob.c_ob_pypy_link = 0 |
| 166 |
if ob.c_ob_refcnt == 0: |
| 167 |
ob.c_ob_refcnt = 1 |
| 168 |
_d_list.append(ob) |
| 169 |
return None |
| 170 |
|
| 171 |
_p_list = new_p_list |
| 172 |
for ob, wr in wr_p_list: |
| 173 |
p = attach(ob, wr, _p_list) |
| 174 |
if p is not None: |
| 175 |
_pypy2ob[p] = ob |
| 176 |
_pypy2ob_rev.clear() |
| 177 |
for p, ob in _pypy2ob.items(): |
| 178 |
assert ob._obj not in _pypy2ob_rev |
| 179 |
_pypy2ob_rev[ob._obj] = p |
| 180 |
_o_list = [] |
| 181 |
for ob, wr in wr_o_list: |
| 182 |
attach(ob, wr, _o_list) |
| 183 |
|
| 184 |
if _d_list: |
| 185 |
res = _dealloc_trigger_callback() |
| 186 |
if res == "RETRY": |
| 187 |
_collect(track_allocation=track_allocation) |
| 188 |
|
| 189 |
_keepalive_forever = set() |
| 190 |
def _dont_free_any_more(): |
| 191 |
"Make sure that any object still referenced won't be freed any more." |
| 192 |
for ob in _p_list + _o_list: |
| 193 |
_keepalive_forever.add(to_obj(object, ob)) |
| 194 |
del _d_list[:] |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
def _unspec_p(hop, v_p): |
| 200 |
assert isinstance(v_p.concretetype, lltype.Ptr) |
| 201 |
assert v_p.concretetype.TO._gckind == 'gc' |
| 202 |
return hop.genop('cast_opaque_ptr', [v_p], resulttype=llmemory.GCREF) |
| 203 |
|
| 204 |
def _unspec_ob(hop, v_ob): |
| 205 |
assert isinstance(v_ob.concretetype, lltype.Ptr) |
| 206 |
assert v_ob.concretetype.TO._gckind == 'raw' |
| 207 |
return hop.genop('cast_ptr_to_adr', [v_ob], resulttype=llmemory.Address) |
| 208 |
|
| 209 |
def _spec_p(hop, v_p): |
| 210 |
assert v_p.concretetype == llmemory.GCREF |
| 211 |
return hop.genop('cast_opaque_ptr', [v_p], |
| 212 |
resulttype=hop.r_result.lowleveltype) |
| 213 |
|
| 214 |
def _spec_ob(hop, v_ob): |
| 215 |
assert v_ob.concretetype == llmemory.Address |
| 216 |
return hop.genop('cast_adr_to_ptr', [v_ob], |
| 217 |
resulttype=hop.r_result.lowleveltype) |
| 218 |
|
| 219 |
|
| 220 |
class Entry(ExtRegistryEntry): |
| 221 |
_about_ = init |
| 222 |
|
| 223 |
def compute_result_annotation(self, s_dealloc_callback): |
| 224 |
from rpython.rtyper.llannotation import SomePtr |
| 225 |
assert isinstance(s_dealloc_callback, SomePtr) |
| 226 |
|
| 227 |
def specialize_call(self, hop): |
| 228 |
hop.exception_cannot_occur() |
| 229 |
[v_dealloc_callback] = hop.inputargs(hop.args_r[0]) |
| 230 |
hop.genop('gc_rawrefcount_init', [v_dealloc_callback]) |
| 231 |
|
| 232 |
|
| 233 |
class Entry(ExtRegistryEntry): |
| 234 |
_about_ = (create_link_pypy, create_link_pyobj, mark_deallocating) |
| 235 |
|
| 236 |
def compute_result_annotation(self, s_p, s_ob): |
| 237 |
pass |
| 238 |
|
| 239 |
def specialize_call(self, hop): |
| 240 |
if self.instance is create_link_pypy: |
| 241 |
name = 'gc_rawrefcount_create_link_pypy' |
| 242 |
elif self.instance is create_link_pyobj: |
| 243 |
name = 'gc_rawrefcount_create_link_pyobj' |
| 244 |
elif self.instance is mark_deallocating: |
| 245 |
name = 'gc_rawrefcount_mark_deallocating' |
| 246 |
v_p, v_ob = hop.inputargs(*hop.args_r) |
| 247 |
hop.exception_cannot_occur() |
| 248 |
hop.genop(name, [_unspec_p(hop, v_p), _unspec_ob(hop, v_ob)]) |
| 249 |
|
| 250 |
if hop.rtyper.annotator.translator.config.translation.gc == "boehm": |
| 251 |
c_func = hop.inputconst(lltype.typeOf(func_boehm_eci), |
| 252 |
func_boehm_eci) |
| 253 |
hop.genop('direct_call', [c_func]) |
| 254 |
|
| 255 |
|
| 256 |
class Entry(ExtRegistryEntry): |
| 257 |
_about_ = from_obj |
| 258 |
|
| 259 |
def compute_result_annotation(self, s_OB_PTR_TYPE, s_p): |
| 260 |
from rpython.annotator import model as annmodel |
| 261 |
from rpython.rtyper.llannotation import lltype_to_annotation |
| 262 |
assert (isinstance(s_p, annmodel.SomeInstance) or |
| 263 |
annmodel.s_None.contains(s_p)) |
| 264 |
assert s_OB_PTR_TYPE.is_constant() |
| 265 |
return lltype_to_annotation(s_OB_PTR_TYPE.const) |
| 266 |
|
| 267 |
def specialize_call(self, hop): |
| 268 |
hop.exception_cannot_occur() |
| 269 |
v_p = hop.inputarg(hop.args_r[1], arg=1) |
| 270 |
v_ob = hop.genop('gc_rawrefcount_from_obj', [_unspec_p(hop, v_p)], |
| 271 |
resulttype = llmemory.Address) |
| 272 |
return _spec_ob(hop, v_ob) |
| 273 |
|
| 274 |
class Entry(ExtRegistryEntry): |
| 275 |
_about_ = to_obj |
| 276 |
|
| 277 |
def compute_result_annotation(self, s_Class, s_ob): |
| 278 |
from rpython.annotator import model as annmodel |
| 279 |
from rpython.rtyper.llannotation import SomePtr |
| 280 |
assert isinstance(s_ob, SomePtr) |
| 281 |
assert s_Class.is_constant() |
| 282 |
classdef = self.bookkeeper.getuniqueclassdef(s_Class.const) |
| 283 |
return annmodel.SomeInstance(classdef, can_be_None=True) |
| 284 |
|
| 285 |
def specialize_call(self, hop): |
| 286 |
hop.exception_cannot_occur() |
| 287 |
v_ob = hop.inputarg(hop.args_r[1], arg=1) |
| 288 |
v_p = hop.genop('gc_rawrefcount_to_obj', [_unspec_ob(hop, v_ob)], |
| 289 |
resulttype = llmemory.GCREF) |
| 290 |
return _spec_p(hop, v_p) |
| 291 |
|
| 292 |
class Entry(ExtRegistryEntry): |
| 293 |
_about_ = next_dead |
| 294 |
|
| 295 |
def compute_result_annotation(self, s_OB_PTR_TYPE): |
| 296 |
from rpython.annotator import model as annmodel |
| 297 |
from rpython.rtyper.llannotation import lltype_to_annotation |
| 298 |
assert s_OB_PTR_TYPE.is_constant() |
| 299 |
return lltype_to_annotation(s_OB_PTR_TYPE.const) |
| 300 |
|
| 301 |
def specialize_call(self, hop): |
| 302 |
hop.exception_cannot_occur() |
| 303 |
v_ob = hop.genop('gc_rawrefcount_next_dead', [], |
| 304 |
resulttype = llmemory.Address) |
| 305 |
return _spec_ob(hop, v_ob) |
| 306 |
|
| 307 |
src_dir = py.path.local(__file__).dirpath() / 'src' |
| 308 |
boehm_eci = ExternalCompilationInfo( |
| 309 |
post_include_bits = [(src_dir / 'boehm-rawrefcount.h').read()], |
| 310 |
separate_module_files = [(src_dir / 'boehm-rawrefcount.c')], |
| 311 |
) |
| 312 |
func_boehm_eci = rffi.llexternal_use_eci(boehm_eci) |