malloc: poison heap when DEBUG is defined

This commit is contained in:
anna 2021-08-11 23:12:10 +02:00
parent 8293d9372b
commit 040b5af5d6
Signed by: fef
GPG key ID: EC22E476DC2D3D84

View file

@ -220,6 +220,9 @@ void *malloc(size_t size)
cursor = blk_slice(&generic_heap, cursor, size);
generic_heap_free -= blk_get_size(cursor);
ptr = cursor->data;
# ifdef DEBUG
memset(cursor->data, 0xaa, blk_get_size(cursor));
# endif
}
mutex_unlock(&generic_heap_lock);
@ -249,6 +252,9 @@ void *atomic_malloc(size_t size)
cursor = blk_slice(&atomic_heap, cursor, size);
atomic_heap_free -= blk_get_size(cursor);
ptr = cursor->data;
# ifdef DEBUG
memset(cursor->data, 0xaa, blk_get_size(cursor));
# endif
}
return ptr;
@ -284,7 +290,12 @@ void free(void *ptr)
mutex_lock(&generic_heap_lock);
generic_heap_free += blk_get_size(blk);
blk_clear_alloc(blk);
blk_try_merge(&generic_heap, blk);
blk = blk_try_merge(&generic_heap, blk);
# ifdef DEBUG
memset(&blk->data[MIN_SIZE], 0xaa, blk_get_size(blk) - MIN_SIZE);
# endif
mutex_unlock(&generic_heap_lock);
} else if (ptr >= atomic_heap_start && ptr <= atomic_heap_end) {
if (!blk_is_alloc(blk))
@ -293,7 +304,12 @@ void free(void *ptr)
atomic_enter();
atomic_heap_free += blk_get_size(blk);
blk_clear_alloc(blk);
blk_try_merge(&atomic_heap, blk);
blk = blk_try_merge(&atomic_heap, blk);
# ifdef DEBUG
memset(&blk->data[MIN_SIZE], 0xaa, blk_get_size(blk) - MIN_SIZE);
# endif
atomic_leave();
} else {
__breakpoint;