heap/test_multi_heap_host: added initial tests for heap aligned alloc

This commit is contained in:
Felipe Neves 2019-11-13 10:37:23 +08:00
parent d26ddaa644
commit 8bd09429d3
2 changed files with 49 additions and 14 deletions

View file

@ -195,32 +195,36 @@ void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t ali
return NULL;
}
if(!alignment) {
return NULL;
}
//Alignment must be a power of two...
if((alignment & (alignment - 1)) != 0) {
return NULL;
}
if(size > SIZE_MAX - POISON_OVERHEAD) {
if(size > SIZE_MAX /* - POISON_OVERHEAD*/) {
return NULL;
}
uint32_t overhead = (sizeof(uint32_t) + (alignment - 1) + POISON_OVERHEAD);
uint32_t overhead = (sizeof(uint32_t) + (alignment - 1) /*+ POISON_OVERHEAD*/);
multi_heap_internal_lock(heap);
poison_head_t *head = multi_heap_malloc_impl(heap, size + overhead);
uint8_t *data = NULL;
//uint8_t *data = NULL;
if (head != NULL) {
data = poison_allocated_region(head, size);
//data = poison_allocated_region(head, size);
#ifdef SLOW
/* check everything we got back is FREE_FILL_PATTERN & swap for MALLOC_FILL_PATTERN */
bool ret = verify_fill_pattern(data, size, true, true, true);
assert( ret );
//bool ret = verify_fill_pattern(data, size, true, true, true);
//assert( ret );
#endif
}
//Lets align our new obtained block address:
//and save the original heap pointer to allow deallocation
void *ptr = (void *)ALIGN_UP((uintptr_t)head + sizeof(uint32_t) + POISON_OVERHEAD, alignment);
void *ptr = (void *)ALIGN_UP((uintptr_t)head + sizeof(uint32_t) /* + POISON_OVERHEAD*/, alignment);
*((uint32_t *)ptr - 1) = (uint32_t)((uintptr_t)ptr - (uintptr_t)head);
multi_heap_internal_unlock(heap);
@ -258,19 +262,17 @@ void multi_heap_aligned_free(multi_heap_handle_t heap, void *p)
multi_heap_internal_lock(heap);
uint32_t offset = *((uint32_t *)p - 1);
void *block_head = (void *)((uint8_t *)p - (offset - POISON_OVERHEAD));
void *block_head = (void *)((uint8_t *)p - (offset /*- POISON_OVERHEAD*/));
poison_head_t *head = verify_allocated_region(block_head, true);
assert(head != NULL);
/* poison_head_t *head = verify_allocated_region(block_head, true);
assert(head != NULL); */
#ifdef SLOW
/* replace everything with FREE_FILL_PATTERN, including the poison head/tail */
memset(head, FREE_FILL_PATTERN,
head->alloc_size + POISON_OVERHEAD);
//memset(head, FREE_FILL_PATTERN, head->alloc_size + POISON_OVERHEAD);
#endif
multi_heap_free_impl(heap, head);
multi_heap_free_impl(heap, block_head);
multi_heap_internal_unlock(heap);
}

View file

@ -494,3 +494,36 @@ TEST_CASE("unaligned heaps", "[multi_heap]")
}
}
}
TEST_CASE("multi_heap aligned allocations", "[multi_heap]")
{
uint8_t test_heap[1024 * 1024];
multi_heap_handle_t heap = multi_heap_register(test_heap, sizeof(test_heap));
uint32_t aligments = 0; // starts from alignment by 4-byte boundary
printf("New heap:\n");
multi_heap_dump(heap);
printf("*********************\n");
for(;aligments < 4096; aligments++) {
//Use a non-sense size to test correct alignment even in strange
//memory layout objects:
uint8_t *buf = (uint8_t *)multi_heap_aligned_alloc(heap, (aligments + 117), aligments );
if(((aligments & (aligments - 1)) != 0) || (!aligments)) {
REQUIRE( buf == NULL );
//printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
} else {
REQUIRE( buf != NULL );
REQUIRE((intptr_t)buf >= (intptr_t)test_heap);
REQUIRE((intptr_t)buf < (intptr_t)(test_heap + sizeof(test_heap)));
printf("[ALIGNED_ALLOC] alignment required: %u \n", aligments);
//printf("[ALIGNED_ALLOC] allocated size: %d \n", multi_heap_get_allocated_size(heap, buf));
printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
REQUIRE(((intptr_t)buf & (aligments - 1)) == 0);
multi_heap_aligned_free(heap, buf);
}
}
}