1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #ifdef ESP_PLATFORM
17 
18 #include <freertos/FreeRTOS.h>
19 #include <rom/ets_sys.h>
20 #include <assert.h>
21 
22 /* Because malloc/free can happen inside an ISR context,
23    we need to use portmux spinlocks here not RTOS mutexes */
24 #define MULTI_HEAP_LOCK(PLOCK) do {               \
25         if((PLOCK) != NULL) {                               \
26             portENTER_CRITICAL((portMUX_TYPE *)(PLOCK));    \
27         }                                                   \
28     } while(0)
29 
30 
31 #define MULTI_HEAP_UNLOCK(PLOCK) do {                \
32         if ((PLOCK) != NULL) {                              \
33             portEXIT_CRITICAL((portMUX_TYPE *)(PLOCK));     \
34         }                                                   \
35     } while(0)
36 
37 /* Not safe to use std i/o while in a portmux critical section,
38    can deadlock, so we use the ROM equivalent functions. */
39 
40 #define MULTI_HEAP_PRINTF ets_printf
41 #define MULTI_HEAP_STDERR_PRINTF(MSG, ...) ets_printf(MSG, __VA_ARGS__)
42 
multi_heap_assert(bool condition,const char * format,int line,intptr_t address)43 inline static void multi_heap_assert(bool condition, const char *format, int line, intptr_t address)
44 {
45     /* Can't use libc assert() here as it calls printf() which can cause another malloc() for a newlib lock.
46 
47        Also, it's useful to be able to print the memory address where corruption was detected.
48     */
49 #ifndef NDEBUG
50     if(!condition) {
51 #ifndef CONFIG_OPTIMIZATION_ASSERTIONS_SILENT
52         ets_printf(format, line, address);
53 #endif  // CONFIG_OPTIMIZATION_ASSERTIONS_SILENT
54         abort();
55     }
56 #else // NDEBUG
57     (void) condition;
58 #endif // NDEBUG
59 }
60 
61 #define MULTI_HEAP_ASSERT(CONDITION, ADDRESS) \
62     multi_heap_assert((CONDITION), "CORRUPT HEAP: multi_heap.c:%d detected at 0x%08x\n", \
63                       __LINE__, (intptr_t)(ADDRESS))
64 
65 #ifdef CONFIG_HEAP_TASK_TRACKING
66 #include <freertos/task.h>
67 #define MULTI_HEAP_BLOCK_OWNER TaskHandle_t task;
68 #define MULTI_HEAP_SET_BLOCK_OWNER(HEAD) (HEAD)->task = xTaskGetCurrentTaskHandle()
69 #define MULTI_HEAP_GET_BLOCK_OWNER(HEAD) ((HEAD)->task)
70 #else
71 #define MULTI_HEAP_BLOCK_OWNER
72 #define MULTI_HEAP_SET_BLOCK_OWNER(HEAD)
73 #define MULTI_HEAP_GET_BLOCK_OWNER(HEAD) (NULL)
74 #endif
75 
76 #else // ESP_PLATFORM
77 #include "stdint.h"
78 #include "cmsis.h"
79 #include "assert.h"
80 #include "hal_trace.h"
81 
82 #define MULTI_HEAP_PRINTF hal_trace_printf
83 #define MULTI_HEAP_STDERR_PRINTF(MSG, ...) hal_trace_printf(MSG, __VA_ARGS__)
84 #define MULTI_HEAP_LOCK(PLOCK)  do {if((PLOCK) != NULL) { uint32_t lockd = int_lock(); *((uint32_t *)(PLOCK)) = lockd; }} while (0)
85 #define MULTI_HEAP_UNLOCK(PLOCK) do {if((PLOCK) != NULL) { int_unlock(*(((uint32_t *)(PLOCK))));}} while(0)
86 
87 #define MULTI_HEAP_ASSERT(CONDITION, ADDRESS) do {if (!(CONDITION)) {assert(0 && "Heap corrupt");} } while (0)
88 
89 #define MULTI_HEAP_BLOCK_OWNER
90 #define MULTI_HEAP_SET_BLOCK_OWNER(HEAD)
91 #define MULTI_HEAP_GET_BLOCK_OWNER(HEAD) (NULL)
92 
93 #endif
94