]> git.sur5r.net Git - freertos/blob - Source/portable/MemMang/heap_2.c
Change where the free heap space variable is initialised.
[freertos] / Source / portable / MemMang / heap_2.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     This file is part of the FreeRTOS distribution.\r
5 \r
6     FreeRTOS is free software; you can redistribute it and/or modify it    under\r
7     the terms of the GNU General Public License (version 2) as published by the\r
8     Free Software Foundation and modified by the FreeRTOS exception.\r
9     **NOTE** The exception to the GPL is included to allow you to distribute a\r
10     combined work that includes FreeRTOS without being obliged to provide the\r
11     source code for proprietary components outside of the FreeRTOS kernel.\r
12     Alternative commercial license and support terms are also available upon\r
13     request.  See the licensing section of http://www.FreeRTOS.org for full\r
14     license details.\r
15 \r
16     FreeRTOS is distributed in the hope that it will be useful,    but WITHOUT\r
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19     more details.\r
20 \r
21     You should have received a copy of the GNU General Public License along\r
22     with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23     Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26     ***************************************************************************\r
27     *                                                                         *\r
28     * The FreeRTOS eBook and reference manual are available to purchase for a *\r
29     * small fee. Help yourself get started quickly while also helping the     *\r
30     * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
31     *                                                                         *\r
32     ***************************************************************************\r
33 \r
34     1 tab == 4 spaces!\r
35 \r
36     Please ensure to read the configuration and relevant port sections of the\r
37     online documentation.\r
38 \r
39     http://www.FreeRTOS.org - Documentation, latest information, license and\r
40     contact details.\r
41 \r
42     http://www.SafeRTOS.com - A version that is certified for use in safety\r
43     critical systems.\r
44 \r
45     http://www.OpenRTOS.com - Commercial support, development, porting,\r
46     licensing and training services.\r
47 */\r
48 \r
49 /*\r
50  * A sample implementation of pvPortMalloc() and vPortFree() that permits\r
51  * allocated blocks to be freed, but does not combine adjacent free blocks\r
52  * into a single larger block.\r
53  *\r
54  * See heap_1.c and heap_3.c for alternative implementations, and the memory\r
55  * management pages of http://www.FreeRTOS.org for more information.\r
56  */\r
57 #include <stdlib.h>\r
58 \r
59 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
60 all the API functions to use the MPU wrappers.  That should only be done when\r
61 task.h is included from an application file. */\r
62 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
63 \r
64 #include "FreeRTOS.h"\r
65 #include "task.h"\r
66 \r
67 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
68 \r
69 /* Allocate the memory for the heap.  The struct is used to force byte\r
70 alignment without using any non-portable code. */\r
71 static union xRTOS_HEAP\r
72 {\r
73         #if portBYTE_ALIGNMENT == 8\r
74                 volatile portDOUBLE dDummy;\r
75         #else\r
76                 volatile unsigned long ulDummy;\r
77         #endif\r
78         unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];\r
79 } xHeap;\r
80 \r
81 /* Define the linked list structure.  This is used to link free blocks in order\r
82 of their size. */\r
83 typedef struct A_BLOCK_LINK\r
84 {\r
85         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
86         size_t xBlockSize;                                              /*<< The size of the free block. */\r
87 } xBlockLink;\r
88 \r
89 \r
90 static const unsigned short  heapSTRUCT_SIZE    = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );\r
91 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )\r
92 \r
93 /* Create a couple of list links to mark the start and end of the list. */\r
94 static xBlockLink xStart, xEnd;\r
95 \r
96 /* Keeps track of the number of free bytes remaining, but says nothing about\r
97 fragmentation. */\r
98 static size_t xFreeBytesRemaining;\r
99 \r
100 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */\r
101 \r
102 /*\r
103  * Insert a block into the list of free blocks - which is ordered by size of\r
104  * the block.  Small blocks at the start of the list and large blocks at the end\r
105  * of the list.\r
106  */\r
107 #define prvInsertBlockIntoFreeList( pxBlockToInsert )                                                           \\r
108 {                                                                                                                                                                       \\r
109 xBlockLink *pxIterator;                                                                                                                         \\r
110 size_t xBlockSize;                                                                                                                                      \\r
111                                                                                                                                                                         \\r
112         xBlockSize = pxBlockToInsert->xBlockSize;                                                                               \\r
113                                                                                                                                                                         \\r
114         /* Iterate through the list until a block is found that has a larger size */    \\r
115         /* than the block we are inserting. */                                                                                  \\r
116         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock )     \\r
117         {                                                                                                                                                               \\r
118                 /* There is nothing to do here - just iterate to the correct position. */       \\r
119         }                                                                                                                                                               \\r
120                                                                                                                                                                         \\r
121         /* Update the list to include the block being inserted in the correct */                \\r
122         /* position. */                                                                                                                                 \\r
123         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;                                 \\r
124         pxIterator->pxNextFreeBlock = pxBlockToInsert;                                                                  \\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 #define prvHeapInit()                                                                                                                           \\r
129 {                                                                                                                                                                       \\r
130 xBlockLink *pxFirstFreeBlock;                                                                                                           \\r
131                                                                                                                                                                         \\r
132         /* xStart is used to hold a pointer to the first item in the list of free */    \\r
133         /* blocks.  The void cast is used to prevent compiler warnings. */                              \\r
134         xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap;                                                               \\r
135         xStart.xBlockSize = ( size_t ) 0;                                                                                               \\r
136                                                                                                                                                                         \\r
137         /* xEnd is used to mark the end of the list of free blocks. */                                  \\r
138         xEnd.xBlockSize = configTOTAL_HEAP_SIZE;                                                                                \\r
139         xEnd.pxNextFreeBlock = NULL;                                                                                                    \\r
140                                                                                                                                                                         \\r
141         /* To start with there is a single free block that is sized to take up the              \\r
142         entire heap space. */                                                                                                                   \\r
143         pxFirstFreeBlock = ( void * ) xHeap.ucHeap;                                                                             \\r
144         pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE;                                                   \\r
145         pxFirstFreeBlock->pxNextFreeBlock = &xEnd;                                                                              \\r
146                                                                                                                                                                         \\r
147         xFreeBytesRemaining = configTOTAL_HEAP_SIZE;                                                                    \\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 void *pvPortMalloc( size_t xWantedSize )\r
152 {\r
153 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
154 static portBASE_TYPE xHeapHasBeenInitialised = pdFALSE;\r
155 void *pvReturn = NULL;\r
156 \r
157         vTaskSuspendAll();\r
158         {\r
159                 /* If this is the first call to malloc then the heap will require\r
160                 initialisation to setup the list of free blocks. */\r
161                 if( xHeapHasBeenInitialised == pdFALSE )\r
162                 {\r
163                         prvHeapInit();\r
164                         xHeapHasBeenInitialised = pdTRUE;\r
165                 }\r
166 \r
167                 /* The wanted size is increased so it can contain a xBlockLink\r
168                 structure in addition to the requested amount of bytes. */\r
169                 if( xWantedSize > 0 )\r
170                 {\r
171                         xWantedSize += heapSTRUCT_SIZE;\r
172 \r
173                         /* Ensure that blocks are always aligned to the required number of bytes. */\r
174                         if( xWantedSize & portBYTE_ALIGNMENT_MASK )\r
175                         {\r
176                                 /* Byte alignment required. */\r
177                                 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
178                         }\r
179                 }\r
180 \r
181                 if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_HEAP_SIZE ) )\r
182                 {\r
183                         /* Blocks are stored in byte order - traverse the list from the start\r
184                         (smallest) block until one of adequate size is found. */\r
185                         pxPreviousBlock = &xStart;\r
186                         pxBlock = xStart.pxNextFreeBlock;\r
187                         while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock ) )\r
188                         {\r
189                                 pxPreviousBlock = pxBlock;\r
190                                 pxBlock = pxBlock->pxNextFreeBlock;\r
191                         }\r
192 \r
193                         /* If we found the end marker then a block of adequate size was not found. */\r
194                         if( pxBlock != &xEnd )\r
195                         {\r
196                                 /* Return the memory space - jumping over the xBlockLink structure\r
197                                 at its start. */\r
198                                 pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );\r
199 \r
200                                 /* This block is being returned for use so must be taken our of the\r
201                                 list of free blocks. */\r
202                                 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
203 \r
204                                 /* If the block is larger than required it can be split into two. */\r
205                                 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
206                                 {\r
207                                         /* This block is to be split into two.  Create a new block\r
208                                         following the number of bytes requested. The void cast is\r
209                                         used to prevent byte alignment warnings from the compiler. */\r
210                                         pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );\r
211 \r
212                                         /* Calculate the sizes of two blocks split from the single\r
213                                         block. */\r
214                                         pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
215                                         pxBlock->xBlockSize = xWantedSize;\r
216 \r
217                                         /* Insert the new block into the list of free blocks. */\r
218                                         prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
219                                 }\r
220                                 \r
221                                 xFreeBytesRemaining -= xWantedSize;\r
222                         }\r
223                 }\r
224         }\r
225         xTaskResumeAll();\r
226 \r
227         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
228         {\r
229                 if( pvReturn == NULL )\r
230                 {\r
231                         extern void vApplicationMallocFailedHook( void );\r
232                         vApplicationMallocFailedHook();\r
233                 }\r
234         }\r
235         #endif\r
236 \r
237         return pvReturn;\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 void vPortFree( void *pv )\r
242 {\r
243 unsigned char *puc = ( unsigned char * ) pv;\r
244 xBlockLink *pxLink;\r
245 \r
246         if( pv )\r
247         {\r
248                 /* The memory being freed will have an xBlockLink structure immediately\r
249                 before it. */\r
250                 puc -= heapSTRUCT_SIZE;\r
251 \r
252                 /* This casting is to keep the compiler from issuing warnings. */\r
253                 pxLink = ( void * ) puc;\r
254 \r
255                 vTaskSuspendAll();\r
256                 {\r
257                         /* Add this block to the list of free blocks. */\r
258                         prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );\r
259                         xFreeBytesRemaining += pxLink->xBlockSize;\r
260                 }\r
261                 xTaskResumeAll();\r
262         }\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 size_t xPortGetFreeHeapSize( void )\r
267 {\r
268         return xFreeBytesRemaining;\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 void vPortInitialiseBlocks( void )\r
273 {\r
274         /* This just exists to keep the linker quiet. */\r
275 }\r