STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
heap_4.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() and vPortFree() that combines
72  * (coalescences) adjacent memory blocks as they are freed, and in so doing
73  * limits memory fragmentation.
74  *
75  * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
76  * memory management pages of http://www.FreeRTOS.org for more information.
77  */
78 #include <stdlib.h>
79 
80 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
81 all the API functions to use the MPU wrappers. That should only be done when
82 task.h is included from an application file. */
83 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
84 
85 #include "FreeRTOS.h"
86 #include "task.h"
87 
88 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
89 
90 /* Block sizes must not get too small. */
91 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
92 
93 /* Assumes 8bit bytes! */
94 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
95 
96 /* Allocate the memory for the heap. */
97 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )
98  /* The application writer has already defined the array used for the RTOS
99  heap - probably so it can be placed in a special segment or address. */
100  extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
101 #else
102  static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
103 #endif /* configAPPLICATION_ALLOCATED_HEAP */
104 
105 /* Define the linked list structure. This is used to link free blocks in order
106 of their memory address. */
107 typedef struct A_BLOCK_LINK
108 {
109  struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
110  size_t xBlockSize; /*<< The size of the free block. */
111 } BlockLink_t;
112 
113 /*-----------------------------------------------------------*/
114 
115 /*
116  * Inserts a block of memory that is being freed into the correct position in
117  * the list of free memory blocks. The block being freed will be merged with
118  * the block in front it and/or the block behind it if the memory blocks are
119  * adjacent to each other.
120  */
121 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
122 
123 /*
124  * Called automatically to setup the required heap structures the first time
125  * pvPortMalloc() is called.
126  */
127 static void prvHeapInit( void );
128 
129 /*-----------------------------------------------------------*/
130 
131 /* The size of the structure placed at the beginning of each allocated memory
132 block must by correctly byte aligned. */
133 static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
134 
135 /* Create a couple of list links to mark the start and end of the list. */
136 static BlockLink_t xStart, *pxEnd = NULL;
137 
138 /* Keeps track of the number of free bytes remaining, but says nothing about
139 fragmentation. */
140 static size_t xFreeBytesRemaining = 0U;
141 static size_t xMinimumEverFreeBytesRemaining = 0U;
142 
143 /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
144 member of an BlockLink_t structure is set then the block belongs to the
145 application. When the bit is free the block is still part of the free heap
146 space. */
147 static size_t xBlockAllocatedBit = 0;
148 
149 /*-----------------------------------------------------------*/
150 
151 void *pvPortMalloc( size_t xWantedSize )
152 {
153 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
154 void *pvReturn = NULL;
155 
156  vTaskSuspendAll();
157  {
158  /* If this is the first call to malloc then the heap will require
159  initialisation to setup the list of free blocks. */
160  if( pxEnd == NULL )
161  {
162  prvHeapInit();
163  }
164  else
165  {
167  }
168 
169  /* Check the requested block size is not so large that the top bit is
170  set. The top bit of the block size member of the BlockLink_t structure
171  is used to determine who owns the block - the application or the
172  kernel, so it must be free. */
173  if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
174  {
175  /* The wanted size is increased so it can contain a BlockLink_t
176  structure in addition to the requested amount of bytes. */
177  if( xWantedSize > 0 )
178  {
179  xWantedSize += xHeapStructSize;
180 
181  /* Ensure that blocks are always aligned to the required number
182  of bytes. */
183  if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
184  {
185  /* Byte alignment required. */
186  xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
187  configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
188  }
189  else
190  {
192  }
193  }
194  else
195  {
197  }
198 
199  if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
200  {
201  /* Traverse the list from the start (lowest address) block until
202  one of adequate size is found. */
203  pxPreviousBlock = &xStart;
204  pxBlock = xStart.pxNextFreeBlock;
205  while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
206  {
207  pxPreviousBlock = pxBlock;
208  pxBlock = pxBlock->pxNextFreeBlock;
209  }
210 
211  /* If the end marker was reached then a block of adequate size
212  was not found. */
213  if( pxBlock != pxEnd )
214  {
215  /* Return the memory space pointed to - jumping over the
216  BlockLink_t structure at its start. */
217  pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
218 
219  /* This block is being returned for use so must be taken out
220  of the list of free blocks. */
221  pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
222 
223  /* If the block is larger than required it can be split into
224  two. */
225  if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
226  {
227  /* This block is to be split into two. Create a new
228  block following the number of bytes requested. The void
229  cast is used to prevent byte alignment warnings from the
230  compiler. */
231  pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
232  configASSERT( ( ( ( size_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );
233 
234  /* Calculate the sizes of two blocks split from the
235  single block. */
236  pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
237  pxBlock->xBlockSize = xWantedSize;
238 
239  /* Insert the new block into the list of free blocks. */
240  prvInsertBlockIntoFreeList( pxNewBlockLink );
241  }
242  else
243  {
245  }
246 
247  xFreeBytesRemaining -= pxBlock->xBlockSize;
248 
249  if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
250  {
251  xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
252  }
253  else
254  {
256  }
257 
258  /* The block is being returned - it is allocated and owned
259  by the application and has no "next" block. */
260  pxBlock->xBlockSize |= xBlockAllocatedBit;
261  pxBlock->pxNextFreeBlock = NULL;
262  }
263  else
264  {
266  }
267  }
268  else
269  {
271  }
272  }
273  else
274  {
276  }
277 
278  traceMALLOC( pvReturn, xWantedSize );
279  }
280  ( void ) xTaskResumeAll();
281 
282  #if( configUSE_MALLOC_FAILED_HOOK == 1 )
283  {
284  if( pvReturn == NULL )
285  {
286  extern void vApplicationMallocFailedHook( void );
287  vApplicationMallocFailedHook();
288  }
289  else
290  {
292  }
293  }
294  #endif
295 
296  configASSERT( ( ( ( uint32_t ) pvReturn ) & portBYTE_ALIGNMENT_MASK ) == 0 );
297  return pvReturn;
298 }
299 /*-----------------------------------------------------------*/
300 
301 void vPortFree( void *pv )
302 {
303 uint8_t *puc = ( uint8_t * ) pv;
304 BlockLink_t *pxLink;
305 
306  if( pv != NULL )
307  {
308  /* The memory being freed will have an BlockLink_t structure immediately
309  before it. */
310  puc -= xHeapStructSize;
311 
312  /* This casting is to keep the compiler from issuing warnings. */
313  pxLink = ( void * ) puc;
314 
315  /* Check the block is actually allocated. */
316  configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
317  configASSERT( pxLink->pxNextFreeBlock == NULL );
318 
319  if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
320  {
321  if( pxLink->pxNextFreeBlock == NULL )
322  {
323  /* The block is being returned to the heap - it is no longer
324  allocated. */
325  pxLink->xBlockSize &= ~xBlockAllocatedBit;
326 
327  vTaskSuspendAll();
328  {
329  /* Add this block to the list of free blocks. */
330  xFreeBytesRemaining += pxLink->xBlockSize;
331  traceFREE( pv, pxLink->xBlockSize );
332  prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
333  }
334  ( void ) xTaskResumeAll();
335  }
336  else
337  {
339  }
340  }
341  else
342  {
344  }
345  }
346 }
347 /*-----------------------------------------------------------*/
348 
349 size_t xPortGetFreeHeapSize( void )
350 {
351  return xFreeBytesRemaining;
352 }
353 /*-----------------------------------------------------------*/
354 
356 {
357  return xMinimumEverFreeBytesRemaining;
358 }
359 /*-----------------------------------------------------------*/
360 
362 {
363  /* This just exists to keep the linker quiet. */
364 }
365 /*-----------------------------------------------------------*/
366 
367 static void prvHeapInit( void )
368 {
369 BlockLink_t *pxFirstFreeBlock;
370 uint8_t *pucAlignedHeap;
371 size_t uxAddress;
372 size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
373 
374  /* Ensure the heap starts on a correctly aligned boundary. */
375  uxAddress = ( size_t ) ucHeap;
376 
377  if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
378  {
379  uxAddress += ( portBYTE_ALIGNMENT - 1 );
380  uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
381  xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;
382  }
383 
384  pucAlignedHeap = ( uint8_t * ) uxAddress;
385 
386  /* xStart is used to hold a pointer to the first item in the list of free
387  blocks. The void cast is used to prevent compiler warnings. */
388  xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
389  xStart.xBlockSize = ( size_t ) 0;
390 
391  /* pxEnd is used to mark the end of the list of free blocks and is inserted
392  at the end of the heap space. */
393  uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;
394  uxAddress -= xHeapStructSize;
395  uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
396  pxEnd = ( void * ) uxAddress;
397  pxEnd->xBlockSize = 0;
398  pxEnd->pxNextFreeBlock = NULL;
399 
400  /* To start with there is a single free block that is sized to take up the
401  entire heap space, minus the space taken by pxEnd. */
402  pxFirstFreeBlock = ( void * ) pucAlignedHeap;
403  pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;
404  pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
405 
406  /* Only one block exists - and it covers the entire usable heap space. */
407  xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
408  xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
409 
410  /* Work out the position of the top bit in a size_t variable. */
411  xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
412 }
413 /*-----------------------------------------------------------*/
414 
415 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
416 {
417 BlockLink_t *pxIterator;
418 uint8_t *puc;
419 
420  /* Iterate through the list until a block is found that has a higher address
421  than the block being inserted. */
422  for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
423  {
424  /* Nothing to do here, just iterate to the right position. */
425  }
426 
427  /* Do the block being inserted, and the block it is being inserted after
428  make a contiguous block of memory? */
429  puc = ( uint8_t * ) pxIterator;
430  if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
431  {
432  pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
433  pxBlockToInsert = pxIterator;
434  }
435  else
436  {
438  }
439 
440  /* Do the block being inserted, and the block it is being inserted before
441  make a contiguous block of memory? */
442  puc = ( uint8_t * ) pxBlockToInsert;
443  if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
444  {
445  if( pxIterator->pxNextFreeBlock != pxEnd )
446  {
447  /* Form one big block from the two blocks. */
448  pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
449  pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
450  }
451  else
452  {
453  pxBlockToInsert->pxNextFreeBlock = pxEnd;
454  }
455  }
456  else
457  {
458  pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
459  }
460 
461  /* If the block being inserted plugged a gab, so was merged with the block
462  before and the block after, then it's pxNextFreeBlock pointer will have
463  already been set, and should not be set here as that would make it point
464  to itself. */
465  if( pxIterator != pxBlockToInsert )
466  {
467  pxIterator->pxNextFreeBlock = pxBlockToInsert;
468  }
469  else
470  {
472  }
473 }
474 
struct A_BLOCK_LINK * pxNextFreeBlock
Definition: heap_2.c:106
#define mtCOVERAGE_TEST_MARKER()
Definition: FreeRTOS.h:752
void vPortInitialiseBlocks(void)
Definition: heap_4.c:361
void vTaskSuspendAll(void) PRIVILEGED_FUNCTION
Definition: tasks.c:1633
#define configASSERT(x)
size_t xPortGetMinimumEverFreeHeapSize(void)
Definition: heap_4.c:355
#define heapMINIMUM_BLOCK_SIZE
Definition: heap_4.c:91
#define configTOTAL_HEAP_SIZE
#define prvInsertBlockIntoFreeList(pxBlockToInsert)
Definition: heap_2.c:128
#define heapBITS_PER_BYTE
Definition: heap_4.c:94
#define NULL
Definition: usbd_def.h:53
void vPortFree(void *pv)
Definition: heap_4.c:301
#define traceFREE(pvAddress, uiSize)
Definition: FreeRTOS.h:566
struct A_BLOCK_LINK BlockLink_t
void * pvPortMalloc(size_t xWantedSize)
Definition: heap_4.c:151
BaseType_t xTaskResumeAll(void) PRIVILEGED_FUNCTION
Definition: tasks.c:1671
#define portBYTE_ALIGNMENT
Definition: portmacro.h:117
#define traceMALLOC(pvAddress, uiSize)
Definition: FreeRTOS.h:562
size_t xPortGetFreeHeapSize(void)
Definition: heap_4.c:349
size_t xBlockSize
Definition: heap_2.c:107