heap_4.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * FreeRTOS Kernel V11.1.0
  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 combines
  30. * (coalescences) adjacent memory blocks as they are freed, and in so doing
  31. * limits memory fragmentation.
  32. *
  33. * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
  34. * memory management pages of https://www.FreeRTOS.org for more information.
  35. */
  36. #include <stdlib.h>
  37. #include <string.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. #ifndef configHEAP_CLEAR_MEMORY_ON_FREE
  49. #define configHEAP_CLEAR_MEMORY_ON_FREE 0
  50. #endif
  51. /* Block sizes must not get too small. */
  52. #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
  53. /* Assumes 8bit bytes! */
  54. #define heapBITS_PER_BYTE ( ( size_t ) 8 )
  55. /* Max value that fits in a size_t type. */
  56. #define heapSIZE_MAX ( ~( ( size_t ) 0 ) )
  57. /* Check if multiplying a and b will result in overflow. */
  58. #define heapMULTIPLY_WILL_OVERFLOW( a, b ) ( ( ( a ) > 0 ) && ( ( b ) > ( heapSIZE_MAX / ( a ) ) ) )
  59. /* Check if adding a and b will result in overflow. */
  60. #define heapADD_WILL_OVERFLOW( a, b ) ( ( a ) > ( heapSIZE_MAX - ( b ) ) )
  61. /* Check if the subtraction operation ( a - b ) will result in underflow. */
  62. #define heapSUBTRACT_WILL_UNDERFLOW( a, b ) ( ( a ) < ( b ) )
  63. /* MSB of the xBlockSize member of an BlockLink_t structure is used to track
  64. * the allocation status of a block. When MSB of the xBlockSize member of
  65. * an BlockLink_t structure is set then the block belongs to the application.
  66. * When the bit is free the block is still part of the free heap space. */
  67. #define heapBLOCK_ALLOCATED_BITMASK ( ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 ) )
  68. #define heapBLOCK_SIZE_IS_VALID( xBlockSize ) ( ( ( xBlockSize ) & heapBLOCK_ALLOCATED_BITMASK ) == 0 )
  69. #define heapBLOCK_IS_ALLOCATED( pxBlock ) ( ( ( pxBlock->xBlockSize ) & heapBLOCK_ALLOCATED_BITMASK ) != 0 )
  70. #define heapALLOCATE_BLOCK( pxBlock ) ( ( pxBlock->xBlockSize ) |= heapBLOCK_ALLOCATED_BITMASK )
  71. #define heapFREE_BLOCK( pxBlock ) ( ( pxBlock->xBlockSize ) &= ~heapBLOCK_ALLOCATED_BITMASK )
  72. /*-----------------------------------------------------------*/
  73. /* Allocate the memory for the heap. */
  74. #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
  75. /* The application writer has already defined the array used for the RTOS
  76. * heap - probably so it can be placed in a special segment or address. */
  77. extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  78. #else
  79. PRIVILEGED_DATA static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  80. #endif /* configAPPLICATION_ALLOCATED_HEAP */
  81. /* Define the linked list structure. This is used to link free blocks in order
  82. * of their memory address. */
  83. typedef struct A_BLOCK_LINK
  84. {
  85. struct A_BLOCK_LINK * pxNextFreeBlock; /**< The next free block in the list. */
  86. size_t xBlockSize; /**< The size of the free block. */
  87. } BlockLink_t;
  88. /* Setting configENABLE_HEAP_PROTECTOR to 1 enables heap block pointers
  89. * protection using an application supplied canary value to catch heap
  90. * corruption should a heap buffer overflow occur.
  91. */
  92. #if ( configENABLE_HEAP_PROTECTOR == 1 )
  93. /**
  94. * @brief Application provided function to get a random value to be used as canary.
  95. *
  96. * @param pxHeapCanary [out] Output parameter to return the canary value.
  97. */
  98. extern void vApplicationGetRandomHeapCanary( portPOINTER_SIZE_TYPE * pxHeapCanary );
  99. /* Canary value for protecting internal heap pointers. */
  100. PRIVILEGED_DATA static portPOINTER_SIZE_TYPE xHeapCanary;
  101. /* Macro to load/store BlockLink_t pointers to memory. By XORing the
  102. * pointers with a random canary value, heap overflows will result
  103. * in randomly unpredictable pointer values which will be caught by
  104. * heapVALIDATE_BLOCK_POINTER assert. */
  105. #define heapPROTECT_BLOCK_POINTER( pxBlock ) ( ( BlockLink_t * ) ( ( ( portPOINTER_SIZE_TYPE ) ( pxBlock ) ) ^ xHeapCanary ) )
  106. #else
  107. #define heapPROTECT_BLOCK_POINTER( pxBlock ) ( pxBlock )
  108. #endif /* configENABLE_HEAP_PROTECTOR */
  109. /* Assert that a heap block pointer is within the heap bounds. */
  110. #define heapVALIDATE_BLOCK_POINTER( pxBlock ) \
  111. configASSERT( ( ( uint8_t * ) ( pxBlock ) >= &( ucHeap[ 0 ] ) ) && \
  112. ( ( uint8_t * ) ( pxBlock ) <= &( ucHeap[ configTOTAL_HEAP_SIZE - 1 ] ) ) )
  113. /*-----------------------------------------------------------*/
  114. /*
  115. * Inserts a block of memory that is being freed into the correct position in
  116. * the list of free memory blocks. The block being freed will be merged with
  117. * the block in front it and/or the block behind it if the memory blocks are
  118. * adjacent to each other.
  119. */
  120. static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert ) PRIVILEGED_FUNCTION;
  121. /*
  122. * Called automatically to setup the required heap structures the first time
  123. * pvPortMalloc() is called.
  124. */
  125. static void prvHeapInit( void ) PRIVILEGED_FUNCTION;
  126. /*-----------------------------------------------------------*/
  127. /* The size of the structure placed at the beginning of each allocated memory
  128. * block must by correctly byte aligned. */
  129. static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
  130. /* Create a couple of list links to mark the start and end of the list. */
  131. PRIVILEGED_DATA static BlockLink_t xStart;
  132. PRIVILEGED_DATA static BlockLink_t * pxEnd = NULL;
  133. /* Keeps track of the number of calls to allocate and free memory as well as the
  134. * number of free bytes remaining, but says nothing about fragmentation. */
  135. PRIVILEGED_DATA static size_t xFreeBytesRemaining = ( size_t ) 0U;
  136. PRIVILEGED_DATA static size_t xMinimumEverFreeBytesRemaining = ( size_t ) 0U;
  137. PRIVILEGED_DATA static size_t xNumberOfSuccessfulAllocations = ( size_t ) 0U;
  138. PRIVILEGED_DATA static size_t xNumberOfSuccessfulFrees = ( size_t ) 0U;
  139. /*-----------------------------------------------------------*/
  140. void * pvPortMalloc( size_t xWantedSize )
  141. {
  142. BlockLink_t * pxBlock;
  143. BlockLink_t * pxPreviousBlock;
  144. BlockLink_t * pxNewBlockLink;
  145. void * pvReturn = NULL;
  146. size_t xAdditionalRequiredSize;
  147. if( xWantedSize > 0 )
  148. {
  149. /* The wanted size must be increased so it can contain a BlockLink_t
  150. * structure in addition to the requested amount of bytes. */
  151. if( heapADD_WILL_OVERFLOW( xWantedSize, xHeapStructSize ) == 0 )
  152. {
  153. xWantedSize += xHeapStructSize;
  154. /* Ensure that blocks are always aligned to the required number
  155. * of bytes. */
  156. if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
  157. {
  158. /* Byte alignment required. */
  159. xAdditionalRequiredSize = portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
  160. if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
  161. {
  162. xWantedSize += xAdditionalRequiredSize;
  163. }
  164. else
  165. {
  166. xWantedSize = 0;
  167. }
  168. }
  169. else
  170. {
  171. mtCOVERAGE_TEST_MARKER();
  172. }
  173. }
  174. else
  175. {
  176. xWantedSize = 0;
  177. }
  178. }
  179. else
  180. {
  181. mtCOVERAGE_TEST_MARKER();
  182. }
  183. vTaskSuspendAll();
  184. {
  185. /* If this is the first call to malloc then the heap will require
  186. * initialisation to setup the list of free blocks. */
  187. if( pxEnd == NULL )
  188. {
  189. prvHeapInit();
  190. }
  191. else
  192. {
  193. mtCOVERAGE_TEST_MARKER();
  194. }
  195. /* Check the block size we are trying to allocate is not so large that the
  196. * top bit is set. The top bit of the block size member of the BlockLink_t
  197. * structure is used to determine who owns the block - the application or
  198. * the kernel, so it must be free. */
  199. if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) != 0 )
  200. {
  201. if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
  202. {
  203. /* Traverse the list from the start (lowest address) block until
  204. * one of adequate size is found. */
  205. pxPreviousBlock = &xStart;
  206. pxBlock = heapPROTECT_BLOCK_POINTER( xStart.pxNextFreeBlock );
  207. heapVALIDATE_BLOCK_POINTER( pxBlock );
  208. while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != heapPROTECT_BLOCK_POINTER( NULL ) ) )
  209. {
  210. pxPreviousBlock = pxBlock;
  211. pxBlock = heapPROTECT_BLOCK_POINTER( pxBlock->pxNextFreeBlock );
  212. heapVALIDATE_BLOCK_POINTER( pxBlock );
  213. }
  214. /* If the end marker was reached then a block of adequate size
  215. * was not found. */
  216. if( pxBlock != pxEnd )
  217. {
  218. /* Return the memory space pointed to - jumping over the
  219. * BlockLink_t structure at its start. */
  220. pvReturn = ( void * ) ( ( ( uint8_t * ) heapPROTECT_BLOCK_POINTER( pxPreviousBlock->pxNextFreeBlock ) ) + xHeapStructSize );
  221. heapVALIDATE_BLOCK_POINTER( pvReturn );
  222. /* This block is being returned for use so must be taken out
  223. * of the list of free blocks. */
  224. pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
  225. /* If the block is larger than required it can be split into
  226. * two. */
  227. configASSERT( heapSUBTRACT_WILL_UNDERFLOW( pxBlock->xBlockSize, xWantedSize ) == 0 );
  228. if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
  229. {
  230. /* This block is to be split into two. Create a new
  231. * block following the number of bytes requested. The void
  232. * cast is used to prevent byte alignment warnings from the
  233. * compiler. */
  234. pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
  235. configASSERT( ( ( ( size_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );
  236. /* Calculate the sizes of two blocks split from the
  237. * single block. */
  238. pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
  239. pxBlock->xBlockSize = xWantedSize;
  240. /* Insert the new block into the list of free blocks. */
  241. pxNewBlockLink->pxNextFreeBlock = pxPreviousBlock->pxNextFreeBlock;
  242. pxPreviousBlock->pxNextFreeBlock = heapPROTECT_BLOCK_POINTER( pxNewBlockLink );
  243. }
  244. else
  245. {
  246. mtCOVERAGE_TEST_MARKER();
  247. }
  248. xFreeBytesRemaining -= pxBlock->xBlockSize;
  249. if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
  250. {
  251. xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
  252. }
  253. else
  254. {
  255. mtCOVERAGE_TEST_MARKER();
  256. }
  257. /* The block is being returned - it is allocated and owned
  258. * by the application and has no "next" block. */
  259. heapALLOCATE_BLOCK( pxBlock );
  260. pxBlock->pxNextFreeBlock = NULL;
  261. xNumberOfSuccessfulAllocations++;
  262. }
  263. else
  264. {
  265. mtCOVERAGE_TEST_MARKER();
  266. }
  267. }
  268. else
  269. {
  270. mtCOVERAGE_TEST_MARKER();
  271. }
  272. }
  273. else
  274. {
  275. mtCOVERAGE_TEST_MARKER();
  276. }
  277. traceMALLOC( pvReturn, xWantedSize );
  278. }
  279. ( void ) xTaskResumeAll();
  280. #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
  281. {
  282. if( pvReturn == NULL )
  283. {
  284. vApplicationMallocFailedHook();
  285. }
  286. else
  287. {
  288. mtCOVERAGE_TEST_MARKER();
  289. }
  290. }
  291. #endif /* if ( configUSE_MALLOC_FAILED_HOOK == 1 ) */
  292. configASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) portBYTE_ALIGNMENT_MASK ) == 0 );
  293. return pvReturn;
  294. }
  295. /*-----------------------------------------------------------*/
  296. void vPortFree( void * pv )
  297. {
  298. uint8_t * puc = ( uint8_t * ) pv;
  299. BlockLink_t * pxLink;
  300. if( pv != NULL )
  301. {
  302. /* The memory being freed will have an BlockLink_t structure immediately
  303. * before it. */
  304. puc -= xHeapStructSize;
  305. /* This casting is to keep the compiler from issuing warnings. */
  306. pxLink = ( void * ) puc;
  307. heapVALIDATE_BLOCK_POINTER( pxLink );
  308. configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 );
  309. configASSERT( pxLink->pxNextFreeBlock == NULL );
  310. if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 )
  311. {
  312. if( pxLink->pxNextFreeBlock == NULL )
  313. {
  314. /* The block is being returned to the heap - it is no longer
  315. * allocated. */
  316. heapFREE_BLOCK( pxLink );
  317. #if ( configHEAP_CLEAR_MEMORY_ON_FREE == 1 )
  318. {
  319. /* Check for underflow as this can occur if xBlockSize is
  320. * overwritten in a heap block. */
  321. if( heapSUBTRACT_WILL_UNDERFLOW( pxLink->xBlockSize, xHeapStructSize ) == 0 )
  322. {
  323. ( void ) memset( puc + xHeapStructSize, 0, pxLink->xBlockSize - xHeapStructSize );
  324. }
  325. }
  326. #endif
  327. vTaskSuspendAll();
  328. {
  329. /* Add this block to the list of free blocks. */
  330. xFreeBytesRemaining += pxLink->xBlockSize;
  331. traceFREE( pv, pxLink->xBlockSize );
  332. prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
  333. xNumberOfSuccessfulFrees++;
  334. }
  335. ( void ) xTaskResumeAll();
  336. }
  337. else
  338. {
  339. mtCOVERAGE_TEST_MARKER();
  340. }
  341. }
  342. else
  343. {
  344. mtCOVERAGE_TEST_MARKER();
  345. }
  346. }
  347. }
  348. /*-----------------------------------------------------------*/
  349. size_t xPortGetFreeHeapSize( void )
  350. {
  351. return xFreeBytesRemaining;
  352. }
  353. /*-----------------------------------------------------------*/
  354. size_t xPortGetMinimumEverFreeHeapSize( void )
  355. {
  356. return xMinimumEverFreeBytesRemaining;
  357. }
  358. /*-----------------------------------------------------------*/
  359. void vPortInitialiseBlocks( void )
  360. {
  361. /* This just exists to keep the linker quiet. */
  362. }
  363. /*-----------------------------------------------------------*/
  364. void * pvPortCalloc( size_t xNum,
  365. size_t xSize )
  366. {
  367. void * pv = NULL;
  368. if( heapMULTIPLY_WILL_OVERFLOW( xNum, xSize ) == 0 )
  369. {
  370. pv = pvPortMalloc( xNum * xSize );
  371. if( pv != NULL )
  372. {
  373. ( void ) memset( pv, 0, xNum * xSize );
  374. }
  375. }
  376. return pv;
  377. }
  378. /*-----------------------------------------------------------*/
  379. static void prvHeapInit( void ) /* PRIVILEGED_FUNCTION */
  380. {
  381. BlockLink_t * pxFirstFreeBlock;
  382. portPOINTER_SIZE_TYPE uxStartAddress, uxEndAddress;
  383. size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
  384. /* Ensure the heap starts on a correctly aligned boundary. */
  385. uxStartAddress = ( portPOINTER_SIZE_TYPE ) ucHeap;
  386. if( ( uxStartAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
  387. {
  388. uxStartAddress += ( portBYTE_ALIGNMENT - 1 );
  389. uxStartAddress &= ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK );
  390. xTotalHeapSize -= ( size_t ) ( uxStartAddress - ( portPOINTER_SIZE_TYPE ) ucHeap );
  391. }
  392. #if ( configENABLE_HEAP_PROTECTOR == 1 )
  393. {
  394. vApplicationGetRandomHeapCanary( &( xHeapCanary ) );
  395. }
  396. #endif
  397. /* xStart is used to hold a pointer to the first item in the list of free
  398. * blocks. The void cast is used to prevent compiler warnings. */
  399. xStart.pxNextFreeBlock = ( void * ) heapPROTECT_BLOCK_POINTER( uxStartAddress );
  400. xStart.xBlockSize = ( size_t ) 0;
  401. /* pxEnd is used to mark the end of the list of free blocks and is inserted
  402. * at the end of the heap space. */
  403. uxEndAddress = uxStartAddress + ( portPOINTER_SIZE_TYPE ) xTotalHeapSize;
  404. uxEndAddress -= ( portPOINTER_SIZE_TYPE ) xHeapStructSize;
  405. uxEndAddress &= ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK );
  406. pxEnd = ( BlockLink_t * ) uxEndAddress;
  407. pxEnd->xBlockSize = 0;
  408. pxEnd->pxNextFreeBlock = heapPROTECT_BLOCK_POINTER( NULL );
  409. /* To start with there is a single free block that is sized to take up the
  410. * entire heap space, minus the space taken by pxEnd. */
  411. pxFirstFreeBlock = ( BlockLink_t * ) uxStartAddress;
  412. pxFirstFreeBlock->xBlockSize = ( size_t ) ( uxEndAddress - ( portPOINTER_SIZE_TYPE ) pxFirstFreeBlock );
  413. pxFirstFreeBlock->pxNextFreeBlock = heapPROTECT_BLOCK_POINTER( pxEnd );
  414. /* Only one block exists - and it covers the entire usable heap space. */
  415. xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
  416. xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
  417. }
  418. /*-----------------------------------------------------------*/
  419. static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert ) /* PRIVILEGED_FUNCTION */
  420. {
  421. BlockLink_t * pxIterator;
  422. uint8_t * puc;
  423. /* Iterate through the list until a block is found that has a higher address
  424. * than the block being inserted. */
  425. for( pxIterator = &xStart; heapPROTECT_BLOCK_POINTER( pxIterator->pxNextFreeBlock ) < pxBlockToInsert; pxIterator = heapPROTECT_BLOCK_POINTER( pxIterator->pxNextFreeBlock ) )
  426. {
  427. /* Nothing to do here, just iterate to the right position. */
  428. }
  429. if( pxIterator != &xStart )
  430. {
  431. heapVALIDATE_BLOCK_POINTER( pxIterator );
  432. }
  433. /* Do the block being inserted, and the block it is being inserted after
  434. * make a contiguous block of memory? */
  435. puc = ( uint8_t * ) pxIterator;
  436. if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
  437. {
  438. pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
  439. pxBlockToInsert = pxIterator;
  440. }
  441. else
  442. {
  443. mtCOVERAGE_TEST_MARKER();
  444. }
  445. /* Do the block being inserted, and the block it is being inserted before
  446. * make a contiguous block of memory? */
  447. puc = ( uint8_t * ) pxBlockToInsert;
  448. if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) heapPROTECT_BLOCK_POINTER( pxIterator->pxNextFreeBlock ) )
  449. {
  450. if( heapPROTECT_BLOCK_POINTER( pxIterator->pxNextFreeBlock ) != pxEnd )
  451. {
  452. /* Form one big block from the two blocks. */
  453. pxBlockToInsert->xBlockSize += heapPROTECT_BLOCK_POINTER( pxIterator->pxNextFreeBlock )->xBlockSize;
  454. pxBlockToInsert->pxNextFreeBlock = heapPROTECT_BLOCK_POINTER( pxIterator->pxNextFreeBlock )->pxNextFreeBlock;
  455. }
  456. else
  457. {
  458. pxBlockToInsert->pxNextFreeBlock = heapPROTECT_BLOCK_POINTER( pxEnd );
  459. }
  460. }
  461. else
  462. {
  463. pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
  464. }
  465. /* If the block being inserted plugged a gab, so was merged with the block
  466. * before and the block after, then it's pxNextFreeBlock pointer will have
  467. * already been set, and should not be set here as that would make it point
  468. * to itself. */
  469. if( pxIterator != pxBlockToInsert )
  470. {
  471. pxIterator->pxNextFreeBlock = heapPROTECT_BLOCK_POINTER( pxBlockToInsert );
  472. }
  473. else
  474. {
  475. mtCOVERAGE_TEST_MARKER();
  476. }
  477. }
  478. /*-----------------------------------------------------------*/
  479. void vPortGetHeapStats( HeapStats_t * pxHeapStats )
  480. {
  481. BlockLink_t * pxBlock;
  482. size_t xBlocks = 0, xMaxSize = 0, xMinSize = (size_t)portMAX_DELAY; /* portMAX_DELAY used as a portable way of getting the maximum value. */
  483. vTaskSuspendAll();
  484. {
  485. pxBlock = heapPROTECT_BLOCK_POINTER( xStart.pxNextFreeBlock );
  486. /* pxBlock will be NULL if the heap has not been initialised. The heap
  487. * is initialised automatically when the first allocation is made. */
  488. if( pxBlock != NULL )
  489. {
  490. while( pxBlock != pxEnd )
  491. {
  492. /* Increment the number of blocks and record the largest block seen
  493. * so far. */
  494. xBlocks++;
  495. if( pxBlock->xBlockSize > xMaxSize )
  496. {
  497. xMaxSize = pxBlock->xBlockSize;
  498. }
  499. if( pxBlock->xBlockSize < xMinSize )
  500. {
  501. xMinSize = pxBlock->xBlockSize;
  502. }
  503. /* Move to the next block in the chain until the last block is
  504. * reached. */
  505. pxBlock = heapPROTECT_BLOCK_POINTER( pxBlock->pxNextFreeBlock );
  506. }
  507. }
  508. }
  509. ( void ) xTaskResumeAll();
  510. pxHeapStats->xSizeOfLargestFreeBlockInBytes = xMaxSize;
  511. pxHeapStats->xSizeOfSmallestFreeBlockInBytes = xMinSize;
  512. pxHeapStats->xNumberOfFreeBlocks = xBlocks;
  513. taskENTER_CRITICAL();
  514. {
  515. pxHeapStats->xAvailableHeapSpaceInBytes = xFreeBytesRemaining;
  516. pxHeapStats->xNumberOfSuccessfulAllocations = xNumberOfSuccessfulAllocations;
  517. pxHeapStats->xNumberOfSuccessfulFrees = xNumberOfSuccessfulFrees;
  518. pxHeapStats->xMinimumEverFreeBytesRemaining = xMinimumEverFreeBytesRemaining;
  519. }
  520. taskEXIT_CRITICAL();
  521. }
  522. /*-----------------------------------------------------------*/
  523. /*
  524. * Reset the state in this file. This state is normally initialized at start up.
  525. * This function must be called by the application before restarting the
  526. * scheduler.
  527. */
  528. void vPortHeapResetState( void )
  529. {
  530. pxEnd = NULL;
  531. xFreeBytesRemaining = ( size_t ) 0U;
  532. xMinimumEverFreeBytesRemaining = ( size_t ) 0U;
  533. xNumberOfSuccessfulAllocations = ( size_t ) 0U;
  534. xNumberOfSuccessfulFrees = ( size_t ) 0U;
  535. }
  536. /*-----------------------------------------------------------*/