multi_heap_platform.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #ifdef MULTI_HEAP_FREERTOS
  15. #include "freertos/FreeRTOS.h"
  16. #include "sdkconfig.h"
  17. #include "esp_rom_sys.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #include "esp32/rom/ets_sys.h" // will be removed in idf v5.0
  20. #elif CONFIG_IDF_TARGET_ESP32S2
  21. #include "esp32s2/rom/ets_sys.h"
  22. #endif
  23. #include <assert.h>
  24. typedef portMUX_TYPE multi_heap_lock_t;
  25. /* Because malloc/free can happen inside an ISR context,
  26. we need to use portmux spinlocks here not RTOS mutexes */
  27. #define MULTI_HEAP_LOCK(PLOCK) do { \
  28. if((PLOCK) != NULL) { \
  29. portENTER_CRITICAL((PLOCK)); \
  30. } \
  31. } while(0)
  32. #define MULTI_HEAP_UNLOCK(PLOCK) do { \
  33. if ((PLOCK) != NULL) { \
  34. portEXIT_CRITICAL((PLOCK)); \
  35. } \
  36. } while(0)
  37. #define MULTI_HEAP_LOCK_INIT(PLOCK) do { \
  38. vPortCPUInitializeMutex((PLOCK)); \
  39. } while(0)
  40. #define MULTI_HEAP_LOCK_STATIC_INITIALIZER portMUX_INITIALIZER_UNLOCKED
  41. /* Not safe to use std i/o while in a portmux critical section,
  42. can deadlock, so we use the ROM equivalent functions. */
  43. #define MULTI_HEAP_PRINTF esp_rom_printf
  44. #define MULTI_HEAP_STDERR_PRINTF(MSG, ...) esp_rom_printf(MSG, __VA_ARGS__)
  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. Also, it's useful to be able to print the memory address where corruption was detected.
  49. */
  50. #ifndef NDEBUG
  51. if(!condition) {
  52. #ifndef CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
  53. esp_rom_printf(format, line, address);
  54. #endif // CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
  55. abort();
  56. }
  57. #else // NDEBUG
  58. (void) condition;
  59. #endif // NDEBUG
  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. #ifdef CONFIG_HEAP_TASK_TRACKING
  65. #include <freertos/task.h>
  66. #define MULTI_HEAP_BLOCK_OWNER TaskHandle_t task;
  67. #define MULTI_HEAP_SET_BLOCK_OWNER(HEAD) (HEAD)->task = xTaskGetCurrentTaskHandle()
  68. #define MULTI_HEAP_GET_BLOCK_OWNER(HEAD) ((HEAD)->task)
  69. #else
  70. #define MULTI_HEAP_BLOCK_OWNER
  71. #define MULTI_HEAP_SET_BLOCK_OWNER(HEAD)
  72. #define MULTI_HEAP_GET_BLOCK_OWNER(HEAD) (NULL)
  73. #endif
  74. #else // MULTI_HEAP_FREERTOS
  75. #include <assert.h>
  76. #define MULTI_HEAP_PRINTF printf
  77. #define MULTI_HEAP_STDERR_PRINTF(MSG, ...) fprintf(stderr, MSG, __VA_ARGS__)
  78. #define MULTI_HEAP_LOCK(PLOCK) (void) (PLOCK)
  79. #define MULTI_HEAP_UNLOCK(PLOCK) (void) (PLOCK)
  80. #define MULTI_HEAP_LOCK_INIT(PLOCK) (void) (PLOCK)
  81. #define MULTI_HEAP_LOCK_STATIC_INITIALIZER 0
  82. #define MULTI_HEAP_ASSERT(CONDITION, ADDRESS) assert((CONDITION) && "Heap corrupt")
  83. #define MULTI_HEAP_BLOCK_OWNER
  84. #define MULTI_HEAP_SET_BLOCK_OWNER(HEAD)
  85. #define MULTI_HEAP_GET_BLOCK_OWNER(HEAD) (NULL)
  86. #endif // MULTI_HEAP_FREERTOS