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