]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/src/asf/thirdparty/FreeRTOS/portable/MemMang/heap_2.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / CORTEX_M4_ATSAM4S_Atmel_Studio / src / asf / thirdparty / FreeRTOS / portable / MemMang / heap_2.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*\r
68  * A sample implementation of pvPortMalloc() and vPortFree() that permits\r
69  * allocated blocks to be freed, but does not combine adjacent free blocks\r
70  * into a single larger block.\r
71  *\r
72  * See heap_1.c and heap_3.c for alternative implementations, and the memory\r
73  * management pages of http://www.FreeRTOS.org for more information.\r
74  */\r
75 #include <stdlib.h>\r
76 \r
77 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
78 all the API functions to use the MPU wrappers.  That should only be done when\r
79 task.h is included from an application file. */\r
80 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
81 \r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 \r
85 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
86 \r
87 /* Allocate the memory for the heap.  The struct is used to force byte\r
88 alignment without using any non-portable code. */\r
89 static union xRTOS_HEAP\r
90 {\r
91         #if portBYTE_ALIGNMENT == 8\r
92                 volatile portDOUBLE dDummy;\r
93         #else\r
94                 volatile unsigned long ulDummy;\r
95         #endif\r
96         unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];\r
97 } xHeap;\r
98 \r
99 /* Define the linked list structure.  This is used to link free blocks in order\r
100 of their size. */\r
101 typedef struct A_BLOCK_LINK\r
102 {\r
103         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
104         size_t xBlockSize;                                              /*<< The size of the free block. */\r
105 } xBlockLink;\r
106 \r
107 \r
108 static const unsigned short  heapSTRUCT_SIZE    = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );\r
109 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )\r
110 \r
111 /* Create a couple of list links to mark the start and end of the list. */\r
112 static xBlockLink xStart, xEnd;\r
113 \r
114 /* Keeps track of the number of free bytes remaining, but says nothing about\r
115 fragmentation. */\r
116 static size_t xFreeBytesRemaining = configTOTAL_HEAP_SIZE;\r
117 \r
118 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */\r
119 \r
120 /*\r
121  * Insert a block into the list of free blocks - which is ordered by size of\r
122  * the block.  Small blocks at the start of the list and large blocks at the end\r
123  * of the list.\r
124  */\r
125 #define prvInsertBlockIntoFreeList( pxBlockToInsert )                                                           \\r
126 {                                                                                                                                                                       \\r
127 xBlockLink *pxIterator;                                                                                                                         \\r
128 size_t xBlockSize;                                                                                                                                      \\r
129                                                                                                                                                                         \\r
130         xBlockSize = pxBlockToInsert->xBlockSize;                                                                               \\r
131                                                                                                                                                                         \\r
132         /* Iterate through the list until a block is found that has a larger size */    \\r
133         /* than the block we are inserting. */                                                                                  \\r
134         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock )     \\r
135         {                                                                                                                                                               \\r
136                 /* There is nothing to do here - just iterate to the correct position. */       \\r
137         }                                                                                                                                                               \\r
138                                                                                                                                                                         \\r
139         /* Update the list to include the block being inserted in the correct */                \\r
140         /* position. */                                                                                                                                 \\r
141         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;                                 \\r
142         pxIterator->pxNextFreeBlock = pxBlockToInsert;                                                                  \\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 #define prvHeapInit()                                                                                                                           \\r
147 {                                                                                                                                                                       \\r
148 xBlockLink *pxFirstFreeBlock;                                                                                                           \\r
149                                                                                                                                                                         \\r
150         /* xStart is used to hold a pointer to the first item in the list of free */    \\r
151         /* blocks.  The void cast is used to prevent compiler warnings. */                              \\r
152         xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap;                                                               \\r
153         xStart.xBlockSize = ( size_t ) 0;                                                                                               \\r
154                                                                                                                                                                         \\r
155         /* xEnd is used to mark the end of the list of free blocks. */                                  \\r
156         xEnd.xBlockSize = configTOTAL_HEAP_SIZE;                                                                                \\r
157         xEnd.pxNextFreeBlock = NULL;                                                                                                    \\r
158                                                                                                                                                                         \\r
159         /* To start with there is a single free block that is sized to take up the              \\r
160         entire heap space. */                                                                                                                   \\r
161         pxFirstFreeBlock = ( void * ) xHeap.ucHeap;                                                                             \\r
162         pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE;                                                   \\r
163         pxFirstFreeBlock->pxNextFreeBlock = &xEnd;                                                                              \\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 void *pvPortMalloc( size_t xWantedSize )\r
168 {\r
169 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
170 static portBASE_TYPE xHeapHasBeenInitialised = pdFALSE;\r
171 void *pvReturn = NULL;\r
172 \r
173         vTaskSuspendAll();\r
174         {\r
175                 /* If this is the first call to malloc then the heap will require\r
176                 initialisation to setup the list of free blocks. */\r
177                 if( xHeapHasBeenInitialised == pdFALSE )\r
178                 {\r
179                         prvHeapInit();\r
180                         xHeapHasBeenInitialised = pdTRUE;\r
181                 }\r
182 \r
183                 /* The wanted size is increased so it can contain a xBlockLink\r
184                 structure in addition to the requested amount of bytes. */\r
185                 if( xWantedSize > 0 )\r
186                 {\r
187                         xWantedSize += heapSTRUCT_SIZE;\r
188 \r
189                         /* Ensure that blocks are always aligned to the required number of bytes. */\r
190                         if( xWantedSize & portBYTE_ALIGNMENT_MASK )\r
191                         {\r
192                                 /* Byte alignment required. */\r
193                                 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
194                         }\r
195                 }\r
196 \r
197                 if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_HEAP_SIZE ) )\r
198                 {\r
199                         /* Blocks are stored in byte order - traverse the list from the start\r
200                         (smallest) block until one of adequate size is found. */\r
201                         pxPreviousBlock = &xStart;\r
202                         pxBlock = xStart.pxNextFreeBlock;\r
203                         while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock ) )\r
204                         {\r
205                                 pxPreviousBlock = pxBlock;\r
206                                 pxBlock = pxBlock->pxNextFreeBlock;\r
207                         }\r
208 \r
209                         /* If we found the end marker then a block of adequate size was not found. */\r
210                         if( pxBlock != &xEnd )\r
211                         {\r
212                                 /* Return the memory space - jumping over the xBlockLink structure\r
213                                 at its start. */\r
214                                 pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );\r
215 \r
216                                 /* This block is being returned for use so must be taken our of the\r
217                                 list of free blocks. */\r
218                                 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
219 \r
220                                 /* If the block is larger than required it can be split into two. */\r
221                                 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
222                                 {\r
223                                         /* This block is to be split into two.  Create a new block\r
224                                         following the number of bytes requested. The void cast is\r
225                                         used to prevent byte alignment warnings from the compiler. */\r
226                                         pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );\r
227 \r
228                                         /* Calculate the sizes of two blocks split from the single\r
229                                         block. */\r
230                                         pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
231                                         pxBlock->xBlockSize = xWantedSize;\r
232 \r
233                                         /* Insert the new block into the list of free blocks. */\r
234                                         prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
235                                 }\r
236                                 \r
237                                 xFreeBytesRemaining -= pxBlock->xBlockSize;\r
238                         }\r
239                 }\r
240         }\r
241         xTaskResumeAll();\r
242 \r
243         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
244         {\r
245                 if( pvReturn == NULL )\r
246                 {\r
247                         extern void vApplicationMallocFailedHook( void );\r
248                         vApplicationMallocFailedHook();\r
249                 }\r
250         }\r
251         #endif\r
252 \r
253         return pvReturn;\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 void vPortFree( void *pv )\r
258 {\r
259 unsigned char *puc = ( unsigned char * ) pv;\r
260 xBlockLink *pxLink;\r
261 \r
262         if( pv )\r
263         {\r
264                 /* The memory being freed will have an xBlockLink structure immediately\r
265                 before it. */\r
266                 puc -= heapSTRUCT_SIZE;\r
267 \r
268                 /* This casting is to keep the compiler from issuing warnings. */\r
269                 pxLink = ( void * ) puc;\r
270 \r
271                 vTaskSuspendAll();\r
272                 {\r
273                         /* Add this block to the list of free blocks. */\r
274                         prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );\r
275                         xFreeBytesRemaining += pxLink->xBlockSize;\r
276                 }\r
277                 xTaskResumeAll();\r
278         }\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 size_t xPortGetFreeHeapSize( void )\r
283 {\r
284         return xFreeBytesRemaining;\r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r
288 void vPortInitialiseBlocks( void )\r
289 {\r
290         /* This just exists to keep the linker quiet. */\r
291 }\r