From df0cc5d42c6b6ce77b28b19415a2d25e41fb0b97 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sat, 5 Feb 2011 14:48:24 -0800 Subject: Adding library acorn, with malloc wrapping. --- acorn/Makefile | 18 ++++++++++++++++++ acorn/config.mk | 1 + acorn/include/malloc_wrapper.h | 6 ++++++ acorn/src/malloc_wrapper.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 acorn/Makefile create mode 100644 acorn/config.mk create mode 100644 acorn/include/malloc_wrapper.h create mode 100644 acorn/src/malloc_wrapper.c (limited to 'acorn') diff --git a/acorn/Makefile b/acorn/Makefile new file mode 100644 index 0000000..f445bc6 --- /dev/null +++ b/acorn/Makefile @@ -0,0 +1,18 @@ +TARGET_LIB = libacorn.a + +all: $(TARGET_LIB) + +include $(ROOTDIR)/common.mk +include config.mk +include $(ROOTDIR)/FreeRTOS/config.mk +include $(ROOTDIR)/arch/config.mk +include $(ROOTDIR)/os/config.mk +include $(ROOTDIR)/libc/config.mk +include $(ROOTDIR)/libm/config.mk + +TARGET_SRCS = \ +src/malloc_wrapper.c \ + +include $(ROOTDIR)/target-rules.mk + +clean: clean-generic diff --git a/acorn/config.mk b/acorn/config.mk new file mode 100644 index 0000000..c4bfb1b --- /dev/null +++ b/acorn/config.mk @@ -0,0 +1 @@ +TARGET_INCLUDES += $(ROOTDIR)/acorn/include diff --git a/acorn/include/malloc_wrapper.h b/acorn/include/malloc_wrapper.h new file mode 100644 index 0000000..bbaef9f --- /dev/null +++ b/acorn/include/malloc_wrapper.h @@ -0,0 +1,6 @@ +#ifndef __MALLOC_WRAPPER_H__ +#define __MALLOC_WRAPPER_H__ + +void init_malloc_wrapper(); + +#endif diff --git a/acorn/src/malloc_wrapper.c b/acorn/src/malloc_wrapper.c new file mode 100644 index 0000000..3b5f9df --- /dev/null +++ b/acorn/src/malloc_wrapper.c @@ -0,0 +1,41 @@ +#include +#include +#include + +static xSemaphoreHandle malloc_sem = NULL; +static malloc_t old_malloc; +static realloc_t old_realloc; +static free_t old_free; + +static void * malloc_wrap(size_t s) { + void * r; + xSemaphoreTakeRecursive(malloc_sem, portMAX_DELAY); + r = old_malloc(s); + xSemaphoreGiveRecursive(malloc_sem); + return r; +} + +static void * realloc_wrap(void * p, size_t s) { + void * r; + xSemaphoreTakeRecursive(malloc_sem, portMAX_DELAY); + r = old_realloc(p, s); + xSemaphoreGiveRecursive(malloc_sem); + return r; +} + +static void free_wrap(void * p) { + xSemaphoreTakeRecursive(malloc_sem, portMAX_DELAY); + old_free(p); + xSemaphoreGiveRecursive(malloc_sem); +} + +void init_malloc_wrapper() { + malloc_sem = xSemaphoreCreateRecursiveMutex(); + old_malloc = malloc; + old_realloc = realloc; + old_free = free; + malloc = malloc_wrap; + realloc = realloc_wrap; + free = free_wrap; +} + -- cgit v1.2.3