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 MULTI_HEAP_FREERTOS
17 
18 #include "freertos/FreeRTOS.h"
19 
20 #include "sdkconfig.h"
21 #include "esp_rom_sys.h"
22 #if CONFIG_IDF_TARGET_ESP32
23 #include "esp32/rom/ets_sys.h" // will be removed in idf v5.0
24 #elif CONFIG_IDF_TARGET_ESP32S2
25 #include "esp32s2/rom/ets_sys.h"
26 #endif
27 #include <assert.h>
28 
29 typedef portMUX_TYPE multi_heap_lock_t;
30 
31 /* Because malloc/free can happen inside an ISR context,
32    we need to use portmux spinlocks here not RTOS mutexes */
33 #define MULTI_HEAP_LOCK(PLOCK) do {                         \
34         if((PLOCK) != NULL) {                               \
35             portENTER_CRITICAL((PLOCK));                    \
36         }                                                   \
37     } while(0)
38 
39 
40 #define MULTI_HEAP_UNLOCK(PLOCK) do {                       \
41         if ((PLOCK) != NULL) {                              \
42             portEXIT_CRITICAL((PLOCK));                     \
43         }                                                   \
44     } while(0)
45 
46 #define MULTI_HEAP_LOCK_INIT(PLOCK) do {                    \
47         vPortCPUInitializeMutex((PLOCK));                   \
48     } while(0)
49 
50 #define MULTI_HEAP_LOCK_STATIC_INITIALIZER     portMUX_INITIALIZER_UNLOCKED
51 
52 /* Not safe to use std i/o while in a portmux critical section,
53    can deadlock, so we use the ROM equivalent functions. */
54 
55 #define MULTI_HEAP_PRINTF esp_rom_printf
56 #define MULTI_HEAP_STDERR_PRINTF(MSG, ...) esp_rom_printf(MSG, __VA_ARGS__)
57 
multi_heap_assert(bool condition,const char * format,int line,intptr_t address)58 inline static void multi_heap_assert(bool condition, const char *format, int line, intptr_t address)
59 {
60     /* Can't use libc assert() here as it calls printf() which can cause another malloc() for a newlib lock.
61 
62        Also, it's useful to be able to print the memory address where corruption was detected.
63     */
64 #ifndef NDEBUG
65     if(!condition) {
66 #ifndef CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
67         esp_rom_printf(format, line, address);
68 #endif  // CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
69         abort();
70     }
71 #else // NDEBUG
72     (void) condition;
73 #endif // NDEBUG
74 }
75 
76 #define MULTI_HEAP_ASSERT(CONDITION, ADDRESS) \
77     multi_heap_assert((CONDITION), "CORRUPT HEAP: multi_heap.c:%d detected at 0x%08x\n", \
78                       __LINE__, (intptr_t)(ADDRESS))
79 
80 #ifdef CONFIG_HEAP_TASK_TRACKING
81 #include <freertos/task.h>
82 #define MULTI_HEAP_BLOCK_OWNER TaskHandle_t task;
83 #define MULTI_HEAP_SET_BLOCK_OWNER(HEAD) (HEAD)->task = xTaskGetCurrentTaskHandle()
84 #define MULTI_HEAP_GET_BLOCK_OWNER(HEAD) ((HEAD)->task)
85 #else
86 #define MULTI_HEAP_BLOCK_OWNER
87 #define MULTI_HEAP_SET_BLOCK_OWNER(HEAD)
88 #define MULTI_HEAP_GET_BLOCK_OWNER(HEAD) (NULL)
89 #endif
90 
91 #else // MULTI_HEAP_FREERTOS
92 
93 #include <assert.h>
94 
95 #define MULTI_HEAP_PRINTF printf
96 #define MULTI_HEAP_STDERR_PRINTF(MSG, ...) fprintf(stderr, MSG, __VA_ARGS__)
97 #define MULTI_HEAP_LOCK(PLOCK)  (void) (PLOCK)
98 #define MULTI_HEAP_UNLOCK(PLOCK)  (void) (PLOCK)
99 #define MULTI_HEAP_LOCK_INIT(PLOCK)  (void) (PLOCK)
100 #define MULTI_HEAP_LOCK_STATIC_INITIALIZER  0
101 
102 #define MULTI_HEAP_ASSERT(CONDITION, ADDRESS) assert((CONDITION) && "Heap corrupt")
103 
104 #define MULTI_HEAP_BLOCK_OWNER
105 #define MULTI_HEAP_SET_BLOCK_OWNER(HEAD)
106 #define MULTI_HEAP_GET_BLOCK_OWNER(HEAD) (NULL)
107 
108 #endif // MULTI_HEAP_FREERTOS
109