2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * A sample implementation of pvPortMalloc() and vPortFree() that permits
\r
72 * allocated blocks to be freed, but does not combine adjacent free blocks
\r
73 * into a single larger block (and so will fragment memory). See heap_4.c for
\r
74 * an equivalent that does combine adjacent blocks into single larger blocks.
\r
76 * See heap_1.c, heap_3.c and heap_4.c for alternative implementations, and the
\r
77 * memory management pages of http://www.FreeRTOS.org for more information.
\r
81 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
82 all the API functions to use the MPU wrappers. That should only be done when
\r
83 task.h is included from an application file. */
\r
84 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
86 #include "FreeRTOS.h"
\r
89 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
91 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
\r
92 #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
\r
95 /* A few bytes might be lost to byte aligning the heap start address. */
\r
96 #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
\r
99 * Initialises the heap structures before their first use.
\r
101 static void prvHeapInit( void );
\r
103 /* Allocate the memory for the heap. */
\r
104 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )
\r
105 /* The application writer has already defined the array used for the RTOS
\r
106 heap - probably so it can be placed in a special segment or address. */
\r
107 extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
109 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
110 #endif /* configAPPLICATION_ALLOCATED_HEAP */
\r
113 /* Define the linked list structure. This is used to link free blocks in order
\r
115 typedef struct A_BLOCK_LINK
\r
117 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
118 size_t xBlockSize; /*<< The size of the free block. */
\r
122 static const uint16_t heapSTRUCT_SIZE = ( ( sizeof ( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );
\r
123 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
\r
125 /* Create a couple of list links to mark the start and end of the list. */
\r
126 static BlockLink_t xStart, xEnd;
\r
128 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
130 static size_t xFreeBytesRemaining = configADJUSTED_HEAP_SIZE;
\r
132 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
\r
135 * Insert a block into the list of free blocks - which is ordered by size of
\r
136 * the block. Small blocks at the start of the list and large blocks at the end
\r
139 #define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
\r
141 BlockLink_t *pxIterator; \
\r
142 size_t xBlockSize; \
\r
144 xBlockSize = pxBlockToInsert->xBlockSize; \
\r
146 /* Iterate through the list until a block is found that has a larger size */ \
\r
147 /* than the block we are inserting. */ \
\r
148 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
\r
150 /* There is nothing to do here - just iterate to the correct position. */ \
\r
153 /* Update the list to include the block being inserted in the correct */ \
\r
155 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
\r
156 pxIterator->pxNextFreeBlock = pxBlockToInsert; \
\r
158 /*-----------------------------------------------------------*/
\r
160 void *pvPortMalloc( size_t xWantedSize )
\r
162 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
163 static BaseType_t xHeapHasBeenInitialised = pdFALSE;
\r
164 void *pvReturn = NULL;
\r
168 /* If this is the first call to malloc then the heap will require
\r
169 initialisation to setup the list of free blocks. */
\r
170 if( xHeapHasBeenInitialised == pdFALSE )
\r
173 xHeapHasBeenInitialised = pdTRUE;
\r
176 /* The wanted size is increased so it can contain a BlockLink_t
\r
177 structure in addition to the requested amount of bytes. */
\r
178 if( xWantedSize > 0 )
\r
180 xWantedSize += heapSTRUCT_SIZE;
\r
182 /* Ensure that blocks are always aligned to the required number of bytes. */
\r
183 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0 )
\r
185 /* Byte alignment required. */
\r
186 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
190 if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED_HEAP_SIZE ) )
\r
192 /* Blocks are stored in byte order - traverse the list from the start
\r
193 (smallest) block until one of adequate size is found. */
\r
194 pxPreviousBlock = &xStart;
\r
195 pxBlock = xStart.pxNextFreeBlock;
\r
196 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
198 pxPreviousBlock = pxBlock;
\r
199 pxBlock = pxBlock->pxNextFreeBlock;
\r
202 /* If we found the end marker then a block of adequate size was not found. */
\r
203 if( pxBlock != &xEnd )
\r
205 /* Return the memory space - jumping over the BlockLink_t structure
\r
207 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
\r
209 /* This block is being returned for use so must be taken out of the
\r
210 list of free blocks. */
\r
211 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
213 /* If the block is larger than required it can be split into two. */
\r
214 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
216 /* This block is to be split into two. Create a new block
\r
217 following the number of bytes requested. The void cast is
\r
218 used to prevent byte alignment warnings from the compiler. */
\r
219 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
\r
221 /* Calculate the sizes of two blocks split from the single
\r
223 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
224 pxBlock->xBlockSize = xWantedSize;
\r
226 /* Insert the new block into the list of free blocks. */
\r
227 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
230 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
234 traceMALLOC( pvReturn, xWantedSize );
\r
236 ( void ) xTaskResumeAll();
\r
238 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
240 if( pvReturn == NULL )
\r
242 extern void vApplicationMallocFailedHook( void );
\r
243 vApplicationMallocFailedHook();
\r
250 /*-----------------------------------------------------------*/
\r
252 void vPortFree( void *pv )
\r
254 uint8_t *puc = ( uint8_t * ) pv;
\r
255 BlockLink_t *pxLink;
\r
259 /* The memory being freed will have an BlockLink_t structure immediately
\r
261 puc -= heapSTRUCT_SIZE;
\r
263 /* This unexpected casting is to keep some compilers from issuing
\r
264 byte alignment warnings. */
\r
265 pxLink = ( void * ) puc;
\r
269 /* Add this block to the list of free blocks. */
\r
270 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
\r
271 xFreeBytesRemaining += pxLink->xBlockSize;
\r
272 traceFREE( pv, pxLink->xBlockSize );
\r
274 ( void ) xTaskResumeAll();
\r
277 /*-----------------------------------------------------------*/
\r
279 size_t xPortGetFreeHeapSize( void )
\r
281 return xFreeBytesRemaining;
\r
283 /*-----------------------------------------------------------*/
\r
285 void vPortInitialiseBlocks( void )
\r
287 /* This just exists to keep the linker quiet. */
\r
289 /*-----------------------------------------------------------*/
\r
291 static void prvHeapInit( void )
\r
293 BlockLink_t *pxFirstFreeBlock;
\r
294 uint8_t *pucAlignedHeap;
\r
296 /* Ensure the heap starts on a correctly aligned boundary. */
\r
297 pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
\r
299 /* xStart is used to hold a pointer to the first item in the list of free
\r
300 blocks. The void cast is used to prevent compiler warnings. */
\r
301 xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
\r
302 xStart.xBlockSize = ( size_t ) 0;
\r
304 /* xEnd is used to mark the end of the list of free blocks. */
\r
305 xEnd.xBlockSize = configADJUSTED_HEAP_SIZE;
\r
306 xEnd.pxNextFreeBlock = NULL;
\r
308 /* To start with there is a single free block that is sized to take up the
\r
309 entire heap space. */
\r
310 pxFirstFreeBlock = ( void * ) pucAlignedHeap;
\r
311 pxFirstFreeBlock->xBlockSize = configADJUSTED_HEAP_SIZE;
\r
312 pxFirstFreeBlock->pxNextFreeBlock = &xEnd;
\r
314 /*-----------------------------------------------------------*/
\r