From: Adam Green Date: Fri, 1 Mar 2013 08:36:13 +0000 (-0800) Subject: Verify that malloc/realloc/free aren't called from ISR. X-Git-Url: http://git.hcoop.net/clinton/Smoothieware.git/commitdiff_plain/b4e5c8301e9b4d4b74e72d0f8330d944032a4b5e Verify that malloc/realloc/free aren't called from ISR. 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. --- diff --git a/build/common.mk b/build/common.mk index b03382f5..98a55c09 100755 --- a/build/common.mk +++ b/build/common.mk @@ -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 diff --git a/build/mbed_custom.cpp b/build/mbed_custom.cpp index c884696e..a353e3c4 100644 --- a/build/mbed_custom.cpp +++ b/build/mbed_custom.cpp @@ -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