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