heap_2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. * A sample implementation of pvPortMalloc() and vPortFree() that permits
  30. * allocated blocks to be freed, but does not combine adjacent free blocks
  31. * into a single larger block (and so will fragment memory). See heap_4.c for
  32. * an equivalent that does combine adjacent blocks into single larger blocks.
  33. *
  34. * See heap_1.c, heap_3.c and heap_4.c for alternative implementations, and the
  35. * memory management pages of https://www.FreeRTOS.org for more information.
  36. */
  37. #include <stdlib.h>
  38. #include "FreeRTOS.h"
  39. #include "task.h"
  40. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
  41. #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
  42. #endif
  43. /* A few bytes might be lost to byte aligning the heap start address. */
  44. #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
  45. /*
  46. * Initialises the heap structures before their first use.
  47. */
  48. static void prvHeapInit( void );
  49. /* Allocate the memory for the heap. */
  50. #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
  51. /* The application writer has already defined the array used for the RTOS
  52. * heap - probably so it can be placed in a special segment or address. */
  53. extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  54. #else
  55. static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  56. #endif /* configAPPLICATION_ALLOCATED_HEAP */
  57. /* Define the linked list structure. This is used to link free blocks in order
  58. * of their size. */
  59. typedef struct A_BLOCK_LINK
  60. {
  61. struct A_BLOCK_LINK * pxNextFreeBlock; /*<< The next free block in the list. */
  62. size_t xBlockSize; /*<< The size of the free block. */
  63. } BlockLink_t;
  64. static const uint16_t heapSTRUCT_SIZE = ( ( sizeof( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );
  65. #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
  66. /* Create a couple of list links to mark the start and end of the list. */
  67. static BlockLink_t xStart, xEnd;
  68. /* Keeps track of the number of free bytes remaining, but says nothing about
  69. * fragmentation. */
  70. static size_t xFreeBytesRemaining = configADJUSTED_HEAP_SIZE;
  71. /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
  72. /*
  73. * Insert a block into the list of free blocks - which is ordered by size of
  74. * the block. Small blocks at the start of the list and large blocks at the end
  75. * of the list.
  76. */
  77. #define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
  78. { \
  79. BlockLink_t * pxIterator; \
  80. size_t xBlockSize; \
  81. \
  82. xBlockSize = pxBlockToInsert->xBlockSize; \
  83. \
  84. /* Iterate through the list until a block is found that has a larger size */ \
  85. /* than the block we are inserting. */ \
  86. for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
  87. { \
  88. /* There is nothing to do here - just iterate to the correct position. */ \
  89. } \
  90. \
  91. /* Update the list to include the block being inserted in the correct */ \
  92. /* position. */ \
  93. pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
  94. pxIterator->pxNextFreeBlock = pxBlockToInsert; \
  95. }
  96. /*-----------------------------------------------------------*/
  97. void * pvPortMalloc( size_t xWantedSize )
  98. {
  99. BlockLink_t * pxBlock, * pxPreviousBlock, * pxNewBlockLink;
  100. static BaseType_t xHeapHasBeenInitialised = pdFALSE;
  101. void * pvReturn = NULL;
  102. vTaskSuspendAll();
  103. {
  104. /* If this is the first call to malloc then the heap will require
  105. * initialisation to setup the list of free blocks. */
  106. if( xHeapHasBeenInitialised == pdFALSE )
  107. {
  108. prvHeapInit();
  109. xHeapHasBeenInitialised = pdTRUE;
  110. }
  111. /* The wanted size must be increased so it can contain a BlockLink_t
  112. * structure in addition to the requested amount of bytes. */
  113. if( ( xWantedSize > 0 ) &&
  114. ( ( xWantedSize + heapSTRUCT_SIZE ) > xWantedSize ) ) /* Overflow check */
  115. {
  116. xWantedSize += heapSTRUCT_SIZE;
  117. /* Byte alignment required. Check for overflow. */
  118. if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) )
  119. > xWantedSize )
  120. {
  121. xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
  122. configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
  123. }
  124. else
  125. {
  126. xWantedSize = 0;
  127. }
  128. }
  129. else
  130. {
  131. xWantedSize = 0;
  132. }
  133. if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
  134. {
  135. /* Blocks are stored in byte order - traverse the list from the start
  136. * (smallest) block until one of adequate size is found. */
  137. pxPreviousBlock = &xStart;
  138. pxBlock = xStart.pxNextFreeBlock;
  139. while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
  140. {
  141. pxPreviousBlock = pxBlock;
  142. pxBlock = pxBlock->pxNextFreeBlock;
  143. }
  144. /* If we found the end marker then a block of adequate size was not found. */
  145. if( pxBlock != &xEnd )
  146. {
  147. /* Return the memory space - jumping over the BlockLink_t structure
  148. * at its start. */
  149. pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
  150. /* This block is being returned for use so must be taken out of the
  151. * list of free blocks. */
  152. pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
  153. /* If the block is larger than required it can be split into two. */
  154. if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
  155. {
  156. /* This block is to be split into two. Create a new block
  157. * following the number of bytes requested. The void cast is
  158. * used to prevent byte alignment warnings from the compiler. */
  159. pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
  160. /* Calculate the sizes of two blocks split from the single
  161. * block. */
  162. pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
  163. pxBlock->xBlockSize = xWantedSize;
  164. /* Insert the new block into the list of free blocks. */
  165. prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
  166. }
  167. xFreeBytesRemaining -= pxBlock->xBlockSize;
  168. }
  169. }
  170. }
  171. ( void ) xTaskResumeAll();
  172. #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
  173. {
  174. if( pvReturn == NULL )
  175. {
  176. extern void vApplicationMallocFailedHook( void );
  177. vApplicationMallocFailedHook();
  178. }
  179. }
  180. #endif
  181. return pvReturn;
  182. }
  183. /*-----------------------------------------------------------*/
  184. void vPortFree( void * pv )
  185. {
  186. uint8_t * puc = ( uint8_t * ) pv;
  187. BlockLink_t * pxLink;
  188. if( pv != NULL )
  189. {
  190. /* The memory being freed will have an BlockLink_t structure immediately
  191. * before it. */
  192. puc -= heapSTRUCT_SIZE;
  193. /* This unexpected casting is to keep some compilers from issuing
  194. * byte alignment warnings. */
  195. pxLink = ( void * ) puc;
  196. vTaskSuspendAll();
  197. {
  198. /* Add this block to the list of free blocks. */
  199. prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
  200. xFreeBytesRemaining += pxLink->xBlockSize;
  201. }
  202. ( void ) xTaskResumeAll();
  203. }
  204. }
  205. /*-----------------------------------------------------------*/
  206. size_t xPortGetFreeHeapSize( void )
  207. {
  208. return xFreeBytesRemaining;
  209. }
  210. /*-----------------------------------------------------------*/
  211. void vPortInitialiseBlocks( void )
  212. {
  213. /* This just exists to keep the linker quiet. */
  214. }
  215. /*-----------------------------------------------------------*/
  216. static void prvHeapInit( void )
  217. {
  218. BlockLink_t * pxFirstFreeBlock;
  219. uint8_t * pucAlignedHeap;
  220. /* Ensure the heap starts on a correctly aligned boundary. */
  221. pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT - 1 ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
  222. /* xStart is used to hold a pointer to the first item in the list of free
  223. * blocks. The void cast is used to prevent compiler warnings. */
  224. xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
  225. xStart.xBlockSize = ( size_t ) 0;
  226. /* xEnd is used to mark the end of the list of free blocks. */
  227. xEnd.xBlockSize = configADJUSTED_HEAP_SIZE;
  228. xEnd.pxNextFreeBlock = NULL;
  229. /* To start with there is a single free block that is sized to take up the
  230. * entire heap space. */
  231. pxFirstFreeBlock = ( void * ) pucAlignedHeap;
  232. pxFirstFreeBlock->xBlockSize = configADJUSTED_HEAP_SIZE;
  233. pxFirstFreeBlock->pxNextFreeBlock = &xEnd;
  234. }
  235. /*-----------------------------------------------------------*/