diff --git a/tests/malloc/Makefile b/tests/malloc/Makefile new file mode 100644 index 0000000000..ccc0417173 --- /dev/null +++ b/tests/malloc/Makefile @@ -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 diff --git a/tests/malloc/main.c b/tests/malloc/main.c new file mode 100644 index 0000000000..0af810345e --- /dev/null +++ b/tests/malloc/main.c @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2013 Benjamin Valentin + * + * 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 + * + * @} + */ + +#include +#include +#include + +#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; +}