multi_heap_platform.h 2.6 KB

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