diff --git a/tests/malloc/main.c b/tests/malloc/main.c index 41184ae5ab..d5db60f845 100644 --- a/tests/malloc/main.c +++ b/tests/malloc/main.c @@ -22,9 +22,22 @@ #include #include #include +#include #ifndef CHUNK_SIZE -#define CHUNK_SIZE 1024 +#ifdef BOARD_NATIVE +#define CHUNK_SIZE (1024 * 1024U) +#else +#define CHUNK_SIZE (1024U) +#endif +#endif + +#ifndef NUMBER_OF_TESTS +#define NUMBER_OF_TESTS 3 +#endif + +#ifndef MAX_MEM +#define MAX_MEM (256 * 1024UL * 1024UL) #endif struct node { @@ -32,13 +45,20 @@ struct node { void *ptr; }; -static int total = 0; +static uint32_t total = 0; static void fill_memory(struct node *head) { - while (head && (head->ptr = malloc(CHUNK_SIZE))) { - printf("Allocated %d Bytes at 0x%p, total %d\n", - CHUNK_SIZE, head->ptr, total += CHUNK_SIZE); + uint32_t allocations = 0; + + if (head) { + head->next = NULL; + } + + total = 0; + while (head && (head->ptr = malloc(CHUNK_SIZE)) && total < MAX_MEM) { + printf("Allocated %"PRIu32" Bytes at 0x%p, total %"PRIu32"\n", + (uint32_t)CHUNK_SIZE, head->ptr, total += CHUNK_SIZE); memset(head->ptr, '@', CHUNK_SIZE); head = head->next = malloc(sizeof(struct node)); if (head) { @@ -46,7 +66,10 @@ static void fill_memory(struct node *head) head->ptr = 0; head->next = 0; } + allocations++; } + + printf("Allocations count: %"PRIu32"\n", allocations); } static void free_memory(struct node *head) @@ -55,8 +78,11 @@ static void free_memory(struct node *head) while (head) { if (head->ptr) { - printf("Free %d Bytes at 0x%p, total %d\n", - CHUNK_SIZE, head->ptr, total -= CHUNK_SIZE); + if (total > CHUNK_SIZE) { + total -= CHUNK_SIZE; + } + printf("Free %"PRIu32" Bytes at 0x%p, total %"PRIu32"\n", + (uint32_t)CHUNK_SIZE, head->ptr, total); free(head->ptr); } @@ -76,7 +102,11 @@ static void free_memory(struct node *head) int main(void) { - while (1) { + printf("CHUNK_SIZE: %"PRIu32"\n", (uint32_t)CHUNK_SIZE); + printf("NUMBER_OF_TESTS: %d\n", NUMBER_OF_TESTS); + + uint8_t test = NUMBER_OF_TESTS; + while (test--) { struct node *head = malloc(sizeof(struct node)); total += sizeof(struct node); @@ -84,5 +114,7 @@ int main(void) free_memory(head); } + puts("[SUCCESS]"); + return 0; }