heap_2.c 12 KB

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