Skip to content

Commit 12d4fa9

Browse files
committed
py/gc: Refactor assertions in gc_free function.
gc_free() expects either NULL or a valid pointer into the heap, so the checks for a valid pointer can be turned into assertions.
1 parent 1e6fd9f commit 12d4fa9

1 file changed

Lines changed: 23 additions & 26 deletions

File tree

py/gc.c

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -536,37 +536,34 @@ void gc_free(void *ptr) {
536536

537537
DEBUG_printf("gc_free(%p)\n", ptr);
538538

539-
if (VERIFY_PTR(ptr)) {
539+
if (ptr == NULL) {
540+
GC_EXIT();
541+
} else {
542+
// get the GC block number corresponding to this pointer
543+
assert(VERIFY_PTR(ptr));
540544
size_t block = BLOCK_FROM_PTR(ptr);
541-
if (ATB_GET_KIND(block) == AT_HEAD) {
542-
#if MICROPY_ENABLE_FINALISER
543-
FTB_CLEAR(block);
544-
#endif
545-
// set the last_free pointer to this block if it's earlier in the heap
546-
if (block / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) {
547-
MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB;
548-
}
545+
assert(ATB_GET_KIND(block) == AT_HEAD);
549546

550-
// free head and all of its tail blocks
551-
do {
552-
ATB_ANY_TO_FREE(block);
553-
block += 1;
554-
} while (ATB_GET_KIND(block) == AT_TAIL);
555-
556-
GC_EXIT();
547+
#if MICROPY_ENABLE_FINALISER
548+
FTB_CLEAR(block);
549+
#endif
557550

558-
#if EXTENSIVE_HEAP_PROFILING
559-
gc_dump_alloc_table();
560-
#endif
561-
} else {
562-
GC_EXIT();
563-
assert(!"bad free");
551+
// set the last_free pointer to this block if it's earlier in the heap
552+
if (block / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) {
553+
MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB;
564554
}
565-
} else if (ptr != NULL) {
566-
GC_EXIT();
567-
assert(!"bad free");
568-
} else {
555+
556+
// free head and all of its tail blocks
557+
do {
558+
ATB_ANY_TO_FREE(block);
559+
block += 1;
560+
} while (ATB_GET_KIND(block) == AT_TAIL);
561+
569562
GC_EXIT();
563+
564+
#if EXTENSIVE_HEAP_PROFILING
565+
gc_dump_alloc_table();
566+
#endif
570567
}
571568
}
572569

0 commit comments

Comments
 (0)