heap_idf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. /* This file implements the heap related functions that are called by FreeRTOS.
  8. * ESP-IDF provides its own heap containing memory with different capabilities
  9. * (see esp_heap_caps.h). Thus, this file maps a subset of the ESP-IDF heap to
  10. * act as the FreeRTOS heap.
  11. *
  12. * All dynamic allocation done by FreeRTOS should be placed in internal 8-bit
  13. * accessible RAM (i.e., using the MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT flags).
  14. * This is due to the fact that FreeRTOS objects (e.g., task stacks, TCBs,
  15. * queues etc) must be accessible even if the cache is disabled. Therefore, the
  16. * heap that is made available to FreeRTOS for dynamic allocation is a subset of
  17. * the ESP-IDF heap (where all MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT memory is
  18. * made available to FreeRTOS for dynamic allocation).
  19. */
  20. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  21. * all the API functions to use the MPU wrappers. That should only be done when
  22. * task.h is included from an application file. */
  23. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  24. #include "FreeRTOS.h"
  25. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  26. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
  27. #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
  28. #endif
  29. #include "esp_heap_caps.h"
  30. #if !CONFIG_IDF_TARGET_LINUX
  31. /* Memory util functions are not implemented in the Linux simulator */
  32. #include "esp_memory_utils.h"
  33. #endif /* CONFIG_IDF_TARGET_LINUX */
  34. #define portFREERTOS_HEAP_CAPS ( MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT )
  35. /*-----------------------------------------------------------*/
  36. void * pvPortMalloc( size_t xWantedSize )
  37. {
  38. void * pvReturn = NULL;
  39. /* All dynamic allocation done by FreeRTOS goes through this function. If
  40. * users need to allocate FreeRTOS objects into external RAM, they should
  41. * use the "static" equivalents of FreeRTOS API to create FreeRTOS objects
  42. * (e.g., queues). */
  43. pvReturn = heap_caps_malloc( xWantedSize, portFREERTOS_HEAP_CAPS );
  44. return pvReturn;
  45. }
  46. /*-----------------------------------------------------------*/
  47. void vPortFree( void * pv )
  48. {
  49. heap_caps_free( pv );
  50. }
  51. /*-----------------------------------------------------------*/
  52. size_t xPortGetFreeHeapSize( void )
  53. {
  54. return heap_caps_get_free_size( portFREERTOS_HEAP_CAPS );
  55. }
  56. /*-----------------------------------------------------------*/
  57. size_t xPortGetMinimumEverFreeHeapSize( void )
  58. {
  59. return heap_caps_get_minimum_free_size( portFREERTOS_HEAP_CAPS );
  60. }
  61. /*-----------------------------------------------------------*/
  62. bool xPortCheckValidTCBMem( const void * ptr )
  63. {
  64. #if CONFIG_IDF_TARGET_LINUX
  65. return true;
  66. #else /* CONFIG_IDF_TARGET_LINUX */
  67. return esp_ptr_internal( ptr ) && esp_ptr_byte_accessible( ptr );
  68. #endif /* CONFIG_IDF_TARGET_LINUX */
  69. }
  70. bool xPortcheckValidStackMem( const void * ptr )
  71. {
  72. #if CONFIG_IDF_TARGET_LINUX
  73. return true;
  74. #else /* CONFIG_IDF_TARGET_LINUX */
  75. #ifdef CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  76. return esp_ptr_byte_accessible( ptr );
  77. #else
  78. return esp_ptr_internal( ptr ) && esp_ptr_byte_accessible( ptr );
  79. #endif
  80. #endif /* CONFIG_IDF_TARGET_LINUX */
  81. }