]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MemMang/heap_2.c
FreeRTOS source changes:
[freertos] / FreeRTOS / Source / portable / MemMang / heap_2.c
1 /*\r
2     FreeRTOS V8.2.3 - Copyright (C) 2015 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 /* A few bytes might be lost to byte aligning the heap start address. */\r
92 #define configADJUSTED_HEAP_SIZE        ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )\r
93 \r
94 /*\r
95  * Initialises the heap structures before their first use.\r
96  */\r
97 static void prvHeapInit( void );\r
98 \r
99 /* Allocate the memory for the heap. */\r
100 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )\r
101         /* The application writer has already defined the array used for the RTOS\r
102         heap - probably so it can be placed in a special segment or address. */\r
103         extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
104 #else\r
105         static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
106 #endif /* configAPPLICATION_ALLOCATED_HEAP */\r
107 \r
108 \r
109 /* Define the linked list structure.  This is used to link free blocks in order\r
110 of their size. */\r
111 typedef struct A_BLOCK_LINK\r
112 {\r
113         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
114         size_t xBlockSize;                                              /*<< The size of the free block. */\r
115 } BlockLink_t;\r
116 \r
117 \r
118 static const uint16_t heapSTRUCT_SIZE   = ( ( sizeof ( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );\r
119 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )\r
120 \r
121 /* Create a couple of list links to mark the start and end of the list. */\r
122 static BlockLink_t xStart, xEnd;\r
123 \r
124 /* Keeps track of the number of free bytes remaining, but says nothing about\r
125 fragmentation. */\r
126 static size_t xFreeBytesRemaining = configADJUSTED_HEAP_SIZE;\r
127 \r
128 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */\r
129 \r
130 /*\r
131  * Insert a block into the list of free blocks - which is ordered by size of\r
132  * the block.  Small blocks at the start of the list and large blocks at the end\r
133  * of the list.\r
134  */\r
135 #define prvInsertBlockIntoFreeList( pxBlockToInsert )                                                           \\r
136 {                                                                                                                                                                       \\r
137 BlockLink_t *pxIterator;                                                                                                                        \\r
138 size_t xBlockSize;                                                                                                                                      \\r
139                                                                                                                                                                         \\r
140         xBlockSize = pxBlockToInsert->xBlockSize;                                                                               \\r
141                                                                                                                                                                         \\r
142         /* Iterate through the list until a block is found that has a larger size */    \\r
143         /* than the block we are inserting. */                                                                                  \\r
144         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock )     \\r
145         {                                                                                                                                                               \\r
146                 /* There is nothing to do here - just iterate to the correct position. */       \\r
147         }                                                                                                                                                               \\r
148                                                                                                                                                                         \\r
149         /* Update the list to include the block being inserted in the correct */                \\r
150         /* position. */                                                                                                                                 \\r
151         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;                                 \\r
152         pxIterator->pxNextFreeBlock = pxBlockToInsert;                                                                  \\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 void *pvPortMalloc( size_t xWantedSize )\r
157 {\r
158 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
159 static BaseType_t xHeapHasBeenInitialised = pdFALSE;\r
160 void *pvReturn = NULL;\r
161 \r
162         vTaskSuspendAll();\r
163         {\r
164                 /* If this is the first call to malloc then the heap will require\r
165                 initialisation to setup the list of free blocks. */\r
166                 if( xHeapHasBeenInitialised == pdFALSE )\r
167                 {\r
168                         prvHeapInit();\r
169                         xHeapHasBeenInitialised = pdTRUE;\r
170                 }\r
171 \r
172                 /* The wanted size is increased so it can contain a BlockLink_t\r
173                 structure in addition to the requested amount of bytes. */\r
174                 if( xWantedSize > 0 )\r
175                 {\r
176                         xWantedSize += heapSTRUCT_SIZE;\r
177 \r
178                         /* Ensure that blocks are always aligned to the required number of bytes. */\r
179                         if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0 )\r
180                         {\r
181                                 /* Byte alignment required. */\r
182                                 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
183                         }\r
184                 }\r
185 \r
186                 if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED_HEAP_SIZE ) )\r
187                 {\r
188                         /* Blocks are stored in byte order - traverse the list from the start\r
189                         (smallest) block until one of adequate size is found. */\r
190                         pxPreviousBlock = &xStart;\r
191                         pxBlock = xStart.pxNextFreeBlock;\r
192                         while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )\r
193                         {\r
194                                 pxPreviousBlock = pxBlock;\r
195                                 pxBlock = pxBlock->pxNextFreeBlock;\r
196                         }\r
197 \r
198                         /* If we found the end marker then a block of adequate size was not found. */\r
199                         if( pxBlock != &xEnd )\r
200                         {\r
201                                 /* Return the memory space - jumping over the BlockLink_t structure\r
202                                 at its start. */\r
203                                 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );\r
204 \r
205                                 /* This block is being returned for use so must be taken out of the\r
206                                 list of free blocks. */\r
207                                 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
208 \r
209                                 /* If the block is larger than required it can be split into two. */\r
210                                 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
211                                 {\r
212                                         /* This block is to be split into two.  Create a new block\r
213                                         following the number of bytes requested. The void cast is\r
214                                         used to prevent byte alignment warnings from the compiler. */\r
215                                         pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
216 \r
217                                         /* Calculate the sizes of two blocks split from the single\r
218                                         block. */\r
219                                         pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
220                                         pxBlock->xBlockSize = xWantedSize;\r
221 \r
222                                         /* Insert the new block into the list of free blocks. */\r
223                                         prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
224                                 }\r
225 \r
226                                 xFreeBytesRemaining -= pxBlock->xBlockSize;\r
227                         }\r
228                 }\r
229 \r
230                 traceMALLOC( pvReturn, xWantedSize );\r
231         }\r
232         ( void ) xTaskResumeAll();\r
233 \r
234         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
235         {\r
236                 if( pvReturn == NULL )\r
237                 {\r
238                         extern void vApplicationMallocFailedHook( void );\r
239                         vApplicationMallocFailedHook();\r
240                 }\r
241         }\r
242         #endif\r
243 \r
244         return pvReturn;\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 void vPortFree( void *pv )\r
249 {\r
250 uint8_t *puc = ( uint8_t * ) pv;\r
251 BlockLink_t *pxLink;\r
252 \r
253         if( pv != NULL )\r
254         {\r
255                 /* The memory being freed will have an BlockLink_t structure immediately\r
256                 before it. */\r
257                 puc -= heapSTRUCT_SIZE;\r
258 \r
259                 /* This unexpected casting is to keep some compilers from issuing\r
260                 byte alignment warnings. */\r
261                 pxLink = ( void * ) puc;\r
262 \r
263                 vTaskSuspendAll();\r
264                 {\r
265                         /* Add this block to the list of free blocks. */\r
266                         prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
267                         xFreeBytesRemaining += pxLink->xBlockSize;\r
268                         traceFREE( pv, pxLink->xBlockSize );\r
269                 }\r
270                 ( void ) xTaskResumeAll();\r
271         }\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 size_t xPortGetFreeHeapSize( void )\r
276 {\r
277         return xFreeBytesRemaining;\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 void vPortInitialiseBlocks( void )\r
282 {\r
283         /* This just exists to keep the linker quiet. */\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 static void prvHeapInit( void )\r
288 {\r
289 BlockLink_t *pxFirstFreeBlock;\r
290 uint8_t *pucAlignedHeap;\r
291 \r
292         /* Ensure the heap starts on a correctly aligned boundary. */\r
293         pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );\r
294 \r
295         /* xStart is used to hold a pointer to the first item in the list of free\r
296         blocks.  The void cast is used to prevent compiler warnings. */\r
297         xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;\r
298         xStart.xBlockSize = ( size_t ) 0;\r
299 \r
300         /* xEnd is used to mark the end of the list of free blocks. */\r
301         xEnd.xBlockSize = configADJUSTED_HEAP_SIZE;\r
302         xEnd.pxNextFreeBlock = NULL;\r
303 \r
304         /* To start with there is a single free block that is sized to take up the\r
305         entire heap space. */\r
306         pxFirstFreeBlock = ( void * ) pucAlignedHeap;\r
307         pxFirstFreeBlock->xBlockSize = configADJUSTED_HEAP_SIZE;\r
308         pxFirstFreeBlock->pxNextFreeBlock = &xEnd;\r
309 }\r
310 /*-----------------------------------------------------------*/\r