heap_2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
  3. All rights reserved
  4. VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. This file is part of the FreeRTOS distribution.
  6. FreeRTOS is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License (version 2) as published by the
  8. Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
  9. ***************************************************************************
  10. >>! NOTE: The modification to the GPL is included to allow you to !<<
  11. >>! distribute a combined work that includes FreeRTOS without being !<<
  12. >>! obliged to provide the source code for proprietary components !<<
  13. >>! outside of the FreeRTOS kernel. !<<
  14. ***************************************************************************
  15. FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. FOR A PARTICULAR PURPOSE. Full license text is available on the following
  18. link: http://www.freertos.org/a00114.html
  19. ***************************************************************************
  20. * *
  21. * FreeRTOS provides completely free yet professionally developed, *
  22. * robust, strictly quality controlled, supported, and cross *
  23. * platform software that is more than just the market leader, it *
  24. * is the industry's de facto standard. *
  25. * *
  26. * Help yourself get started quickly while simultaneously helping *
  27. * to support the FreeRTOS project by purchasing a FreeRTOS *
  28. * tutorial book, reference manual, or both: *
  29. * http://www.FreeRTOS.org/Documentation *
  30. * *
  31. ***************************************************************************
  32. http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
  33. the FAQ page "My application does not run, what could be wrong?". Have you
  34. defined configASSERT()?
  35. http://www.FreeRTOS.org/support - In return for receiving this top quality
  36. embedded software for free we request you assist our global community by
  37. participating in the support forum.
  38. http://www.FreeRTOS.org/training - Investing in training allows your team to
  39. be as productive as possible as early as possible. Now you can receive
  40. FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
  41. Ltd, and the world's leading authority on the world's leading RTOS.
  42. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  43. including FreeRTOS+Trace - an indispensable productivity tool, a DOS
  44. compatible FAT file system, and our tiny thread aware UDP/IP stack.
  45. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
  46. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
  47. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
  48. Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
  49. licenses offer ticketed support, indemnification and commercial middleware.
  50. http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  51. engineered and independently SIL3 certified version for use in safety and
  52. mission critical applications that require provable dependability.
  53. 1 tab == 4 spaces!
  54. */
  55. /*
  56. * A sample implementation of pvPortMalloc() and vPortFree() that permits
  57. * allocated blocks to be freed, but does not combine adjacent free blocks
  58. * into a single larger block (and so will fragment memory). See heap_4.c for
  59. * an equivalent that does combine adjacent blocks into single larger blocks.
  60. *
  61. * See heap_1.c, heap_3.c and heap_4.c for alternative implementations, and the
  62. * memory management pages of http://www.FreeRTOS.org for more information.
  63. */
  64. #include <stdlib.h>
  65. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  66. all the API functions to use the MPU wrappers. That should only be done when
  67. task.h is included from an application file. */
  68. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  69. #include "FreeRTOS.h"
  70. #include "task.h"
  71. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  72. /* A few bytes might be lost to byte aligning the heap start address. */
  73. #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
  74. /*
  75. * Initialises the heap structures before their first use.
  76. */
  77. static void prvHeapInit( void );
  78. /* Allocate the memory for the heap. */
  79. static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  80. /* Define the linked list structure. This is used to link free blocks in order
  81. of their size. */
  82. typedef struct A_BLOCK_LINK
  83. {
  84. struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
  85. size_t xBlockSize; /*<< The size of the free block. */
  86. } BlockLink_t;
  87. static const uint16_t heapSTRUCT_SIZE = ( ( sizeof ( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );
  88. #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
  89. /* Create a couple of list links to mark the start and end of the list. */
  90. static BlockLink_t xStart, xEnd;
  91. /* Keeps track of the number of free bytes remaining, but says nothing about
  92. fragmentation. */
  93. static size_t xFreeBytesRemaining = configADJUSTED_HEAP_SIZE;
  94. /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
  95. /*
  96. * Insert a block into the list of free blocks - which is ordered by size of
  97. * the block. Small blocks at the start of the list and large blocks at the end
  98. * of the list.
  99. */
  100. #define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
  101. { \
  102. BlockLink_t *pxIterator; \
  103. size_t xBlockSize; \
  104. \
  105. xBlockSize = pxBlockToInsert->xBlockSize; \
  106. \
  107. /* Iterate through the list until a block is found that has a larger size */ \
  108. /* than the block we are inserting. */ \
  109. for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
  110. { \
  111. /* There is nothing to do here - just iterate to the correct position. */ \
  112. } \
  113. \
  114. /* Update the list to include the block being inserted in the correct */ \
  115. /* position. */ \
  116. pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
  117. pxIterator->pxNextFreeBlock = pxBlockToInsert; \
  118. }
  119. /*-----------------------------------------------------------*/
  120. void *pvPortMalloc( size_t xWantedSize )
  121. {
  122. BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
  123. static BaseType_t xHeapHasBeenInitialised = pdFALSE;
  124. void *pvReturn = NULL;
  125. vTaskSuspendAll();
  126. {
  127. /* If this is the first call to malloc then the heap will require
  128. initialisation to setup the list of free blocks. */
  129. if( xHeapHasBeenInitialised == pdFALSE )
  130. {
  131. prvHeapInit();
  132. xHeapHasBeenInitialised = pdTRUE;
  133. }
  134. /* The wanted size is increased so it can contain a BlockLink_t
  135. structure in addition to the requested amount of bytes. */
  136. if( xWantedSize > 0 )
  137. {
  138. xWantedSize += heapSTRUCT_SIZE;
  139. /* Ensure that blocks are always aligned to the required number of bytes. */
  140. if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0 )
  141. {
  142. /* Byte alignment required. */
  143. xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
  144. }
  145. }
  146. if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED_HEAP_SIZE ) )
  147. {
  148. /* Blocks are stored in byte order - traverse the list from the start
  149. (smallest) block until one of adequate size is found. */
  150. pxPreviousBlock = &xStart;
  151. pxBlock = xStart.pxNextFreeBlock;
  152. while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
  153. {
  154. pxPreviousBlock = pxBlock;
  155. pxBlock = pxBlock->pxNextFreeBlock;
  156. }
  157. /* If we found the end marker then a block of adequate size was not found. */
  158. if( pxBlock != &xEnd )
  159. {
  160. /* Return the memory space - jumping over the BlockLink_t structure
  161. at its start. */
  162. pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
  163. /* This block is being returned for use so must be taken out of the
  164. list of free blocks. */
  165. pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
  166. /* If the block is larger than required it can be split into two. */
  167. if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
  168. {
  169. /* This block is to be split into two. Create a new block
  170. following the number of bytes requested. The void cast is
  171. used to prevent byte alignment warnings from the compiler. */
  172. pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
  173. /* Calculate the sizes of two blocks split from the single
  174. block. */
  175. pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
  176. pxBlock->xBlockSize = xWantedSize;
  177. /* Insert the new block into the list of free blocks. */
  178. prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
  179. }
  180. xFreeBytesRemaining -= pxBlock->xBlockSize;
  181. }
  182. }
  183. traceMALLOC( pvReturn, xWantedSize );
  184. }
  185. ( void ) xTaskResumeAll();
  186. #if( configUSE_MALLOC_FAILED_HOOK == 1 )
  187. {
  188. if( pvReturn == NULL )
  189. {
  190. extern void vApplicationMallocFailedHook( void );
  191. vApplicationMallocFailedHook();
  192. }
  193. }
  194. #endif
  195. return pvReturn;
  196. }
  197. /*-----------------------------------------------------------*/
  198. void vPortFree( void *pv )
  199. {
  200. uint8_t *puc = ( uint8_t * ) pv;
  201. BlockLink_t *pxLink;
  202. if( pv != NULL )
  203. {
  204. /* The memory being freed will have an BlockLink_t structure immediately
  205. before it. */
  206. puc -= heapSTRUCT_SIZE;
  207. /* This unexpected casting is to keep some compilers from issuing
  208. byte alignment warnings. */
  209. pxLink = ( void * ) puc;
  210. vTaskSuspendAll();
  211. {
  212. /* Add this block to the list of free blocks. */
  213. prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
  214. xFreeBytesRemaining += pxLink->xBlockSize;
  215. traceFREE( pv, pxLink->xBlockSize );
  216. }
  217. ( void ) xTaskResumeAll();
  218. }
  219. }
  220. /*-----------------------------------------------------------*/
  221. size_t xPortGetFreeHeapSize( void )
  222. {
  223. return xFreeBytesRemaining;
  224. }
  225. /*-----------------------------------------------------------*/
  226. void vPortInitialiseBlocks( void )
  227. {
  228. /* This just exists to keep the linker quiet. */
  229. }
  230. /*-----------------------------------------------------------*/
  231. static void prvHeapInit( void )
  232. {
  233. BlockLink_t *pxFirstFreeBlock;
  234. uint8_t *pucAlignedHeap;
  235. /* Ensure the heap starts on a correctly aligned boundary. */
  236. pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
  237. /* xStart is used to hold a pointer to the first item in the list of free
  238. blocks. The void cast is used to prevent compiler warnings. */
  239. xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
  240. xStart.xBlockSize = ( size_t ) 0;
  241. /* xEnd is used to mark the end of the list of free blocks. */
  242. xEnd.xBlockSize = configADJUSTED_HEAP_SIZE;
  243. xEnd.pxNextFreeBlock = NULL;
  244. /* To start with there is a single free block that is sized to take up the
  245. entire heap space. */
  246. pxFirstFreeBlock = ( void * ) pucAlignedHeap;
  247. pxFirstFreeBlock->xBlockSize = configADJUSTED_HEAP_SIZE;
  248. pxFirstFreeBlock->pxNextFreeBlock = &xEnd;
  249. }
  250. /*-----------------------------------------------------------*/