]> git.sur5r.net Git - freertos/blob - Source/portable/MemMang/heap_2.c
Remove the portBYTE_ALIGNMENT_MASK definitions as they are now in the common portable...
[freertos] / Source / portable / MemMang / heap_2.c
1 /*\r
2         FreeRTOS V5.4.2 - 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         * Looking for a quick start?  Then check out the FreeRTOS eBook!                  *\r
29         * See http://www.FreeRTOS.org/Documentation for details                            *\r
30         *                                                                                                                                                *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /*\r
49  * A sample implementation of pvPortMalloc() and vPortFree() that permits\r
50  * allocated blocks to be freed, but does not combine adjacent free blocks\r
51  * into a single larger block.\r
52  *\r
53  * See heap_1.c and heap_3.c for alternative implementations, and the memory\r
54  * management pages of http://www.FreeRTOS.org for more information.\r
55  */\r
56 #include <stdlib.h>\r
57 \r
58 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
59 all the API functions to use the MPU wrappers.  That should only be done when\r
60 task.h is included from an application file. */\r
61 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
62 \r
63 #include "FreeRTOS.h"\r
64 #include "task.h"\r
65 \r
66 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
67 \r
68 /* Allocate the memory for the heap.  The struct is used to force byte\r
69 alignment without using any non-portable code. */\r
70 static union xRTOS_HEAP\r
71 {\r
72         #if portBYTE_ALIGNMENT == 8\r
73                 volatile portDOUBLE dDummy;\r
74         #else\r
75                 volatile unsigned portLONG ulDummy;\r
76         #endif\r
77         unsigned portCHAR ucHeap[ configTOTAL_HEAP_SIZE ];\r
78 } xHeap;\r
79 \r
80 /* Define the linked list structure.  This is used to link free blocks in order\r
81 of their size. */\r
82 typedef struct A_BLOCK_LINK\r
83 {\r
84         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
85         size_t xBlockSize;                                              /*<< The size of the free block. */\r
86 } xBlockLink;\r
87 \r
88 \r
89 static const unsigned portSHORT  heapSTRUCT_SIZE        = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );\r
90 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )\r
91 \r
92 /* Create a couple of list links to mark the start and end of the list. */\r
93 static xBlockLink xStart, xEnd;\r
94 \r
95 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */\r
96 \r
97 /*\r
98  * Insert a block into the list of free blocks - which is ordered by size of\r
99  * the block.  Small blocks at the start of the list and large blocks at the end\r
100  * of the list.\r
101  */\r
102 #define prvInsertBlockIntoFreeList( pxBlockToInsert )                                                           \\r
103 {                                                                                                                                                                       \\r
104 xBlockLink *pxIterator;                                                                                                                         \\r
105 size_t xBlockSize;                                                                                                                                      \\r
106                                                                                                                                                                         \\r
107         xBlockSize = pxBlockToInsert->xBlockSize;                                                                               \\r
108                                                                                                                                                                         \\r
109         /* Iterate through the list until a block is found that has a larger size */    \\r
110         /* than the block we are inserting. */                                                                                  \\r
111         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock )     \\r
112         {                                                                                                                                                               \\r
113                 /* There is nothing to do here - just iterate to the correct position. */       \\r
114         }                                                                                                                                                               \\r
115                                                                                                                                                                         \\r
116         /* Update the list to include the block being inserted in the correct */                \\r
117         /* position. */                                                                                                                                 \\r
118         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;                                 \\r
119         pxIterator->pxNextFreeBlock = pxBlockToInsert;                                                                  \\r
120 }\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 #define prvHeapInit()                                                                                                                           \\r
124 {                                                                                                                                                                       \\r
125 xBlockLink *pxFirstFreeBlock;                                                                                                           \\r
126                                                                                                                                                                         \\r
127         /* xStart is used to hold a pointer to the first item in the list of free */    \\r
128         /* blocks.  The void cast is used to prevent compiler warnings. */                              \\r
129         xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap;                                                               \\r
130         xStart.xBlockSize = ( size_t ) 0;                                                                                               \\r
131                                                                                                                                                                         \\r
132         /* xEnd is used to mark the end of the list of free blocks. */                                  \\r
133         xEnd.xBlockSize = configTOTAL_HEAP_SIZE;                                                                                \\r
134         xEnd.pxNextFreeBlock = NULL;                                                                                                    \\r
135                                                                                                                                                                         \\r
136         /* To start with there is a single free block that is sized to take up the              \\r
137         entire heap space. */                                                                                                                   \\r
138         pxFirstFreeBlock = ( void * ) xHeap.ucHeap;                                                                             \\r
139         pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE;                                                   \\r
140         pxFirstFreeBlock->pxNextFreeBlock = &xEnd;                                                                              \\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 )\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 < configTOTAL_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 ) )\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 portCHAR * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );\r
192 \r
193                                 /* This block is being returned for use so must be taken our 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 portCHAR * ) 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                 }\r
215         }\r
216         xTaskResumeAll();\r
217 \r
218         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
219         {\r
220                 if( pvReturn == NULL )\r
221                 {\r
222                         extern void vApplicationMallocFailedHook( void );\r
223                         vApplicationMallocFailedHook();\r
224                 }\r
225         }\r
226         #endif\r
227 \r
228         return pvReturn;\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 void vPortFree( void *pv )\r
233 {\r
234 unsigned portCHAR *puc = ( unsigned portCHAR * ) pv;\r
235 xBlockLink *pxLink;\r
236 \r
237         if( pv )\r
238         {\r
239                 /* The memory being freed will have an xBlockLink structure immediately\r
240                 before it. */\r
241                 puc -= heapSTRUCT_SIZE;\r
242 \r
243                 /* This casting is to keep the compiler from issuing warnings. */\r
244                 pxLink = ( void * ) puc;\r
245 \r
246                 vTaskSuspendAll();\r
247                 {\r
248                         /* Add this block to the list of free blocks. */\r
249                         prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );\r
250                 }\r
251                 xTaskResumeAll();\r
252         }\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r