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