mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-24 03:29:57 -07:00
unit tests: aligned_alloc
This commit is contained in:
@@ -76,7 +76,7 @@ static void test_memmgr_malloc(const size_t allocation_size) {
|
||||
|
||||
// check that at least 75% of memory is zero-initialized
|
||||
if(zero_count < (allocation_size * 0.75)) {
|
||||
error_message = "seems that memory is not zero-initialized after free";
|
||||
error_message = "seems that memory is not zero-initialized after free (malloc)";
|
||||
}
|
||||
|
||||
FURI_CRITICAL_EXIT();
|
||||
@@ -174,9 +174,67 @@ static void test_memmgr_realloc(const size_t allocation_size) {
|
||||
}
|
||||
}
|
||||
|
||||
static void test_memmgr_alloc_aligned(const size_t allocation_size, const size_t alignment) {
|
||||
uint8_t* ptr = NULL;
|
||||
const char* error_message = NULL;
|
||||
|
||||
FURI_CRITICAL_ENTER();
|
||||
|
||||
ptr = aligned_alloc(alignment, allocation_size);
|
||||
|
||||
// test that we can allocate memory
|
||||
if(ptr == NULL) {
|
||||
error_message = "aligned_alloc failed";
|
||||
}
|
||||
|
||||
// test that memory is aligned
|
||||
if(((uintptr_t)ptr % alignment) != 0) {
|
||||
error_message = "memory is not aligned after aligned_alloc";
|
||||
}
|
||||
|
||||
// test that memory is zero-initialized after allocation
|
||||
for(size_t i = 0; i < allocation_size; i++) {
|
||||
if(ptr[i] != 0) {
|
||||
error_message = "memory is not zero-initialized after aligned_alloc";
|
||||
break;
|
||||
}
|
||||
}
|
||||
memset(ptr, 0x55, allocation_size);
|
||||
free(ptr);
|
||||
|
||||
// test that memory is zero-initialized after free
|
||||
// we know that allocator can use this memory for inner purposes
|
||||
// so we check that memory at least partially zero-initialized
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wuse-after-free"
|
||||
|
||||
size_t zero_count = 0;
|
||||
for(size_t i = 0; i < allocation_size; i++) {
|
||||
if(ptr[i] == 0) {
|
||||
zero_count++;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
// check that at least 75% of memory is zero-initialized
|
||||
if(zero_count < (allocation_size * 0.75)) {
|
||||
error_message = "seems that memory is not zero-initialized after free (aligned_alloc)";
|
||||
}
|
||||
|
||||
FURI_CRITICAL_EXIT();
|
||||
|
||||
if(error_message != NULL) {
|
||||
mu_fail(error_message);
|
||||
}
|
||||
}
|
||||
|
||||
void test_furi_memmgr_advanced(void) {
|
||||
const size_t sizes[] = {50, 100, 500, 1000, 5000, 10000};
|
||||
const size_t sizes_count = sizeof(sizes) / sizeof(sizes[0]);
|
||||
const size_t alignments[] = {4, 8, 16, 32, 64, 128, 256, 512, 1024};
|
||||
const size_t alignments_count = sizeof(alignments) / sizeof(alignments[0]);
|
||||
|
||||
// do test without memory fragmentation
|
||||
{
|
||||
@@ -187,6 +245,12 @@ void test_furi_memmgr_advanced(void) {
|
||||
for(size_t i = 0; i < sizes_count; i++) {
|
||||
test_memmgr_realloc(sizes[i]);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < sizes_count; i++) {
|
||||
for(size_t j = 0; j < alignments_count; j++) {
|
||||
test_memmgr_alloc_aligned(sizes[i], alignments[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// do test with memory fragmentation
|
||||
@@ -194,6 +258,7 @@ void test_furi_memmgr_advanced(void) {
|
||||
void* blocks[sizes_count];
|
||||
void* guards[sizes_count - 1];
|
||||
|
||||
// setup guards
|
||||
for(size_t i = 0; i < sizes_count; i++) {
|
||||
blocks[i] = malloc(sizes[i]);
|
||||
if(i < sizes_count - 1) {
|
||||
@@ -205,6 +270,7 @@ void test_furi_memmgr_advanced(void) {
|
||||
free(blocks[i]);
|
||||
}
|
||||
|
||||
// do test
|
||||
for(size_t i = 0; i < sizes_count; i++) {
|
||||
test_memmgr_malloc(sizes[i]);
|
||||
}
|
||||
@@ -213,6 +279,13 @@ void test_furi_memmgr_advanced(void) {
|
||||
test_memmgr_realloc(sizes[i]);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < sizes_count; i++) {
|
||||
for(size_t j = 0; j < alignments_count; j++) {
|
||||
test_memmgr_alloc_aligned(sizes[i], alignments[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// cleanup guards
|
||||
for(size_t i = 0; i < sizes_count - 1; i++) {
|
||||
free(guards[i]);
|
||||
}
|
||||
|
||||
@@ -327,6 +327,7 @@ extern void* pvPortRealloc(void* pv, size_t xSize) {
|
||||
memmgr_unlock();
|
||||
|
||||
// clear remain block content, if the new size is bigger
|
||||
// can't guarantee that all data will be zeroed, cos tlsf_block_size is not always the same as xSize
|
||||
if(xSize > old_size) {
|
||||
memset((uint8_t*)data + old_size, 0, xSize - old_size);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user