]> git.sur5r.net Git - freertos/blob - Source/portable/MemMang/heap_2.c
3d7a1fa5fd1a0887d3fc317e7a298bb03f8b471b
[freertos] / Source / portable / MemMang / heap_2.c
1 /*\r
2         FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*\r
37  * A sample implementation of pvPortMalloc() and vPortFree() that permits\r
38  * allocated blocks to be freed, but does not combine adjacent free blocks\r
39  * into a single larger block.\r
40  *\r
41  * See heap_1.c and heap_3.c for alternative implementations, and the memory\r
42  * management pages of http://www.FreeRTOS.org for more information.\r
43  */\r
44 #include <stdlib.h>\r
45 \r
46 #include "FreeRTOS.h"\r
47 #include "task.h"\r
48 \r
49 /* Setup the correct byte alignment mask for the defined byte alignment. */\r
50 \r
51 #if portBYTE_ALIGNMENT == 8\r
52         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0007 )\r
53 #endif\r
54 \r
55 #if portBYTE_ALIGNMENT == 4\r
56         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0003 )\r
57 #endif\r
58 \r
59 #if portBYTE_ALIGNMENT == 2\r
60         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0001 )\r
61 #endif\r
62 \r
63 #if portBYTE_ALIGNMENT == 1\r
64         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0000 )\r
65 #endif\r
66 \r
67 #ifndef heapBYTE_ALIGNMENT_MASK\r
68         #error "Invalid portBYTE_ALIGNMENT definition"\r
69 #endif\r
70 \r
71 /* Allocate the memory for the heap.  The struct is used to force byte\r
72 alignment without using any non-portable code. */\r
73 static struct xRTOS_HEAP\r
74 {\r
75         unsigned portLONG ulDummy;\r
76         unsigned portCHAR ucHeap[ configTOTAL_HEAP_SIZE ];\r
77 } xHeap;\r
78 \r
79 /* Define the linked list structure.  This is used to link free blocks in order\r
80 of their size. */\r
81 typedef struct A_BLOCK_LINK\r
82 {\r
83         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
84         size_t xBlockSize;                                              /*<< The size of the free block. */\r
85 } xBlockLink;\r
86 \r
87 \r
88 static const unsigned portSHORT  heapSTRUCT_SIZE        = ( sizeof( xBlockLink ) + ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );\r
89 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )\r
90 \r
91 /* Create a couple of list links to mark the start and end of the list. */\r
92 static xBlockLink xStart, xEnd;\r
93 \r
94 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */\r
95 \r
96 /*\r
97  * Insert a block into the list of free blocks - which is ordered by size of\r
98  * the block.  Small blocks at the start of the list and large blocks at the end\r
99  * of the list.\r
100  */\r
101 #define prvInsertBlockIntoFreeList( pxBlockToInsert )                                                           \\r
102 {                                                                                                                                                                       \\r
103 xBlockLink *pxIterator;                                                                                                                         \\r
104 size_t xBlockSize;                                                                                                                                      \\r
105                                                                                                                                                                         \\r
106         xBlockSize = pxBlockToInsert->xBlockSize;                                                                               \\r
107                                                                                                                                                                         \\r
108         /* Iterate through the list until a block is found that has a larger size */    \\r
109         /* than the block we are inserting. */                                                                                  \\r
110         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock )     \\r
111         {                                                                                                                                                               \\r
112                 /* There is nothing to do here - just iterate to the correct position. */       \\r
113         }                                                                                                                                                               \\r
114                                                                                                                                                                         \\r
115         /* Update the list to include the block being inserted in the correct */                \\r
116         /* position. */                                                                                                                                 \\r
117         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;                                 \\r
118         pxIterator->pxNextFreeBlock = pxBlockToInsert;                                                                  \\r
119 }\r
120 /*-----------------------------------------------------------*/\r
121 \r
122 #define prvHeapInit()                                                                                                                           \\r
123 {                                                                                                                                                                       \\r
124 xBlockLink *pxFirstFreeBlock;                                                                                                           \\r
125                                                                                                                                                                         \\r
126         /* xStart is used to hold a pointer to the first item in the list of free */    \\r
127         /* blocks.  The void cast is used to prevent compiler warnings. */                              \\r
128         xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap;                                                               \\r
129         xStart.xBlockSize = ( size_t ) 0;                                                                                               \\r
130                                                                                                                                                                         \\r
131         /* xEnd is used to mark the end of the list of free blocks. */                                  \\r
132         xEnd.xBlockSize = configTOTAL_HEAP_SIZE;                                                                                \\r
133         xEnd.pxNextFreeBlock = NULL;                                                                                                    \\r
134                                                                                                                                                                         \\r
135         /* To start with there is a single free block that is sized to take up the              \\r
136         entire heap space. */                                                                                                                   \\r
137         pxFirstFreeBlock = ( void * ) xHeap.ucHeap;                                                                             \\r
138         pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE;                                                   \\r
139         pxFirstFreeBlock->pxNextFreeBlock = &xEnd;                                                                              \\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 void *pvPortMalloc( size_t xWantedSize )\r
144 {\r
145 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
146 static portBASE_TYPE xHeapHasBeenInitialised = pdFALSE;\r
147 void *pvReturn = NULL;\r
148 \r
149         vTaskSuspendAll();\r
150         {\r
151                 /* If this is the first call to malloc then the heap will require\r
152                 initialisation to setup the list of free blocks. */\r
153                 if( xHeapHasBeenInitialised == pdFALSE )\r
154                 {\r
155                         prvHeapInit();\r
156                         xHeapHasBeenInitialised = pdTRUE;\r
157                 }\r
158 \r
159                 /* The wanted size is increased so it can contain a xBlockLink\r
160                 structure in addition to the requested amount of bytes. */\r
161                 if( xWantedSize > 0 )\r
162                 {\r
163                         xWantedSize += heapSTRUCT_SIZE;\r
164 \r
165                         /* Ensure that blocks are always aligned to the required number of bytes. */\r
166                         if( xWantedSize & heapBYTE_ALIGNMENT_MASK )\r
167                         {\r
168                                 /* Byte alignment required. */\r
169                                 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & heapBYTE_ALIGNMENT_MASK ) );\r
170                         }\r
171                 }\r
172 \r
173                 if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_HEAP_SIZE ) )\r
174                 {\r
175                         /* Blocks are stored in byte order - traverse the list from the start\r
176                         (smallest) block until one of adequate size is found. */\r
177                         pxPreviousBlock = &xStart;\r
178                         pxBlock = xStart.pxNextFreeBlock;\r
179                         while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock ) )\r
180                         {\r
181                                 pxPreviousBlock = pxBlock;\r
182                                 pxBlock = pxBlock->pxNextFreeBlock;\r
183                         }\r
184 \r
185                         /* If we found the end marker then a block of adequate size was not found. */\r
186                         if( pxBlock != &xEnd )\r
187                         {\r
188                                 /* Return the memory space - jumping over the xBlockLink structure\r
189                                 at its start. */\r
190                                 pvReturn = ( void * ) ( ( ( unsigned portCHAR * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );\r
191 \r
192                                 /* This block is being returned for use so must be taken our of the\r
193                                 list of free blocks. */\r
194                                 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
195 \r
196                                 /* If the block is larger than required it can be split into two. */\r
197                                 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
198                                 {\r
199                                         /* This block is to be split into two.  Create a new block\r
200                                         following the number of bytes requested. The void cast is\r
201                                         used to prevent byte alignment warnings from the compiler. */\r
202                                         pxNewBlockLink = ( void * ) ( ( ( unsigned portCHAR * ) pxBlock ) + xWantedSize );\r
203                                         \r
204                                         /* Calculate the sizes of two blocks split from the single\r
205                                         block. */\r
206                                         pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize; \r
207                                         pxBlock->xBlockSize = xWantedSize;                      \r
208                                         \r
209                                         /* Insert the new block into the list of free blocks. */\r
210                                         prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
211                                 }\r
212                         }\r
213                 }\r
214         }\r
215         xTaskResumeAll();\r
216 \r
217         return pvReturn;\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 void vPortFree( void *pv )\r
222 {\r
223 unsigned portCHAR *puc = ( unsigned portCHAR * ) pv;\r
224 xBlockLink *pxLink;\r
225 \r
226         if( pv )\r
227         {\r
228                 /* The memory being freed will have an xBlockLink structure immediately\r
229                 before it. */\r
230                 puc -= heapSTRUCT_SIZE;\r
231 \r
232                 /* This casting is to keep the compiler from issuing warnings. */\r
233                 pxLink = ( void * ) puc;\r
234 \r
235                 vTaskSuspendAll();\r
236                 {                               \r
237                         /* Add this block to the list of free blocks. */\r
238                         prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );\r
239                 }\r
240                 xTaskResumeAll();\r
241         }\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r