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