Skip to content

Commit 03f9048

Browse files
authored
Merge pull request adafruit#1816 from tannewt/stage_crash
Correct collect of permanent pointers.
2 parents f548305 + 54ef87c commit 03f9048

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

py/gc.c

+15-12
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,19 @@ STATIC void gc_sweep(void) {
345345
}
346346
}
347347

348+
// Mark can handle NULL pointers because it verifies the pointer is within the heap bounds.
349+
STATIC void gc_mark(void* ptr) {
350+
if (VERIFY_PTR(ptr)) {
351+
size_t block = BLOCK_FROM_PTR(ptr);
352+
if (ATB_GET_KIND(block) == AT_HEAD) {
353+
// An unmarked head: mark it, and mark all its children
354+
TRACE_MARK(block, ptr);
355+
ATB_HEAD_TO_MARK(block);
356+
gc_mark_subtree(block);
357+
}
358+
}
359+
}
360+
348361
void gc_collect_start(void) {
349362
GC_ENTER();
350363
MP_STATE_MEM(gc_lock_depth)++;
@@ -361,9 +374,7 @@ void gc_collect_start(void) {
361374
size_t root_end = offsetof(mp_state_ctx_t, vm.qstr_last_chunk);
362375
gc_collect_root(ptrs + root_start / sizeof(void*), (root_end - root_start) / sizeof(void*));
363376

364-
if (MP_STATE_MEM(permanent_pointers) != NULL) {
365-
gc_collect_root(MP_STATE_MEM(permanent_pointers), BYTES_PER_BLOCK / sizeof(void*));
366-
}
377+
gc_mark(MP_STATE_MEM(permanent_pointers));
367378

368379
#if MICROPY_ENABLE_PYSTACK
369380
// Trace root pointers from the Python stack.
@@ -375,15 +386,7 @@ void gc_collect_start(void) {
375386
void gc_collect_root(void **ptrs, size_t len) {
376387
for (size_t i = 0; i < len; i++) {
377388
void *ptr = ptrs[i];
378-
if (VERIFY_PTR(ptr)) {
379-
size_t block = BLOCK_FROM_PTR(ptr);
380-
if (ATB_GET_KIND(block) == AT_HEAD) {
381-
// An unmarked head: mark it, and mark all its children
382-
TRACE_MARK(block, ptr);
383-
ATB_HEAD_TO_MARK(block);
384-
gc_mark_subtree(block);
385-
}
386-
}
389+
gc_mark(ptr);
387390
}
388391
}
389392

0 commit comments

Comments
 (0)