heap_2.c 9.8 KB

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