heap_1.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /*
  29. * The simplest possible implementation of pvPortMalloc(). Note that this
  30. * implementation does NOT allow allocated memory to be freed again.
  31. *
  32. * See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the
  33. * memory management pages of https://www.FreeRTOS.org for more information.
  34. */
  35. #include <stdlib.h>
  36. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  37. * all the API functions to use the MPU wrappers. That should only be done when
  38. * task.h is included from an application file. */
  39. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  40. #include "FreeRTOS.h"
  41. #include "task.h"
  42. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  43. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
  44. #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
  45. #endif
  46. /* A few bytes might be lost to byte aligning the heap start address. */
  47. #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
  48. /* Allocate the memory for the heap. */
  49. #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
  50. /* The application writer has already defined the array used for the RTOS
  51. * heap - probably so it can be placed in a special segment or address. */
  52. extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  53. #else
  54. static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  55. #endif /* configAPPLICATION_ALLOCATED_HEAP */
  56. /* Index into the ucHeap array. */
  57. static size_t xNextFreeByte = ( size_t ) 0;
  58. /*-----------------------------------------------------------*/
  59. void * pvPortMalloc( size_t xWantedSize )
  60. {
  61. void * pvReturn = NULL;
  62. static uint8_t * pucAlignedHeap = NULL;
  63. /* Ensure that blocks are always aligned. */
  64. #if ( portBYTE_ALIGNMENT != 1 )
  65. {
  66. if( xWantedSize & portBYTE_ALIGNMENT_MASK )
  67. {
  68. /* Byte alignment required. Check for overflow. */
  69. if ( (xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) )) > xWantedSize )
  70. {
  71. xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
  72. }
  73. else
  74. {
  75. xWantedSize = 0;
  76. }
  77. }
  78. }
  79. #endif
  80. vTaskSuspendAll();
  81. {
  82. if( pucAlignedHeap == NULL )
  83. {
  84. /* Ensure the heap starts on a correctly aligned boundary. */
  85. pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT - 1 ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
  86. }
  87. /* Check there is enough room left for the allocation and. */
  88. if( ( xWantedSize > 0 ) && /* valid size */
  89. ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
  90. ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) ) /* Check for overflow. */
  91. {
  92. /* Return the next free byte then increment the index past this
  93. * block. */
  94. pvReturn = pucAlignedHeap + xNextFreeByte;
  95. xNextFreeByte += xWantedSize;
  96. }
  97. traceMALLOC( pvReturn, xWantedSize );
  98. }
  99. ( void ) xTaskResumeAll();
  100. #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
  101. {
  102. if( pvReturn == NULL )
  103. {
  104. extern void vApplicationMallocFailedHook( void );
  105. vApplicationMallocFailedHook();
  106. }
  107. }
  108. #endif
  109. return pvReturn;
  110. }
  111. /*-----------------------------------------------------------*/
  112. void vPortFree( void * pv )
  113. {
  114. /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
  115. * heap_4.c for alternative implementations, and the memory management pages of
  116. * https://www.FreeRTOS.org for more information. */
  117. ( void ) pv;
  118. /* Force an assert as it is invalid to call this function. */
  119. configASSERT( pv == NULL );
  120. }
  121. /*-----------------------------------------------------------*/
  122. void vPortInitialiseBlocks( void )
  123. {
  124. /* Only required when static memory is not cleared. */
  125. xNextFreeByte = ( size_t ) 0;
  126. }
  127. /*-----------------------------------------------------------*/
  128. size_t xPortGetFreeHeapSize( void )
  129. {
  130. return( configADJUSTED_HEAP_SIZE - xNextFreeByte );
  131. }