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