summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-07-12 12:17:38 +1000
committerDamien George <damien.p.george@gmail.com>2017-07-12 12:17:38 +1000
commit12d4fa9b37408ed682e52c3d78ecd6c269a4904a (patch)
treee04cad5355ba46441c94b2842fd9a8b5c3a29616
parent1e6fd9f2b4072873f5d6846b19b2ef0ccc5e4e52 (diff)
downloadmicropython-12d4fa9b37408ed682e52c3d78ecd6c269a4904a.tar.gz
micropython-12d4fa9b37408ed682e52c3d78ecd6c269a4904a.zip
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.
-rw-r--r--py/gc.c49
1 files changed, 23 insertions, 26 deletions
diff --git a/py/gc.c b/py/gc.c
index 937dae44f7..2af886c56b 100644
--- a/py/gc.c
+++ b/py/gc.c
@@ -536,37 +536,34 @@ void gc_free(void *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);
- if (ATB_GET_KIND(block) == AT_HEAD) {
- #if MICROPY_ENABLE_FINALISER
- FTB_CLEAR(block);
- #endif
- // 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)) {
- MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB;
- }
+ assert(ATB_GET_KIND(block) == AT_HEAD);
- // free head and all of its tail blocks
- do {
- ATB_ANY_TO_FREE(block);
- block += 1;
- } while (ATB_GET_KIND(block) == AT_TAIL);
-
- GC_EXIT();
+ #if MICROPY_ENABLE_FINALISER
+ FTB_CLEAR(block);
+ #endif
- #if EXTENSIVE_HEAP_PROFILING
- gc_dump_alloc_table();
- #endif
- } else {
- GC_EXIT();
- assert(!"bad free");
+ // 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)) {
+ MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB;
}
- } else if (ptr != NULL) {
- GC_EXIT();
- assert(!"bad free");
- } else {
+
+ // free head and all of its tail blocks
+ do {
+ ATB_ANY_TO_FREE(block);
+ block += 1;
+ } while (ATB_GET_KIND(block) == AT_TAIL);
+
GC_EXIT();
+
+ #if EXTENSIVE_HEAP_PROFILING
+ gc_dump_alloc_table();
+ #endif
}
}