multi_heap_platform.h 3.2 KB

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