1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/tests/malloc/main.c

83 lines
1.6 KiB
C
Raw Normal View History

2014-10-08 23:21:13 +02:00
/*
* Copyright (C) 2013 Benjamin Valentin <benpicco@zedat.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @ingroup tests
* @{
*
* @file
2015-05-19 12:26:34 +02:00
* @brief Simple malloc/free test
2014-10-08 23:21:13 +02:00
*
*
2015-05-19 12:26:34 +02:00
* @author Benjamin Valentin <benpicco@zedat.fu-berlin.de>
2014-10-08 23:21:13 +02:00
*
* @}
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHUNK_SIZE 1024
struct node {
struct node *next;
void *ptr;
};
int total = 0;
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);
memset(head->ptr, '@', CHUNK_SIZE);
head = head->next = malloc(sizeof(struct node));
head->ptr = 0;
head->next = 0;
total += sizeof(struct node);
}
}
void free_memory(struct node *head)
{
struct node *old_head;
while (head) {
if (head->ptr) {
printf("Free %d Bytes at 0x%p, total %d\n", CHUNK_SIZE, head->ptr, total -= CHUNK_SIZE);
free(head->ptr);
}
if (head->next) {
old_head = head;
head = head->next;
free(old_head);
}
else {
free(head);
head = 0;
}
total -= sizeof(struct node);
}
}
int main(void)
{
while (1) {
struct node *head = malloc(sizeof(struct node));
total += sizeof(struct node);
fill_memory(head);
free_memory(head);
}
return 0;
}