Skip to content
Snippets Groups Projects
Commit 12d4fa9b authored by Damien George's avatar Damien George
Browse files

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.
parent 1e6fd9f2
No related branches found
No related tags found
No related merge requests found
...@@ -536,12 +536,18 @@ void gc_free(void *ptr) { ...@@ -536,12 +536,18 @@ void gc_free(void *ptr) {
DEBUG_printf("gc_free(%p)\n", ptr); DEBUG_printf("gc_free(%p)\n", ptr);
if (VERIFY_PTR(ptr)) { if (ptr == NULL) {
GC_EXIT();
} else {
// get the GC block number corresponding to this pointer
assert(VERIFY_PTR(ptr));
size_t block = BLOCK_FROM_PTR(ptr); size_t block = BLOCK_FROM_PTR(ptr);
if (ATB_GET_KIND(block) == AT_HEAD) { assert(ATB_GET_KIND(block) == AT_HEAD);
#if MICROPY_ENABLE_FINALISER #if MICROPY_ENABLE_FINALISER
FTB_CLEAR(block); FTB_CLEAR(block);
#endif #endif
// set the last_free pointer to this block if it's earlier in the heap // set the last_free pointer to this block if it's earlier in the heap
if (block / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) { if (block / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) {
MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB; MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB;
...@@ -558,15 +564,6 @@ void gc_free(void *ptr) { ...@@ -558,15 +564,6 @@ void gc_free(void *ptr) {
#if EXTENSIVE_HEAP_PROFILING #if EXTENSIVE_HEAP_PROFILING
gc_dump_alloc_table(); gc_dump_alloc_table();
#endif #endif
} else {
GC_EXIT();
assert(!"bad free");
}
} else if (ptr != NULL) {
GC_EXIT();
assert(!"bad free");
} else {
GC_EXIT();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment