Debian Codegraph @

rpython/rlib/src/boehm-rawrefcount.c

raw symbols SCIP
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <assert.h>
5 #include <limits.h>
6 #include <gc/gc.h>
7 #include <gc/gc_mark.h>
8
9 #ifdef TEST_BOEHM_RAWREFCOUNT
10 # define RPY_EXTERN /* nothing */
11 #else
12 # include "common_header.h"
13 #endif
14
15
16 #define REFCNT_FROM_PYPY (LONG_MAX / 4 + 1)
17
18 typedef struct pypy_header0 gcobj_t; /* opaque here */
19
20 #ifndef _WIN32
21 typedef intptr_t Py_ssize_t;
22 #elif defined(_WIN64)
23 typedef long long Py_ssize_t;
24 #else
25 typedef long Py_ssize_t;
26 #endif
27
28 /* this is the first two words of the PyObject structure used in
29 pypy/module/cpyext */
30 typedef struct {
31 Py_ssize_t ob_refcnt;
32 Py_ssize_t ob_pypy_link;
33 } pyobj_t;
34
35 struct link_s {
36 pyobj_t *pyobj; /* NULL if entry unused */
37 uintptr_t gcenc;
38 struct link_s *next_in_bucket;
39 };
40
41 #define MARKER_LIST_START ((pyobj_t *)-1)
42
43 static struct link_s **hash_buckets, *hash_list, *hash_free_list;
44 static uintptr_t hash_mask_bucket;
45 static intptr_t hash_list_walk_next = -1;
46
47 static uintptr_t hash_get_hash(gcobj_t *gcobj)
48 {
49 assert(gcobj != NULL);
50 uintptr_t h = (uintptr_t)gcobj;
51 assert((h & 1) == 0);
52 h -= (h >> 6);
53 return h & hash_mask_bucket;
54 }
55
56 static gcobj_t *decode_gcenc(uintptr_t gcenc)
57 {
58 if (gcenc & 1)
59 gcenc = ~gcenc;
60 return (gcobj_t *)gcenc;
61 }
62
63 static void hash_link(struct link_s *lnk)
64 {
65 uintptr_t h = hash_get_hash(decode_gcenc(lnk->gcenc));
66 lnk->next_in_bucket = hash_buckets[h];
67 hash_buckets[h] = lnk;
68 }
69
70 static void boehm_is_about_to_collect(void);
71
72 static void hash_grow_table(void)
73 {
74 static int rec = 0;
75 assert(!rec); /* recursive hash_grow_table() */
76 rec = 1;
77
78 if (hash_buckets == NULL)
79 GC_set_start_callback(boehm_is_about_to_collect);
80
81 uintptr_t i, num_buckets = (hash_mask_bucket + 1) * 2;
82 if (num_buckets < 16) num_buckets = 16;
83 assert((num_buckets & (num_buckets - 1)) == 0); /* power of two */
84
85 /* The new hash_buckets: an array of pointers to struct link_s, of
86 length a power of two, used as a dictionary hash table. It is
87 not allocated with Boehm because there is no point in Boehm looking
88 in it.
89 */
90 struct link_s **new_buckets = calloc(num_buckets, sizeof(struct link_s *));
91 assert(new_buckets);
92
93 /* The new hash_list: the array of all struct link_s. Their order
94 is irrelevant. There is a GC_register_finalizer() on the 'gcenc'
95 field, so we don't move the array; instead we allocate a new array
96 to use in addition to the old one. There are a total of 2 to 4
97 times as many 'struct link_s' as the length of 'buckets'.
98 */
99 uintptr_t num_list = num_buckets * 2;
100 struct link_s *new_list = GC_MALLOC(num_list * sizeof(struct link_s));
101 for (i = num_list; i-- > 1; ) {
102 new_list[i].next_in_bucket = hash_free_list;
103 hash_free_list = &new_list[i];
104 }
105 /* list[0] is abused to store a pointer to the previous list and
106 the length of the current list */
107 struct link_s *old_list = hash_list;
108 new_list[0].next_in_bucket = old_list;
109 new_list[0].gcenc = num_list;
110 new_list[0].pyobj = MARKER_LIST_START;
111
112 hash_list = new_list;
113 free(hash_buckets);
114 hash_buckets = new_buckets;
115 hash_mask_bucket = num_buckets - 1;
116 hash_list_walk_next = hash_mask_bucket;
117
118 /* re-add all old 'struct link_s' to the hash_buckets */
119 struct link_s *plist = old_list;
120 while (plist != NULL) {
121 uintptr_t count = plist[0].gcenc;
122 for (i = 1; i < count; i++) {
123 if (plist[i].gcenc != 0)
124 hash_link(&plist[i]);
125 }
126 plist = plist[0].next_in_bucket;
127 }
128 GC_reachable_here(old_list);
129
130 rec = 0;
131 }
132
133 static void hash_add_entry(gcobj_t *gcobj, pyobj_t *pyobj)
134 {
135 if (hash_free_list == NULL) {
136 hash_grow_table();
137 }
138 assert(pyobj->ob_pypy_link == 0);
139
140 struct link_s *lnk = hash_free_list;
141 hash_free_list = lnk->next_in_bucket;
142 lnk->pyobj = pyobj;
143 lnk->gcenc = (uintptr_t)gcobj;
144 pyobj->ob_pypy_link = (Py_ssize_t)lnk;
145
146 hash_link(lnk);
147
148 if (GC_base(gcobj) == NULL) {
149 /* 'gcobj' is probably a prebuilt object - it makes no */
150 /* sense to register it then, and it crashes Boehm in */
151 /* quite obscure ways */
152 }
153 else {
154 int j = GC_general_register_disappearing_link(
155 (void **)&lnk->gcenc, gcobj);
156 assert(j == GC_SUCCESS);
157 }
158 }
159
160 static pyobj_t *hash_get_entry(gcobj_t *gcobj)
161 {
162 if (hash_buckets == NULL)
163 return NULL;
164 uintptr_t h = hash_get_hash(gcobj);
165 struct link_s *lnk = hash_buckets[h];
166 while (lnk != NULL) {
167 assert(lnk->pyobj != NULL);
168 if (decode_gcenc(lnk->gcenc) == gcobj)
169 return lnk->pyobj;
170 lnk = lnk->next_in_bucket;
171 }
172 return NULL;
173 }
174
175
176 RPY_EXTERN
177 /*pyobj_t*/void *gc_rawrefcount_next_dead(void)
178 {
179 while (hash_list_walk_next >= 0) {
180 struct link_s *p, **pp = &hash_buckets[hash_list_walk_next];
181 while (1) {
182 p = *pp;
183 if (p == NULL)
184 break;
185 assert(p->pyobj != NULL);
186 if (p->gcenc == 0) {
187 /* quadratic time on the number of links from the same
188 bucket chain, but it should be small with very high
189 probability */
190 pyobj_t *result = p->pyobj;
191 #ifdef TEST_BOEHM_RAWREFCOUNT
192 printf("next_dead: %p\n", result);
193 #endif
194 assert(result->ob_refcnt == REFCNT_FROM_PYPY);
195 result->ob_refcnt = 1;
196 result->ob_pypy_link = 0;
197 p->pyobj = NULL;
198 *pp = p->next_in_bucket;
199 p->next_in_bucket = hash_free_list;
200 hash_free_list = p;
201 return result;
202 }
203 else {
204 assert(p->gcenc != ~(uintptr_t)0);
205 pp = &p->next_in_bucket;
206 }
207 }
208 hash_list_walk_next--;
209 }
210 return NULL;
211 }
212
213 RPY_EXTERN
214 void gc_rawrefcount_create_link_pypy(/*gcobj_t*/void *gcobj,
215 /*pyobj_t*/void *pyobj)
216 {
217 gcobj_t *gcobj1 = (gcobj_t *)gcobj;
218 pyobj_t *pyobj1 = (pyobj_t *)pyobj;
219
220 assert(pyobj1->ob_pypy_link == 0);
221 /*assert(pyobj1->ob_refcnt >= REFCNT_FROM_PYPY);*/
222 /*^^^ could also be fixed just after the call to create_link_pypy()*/
223
224 hash_add_entry(gcobj1, pyobj1);
225 }
226
227 RPY_EXTERN
228 /*pyobj_t*/void *gc_rawrefcount_from_obj(/*gcobj_t*/void *gcobj)
229 {
230 return hash_get_entry((gcobj_t *)gcobj);
231 }
232
233 RPY_EXTERN
234 /*gcobj_t*/void *gc_rawrefcount_to_obj(/*pyobj_t*/void *pyobj)
235 {
236 pyobj_t *pyobj1 = (pyobj_t *)pyobj;
237
238 if (pyobj1->ob_pypy_link == 0)
239 return NULL;
240
241 struct link_s *lnk = (struct link_s *)pyobj1->ob_pypy_link;
242 assert(lnk->pyobj == pyobj1);
243
244 gcobj_t *g = decode_gcenc(lnk->gcenc);
245 assert(g != NULL);
246 return g;
247 }
248
249 static void boehm_is_about_to_collect(void)
250 {
251 struct link_s *plist = hash_list;
252 uintptr_t gcenc_union = 0;
253 while (plist != NULL) {
254 uintptr_t i, count = plist[0].gcenc;
255 for (i = 1; i < count; i++) {
256 if (plist[i].gcenc == 0)
257 continue;
258
259 pyobj_t *p = plist[i].pyobj;
260 assert(p != NULL);
261 assert(p->ob_refcnt >= REFCNT_FROM_PYPY);
262
263 #ifdef TEST_BOEHM_RAWREFCOUNT
264 printf("plist[%d].gcenc: %p ", (int)i, (void *)plist[i].gcenc);
265 #endif
266
267 if ((plist[i].gcenc & 1) ^ (p->ob_refcnt == REFCNT_FROM_PYPY)) {
268 /* ob_refcnt > FROM_PYPY: non-zero regular refcnt,
269 the gc obj must stay alive. decode gcenc.
270 ---OR---
271 ob_refcnt == FROM_PYPY: no refs from C code, the
272 gc obj must not (necessarily) stay alive. encode gcenc.
273 */
274 plist[i].gcenc = ~plist[i].gcenc;
275 }
276 gcenc_union |= plist[i].gcenc;
277 #ifdef TEST_BOEHM_RAWREFCOUNT
278 printf("-> %p\n", (void *)plist[i].gcenc);
279 #endif
280 }
281 plist = plist[0].next_in_bucket;
282 }
283 if (gcenc_union & 1) /* if there is at least one item potentially dead */
284 hash_list_walk_next = hash_mask_bucket;
285 }