multi_heap_platform.h 3.3 KB

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