multi_heap_platform.h 3.1 KB

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