Debian Codegraph @

rpython/rlib/test/test_rawrefcount.py

raw symbols SCIP
1 import gc
2 import weakref
3 from rpython.rlib import rawrefcount, objectmodel, rgc
4 from rpython.rlib.rawrefcount import REFCNT_FROM_PYPY, REFCNT_FROM_PYPY_LIGHT
5 from rpython.rtyper.lltypesystem import lltype
6 from rpython.rtyper.annlowlevel import llhelper
7 from rpython.translator.c.test.test_standalone import StandaloneTests
8 from rpython.config.translationoption import get_combined_translation_config
9
10
11 class W_Root(object):
12 def __init__(self, intval=0):
13 self.intval = intval
14 def __nonzero__(self):
15 raise Exception("you cannot do that, you must use space.is_true()")
16
17 PyObjectS = lltype.Struct('PyObjectS',
18 ('c_ob_refcnt', lltype.Signed),
19 ('c_ob_pypy_link', lltype.Signed))
20 PyObject = lltype.Ptr(PyObjectS)
21
22
23 class TestRawRefCount:
24
25 def setup_method(self, meth):
26 rawrefcount.init()
27
28 def test_create_link_pypy(self):
29 p = W_Root(42)
30 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
31 assert rawrefcount.from_obj(PyObject, p) == lltype.nullptr(PyObjectS)
32 assert rawrefcount.to_obj(W_Root, ob) == None
33 rawrefcount.create_link_pypy(p, ob)
34 assert ob.c_ob_refcnt == 0
35 ob.c_ob_refcnt += REFCNT_FROM_PYPY_LIGHT
36 assert rawrefcount.from_obj(PyObject, p) == ob
37 assert rawrefcount.to_obj(W_Root, ob) == p
38 lltype.free(ob, flavor='raw')
39
40 def test_create_link_pyobj(self):
41 p = W_Root(42)
42 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
43 assert rawrefcount.from_obj(PyObject, p) == lltype.nullptr(PyObjectS)
44 assert rawrefcount.to_obj(W_Root, ob) == None
45 rawrefcount.create_link_pyobj(p, ob)
46 assert ob.c_ob_refcnt == 0
47 ob.c_ob_refcnt += REFCNT_FROM_PYPY
48 assert rawrefcount.from_obj(PyObject, p) == lltype.nullptr(PyObjectS)
49 assert rawrefcount.to_obj(W_Root, ob) == p
50 lltype.free(ob, flavor='raw')
51
52 def test_collect_p_dies(self):
53 p = W_Root(42)
54 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
55 rawrefcount.create_link_pypy(p, ob)
56 ob.c_ob_refcnt += REFCNT_FROM_PYPY_LIGHT
57 assert rawrefcount._p_list == [ob]
58 wr_ob = weakref.ref(ob)
59 wr_p = weakref.ref(p)
60 del ob, p
61 rawrefcount._collect()
62 gc.collect()
63 assert rawrefcount._p_list == []
64 assert wr_ob() is None
65 assert wr_p() is None
66
67 def test_collect_p_keepalive_pyobject(self):
68 p = W_Root(42)
69 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
70 rawrefcount.create_link_pypy(p, ob)
71 ob.c_ob_refcnt += REFCNT_FROM_PYPY_LIGHT
72 assert rawrefcount._p_list == [ob]
73 wr_ob = weakref.ref(ob)
74 wr_p = weakref.ref(p)
75 ob.c_ob_refcnt += 1 # <=
76 del ob, p
77 rawrefcount._collect()
78 gc.collect()
79 ob = wr_ob()
80 p = wr_p()
81 assert ob is not None and p is not None
82 assert rawrefcount._p_list == [ob]
83 assert rawrefcount.to_obj(W_Root, ob) == p
84 assert rawrefcount.from_obj(PyObject, p) == ob
85 lltype.free(ob, flavor='raw')
86
87 def test_collect_p_keepalive_w_root(self):
88 p = W_Root(42)
89 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
90 rawrefcount.create_link_pypy(p, ob)
91 ob.c_ob_refcnt += REFCNT_FROM_PYPY_LIGHT
92 assert rawrefcount._p_list == [ob]
93 wr_ob = weakref.ref(ob)
94 del ob # p remains
95 rawrefcount._collect()
96 ob = wr_ob()
97 assert ob is not None
98 assert rawrefcount._p_list == [ob]
99 assert rawrefcount.to_obj(W_Root, ob) == p
100 assert rawrefcount.from_obj(PyObject, p) == ob
101 lltype.free(ob, flavor='raw')
102
103 def test_collect_o_dies(self):
104 trigger = []; rawrefcount.init(lambda: trigger.append(1))
105 p = W_Root(42)
106 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
107 rawrefcount.create_link_pyobj(p, ob)
108 ob.c_ob_refcnt += REFCNT_FROM_PYPY
109 assert rawrefcount._o_list == [ob]
110 wr_ob = weakref.ref(ob)
111 wr_p = weakref.ref(p)
112 del ob, p
113 rawrefcount._collect()
114 ob = wr_ob()
115 assert ob is not None
116 assert trigger == [1]
117 assert rawrefcount.next_dead(PyObject) == ob
118 assert rawrefcount.next_dead(PyObject) == lltype.nullptr(PyObjectS)
119 assert rawrefcount.next_dead(PyObject) == lltype.nullptr(PyObjectS)
120 assert rawrefcount._o_list == []
121 assert wr_p() is None
122 assert ob.c_ob_refcnt == 1 # from the pending list
123 assert ob.c_ob_pypy_link == 0
124 lltype.free(ob, flavor='raw')
125
126 def test_collect_o_keepalive_pyobject(self):
127 p = W_Root(42)
128 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
129 p.pyobj = ob
130 rawrefcount.create_link_pyobj(p, ob)
131 ob.c_ob_refcnt += REFCNT_FROM_PYPY
132 assert rawrefcount._o_list == [ob]
133 wr_ob = weakref.ref(ob)
134 wr_p = weakref.ref(p)
135 ob.c_ob_refcnt += 1 # <=
136 del p
137 rawrefcount._collect()
138 p = wr_p()
139 assert p is None # was unlinked
140 assert ob.c_ob_refcnt == 1 # != REFCNT_FROM_PYPY_OBJECT + 1
141 assert rawrefcount._o_list == []
142 assert rawrefcount.to_obj(W_Root, ob) == None
143 lltype.free(ob, flavor='raw')
144
145 def test_collect_o_keepalive_w_root(self):
146 p = W_Root(42)
147 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
148 p.pyobj = ob
149 rawrefcount.create_link_pyobj(p, ob)
150 ob.c_ob_refcnt += REFCNT_FROM_PYPY
151 assert rawrefcount._o_list == [ob]
152 wr_ob = weakref.ref(ob)
153 del ob # p remains
154 rawrefcount._collect()
155 ob = wr_ob()
156 assert ob is not None
157 assert rawrefcount._o_list == [ob]
158 assert rawrefcount.to_obj(W_Root, ob) == p
159 assert p.pyobj == ob
160 lltype.free(ob, flavor='raw')
161
162 def test_collect_s_dies(self):
163 trigger = []; rawrefcount.init(lambda: trigger.append(1))
164 p = W_Root(42)
165 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
166 rawrefcount.create_link_pypy(p, ob)
167 ob.c_ob_refcnt += REFCNT_FROM_PYPY
168 assert rawrefcount._p_list == [ob]
169 wr_ob = weakref.ref(ob)
170 wr_p = weakref.ref(p)
171 del ob, p
172 rawrefcount._collect()
173 ob = wr_ob()
174 assert ob is not None
175 assert trigger == [1]
176 assert rawrefcount._d_list == [ob]
177 assert rawrefcount._p_list == []
178 assert wr_p() is None
179 assert ob.c_ob_refcnt == 1 # from _d_list
180 assert ob.c_ob_pypy_link == 0
181 lltype.free(ob, flavor='raw')
182
183 def test_collect_s_keepalive_pyobject(self):
184 p = W_Root(42)
185 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
186 p.pyobj = ob
187 rawrefcount.create_link_pypy(p, ob)
188 ob.c_ob_refcnt += REFCNT_FROM_PYPY
189 assert rawrefcount._p_list == [ob]
190 wr_ob = weakref.ref(ob)
191 wr_p = weakref.ref(p)
192 ob.c_ob_refcnt += 1 # <=
193 del ob, p
194 rawrefcount._collect()
195 ob = wr_ob()
196 p = wr_p()
197 assert ob is not None and p is not None
198 assert rawrefcount._p_list == [ob]
199 assert rawrefcount.to_obj(W_Root, ob) == p
200 lltype.free(ob, flavor='raw')
201
202 def test_collect_s_keepalive_w_root(self):
203 p = W_Root(42)
204 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
205 p.pyobj = ob
206 rawrefcount.create_link_pypy(p, ob)
207 ob.c_ob_refcnt += REFCNT_FROM_PYPY
208 assert rawrefcount._p_list == [ob]
209 wr_ob = weakref.ref(ob)
210 del ob # p remains
211 rawrefcount._collect()
212 ob = wr_ob()
213 assert ob is not None
214 assert rawrefcount._p_list == [ob]
215 assert rawrefcount.to_obj(W_Root, ob) == p
216 lltype.free(ob, flavor='raw')
217
218 def test_mark_deallocating(self):
219 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
220 w_marker = W_Root(42)
221 rawrefcount.mark_deallocating(w_marker, ob)
222 assert rawrefcount.to_obj(W_Root, ob) is w_marker
223 rawrefcount._collect()
224 assert rawrefcount.to_obj(W_Root, ob) is w_marker
225 lltype.free(ob, flavor='raw')
226
227
228 class TestTranslated(StandaloneTests):
229
230 def test_full_translation(self):
231 class State:
232 pass
233 state = State()
234 state.seen = []
235 def dealloc_trigger():
236 state.seen.append(1)
237 w_marker = W_Root(-1)
238
239 def make_p():
240 p = W_Root(42)
241 ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
242 rawrefcount.create_link_pypy(p, ob)
243 ob.c_ob_refcnt += REFCNT_FROM_PYPY
244 assert rawrefcount.from_obj(PyObject, p) == ob
245 assert rawrefcount.to_obj(W_Root, ob) == p
246 return ob, p
247
248 FTYPE = rawrefcount.RAWREFCOUNT_DEALLOC_TRIGGER
249
250 def entry_point(argv):
251 ll_dealloc_trigger_callback = llhelper(FTYPE, dealloc_trigger)
252 rawrefcount.init(ll_dealloc_trigger_callback)
253 ob, p = make_p()
254 if state.seen != []:
255 print "OB COLLECTED REALLY TOO SOON"
256 return 1
257 rgc.collect()
258 if state.seen != []:
259 print "OB COLLECTED TOO SOON"
260 return 1
261 objectmodel.keepalive_until_here(p)
262 p = None
263 rgc.collect()
264 if state.seen != [1]:
265 print "OB NOT COLLECTED"
266 return 1
267 if rawrefcount.next_dead(PyObject) != ob:
268 print "NEXT_DEAD != OB"
269 return 1
270 if ob.c_ob_refcnt != 1:
271 print "next_dead().ob_refcnt != 1"
272 return 1
273 if rawrefcount.next_dead(PyObject) != lltype.nullptr(PyObjectS):
274 print "NEXT_DEAD second time != NULL"
275 return 1
276 if rawrefcount.to_obj(W_Root, ob) is not None:
277 print "to_obj(dead) is not None?"
278 return 1
279 rawrefcount.mark_deallocating(w_marker, ob)
280 if rawrefcount.to_obj(W_Root, ob) is not w_marker:
281 print "to_obj(marked-dead) is not w_marker"
282 return 1
283 print "OK!"
284 lltype.free(ob, flavor='raw')
285 return 0
286
287 self.config = get_combined_translation_config(translating=True)
288 self.config.translation.gc = "incminimark"
289 t, cbuilder = self.compile(entry_point)
290 data = cbuilder.cmdexec('hi there')
291 assert data.startswith('OK!\n')