]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MemMang/heap_5.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Source / portable / MemMang / heap_5.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*\r
97  * A sample implementation of pvPortMalloc() that allows the heap to be defined\r
98  * across multiple non-contigous blocks and combines (coalescences) adjacent\r
99  * memory blocks as they are freed.\r
100  *\r
101  * See heap_1.c, heap_2.c, heap_3.c and heap_4.c for alternative\r
102  * implementations, and the memory management pages of http://www.FreeRTOS.org\r
103  * for more information.\r
104  *\r
105  * Usage notes:\r
106  *\r
107  * vPortDefineHeapRegions() ***must*** be called before pvPortMalloc().\r
108  * pvPortMalloc() will be called if any task objects (tasks, queues, event\r
109  * groups, etc.) are created, therefore vPortDefineHeapRegions() ***must*** be\r
110  * called before any other objects are defined.\r
111  *\r
112  * vPortDefineHeapRegions() takes a single parameter.  The parameter is an array\r
113  * of HeapRegion_t structures.  HeapRegion_t is defined in portable.h as\r
114  *\r
115  * typedef struct HeapRegion\r
116  * {\r
117  *      uint8_t *pucStartAddress; << Start address of a block of memory that will be part of the heap.\r
118  *      size_t xSizeInBytes;      << Size of the block of memory.\r
119  * } HeapRegion_t;\r
120  *\r
121  * The array is terminated using a NULL zero sized region definition, and the\r
122  * memory regions defined in the array ***must*** appear in address order from\r
123  * low address to high address.  So the following is a valid example of how\r
124  * to use the function.\r
125  *\r
126  * HeapRegion_t xHeapRegions[] =\r
127  * {\r
128  *      { ( uint8_t * ) 0x80000000UL, 0x10000 }, << Defines a block of 0x10000 bytes starting at address 0x80000000\r
129  *      { ( uint8_t * ) 0x90000000UL, 0xa0000 }, << Defines a block of 0xa0000 bytes starting at address of 0x90000000\r
130  *      { NULL, 0 }                << Terminates the array.\r
131  * };\r
132  *\r
133  * vPortDefineHeapRegions( xHeapRegions ); << Pass the array into vPortDefineHeapRegions().\r
134  *\r
135  * Note 0x80000000 is the lower address so appears in the array first.\r
136  *\r
137  */\r
138 #include <stdlib.h>\r
139 \r
140 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
141 all the API functions to use the MPU wrappers.  That should only be done when\r
142 task.h is included from an application file. */\r
143 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
144 \r
145 #include "FreeRTOS.h"\r
146 #include "task.h"\r
147 \r
148 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
149 \r
150 /* Block sizes must not get too small. */\r
151 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( uxHeapStructSize << 1 ) )\r
152 \r
153 /* Assumes 8bit bytes! */\r
154 #define heapBITS_PER_BYTE               ( ( size_t ) 8 )\r
155 \r
156 /* Define the linked list structure.  This is used to link free blocks in order\r
157 of their memory address. */\r
158 typedef struct A_BLOCK_LINK\r
159 {\r
160         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
161         size_t xBlockSize;                                              /*<< The size of the free block. */\r
162 } BlockLink_t;\r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /*\r
167  * Inserts a block of memory that is being freed into the correct position in\r
168  * the list of free memory blocks.  The block being freed will be merged with\r
169  * the block in front it and/or the block behind it if the memory blocks are\r
170  * adjacent to each other.\r
171  */\r
172 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );\r
173 \r
174 /*-----------------------------------------------------------*/\r
175 \r
176 /* The size of the structure placed at the beginning of each allocated memory\r
177 block must by correctly byte aligned. */\r
178 static const uint32_t uxHeapStructSize  = ( ( sizeof ( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );\r
179 \r
180 /* Create a couple of list links to mark the start and end of the list. */\r
181 static BlockLink_t xStart, *pxEnd = NULL;\r
182 \r
183 /* Keeps track of the number of free bytes remaining, but says nothing about\r
184 fragmentation. */\r
185 static size_t xFreeBytesRemaining = 0;\r
186 static size_t xMinimumEverFreeBytesRemaining = 0;\r
187 \r
188 /* Gets set to the top bit of an size_t type.  When this bit in the xBlockSize\r
189 member of an BlockLink_t structure is set then the block belongs to the\r
190 application.  When the bit is free the block is still part of the free heap\r
191 space. */\r
192 static size_t xBlockAllocatedBit = 0;\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 void *pvPortMalloc( size_t xWantedSize )\r
197 {\r
198 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
199 void *pvReturn = NULL;\r
200 \r
201         /* The heap must be initialised before the first call to\r
202         prvPortMalloc(). */\r
203         configASSERT( pxEnd );\r
204 \r
205         vTaskSuspendAll();\r
206         {\r
207                 /* Check the requested block size is not so large that the top bit is\r
208                 set.  The top bit of the block size member of the BlockLink_t structure\r
209                 is used to determine who owns the block - the application or the\r
210                 kernel, so it must be free. */\r
211                 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )\r
212                 {\r
213                         /* The wanted size is increased so it can contain a BlockLink_t\r
214                         structure in addition to the requested amount of bytes. */\r
215                         if( xWantedSize > 0 )\r
216                         {\r
217                                 xWantedSize += uxHeapStructSize;\r
218 \r
219                                 /* Ensure that blocks are always aligned to the required number\r
220                                 of bytes. */\r
221                                 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )\r
222                                 {\r
223                                         /* Byte alignment required. */\r
224                                         xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
225                                 }\r
226                                 else\r
227                                 {\r
228                                         mtCOVERAGE_TEST_MARKER();\r
229                                 }\r
230                         }\r
231                         else\r
232                         {\r
233                                 mtCOVERAGE_TEST_MARKER();\r
234                         }\r
235 \r
236                         if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )\r
237                         {\r
238                                 /* Traverse the list from the start     (lowest address) block until\r
239                                 one     of adequate size is found. */\r
240                                 pxPreviousBlock = &xStart;\r
241                                 pxBlock = xStart.pxNextFreeBlock;\r
242                                 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )\r
243                                 {\r
244                                         pxPreviousBlock = pxBlock;\r
245                                         pxBlock = pxBlock->pxNextFreeBlock;\r
246                                 }\r
247 \r
248                                 /* If the end marker was reached then a block of adequate size\r
249                                 was     not found. */\r
250                                 if( pxBlock != pxEnd )\r
251                                 {\r
252                                         /* Return the memory space pointed to - jumping over the\r
253                                         BlockLink_t structure at its start. */\r
254                                         pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + uxHeapStructSize );\r
255 \r
256                                         /* This block is being returned for use so must be taken out\r
257                                         of the list of free blocks. */\r
258                                         pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
259 \r
260                                         /* If the block is larger than required it can be split into\r
261                                         two. */\r
262                                         if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
263                                         {\r
264                                                 /* This block is to be split into two.  Create a new\r
265                                                 block following the number of bytes requested. The void\r
266                                                 cast is used to prevent byte alignment warnings from the\r
267                                                 compiler. */\r
268                                                 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
269 \r
270                                                 /* Calculate the sizes of two blocks split from the\r
271                                                 single block. */\r
272                                                 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
273                                                 pxBlock->xBlockSize = xWantedSize;\r
274 \r
275                                                 /* Insert the new block into the list of free blocks. */\r
276                                                 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
277                                         }\r
278                                         else\r
279                                         {\r
280                                                 mtCOVERAGE_TEST_MARKER();\r
281                                         }\r
282 \r
283                                         xFreeBytesRemaining -= pxBlock->xBlockSize;\r
284 \r
285                                         if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )\r
286                                         {\r
287                                                 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;\r
288                                         }\r
289                                         else\r
290                                         {\r
291                                                 mtCOVERAGE_TEST_MARKER();\r
292                                         }\r
293 \r
294                                         /* The block is being returned - it is allocated and owned\r
295                                         by the application and has no "next" block. */\r
296                                         pxBlock->xBlockSize |= xBlockAllocatedBit;\r
297                                         pxBlock->pxNextFreeBlock = NULL;\r
298                                 }\r
299                                 else\r
300                                 {\r
301                                         mtCOVERAGE_TEST_MARKER();\r
302                                 }\r
303                         }\r
304                         else\r
305                         {\r
306                                 mtCOVERAGE_TEST_MARKER();\r
307                         }\r
308                 }\r
309                 else\r
310                 {\r
311                         mtCOVERAGE_TEST_MARKER();\r
312                 }\r
313 \r
314                 traceMALLOC( pvReturn, xWantedSize );\r
315         }\r
316         ( void ) xTaskResumeAll();\r
317 \r
318         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
319         {\r
320                 if( pvReturn == NULL )\r
321                 {\r
322                         extern void vApplicationMallocFailedHook( void );\r
323                         vApplicationMallocFailedHook();\r
324                 }\r
325                 else\r
326                 {\r
327                         mtCOVERAGE_TEST_MARKER();\r
328                 }\r
329         }\r
330         #endif\r
331 \r
332         return pvReturn;\r
333 }\r
334 /*-----------------------------------------------------------*/\r
335 \r
336 void vPortFree( void *pv )\r
337 {\r
338 uint8_t *puc = ( uint8_t * ) pv;\r
339 BlockLink_t *pxLink;\r
340 \r
341         if( pv != NULL )\r
342         {\r
343                 /* The memory being freed will have an BlockLink_t structure immediately\r
344                 before it. */\r
345                 puc -= uxHeapStructSize;\r
346 \r
347                 /* This casting is to keep the compiler from issuing warnings. */\r
348                 pxLink = ( void * ) puc;\r
349 \r
350                 /* Check the block is actually allocated. */\r
351                 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );\r
352                 configASSERT( pxLink->pxNextFreeBlock == NULL );\r
353 \r
354                 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )\r
355                 {\r
356                         if( pxLink->pxNextFreeBlock == NULL )\r
357                         {\r
358                                 /* The block is being returned to the heap - it is no longer\r
359                                 allocated. */\r
360                                 pxLink->xBlockSize &= ~xBlockAllocatedBit;\r
361 \r
362                                 vTaskSuspendAll();\r
363                                 {\r
364                                         /* Add this block to the list of free blocks. */\r
365                                         xFreeBytesRemaining += pxLink->xBlockSize;\r
366                                         traceFREE( pv, pxLink->xBlockSize );\r
367                                         prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
368                                 }\r
369                                 ( void ) xTaskResumeAll();\r
370                         }\r
371                         else\r
372                         {\r
373                                 mtCOVERAGE_TEST_MARKER();\r
374                         }\r
375                 }\r
376                 else\r
377                 {\r
378                         mtCOVERAGE_TEST_MARKER();\r
379                 }\r
380         }\r
381 }\r
382 /*-----------------------------------------------------------*/\r
383 \r
384 size_t xPortGetFreeHeapSize( void )\r
385 {\r
386         return xFreeBytesRemaining;\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 size_t xPortGetMinimumEverFreeHeapSize( void )\r
391 {\r
392         return xMinimumEverFreeBytesRemaining;\r
393 }\r
394 /*-----------------------------------------------------------*/\r
395 \r
396 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )\r
397 {\r
398 BlockLink_t *pxIterator;\r
399 uint8_t *puc;\r
400 \r
401         /* Iterate through the list until a block is found that has a higher address\r
402         than the block being inserted. */\r
403         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )\r
404         {\r
405                 /* Nothing to do here, just iterate to the right position. */\r
406         }\r
407 \r
408         /* Do the block being inserted, and the block it is being inserted after\r
409         make a contiguous block of memory? */\r
410         puc = ( uint8_t * ) pxIterator;\r
411         if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )\r
412         {\r
413                 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
414                 pxBlockToInsert = pxIterator;\r
415         }\r
416         else\r
417         {\r
418                 mtCOVERAGE_TEST_MARKER();\r
419         }\r
420 \r
421         /* Do the block being inserted, and the block it is being inserted before\r
422         make a contiguous block of memory? */\r
423         puc = ( uint8_t * ) pxBlockToInsert;\r
424         if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )\r
425         {\r
426                 if( pxIterator->pxNextFreeBlock != pxEnd )\r
427                 {\r
428                         /* Form one big block from the two blocks. */\r
429                         pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;\r
430                         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;\r
431                 }\r
432                 else\r
433                 {\r
434                         pxBlockToInsert->pxNextFreeBlock = pxEnd;\r
435                 }\r
436         }\r
437         else\r
438         {\r
439                 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;\r
440         }\r
441 \r
442         /* If the block being inserted plugged a gab, so was merged with the block\r
443         before and the block after, then it's pxNextFreeBlock pointer will have\r
444         already been set, and should not be set here as that would make it point\r
445         to itself. */\r
446         if( pxIterator != pxBlockToInsert )\r
447         {\r
448                 pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
449         }\r
450         else\r
451         {\r
452                 mtCOVERAGE_TEST_MARKER();\r
453         }\r
454 }\r
455 /*-----------------------------------------------------------*/\r
456 \r
457 void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions )\r
458 {\r
459 BlockLink_t *pxFirstFreeBlockInRegion = NULL, *pxPreviousFreeBlock;\r
460 uint8_t *pucAlignedHeap;\r
461 size_t xTotalRegionSize, xTotalHeapSize = 0;\r
462 BaseType_t xDefinedRegions = 0;\r
463 uint32_t ulAddress;\r
464 const HeapRegion_t *pxHeapRegion;\r
465 \r
466         /* Can only call once! */\r
467         configASSERT( pxEnd == NULL );\r
468 \r
469         pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );\r
470 \r
471         while( pxHeapRegion->xSizeInBytes > 0 )\r
472         {\r
473                 xTotalRegionSize = pxHeapRegion->xSizeInBytes;\r
474 \r
475                 /* Ensure the heap region starts on a correctly aligned boundary. */\r
476                 ulAddress = ( uint32_t ) pxHeapRegion->pucStartAddress;\r
477                 if( ( ulAddress & portBYTE_ALIGNMENT_MASK ) != 0 )\r
478                 {\r
479                         ulAddress += ( portBYTE_ALIGNMENT - 1 );\r
480                         ulAddress &= ~portBYTE_ALIGNMENT_MASK;\r
481 \r
482                         /* Adjust the size for the bytes lost to alignment. */\r
483                         xTotalRegionSize -= ulAddress - ( uint32_t ) pxHeapRegion->pucStartAddress;\r
484                 }\r
485 \r
486                 pucAlignedHeap = ( uint8_t * ) ulAddress;\r
487 \r
488                 /* Set xStart if it has not already been set. */\r
489                 if( xDefinedRegions == 0 )\r
490                 {\r
491                         /* xStart is used to hold a pointer to the first item in the list of\r
492                         free blocks.  The void cast is used to prevent compiler warnings. */\r
493                         xStart.pxNextFreeBlock = ( BlockLink_t * ) pucAlignedHeap;\r
494                         xStart.xBlockSize = ( size_t ) 0;\r
495                 }\r
496                 else\r
497                 {\r
498                         /* Should only get here if one region has already been added to the\r
499                         heap. */\r
500                         configASSERT( pxEnd != NULL );\r
501 \r
502                         /* Check blocks are passed in with increasing start addresses. */\r
503                         configASSERT( ulAddress > ( uint32_t ) pxEnd );\r
504                 }\r
505 \r
506                 /* Remember the location of the end marker in the previous region, if\r
507                 any. */\r
508                 pxPreviousFreeBlock = pxEnd;\r
509 \r
510                 /* pxEnd is used to mark the end of the list of free blocks and is\r
511                 inserted at the end of the region space. */\r
512                 ulAddress = ( ( uint32_t ) pucAlignedHeap ) + xTotalRegionSize;\r
513                 ulAddress -= uxHeapStructSize;\r
514                 ulAddress &= ~portBYTE_ALIGNMENT_MASK;\r
515                 pxEnd = ( BlockLink_t * ) ulAddress;\r
516                 pxEnd->xBlockSize = 0;\r
517                 pxEnd->pxNextFreeBlock = NULL;\r
518 \r
519                 /* To start with there is a single free block in this region that is\r
520                 sized to take up the entire heap region minus the space taken by the\r
521                 free block structure. */\r
522                 pxFirstFreeBlockInRegion = ( BlockLink_t * ) pucAlignedHeap;\r
523                 pxFirstFreeBlockInRegion->xBlockSize = ulAddress - ( uint32_t ) pxFirstFreeBlockInRegion;\r
524                 pxFirstFreeBlockInRegion->pxNextFreeBlock = pxEnd;\r
525 \r
526                 /* If this is not the first region that makes up the entire heap space\r
527                 then link the previous region to this region. */\r
528                 if( pxPreviousFreeBlock != NULL )\r
529                 {\r
530                         pxPreviousFreeBlock->pxNextFreeBlock = pxFirstFreeBlockInRegion;\r
531                 }\r
532 \r
533                 xTotalHeapSize += pxFirstFreeBlockInRegion->xBlockSize;\r
534 \r
535                 /* Move onto the next HeapRegion_t structure. */\r
536                 xDefinedRegions++;\r
537                 pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );\r
538         }\r
539 \r
540         xMinimumEverFreeBytesRemaining = xTotalHeapSize;\r
541         xFreeBytesRemaining = xTotalHeapSize;\r
542 \r
543         /* Check something was actually defined before it is accessed. */\r
544         configASSERT( xTotalHeapSize );\r
545 \r
546         /* Work out the position of the top bit in a size_t variable. */\r
547         xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );\r
548 }\r
549 \r