| 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 |
| 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; |
| 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 |
|
| 29 |
|
| 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; |
| 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); |
| 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); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
struct link_s **new_buckets = calloc(num_buckets, sizeof(struct link_s *)); |
| 91 |
assert(new_buckets); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 106 |
|
| 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 |
|
| 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 |
|
| 150 |
|
| 151 |
|
| 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 |
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 |
|
| 188 |
|
| 189 |
|
| 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(void *gcobj, |
| 215 |
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 |
|
| 222 |
|
| 223 |
|
| 224 |
hash_add_entry(gcobj1, pyobj1); |
| 225 |
} |
| 226 |
|
| 227 |
RPY_EXTERN |
| 228 |
void *gc_rawrefcount_from_obj(void *gcobj) |
| 229 |
{ |
| 230 |
return hash_get_entry((gcobj_t *)gcobj); |
| 231 |
} |
| 232 |
|
| 233 |
RPY_EXTERN |
| 234 |
void *gc_rawrefcount_to_obj(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 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 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) |
| 284 |
hash_list_walk_next = hash_mask_bucket; |
| 285 |
} |