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