Skip to content

Commit 2895002

Browse files
committed
resurrect malloc test
1 parent 9fa206c commit 2895002

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

tests/malloc/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# name of your application
2+
APPLICATION = test_malloc
3+
4+
# If no BOARD is found in the environment, use this default:
5+
BOARD ?= native
6+
7+
# This has to be the absolute path to the RIOT base directory:
8+
RIOTBASE ?= $(CURDIR)/../..
9+
10+
# Comment this out to disable code in RIOT that does safety checking
11+
# which is not needed in a production environment but helps in the
12+
# development process:
13+
CFLAGS += -DDEVELHELP
14+
15+
# Change this to 0 show compiler invocation lines by default:
16+
QUIET ?= 1
17+
18+
include $(RIOTBASE)/Makefile.include

tests/malloc/main.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (C) 2013 Benjamin Valentin <benpicco@zedat.fu-berlin.de>
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser General
5+
* Public License v2.1. See the file LICENSE in the top level directory for more
6+
* details.
7+
*/
8+
9+
/**
10+
* @ingroup tests
11+
* @{
12+
*
13+
* @file
14+
* @brief Simple malloc/free test
15+
*
16+
*
17+
* @author Benjamin Valentin <benpicco@zedat.fu-berlin.de>
18+
*
19+
* @}
20+
*/
21+
22+
#include <stdio.h>
23+
#include <stdlib.h>
24+
#include <string.h>
25+
26+
#define CHUNK_SIZE 1024
27+
28+
struct node {
29+
struct node *next;
30+
void *ptr;
31+
};
32+
33+
int total = 0;
34+
35+
void fill_memory(struct node *head)
36+
{
37+
while (head && (head->ptr = malloc(CHUNK_SIZE))) {
38+
printf("Allocated %d Bytes at 0x%p, total %d\n", CHUNK_SIZE, head->ptr, total += CHUNK_SIZE);
39+
memset(head->ptr, '@', CHUNK_SIZE);
40+
head = head->next = malloc(sizeof(struct node));
41+
head->ptr = 0;
42+
head->next = 0;
43+
total += sizeof(struct node);
44+
}
45+
}
46+
47+
void free_memory(struct node *head)
48+
{
49+
struct node *old_head;
50+
51+
while (head) {
52+
if (head->ptr) {
53+
printf("Free %d Bytes at 0x%p, total %d\n", CHUNK_SIZE, head->ptr, total -= CHUNK_SIZE);
54+
free(head->ptr);
55+
}
56+
57+
if (head->next) {
58+
old_head = head;
59+
head = head->next;
60+
free(old_head);
61+
}
62+
else {
63+
free(head);
64+
head = 0;
65+
}
66+
67+
total -= sizeof(struct node);
68+
}
69+
}
70+
71+
int main(void)
72+
{
73+
struct node *head;
74+
75+
while (1) {
76+
head = malloc(sizeof(struct node));
77+
total += sizeof(struct node);
78+
79+
fill_memory(head);
80+
free_memory(head);
81+
}
82+
83+
return 0;
84+
}

0 commit comments

Comments
 (0)