summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2011-02-05 14:48:24 -0800
committerPixel <pixel@nobis-crew.org>2011-02-05 14:48:24 -0800
commitdf0cc5d42c6b6ce77b28b19415a2d25e41fb0b97 (patch)
tree14bfc476ae305467a1990be3f19679ac7434ff19
parentd257cf29744f732c4d9b1a741a26164940e87b90 (diff)
Adding library acorn, with malloc wrapping.
-rw-r--r--Makefile16
-rw-r--r--README.md5
-rw-r--r--acorn/Makefile18
-rw-r--r--acorn/config.mk1
-rw-r--r--acorn/include/malloc_wrapper.h6
-rw-r--r--acorn/src/malloc_wrapper.c41
-rw-r--r--libc/include/malloc.h2
7 files changed, 84 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index 6a4f3d4..b1294f8 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
TARGET = demo.bin
-LIBDEPS = FreeRTOS/libFreeRTOS.a arch/libarch.a os/libos.a libc/libc.a libm/libm.a
+LIBDEPS = FreeRTOS/libFreeRTOS.a arch/libarch.a os/libos.a libc/libc.a libm/libm.a acorn/libacorn.a
LIBS = -Wl,--start-group $(LIBDEPS) -Wl,--end-group
TARGET_SRCS = test-romfs.o
@@ -16,18 +16,20 @@ clean: clean-generic
$(Q)$(MAKE) $(MAKE_OPTS) -C os clean
$(Q)$(MAKE) $(MAKE_OPTS) -C libc clean
$(Q)$(MAKE) $(MAKE_OPTS) -C libm clean
+ $(Q)$(MAKE) $(MAKE_OPTS) -C acorn clean
$(Q)$(MAKE) $(MAKE_OPTS) -C tools clean
$(Q)rm -f test-romfs.bin
-.PHONY: libs FreeRTOS arch os libc libm tools deps
+.PHONY: libs FreeRTOS arch os libc libm acorn tools deps
FreeRTOS/libFreeRTOS.a: FreeRTOS
arch/libarch.a: arch
os/libos.a: os
libc/libc.a: libc
-libm/libm/a: libm
+libm/libm.a: libm
+acorn/libacorn.a: acorn
-libs: FreeRTOS arch os libc libm
+libs: FreeRTOS arch os libc libm acorn
FreeRTOS:
$(E) "[MAKE] Entering FreeRTOS"
@@ -49,6 +51,10 @@ libm:
$(E) "[MAKE] Entering libm"
$(Q)$(MAKE) $(MAKE_OPTS) -C libm
+acorn:
+ $(E) "[MAKE] Entering acorn"
+ $(Q)$(MAKE) $(MAKE_OPTS) -C acorn
+
tools:
$(E) "[MAKE] Entering tools"
$(Q)$(MAKE) $(MAKE_OPTS) -C tools
@@ -70,6 +76,8 @@ deps: ldeps
$(Q)$(MAKE) $(MAKE_OPTS) -C libc ldeps
$(E) "[DEPS] Creating dependency tree for libm"
$(Q)$(MAKE) $(MAKE_OPTS) -C libm ldeps
+ $(E) "[DEPS] Creating dependency tree for acorn"
+ $(Q)$(MAKE) $(MAKE_OPTS) -C acorn ldeps
include FreeRTOS/config.mk
include arch/config.mk
diff --git a/README.md b/README.md
index 62b42ba..ce77a0d 100644
--- a/README.md
+++ b/README.md
@@ -47,6 +47,11 @@ is being done, unlike a normal libm). Be careful though, as most uC processors
dont contain any FPU. Using any kind of math will greatly increase the code
size, as well as CPU usage while processing.
+acorn:
+
+This will be the library containing utilities for embedded development, such as
+generic i2c access, malloc wrapping for RTOS multithreading, etc.
+
ARM / Cortex-M3 / mbed
----------------------
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 <malloc.h>
+#include <FreeRTOS.h>
+#include <semphr.h>
+
+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;
+}
+
diff --git a/libc/include/malloc.h b/libc/include/malloc.h
index 715c90b..58e0481 100644
--- a/libc/include/malloc.h
+++ b/libc/include/malloc.h
@@ -20,7 +20,7 @@ extern realloc_t realloc;
static inline void * calloc(size_t nmemb, size_t size) {
uint8_t * r = malloc(nmemb * size);
size_t i;
- for (i = 0; i < size; i++)
+ for (i = 0; i < (size * nmemb); i++)
r[i] = 0;
return r;
}