heap_4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * FreeRTOS Kernel V10.2.1
  3. * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://www.FreeRTOS.org
  23. * http://aws.amazon.com/freertos
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. /*
  28. * A sample implementation of pvPortMalloc() and vPortFree() that combines
  29. * (coalescences) adjacent memory blocks as they are freed, and in so doing
  30. * limits memory fragmentation.
  31. *
  32. * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
  33. * memory management pages of http://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. /* Block sizes must not get too small. */
  47. #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
  48. /* Assumes 8bit bytes! */
  49. #define heapBITS_PER_BYTE ( ( size_t ) 8 )
  50. /* Allocate the memory for the heap. */
  51. #if( configAPPLICATION_ALLOCATED_HEAP == 1 )
  52. /* The application writer has already defined the array used for the RTOS
  53. heap - probably so it can be placed in a special segment or address. */
  54. extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  55. #else
  56. static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  57. #endif /* configAPPLICATION_ALLOCATED_HEAP */
  58. /* Define the linked list structure. This is used to link free blocks in order
  59. of their memory address. */
  60. typedef struct A_BLOCK_LINK
  61. {
  62. struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
  63. size_t xBlockSize; /*<< The size of the free block. */
  64. } BlockLink_t;
  65. /*-----------------------------------------------------------*/
  66. /*
  67. * Inserts a block of memory that is being freed into the correct position in
  68. * the list of free memory blocks. The block being freed will be merged with
  69. * the block in front it and/or the block behind it if the memory blocks are
  70. * adjacent to each other.
  71. */
  72. static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
  73. /*
  74. * Called automatically to setup the required heap structures the first time
  75. * pvPortMalloc() is called.
  76. */
  77. static void prvHeapInit( void );
  78. /*-----------------------------------------------------------*/
  79. /* The size of the structure placed at the beginning of each allocated memory
  80. block must by correctly byte aligned. */
  81. static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
  82. /* Create a couple of list links to mark the start and end of the list. */
  83. static BlockLink_t xStart, *pxEnd = NULL;
  84. /* Keeps track of the number of free bytes remaining, but says nothing about
  85. fragmentation. */
  86. static size_t xFreeBytesRemaining = 0U;
  87. static size_t xMinimumEverFreeBytesRemaining = 0U;
  88. /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
  89. member of an BlockLink_t structure is set then the block belongs to the
  90. application. When the bit is free the block is still part of the free heap
  91. space. */
  92. static size_t xBlockAllocatedBit = 0;
  93. /*-----------------------------------------------------------*/
  94. void *pvPortMalloc( size_t xWantedSize )
  95. {
  96. BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
  97. void *pvReturn = NULL;
  98. vTaskSuspendAll();
  99. {
  100. /* If this is the first call to malloc then the heap will require
  101. initialisation to setup the list of free blocks. */
  102. if( pxEnd == NULL )
  103. {
  104. prvHeapInit();
  105. }
  106. else
  107. {
  108. mtCOVERAGE_TEST_MARKER();
  109. }
  110. /* Check the requested block size is not so large that the top bit is
  111. set. The top bit of the block size member of the BlockLink_t structure
  112. is used to determine who owns the block - the application or the
  113. kernel, so it must be free. */
  114. if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
  115. {
  116. /* The wanted size is increased so it can contain a BlockLink_t
  117. structure in addition to the requested amount of bytes. */
  118. if( xWantedSize > 0 )
  119. {
  120. xWantedSize += xHeapStructSize;
  121. /* Ensure that blocks are always aligned to the required number
  122. of bytes. */
  123. if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
  124. {
  125. /* Byte alignment required. */
  126. xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
  127. configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
  128. }
  129. else
  130. {
  131. mtCOVERAGE_TEST_MARKER();
  132. }
  133. }
  134. else
  135. {
  136. mtCOVERAGE_TEST_MARKER();
  137. }
  138. if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
  139. {
  140. /* Traverse the list from the start (lowest address) block until
  141. 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 the end marker was reached then a block of adequate size
  150. was not found. */
  151. if( pxBlock != pxEnd )
  152. {
  153. /* Return the memory space pointed to - jumping over the
  154. BlockLink_t structure at its start. */
  155. pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
  156. /* This block is being returned for use so must be taken out
  157. of the list of free blocks. */
  158. pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
  159. /* If the block is larger than required it can be split into
  160. two. */
  161. if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
  162. {
  163. /* This block is to be split into two. Create a new
  164. block following the number of bytes requested. The void
  165. cast is used to prevent byte alignment warnings from the
  166. compiler. */
  167. pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
  168. configASSERT( ( ( ( size_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );
  169. /* Calculate the sizes of two blocks split from the
  170. single block. */
  171. pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
  172. pxBlock->xBlockSize = xWantedSize;
  173. /* Insert the new block into the list of free blocks. */
  174. prvInsertBlockIntoFreeList( pxNewBlockLink );
  175. }
  176. else
  177. {
  178. mtCOVERAGE_TEST_MARKER();
  179. }
  180. xFreeBytesRemaining -= pxBlock->xBlockSize;
  181. if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
  182. {
  183. xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
  184. }
  185. else
  186. {
  187. mtCOVERAGE_TEST_MARKER();
  188. }
  189. /* The block is being returned - it is allocated and owned
  190. by the application and has no "next" block. */
  191. pxBlock->xBlockSize |= xBlockAllocatedBit;
  192. pxBlock->pxNextFreeBlock = NULL;
  193. }
  194. else
  195. {
  196. mtCOVERAGE_TEST_MARKER();
  197. }
  198. }
  199. else
  200. {
  201. mtCOVERAGE_TEST_MARKER();
  202. }
  203. }
  204. else
  205. {
  206. mtCOVERAGE_TEST_MARKER();
  207. }
  208. traceMALLOC( pvReturn, xWantedSize );
  209. }
  210. ( void ) xTaskResumeAll();
  211. #if( configUSE_MALLOC_FAILED_HOOK == 1 )
  212. {
  213. if( pvReturn == NULL )
  214. {
  215. extern void vApplicationMallocFailedHook( void );
  216. vApplicationMallocFailedHook();
  217. }
  218. else
  219. {
  220. mtCOVERAGE_TEST_MARKER();
  221. }
  222. }
  223. #endif
  224. configASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) portBYTE_ALIGNMENT_MASK ) == 0 );
  225. return pvReturn;
  226. }
  227. /*-----------------------------------------------------------*/
  228. void vPortFree( void *pv )
  229. {
  230. uint8_t *puc = ( uint8_t * ) pv;
  231. BlockLink_t *pxLink;
  232. if( pv != NULL )
  233. {
  234. /* The memory being freed will have an BlockLink_t structure immediately
  235. before it. */
  236. puc -= xHeapStructSize;
  237. /* This casting is to keep the compiler from issuing warnings. */
  238. pxLink = ( void * ) puc;
  239. /* Check the block is actually allocated. */
  240. configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
  241. configASSERT( pxLink->pxNextFreeBlock == NULL );
  242. if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
  243. {
  244. if( pxLink->pxNextFreeBlock == NULL )
  245. {
  246. /* The block is being returned to the heap - it is no longer
  247. allocated. */
  248. pxLink->xBlockSize &= ~xBlockAllocatedBit;
  249. vTaskSuspendAll();
  250. {
  251. /* Add this block to the list of free blocks. */
  252. xFreeBytesRemaining += pxLink->xBlockSize;
  253. traceFREE( pv, pxLink->xBlockSize );
  254. prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
  255. }
  256. ( void ) xTaskResumeAll();
  257. }
  258. else
  259. {
  260. mtCOVERAGE_TEST_MARKER();
  261. }
  262. }
  263. else
  264. {
  265. mtCOVERAGE_TEST_MARKER();
  266. }
  267. }
  268. }
  269. /*-----------------------------------------------------------*/
  270. size_t xPortGetFreeHeapSize( void )
  271. {
  272. return xFreeBytesRemaining;
  273. }
  274. /*-----------------------------------------------------------*/
  275. size_t xPortGetMinimumEverFreeHeapSize( void )
  276. {
  277. return xMinimumEverFreeBytesRemaining;
  278. }
  279. /*-----------------------------------------------------------*/
  280. void vPortInitialiseBlocks( void )
  281. {
  282. /* This just exists to keep the linker quiet. */
  283. }
  284. /*-----------------------------------------------------------*/
  285. static void prvHeapInit( void )
  286. {
  287. BlockLink_t *pxFirstFreeBlock;
  288. uint8_t *pucAlignedHeap;
  289. size_t uxAddress;
  290. size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
  291. /* Ensure the heap starts on a correctly aligned boundary. */
  292. uxAddress = ( size_t ) ucHeap;
  293. if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
  294. {
  295. uxAddress += ( portBYTE_ALIGNMENT - 1 );
  296. uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
  297. xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;
  298. }
  299. pucAlignedHeap = ( uint8_t * ) uxAddress;
  300. /* xStart is used to hold a pointer to the first item in the list of free
  301. blocks. The void cast is used to prevent compiler warnings. */
  302. xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
  303. xStart.xBlockSize = ( size_t ) 0;
  304. /* pxEnd is used to mark the end of the list of free blocks and is inserted
  305. at the end of the heap space. */
  306. uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;
  307. uxAddress -= xHeapStructSize;
  308. uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
  309. pxEnd = ( void * ) uxAddress;
  310. pxEnd->xBlockSize = 0;
  311. pxEnd->pxNextFreeBlock = NULL;
  312. /* To start with there is a single free block that is sized to take up the
  313. entire heap space, minus the space taken by pxEnd. */
  314. pxFirstFreeBlock = ( void * ) pucAlignedHeap;
  315. pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;
  316. pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
  317. /* Only one block exists - and it covers the entire usable heap space. */
  318. xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
  319. xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
  320. /* Work out the position of the top bit in a size_t variable. */
  321. xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
  322. }
  323. /*-----------------------------------------------------------*/
  324. static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
  325. {
  326. BlockLink_t *pxIterator;
  327. uint8_t *puc;
  328. /* Iterate through the list until a block is found that has a higher address
  329. than the block being inserted. */
  330. for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
  331. {
  332. /* Nothing to do here, just iterate to the right position. */
  333. }
  334. /* Do the block being inserted, and the block it is being inserted after
  335. make a contiguous block of memory? */
  336. puc = ( uint8_t * ) pxIterator;
  337. if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
  338. {
  339. pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
  340. pxBlockToInsert = pxIterator;
  341. }
  342. else
  343. {
  344. mtCOVERAGE_TEST_MARKER();
  345. }
  346. /* Do the block being inserted, and the block it is being inserted before
  347. make a contiguous block of memory? */
  348. puc = ( uint8_t * ) pxBlockToInsert;
  349. if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
  350. {
  351. if( pxIterator->pxNextFreeBlock != pxEnd )
  352. {
  353. /* Form one big block from the two blocks. */
  354. pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
  355. pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
  356. }
  357. else
  358. {
  359. pxBlockToInsert->pxNextFreeBlock = pxEnd;
  360. }
  361. }
  362. else
  363. {
  364. pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
  365. }
  366. /* If the block being inserted plugged a gab, so was merged with the block
  367. before and the block after, then it's pxNextFreeBlock pointer will have
  368. already been set, and should not be set here as that would make it point
  369. to itself. */
  370. if( pxIterator != pxBlockToInsert )
  371. {
  372. pxIterator->pxNextFreeBlock = pxBlockToInsert;
  373. }
  374. else
  375. {
  376. mtCOVERAGE_TEST_MARKER();
  377. }
  378. }