resurrect malloc test
This commit is contained in:
parent
879768397e
commit
39d73d753c
18
tests/malloc/Makefile
Normal file
18
tests/malloc/Makefile
Normal file
@ -0,0 +1,18 @@
|
||||
# name of your application
|
||||
APPLICATION = test_malloc
|
||||
|
||||
# If no BOARD is found in the environment, use this default:
|
||||
BOARD ?= native
|
||||
|
||||
# This has to be the absolute path to the RIOT base directory:
|
||||
RIOTBASE ?= $(CURDIR)/../..
|
||||
|
||||
# Comment this out to disable code in RIOT that does safety checking
|
||||
# which is not needed in a production environment but helps in the
|
||||
# development process:
|
||||
CFLAGS += -DDEVELHELP
|
||||
|
||||
# Change this to 0 show compiler invocation lines by default:
|
||||
QUIET ?= 1
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
||||
82
tests/malloc/main.c
Normal file
82
tests/malloc/main.c
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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
|
||||
* @brief Simple malloc/free test
|
||||
*
|
||||
*
|
||||
* @author Benjamin Valentin <benpicco@zedat.fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user