multi_heap_platform.h 3.1 KB

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