1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2026-01-01 01:41:18 +01:00

tests/malloc: cleanup in test application

The idea is not fill the memory on the computer running a native instance and to be able to test on hardware. The test application is reworked similarly as the memarray test application. Macro are now overridable and printed at the beginnined of the test: this allows easier automatic testing
This commit is contained in:
Alexandre Abadie 2019-11-12 19:01:41 +01:00
parent d91b756d0b
commit 254f6ff9f9
No known key found for this signature in database
GPG Key ID: 1C919A403CAE1405

View File

@ -22,9 +22,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#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;
}