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