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