Verify that malloc/realloc/free aren't called from ISR.
authorAdam Green <adamgr@foo.bar>
Fri, 1 Mar 2013 08:36:13 +0000 (00:36 -0800)
committerAdam Green <adamgr@foo.bar>
Fri, 1 Mar 2013 08:36:13 +0000 (00:36 -0800)
This was previously done with the malloc_lock() routine but this isn't
called from the newlib nano routines used in the new build.  This new
code provides wraps for malloc/realloc/free to first make this check of
the IPSR register and then call the real routines if not being called
from ISR.

build/common.mk
build/mbed_custom.cpp

index b03382f..98a55c0 100755 (executable)
@@ -138,9 +138,7 @@ MRI_WRAPS=
 endif
 
 # Setup wraps to memory allocations routines if we want to tag heap allocations.
-HEAP_WRAPS=
 ifeq "$(HEAP_TAGS)" "1"
-HEAP_WRAPS=,--wrap=malloc,--wrap=realloc,--wrap=free
 DEFINES += -DHEAP_TAGS
 endif
 
@@ -161,7 +159,7 @@ AS_FLAGS += -g3 $(DEVICE_FLAGS)
 
 # Linker Options.
 LDFLAGS = $(DEVICE_FLAGS) -specs=$(BUILD_DIR)/startfile.spec 
-LDFLAGS += -Wl,-Map=$(OUTDIR)/$(PROJECT).map,--cref,--gc-sections,--wrap=_isatty$(MRI_WRAPS)$(HEAP_WRAPS)
+LDFLAGS += -Wl,-Map=$(OUTDIR)/$(PROJECT).map,--cref,--gc-sections,--wrap=_isatty,--wrap=malloc,--wrap=realloc,--wrap=free$(MRI_WRAPS)
 LDFLAGS += -T$(LSCRIPT)  -L $(EXTERNAL_DIR)/gcc/LPC1768
 ifneq "$(NO_FLOAT_SCANF)" "1"
 LDFLAGS += -u _scanf_float
index c884696..a353e3c 100644 (file)
@@ -332,4 +332,36 @@ __attribute__((naked)) void* operator new(size_t size)
     return (void*)1;
 }
 
+#else
+
+/* Wrap memory allocation routines to make sure that they aren't being called from interrupt handler. */
+static void breakOnHeapOpFromInterruptHandler(void)
+{
+    if (__get_IPSR() != 0)
+        __debugbreak();
+}
+
+extern "C" void* __real_malloc(size_t size);
+extern "C" void* __wrap_malloc(size_t size)
+{
+    breakOnHeapOpFromInterruptHandler();
+    return __real_malloc(size);
+}
+
+
+extern "C" void* __real_realloc(void* ptr, size_t size);
+extern "C" void* __wrap_realloc(void* ptr, size_t size)
+{
+    breakOnHeapOpFromInterruptHandler();
+    return __real_realloc(ptr, size);
+}
+
+
+extern "C" void __real_free(void* ptr);
+extern "C" void __wrap_free(void* ptr)
+{
+    breakOnHeapOpFromInterruptHandler();
+    __real_free(ptr);
+}
+
 #endif // HEAP_TAGS