]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MemMang/heap_4.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Source / portable / MemMang / heap_4.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*\r
97  * A sample implementation of pvPortMalloc() and vPortFree() that combines\r
98  * (coalescences) adjacent memory blocks as they are freed, and in so doing\r
99  * limits memory fragmentation.\r
100  *\r
101  * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the\r
102  * memory management pages of http://www.FreeRTOS.org for more information.\r
103  */\r
104 #include <stdlib.h>\r
105 \r
106 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
107 all the API functions to use the MPU wrappers.  That should only be done when\r
108 task.h is included from an application file. */\r
109 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
110 \r
111 #include "FreeRTOS.h"\r
112 #include "task.h"\r
113 \r
114 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
115 \r
116 /* Block sizes must not get too small. */\r
117 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( xHeapStructSize * 2 ) )\r
118 \r
119 /* Assumes 8bit bytes! */\r
120 #define heapBITS_PER_BYTE               ( ( size_t ) 8 )\r
121 \r
122 /* Allocate the memory for the heap. */\r
123 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )\r
124         /* The application writer has already defined the array used for the RTOS\r
125         heap - probably so it can be placed in a special segment or address. */\r
126         extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
127 #else\r
128         static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
129 #endif /* configAPPLICATION_ALLOCATED_HEAP */\r
130 \r
131 /* Define the linked list structure.  This is used to link free blocks in order\r
132 of their memory address. */\r
133 typedef struct A_BLOCK_LINK\r
134 {\r
135         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
136         size_t xBlockSize;                                              /*<< The size of the free block. */\r
137 } BlockLink_t;\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /*\r
142  * Inserts a block of memory that is being freed into the correct position in\r
143  * the list of free memory blocks.  The block being freed will be merged with\r
144  * the block in front it and/or the block behind it if the memory blocks are\r
145  * adjacent to each other.\r
146  */\r
147 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );\r
148 \r
149 /*\r
150  * Called automatically to setup the required heap structures the first time\r
151  * pvPortMalloc() is called.\r
152  */\r
153 static void prvHeapInit( void );\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 /* The size of the structure placed at the beginning of each allocated memory\r
158 block must by correctly byte aligned. */\r
159 static const size_t xHeapStructSize     = ( ( sizeof( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );\r
160 \r
161 /* Create a couple of list links to mark the start and end of the list. */\r
162 static BlockLink_t xStart, *pxEnd = NULL;\r
163 \r
164 /* Keeps track of the number of free bytes remaining, but says nothing about\r
165 fragmentation. */\r
166 static size_t xFreeBytesRemaining = 0U;\r
167 static size_t xMinimumEverFreeBytesRemaining = 0U;\r
168 \r
169 /* Gets set to the top bit of an size_t type.  When this bit in the xBlockSize\r
170 member of an BlockLink_t structure is set then the block belongs to the\r
171 application.  When the bit is free the block is still part of the free heap\r
172 space. */\r
173 static size_t xBlockAllocatedBit = 0;\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 void *pvPortMalloc( size_t xWantedSize )\r
178 {\r
179 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
180 void *pvReturn = NULL;\r
181 \r
182         vTaskSuspendAll();\r
183         {\r
184                 /* If this is the first call to malloc then the heap will require\r
185                 initialisation to setup the list of free blocks. */\r
186                 if( pxEnd == NULL )\r
187                 {\r
188                         prvHeapInit();\r
189                 }\r
190                 else\r
191                 {\r
192                         mtCOVERAGE_TEST_MARKER();\r
193                 }\r
194 \r
195                 /* Check the requested block size is not so large that the top bit is\r
196                 set.  The top bit of the block size member of the BlockLink_t structure\r
197                 is used to determine who owns the block - the application or the\r
198                 kernel, so it must be free. */\r
199                 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )\r
200                 {\r
201                         /* The wanted size is increased so it can contain a BlockLink_t\r
202                         structure in addition to the requested amount of bytes. */\r
203                         if( xWantedSize > 0 )\r
204                         {\r
205                                 xWantedSize += xHeapStructSize;\r
206 \r
207                                 /* Ensure that blocks are always aligned to the required number\r
208                                 of bytes. */\r
209                                 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )\r
210                                 {\r
211                                         /* Byte alignment required. */\r
212                                         xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
213                                         configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );\r
214                                 }\r
215                                 else\r
216                                 {\r
217                                         mtCOVERAGE_TEST_MARKER();\r
218                                 }\r
219                         }\r
220                         else\r
221                         {\r
222                                 mtCOVERAGE_TEST_MARKER();\r
223                         }\r
224 \r
225                         if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )\r
226                         {\r
227                                 /* Traverse the list from the start     (lowest address) block until\r
228                                 one     of adequate size is found. */\r
229                                 pxPreviousBlock = &xStart;\r
230                                 pxBlock = xStart.pxNextFreeBlock;\r
231                                 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )\r
232                                 {\r
233                                         pxPreviousBlock = pxBlock;\r
234                                         pxBlock = pxBlock->pxNextFreeBlock;\r
235                                 }\r
236 \r
237                                 /* If the end marker was reached then a block of adequate size\r
238                                 was     not found. */\r
239                                 if( pxBlock != pxEnd )\r
240                                 {\r
241                                         /* Return the memory space pointed to - jumping over the\r
242                                         BlockLink_t structure at its start. */\r
243                                         pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );\r
244 \r
245                                         /* This block is being returned for use so must be taken out\r
246                                         of the list of free blocks. */\r
247                                         pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
248 \r
249                                         /* If the block is larger than required it can be split into\r
250                                         two. */\r
251                                         if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
252                                         {\r
253                                                 /* This block is to be split into two.  Create a new\r
254                                                 block following the number of bytes requested. The void\r
255                                                 cast is used to prevent byte alignment warnings from the\r
256                                                 compiler. */\r
257                                                 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
258                                                 configASSERT( ( ( ( uint32_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );\r
259 \r
260                                                 /* Calculate the sizes of two blocks split from the\r
261                                                 single block. */\r
262                                                 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
263                                                 pxBlock->xBlockSize = xWantedSize;\r
264 \r
265                                                 /* Insert the new block into the list of free blocks. */\r
266                                                 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
267                                         }\r
268                                         else\r
269                                         {\r
270                                                 mtCOVERAGE_TEST_MARKER();\r
271                                         }\r
272 \r
273                                         xFreeBytesRemaining -= pxBlock->xBlockSize;\r
274 \r
275                                         if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )\r
276                                         {\r
277                                                 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;\r
278                                         }\r
279                                         else\r
280                                         {\r
281                                                 mtCOVERAGE_TEST_MARKER();\r
282                                         }\r
283 \r
284                                         /* The block is being returned - it is allocated and owned\r
285                                         by the application and has no "next" block. */\r
286                                         pxBlock->xBlockSize |= xBlockAllocatedBit;\r
287                                         pxBlock->pxNextFreeBlock = NULL;\r
288                                 }\r
289                                 else\r
290                                 {\r
291                                         mtCOVERAGE_TEST_MARKER();\r
292                                 }\r
293                         }\r
294                         else\r
295                         {\r
296                                 mtCOVERAGE_TEST_MARKER();\r
297                         }\r
298                 }\r
299                 else\r
300                 {\r
301                         mtCOVERAGE_TEST_MARKER();\r
302                 }\r
303 \r
304                 traceMALLOC( pvReturn, xWantedSize );\r
305         }\r
306         ( void ) xTaskResumeAll();\r
307 \r
308         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
309         {\r
310                 if( pvReturn == NULL )\r
311                 {\r
312                         extern void vApplicationMallocFailedHook( void );\r
313                         vApplicationMallocFailedHook();\r
314                 }\r
315                 else\r
316                 {\r
317                         mtCOVERAGE_TEST_MARKER();\r
318                 }\r
319         }\r
320         #endif\r
321 \r
322         configASSERT( ( ( ( uint32_t ) pvReturn ) & portBYTE_ALIGNMENT_MASK ) == 0 );\r
323         return pvReturn;\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 void vPortFree( void *pv )\r
328 {\r
329 uint8_t *puc = ( uint8_t * ) pv;\r
330 BlockLink_t *pxLink;\r
331 \r
332         if( pv != NULL )\r
333         {\r
334                 /* The memory being freed will have an BlockLink_t structure immediately\r
335                 before it. */\r
336                 puc -= xHeapStructSize;\r
337 \r
338                 /* This casting is to keep the compiler from issuing warnings. */\r
339                 pxLink = ( void * ) puc;\r
340 \r
341                 /* Check the block is actually allocated. */\r
342                 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );\r
343                 configASSERT( pxLink->pxNextFreeBlock == NULL );\r
344 \r
345                 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )\r
346                 {\r
347                         if( pxLink->pxNextFreeBlock == NULL )\r
348                         {\r
349                                 /* The block is being returned to the heap - it is no longer\r
350                                 allocated. */\r
351                                 pxLink->xBlockSize &= ~xBlockAllocatedBit;\r
352 \r
353                                 vTaskSuspendAll();\r
354                                 {\r
355                                         /* Add this block to the list of free blocks. */\r
356                                         xFreeBytesRemaining += pxLink->xBlockSize;\r
357                                         traceFREE( pv, pxLink->xBlockSize );\r
358                                         prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
359                                 }\r
360                                 ( void ) xTaskResumeAll();\r
361                         }\r
362                         else\r
363                         {\r
364                                 mtCOVERAGE_TEST_MARKER();\r
365                         }\r
366                 }\r
367                 else\r
368                 {\r
369                         mtCOVERAGE_TEST_MARKER();\r
370                 }\r
371         }\r
372 }\r
373 /*-----------------------------------------------------------*/\r
374 \r
375 size_t xPortGetFreeHeapSize( void )\r
376 {\r
377         return xFreeBytesRemaining;\r
378 }\r
379 /*-----------------------------------------------------------*/\r
380 \r
381 size_t xPortGetMinimumEverFreeHeapSize( void )\r
382 {\r
383         return xMinimumEverFreeBytesRemaining;\r
384 }\r
385 /*-----------------------------------------------------------*/\r
386 \r
387 void vPortInitialiseBlocks( void )\r
388 {\r
389         /* This just exists to keep the linker quiet. */\r
390 }\r
391 /*-----------------------------------------------------------*/\r
392 \r
393 static void prvHeapInit( void )\r
394 {\r
395 BlockLink_t *pxFirstFreeBlock;\r
396 uint8_t *pucAlignedHeap;\r
397 uint32_t ulAddress;\r
398 size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;\r
399 \r
400         /* Ensure the heap starts on a correctly aligned boundary. */\r
401         ulAddress = ( uint32_t ) ucHeap;\r
402 \r
403         if( ( ulAddress & portBYTE_ALIGNMENT_MASK ) != 0 )\r
404         {\r
405                 ulAddress += ( portBYTE_ALIGNMENT - 1 );\r
406                 ulAddress &= ~portBYTE_ALIGNMENT_MASK;\r
407                 xTotalHeapSize -= ulAddress - ( uint32_t ) ucHeap;\r
408         }\r
409 \r
410         pucAlignedHeap = ( uint8_t * ) ulAddress;\r
411 \r
412         /* xStart is used to hold a pointer to the first item in the list of free\r
413         blocks.  The void cast is used to prevent compiler warnings. */\r
414         xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;\r
415         xStart.xBlockSize = ( size_t ) 0;\r
416 \r
417         /* pxEnd is used to mark the end of the list of free blocks and is inserted\r
418         at the end of the heap space. */\r
419         ulAddress = ( ( uint32_t ) pucAlignedHeap ) + xTotalHeapSize;\r
420         ulAddress -= xHeapStructSize;\r
421         ulAddress &= ~portBYTE_ALIGNMENT_MASK;\r
422         pxEnd = ( void * ) ulAddress;\r
423         pxEnd->xBlockSize = 0;\r
424         pxEnd->pxNextFreeBlock = NULL;\r
425 \r
426         /* To start with there is a single free block that is sized to take up the\r
427         entire heap space, minus the space taken by pxEnd. */\r
428         pxFirstFreeBlock = ( void * ) pucAlignedHeap;\r
429         pxFirstFreeBlock->xBlockSize = ulAddress - ( uint32_t ) pxFirstFreeBlock;\r
430         pxFirstFreeBlock->pxNextFreeBlock = pxEnd;\r
431 \r
432         /* Only one block exists - and it covers the entire usable heap space. */\r
433         xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
434         xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
435 \r
436         /* Work out the position of the top bit in a size_t variable. */\r
437         xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );\r
438 }\r
439 /*-----------------------------------------------------------*/\r
440 \r
441 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )\r
442 {\r
443 BlockLink_t *pxIterator;\r
444 uint8_t *puc;\r
445 \r
446         /* Iterate through the list until a block is found that has a higher address\r
447         than the block being inserted. */\r
448         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )\r
449         {\r
450                 /* Nothing to do here, just iterate to the right position. */\r
451         }\r
452 \r
453         /* Do the block being inserted, and the block it is being inserted after\r
454         make a contiguous block of memory? */\r
455         puc = ( uint8_t * ) pxIterator;\r
456         if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )\r
457         {\r
458                 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
459                 pxBlockToInsert = pxIterator;\r
460         }\r
461         else\r
462         {\r
463                 mtCOVERAGE_TEST_MARKER();\r
464         }\r
465 \r
466         /* Do the block being inserted, and the block it is being inserted before\r
467         make a contiguous block of memory? */\r
468         puc = ( uint8_t * ) pxBlockToInsert;\r
469         if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )\r
470         {\r
471                 if( pxIterator->pxNextFreeBlock != pxEnd )\r
472                 {\r
473                         /* Form one big block from the two blocks. */\r
474                         pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;\r
475                         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;\r
476                 }\r
477                 else\r
478                 {\r
479                         pxBlockToInsert->pxNextFreeBlock = pxEnd;\r
480                 }\r
481         }\r
482         else\r
483         {\r
484                 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;\r
485         }\r
486 \r
487         /* If the block being inserted plugged a gab, so was merged with the block\r
488         before and the block after, then it's pxNextFreeBlock pointer will have\r
489         already been set, and should not be set here as that would make it point\r
490         to itself. */\r
491         if( pxIterator != pxBlockToInsert )\r
492         {\r
493                 pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
494         }\r
495         else\r
496         {\r
497                 mtCOVERAGE_TEST_MARKER();\r
498         }\r
499 }\r
500 \r