]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MemMang/heap_4.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Source / portable / MemMang / heap_4.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53 \r
54     http://www.FreeRTOS.org - Documentation, training, latest information,\r
55     license and contact details.\r
56 \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell\r
61     the code with commercial support, indemnification, and middleware, under\r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under\r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*\r
68  * A sample implementation of pvPortMalloc() and vPortFree() that combines \r
69  * (coalescences) adjacent memory blocks as they are freed, and in so doing \r
70  * limits memory fragmentation.\r
71  *\r
72  * See heap_1.c, heap_2.c and heap_3.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 /* Block sizes must not get too small. */\r
88 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )\r
89 \r
90 /* Allocate the memory for the heap.  The struct is used to force byte\r
91 alignment without using any non-portable code. */\r
92 static union xRTOS_HEAP\r
93 {\r
94         #if portBYTE_ALIGNMENT == 8\r
95                 volatile portDOUBLE dDummy;\r
96         #else\r
97                 volatile unsigned long ulDummy;\r
98         #endif\r
99         unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];\r
100 } xHeap;\r
101 \r
102 /* Define the linked list structure.  This is used to link free blocks in order\r
103 of their memory address. */\r
104 typedef struct A_BLOCK_LINK\r
105 {\r
106         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
107         size_t xBlockSize;                                              /*<< The size of the free block. */\r
108 } xBlockLink;\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * Inserts a block of memory that is being freed into the correct position in \r
114  * the list of free memory blocks.  The block being freed will be merged with\r
115  * the block in front it and/or the block behind it if the memory blocks are\r
116  * adjacent to each other.\r
117  */\r
118 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert );\r
119 \r
120 /*\r
121  * Called automatically to setup the required heap structures the first time\r
122  * pvPortMalloc() is called.\r
123  */\r
124 static void prvHeapInit( void );\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 /* The size of the structure placed at the beginning of each allocated memory\r
129 block must by correctly byte aligned. */\r
130 static const unsigned short heapSTRUCT_SIZE     = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );\r
131 \r
132 /* Ensure the pxEnd pointer will end up on the correct byte alignment. */\r
133 static const size_t xTotalHeapSize = ( ( size_t ) configTOTAL_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
134 \r
135 /* Create a couple of list links to mark the start and end of the list. */\r
136 static xBlockLink xStart, *pxEnd = NULL;\r
137 \r
138 /* Keeps track of the number of free bytes remaining, but says nothing about\r
139 fragmentation. */\r
140 static size_t xFreeBytesRemaining = ( ( size_t ) configTOTAL_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
141 \r
142 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */\r
143 \r
144 /*-----------------------------------------------------------*/\r
145 \r
146 void *pvPortMalloc( size_t xWantedSize )\r
147 {\r
148 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\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( pxEnd == NULL )\r
156                 {\r
157                         prvHeapInit();\r
158                 }\r
159 \r
160                 /* The wanted size is increased so it can contain a xBlockLink\r
161                 structure in addition to the requested amount of bytes. */\r
162                 if( xWantedSize > 0 )\r
163                 {\r
164                         xWantedSize += heapSTRUCT_SIZE;\r
165 \r
166                         /* Ensure that blocks are always aligned to the required number of \r
167                         bytes. */\r
168                         if( xWantedSize & portBYTE_ALIGNMENT_MASK )\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 < xTotalHeapSize ) )\r
176                 {\r
177                         /* Traverse the list from the start     (lowest address) block until one\r
178                         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 the end marker was reached then a block of adequate size was\r
188                         not found. */\r
189                         if( pxBlock != pxEnd )\r
190                         {\r
191                                 /* Return the memory space - jumping over the xBlockLink structure\r
192                                 at its start. */\r
193                                 pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );\r
194 \r
195                                 /* This block is being returned for use so must be taken out of\r
196                                 the     list of free blocks. */\r
197                                 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
198 \r
199                                 /* If the block is larger than required it can be split into two. */\r
200                                 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
201                                 {\r
202                                         /* This block is to be split into two.  Create a new block\r
203                                         following the number of bytes requested. The void cast is\r
204                                         used to prevent byte alignment warnings from the compiler. */\r
205                                         pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );\r
206 \r
207                                         /* Calculate the sizes of two blocks split from the single\r
208                                         block. */\r
209                                         pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
210                                         pxBlock->xBlockSize = xWantedSize;\r
211 \r
212                                         /* Insert the new block into the list of free blocks. */\r
213                                         prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
214                                 }\r
215 \r
216                                 xFreeBytesRemaining -= pxBlock->xBlockSize;\r
217                         }\r
218                 }\r
219         }\r
220         xTaskResumeAll();\r
221 \r
222         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
223         {\r
224                 if( pvReturn == NULL )\r
225                 {\r
226                         extern void vApplicationMallocFailedHook( void );\r
227                         vApplicationMallocFailedHook();\r
228                 }\r
229         }\r
230         #endif\r
231 \r
232         return pvReturn;\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vPortFree( void *pv )\r
237 {\r
238 unsigned char *puc = ( unsigned char * ) pv;\r
239 xBlockLink *pxLink;\r
240 \r
241         if( pv != NULL )\r
242         {\r
243                 /* The memory being freed will have an xBlockLink structure immediately\r
244                 before it. */\r
245                 puc -= heapSTRUCT_SIZE;\r
246 \r
247                 /* This casting is to keep the compiler from issuing warnings. */\r
248                 pxLink = ( void * ) puc;\r
249 \r
250                 vTaskSuspendAll();\r
251                 {\r
252                         /* Add this block to the list of free blocks. */\r
253                         xFreeBytesRemaining += pxLink->xBlockSize;\r
254                         prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );                      \r
255                 }\r
256                 xTaskResumeAll();\r
257         }\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 size_t xPortGetFreeHeapSize( void )\r
262 {\r
263         return xFreeBytesRemaining;\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 void vPortInitialiseBlocks( void )\r
268 {\r
269         /* This just exists to keep the linker quiet. */\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 static void prvHeapInit( void )\r
274 {\r
275 xBlockLink *pxFirstFreeBlock;\r
276 unsigned char *pucHeapEnd;\r
277 \r
278         /* Ensure the start of the heap is aligned. */\r
279         configASSERT( ( ( ( unsigned long ) xHeap.ucHeap ) & ( ( unsigned long ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );\r
280 \r
281         /* xStart is used to hold a pointer to the first item in the list of free\r
282         blocks.  The void cast is used to prevent compiler warnings. */\r
283         xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap;\r
284         xStart.xBlockSize = ( size_t ) 0;\r
285 \r
286         /* pxEnd is used to mark the end of the list of free blocks and is inserted\r
287         at the end of the heap space. */\r
288         pucHeapEnd = xHeap.ucHeap + xTotalHeapSize;\r
289         pucHeapEnd -= heapSTRUCT_SIZE;\r
290         pxEnd = ( void * ) pucHeapEnd;\r
291         configASSERT( ( ( ( unsigned long ) pxEnd ) & ( ( unsigned long ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );\r
292         pxEnd->xBlockSize = 0;\r
293         pxEnd->pxNextFreeBlock = NULL;\r
294 \r
295         /* To start with there is a single free block that is sized to take up the\r
296         entire heap space, minus the space taken by pxEnd. */\r
297         pxFirstFreeBlock = ( void * ) xHeap.ucHeap;\r
298         pxFirstFreeBlock->xBlockSize = xTotalHeapSize - heapSTRUCT_SIZE;\r
299         pxFirstFreeBlock->pxNextFreeBlock = pxEnd;\r
300 \r
301         /* The heap now contains pxEnd. */\r
302         xFreeBytesRemaining -= heapSTRUCT_SIZE;\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert )\r
307 {\r
308 xBlockLink *pxIterator;\r
309 unsigned char *puc;\r
310 \r
311         /* Iterate through the list until a block is found that has a higher address\r
312         than the block being inserted. */\r
313         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )\r
314         {\r
315                 /* Nothing to do here, just iterate to the right position. */\r
316         }\r
317 \r
318         /* Do the block being inserted, and the block it is being inserted after\r
319         make a contiguous block of memory? */   \r
320         puc = ( unsigned char * ) pxIterator;\r
321         if( ( puc + pxIterator->xBlockSize ) == ( unsigned char * ) pxBlockToInsert )\r
322         {\r
323                 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
324                 pxBlockToInsert = pxIterator;\r
325         }\r
326 \r
327         /* Do the block being inserted, and the block it is being inserted before\r
328         make a contiguous block of memory? */\r
329         puc = ( unsigned char * ) pxBlockToInsert;\r
330         if( ( puc + pxBlockToInsert->xBlockSize ) == ( unsigned char * ) pxIterator->pxNextFreeBlock )\r
331         {\r
332                 if( pxIterator->pxNextFreeBlock != pxEnd )\r
333                 {\r
334                         /* Form one big block from the two blocks. */\r
335                         pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;\r
336                         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;\r
337                 }\r
338                 else\r
339                 {\r
340                         pxBlockToInsert->pxNextFreeBlock = pxEnd;\r
341                 }\r
342         }\r
343         else\r
344         {\r
345                 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;         \r
346         }\r
347 \r
348         /* If the block being inserted plugged a gab, so was merged with the block\r
349         before and the block after, then it's pxNextFreeBlock pointer will have\r
350         already been set, and should not be set here as that would make it point\r
351         to itself. */\r
352         if( pxIterator != pxBlockToInsert )\r
353         {\r
354                 pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
355         }\r
356 }\r
357 \r