1 |
/* |
---|
2 |
FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd. |
---|
3 |
|
---|
4 |
This file is part of the FreeRTOS distribution. |
---|
5 |
|
---|
6 |
FreeRTOS is free software; you can redistribute it and/or modify it under |
---|
7 |
the terms of the GNU General Public License (version 2) as published by the |
---|
8 |
Free Software Foundation and modified by the FreeRTOS exception. |
---|
9 |
**NOTE** The exception to the GPL is included to allow you to distribute a |
---|
10 |
combined work that includes FreeRTOS without being obliged to provide the |
---|
11 |
source code for proprietary components outside of the FreeRTOS kernel. |
---|
12 |
Alternative commercial license and support terms are also available upon |
---|
13 |
request. See the licensing section of http://www.FreeRTOS.org for full |
---|
14 |
license details. |
---|
15 |
|
---|
16 |
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT |
---|
17 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
18 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
19 |
more details. |
---|
20 |
|
---|
21 |
You should have received a copy of the GNU General Public License along |
---|
22 |
with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59 |
---|
23 |
Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
---|
24 |
|
---|
25 |
|
---|
26 |
*************************************************************************** |
---|
27 |
* * |
---|
28 |
* Looking for a quick start? Then check out the FreeRTOS eBook! * |
---|
29 |
* See http://www.FreeRTOS.org/Documentation for details * |
---|
30 |
* * |
---|
31 |
*************************************************************************** |
---|
32 |
|
---|
33 |
1 tab == 4 spaces! |
---|
34 |
|
---|
35 |
Please ensure to read the configuration and relevant port sections of the |
---|
36 |
online documentation. |
---|
37 |
|
---|
38 |
http://www.FreeRTOS.org - Documentation, latest information, license and |
---|
39 |
contact details. |
---|
40 |
|
---|
41 |
http://www.SafeRTOS.com - A version that is certified for use in safety |
---|
42 |
critical systems. |
---|
43 |
|
---|
44 |
http://www.OpenRTOS.com - Commercial support, development, porting, |
---|
45 |
licensing and training services. |
---|
46 |
*/ |
---|
47 |
|
---|
48 |
/* |
---|
49 |
* A sample implementation of pvPortMalloc() and vPortFree() that permits |
---|
50 |
* allocated blocks to be freed, but does not combine adjacent free blocks |
---|
51 |
* into a single larger block. |
---|
52 |
* |
---|
53 |
* See heap_1.c and heap_3.c for alternative implementations, and the memory |
---|
54 |
* management pages of http://www.FreeRTOS.org for more information. |
---|
55 |
*/ |
---|
56 |
#include <stdlib.h> |
---|
57 |
|
---|
58 |
#include "FreeRTOS.h" |
---|
59 |
#include "task.h" |
---|
60 |
|
---|
61 |
/* Setup the correct byte alignment mask for the defined byte alignment. */ |
---|
62 |
|
---|
63 |
#if portBYTE_ALIGNMENT == 8 |
---|
64 |
#define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0007 ) |
---|
65 |
#endif |
---|
66 |
|
---|
67 |
#if portBYTE_ALIGNMENT == 4 |
---|
68 |
#define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0003 ) |
---|
69 |
#endif |
---|
70 |
|
---|
71 |
#if portBYTE_ALIGNMENT == 2 |
---|
72 |
#define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0001 ) |
---|
73 |
#endif |
---|
74 |
|
---|
75 |
#if portBYTE_ALIGNMENT == 1 |
---|
76 |
#define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0000 ) |
---|
77 |
#endif |
---|
78 |
|
---|
79 |
#ifndef heapBYTE_ALIGNMENT_MASK |
---|
80 |
#error "Invalid portBYTE_ALIGNMENT definition" |
---|
81 |
#endif |
---|
82 |
|
---|
83 |
/* Allocate the memory for the heap. The struct is used to force byte |
---|
84 |
alignment without using any non-portable code. */ |
---|
85 |
static union xRTOS_HEAP |
---|
86 |
{ |
---|
87 |
#if portBYTE_ALIGNMENT == 8 |
---|
88 |
volatile portDOUBLE dDummy; |
---|
89 |
#else |
---|
90 |
volatile unsigned portLONG ulDummy; |
---|
91 |
#endif |
---|
92 |
unsigned portCHAR ucHeap[ configTOTAL_HEAP_SIZE ]; |
---|
93 |
} xHeap; |
---|
94 |
|
---|
95 |
/* Define the linked list structure. This is used to link free blocks in order |
---|
96 |
of their size. */ |
---|
97 |
typedef struct A_BLOCK_LINK |
---|
98 |
{ |
---|
99 |
struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */ |
---|
100 |
size_t xBlockSize; /*<< The size of the free block. */ |
---|
101 |
} xBlockLink; |
---|
102 |
|
---|
103 |
|
---|
104 |
static const unsigned portSHORT heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) ); |
---|
105 |
#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) ) |
---|
106 |
|
---|
107 |
/* Create a couple of list links to mark the start and end of the list. */ |
---|
108 |
static xBlockLink xStart, xEnd; |
---|
109 |
|
---|
110 |
/* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */ |
---|
111 |
|
---|
112 |
/* |
---|
113 |
* Insert a block into the list of free blocks - which is ordered by size of |
---|
114 |
* the block. Small blocks at the start of the list and large blocks at the end |
---|
115 |
* of the list. |
---|
116 |
*/ |
---|
117 |
#define prvInsertBlockIntoFreeList( pxBlockToInsert ) \ |
---|
118 |
{ \ |
---|
119 |
xBlockLink *pxIterator; \ |
---|
120 |
size_t xBlockSize; \ |
---|
121 |
\ |
---|
122 |
xBlockSize = pxBlockToInsert->xBlockSize; \ |
---|
123 |
\ |
---|
124 |
/* Iterate through the list until a block is found that has a larger size */ \ |
---|
125 |
/* than the block we are inserting. */ \ |
---|
126 |
for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \ |
---|
127 |
{ \ |
---|
128 |
/* There is nothing to do here - just iterate to the correct position. */ \ |
---|
129 |
} \ |
---|
130 |
\ |
---|
131 |
/* Update the list to include the block being inserted in the correct */ \ |
---|
132 |
/* position. */ \ |
---|
133 |
pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \ |
---|
134 |
pxIterator->pxNextFreeBlock = pxBlockToInsert; \ |
---|
135 |
} |
---|
136 |
/*-----------------------------------------------------------*/ |
---|
137 |
|
---|
138 |
#define prvHeapInit() \ |
---|
139 |
{ \ |
---|
140 |
xBlockLink *pxFirstFreeBlock; \ |
---|
141 |
\ |
---|
142 |
/* xStart is used to hold a pointer to the first item in the list of free */ \ |
---|
143 |
/* blocks. The void cast is used to prevent compiler warnings. */ \ |
---|
144 |
xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap; \ |
---|
145 |
xStart.xBlockSize = ( size_t ) 0; \ |
---|
146 |
\ |
---|
147 |
/* xEnd is used to mark the end of the list of free blocks. */ \ |
---|
148 |
xEnd.xBlockSize = configTOTAL_HEAP_SIZE; \ |
---|
149 |
xEnd.pxNextFreeBlock = NULL; \ |
---|
150 |
\ |
---|
151 |
/* To start with there is a single free block that is sized to take up the \ |
---|
152 |
entire heap space. */ \ |
---|
153 |
pxFirstFreeBlock = ( void * ) xHeap.ucHeap; \ |
---|
154 |
pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE; \ |
---|
155 |
pxFirstFreeBlock->pxNextFreeBlock = &xEnd; \ |
---|
156 |
} |
---|
157 |
/*-----------------------------------------------------------*/ |
---|
158 |
|
---|
159 |
void *pvPortMalloc( size_t xWantedSize ) |
---|
160 |
{ |
---|
161 |
xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink; |
---|
162 |
static portBASE_TYPE xHeapHasBeenInitialised = pdFALSE; |
---|
163 |
void *pvReturn = NULL; |
---|
164 |
|
---|
165 |
vTaskSuspendAll(); |
---|
166 |
{ |
---|
167 |
/* If this is the first call to malloc then the heap will require |
---|
168 |
initialisation to setup the list of free blocks. */ |
---|
169 |
if( xHeapHasBeenInitialised == pdFALSE ) |
---|
170 |
{ |
---|
171 |
prvHeapInit(); |
---|
172 |
xHeapHasBeenInitialised = pdTRUE; |
---|
173 |
} |
---|
174 |
|
---|
175 |
/* The wanted size is increased so it can contain a xBlockLink |
---|
176 |
structure in addition to the requested amount of bytes. */ |
---|
177 |
if( xWantedSize > 0 ) |
---|
178 |
{ |
---|
179 |
xWantedSize += heapSTRUCT_SIZE; |
---|
180 |
|
---|
181 |
/* Ensure that blocks are always aligned to the required number of bytes. */ |
---|
182 |
if( xWantedSize & heapBYTE_ALIGNMENT_MASK ) |
---|
183 |
{ |
---|
184 |
/* Byte alignment required. */ |
---|
185 |
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & heapBYTE_ALIGNMENT_MASK ) ); |
---|
186 |
} |
---|
187 |
} |
---|
188 |
|
---|
189 |
if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_HEAP_SIZE ) ) |
---|
190 |
{ |
---|
191 |
/* Blocks are stored in byte order - traverse the list from the start |
---|
192 |
(smallest) block until one of adequate size is found. */ |
---|
193 |
pxPreviousBlock = &xStart; |
---|
194 |
pxBlock = xStart.pxNextFreeBlock; |
---|
195 |
while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock ) ) |
---|
196 |
{ |
---|
197 |
pxPreviousBlock = pxBlock; |
---|
198 |
pxBlock = pxBlock->pxNextFreeBlock; |
---|
199 |
} |
---|
200 |
|
---|
201 |
/* If we found the end marker then a block of adequate size was not found. */ |
---|
202 |
if( pxBlock != &xEnd ) |
---|
203 |
{ |
---|
204 |
/* Return the memory space - jumping over the xBlockLink structure |
---|
205 |
at its start. */ |
---|
206 |
pvReturn = ( void * ) ( ( ( unsigned portCHAR * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE ); |
---|
207 |
|
---|
208 |
/* This block is being returned for use so must be taken our of the |
---|
209 |
list of free blocks. */ |
---|
210 |
pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock; |
---|
211 |
|
---|
212 |
/* If the block is larger than required it can be split into two. */ |
---|
213 |
if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE ) |
---|
214 |
{ |
---|
215 |
/* This block is to be split into two. Create a new block |
---|
216 |
following the number of bytes requested. The void cast is |
---|
217 |
used to prevent byte alignment warnings from the compiler. */ |
---|
218 |
pxNewBlockLink = ( void * ) ( ( ( unsigned portCHAR * ) pxBlock ) + xWantedSize ); |
---|
219 |
|
---|
220 |
/* Calculate the sizes of two blocks split from the single |
---|
221 |
block. */ |
---|
222 |
pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize; |
---|
223 |
pxBlock->xBlockSize = xWantedSize; |
---|
224 |
|
---|
225 |
/* Insert the new block into the list of free blocks. */ |
---|
226 |
prvInsertBlockIntoFreeList( ( pxNewBlockLink ) ); |
---|
227 |
} |
---|
228 |
} |
---|
229 |
} |
---|
230 |
} |
---|
231 |
xTaskResumeAll(); |
---|
232 |
|
---|
233 |
#if( configUSE_MALLOC_FAILED_HOOK == 1 ) |
---|
234 |
{ |
---|
235 |
if( pvReturn == NULL ) |
---|
236 |
{ |
---|
237 |
extern void vApplicationMallocFailedHook( void ); |
---|
238 |
vApplicationMallocFailedHook(); |
---|
239 |
} |
---|
240 |
} |
---|
241 |
#endif |
---|
242 |
|
---|
243 |
return pvReturn; |
---|
244 |
} |
---|
245 |
/*-----------------------------------------------------------*/ |
---|
246 |
|
---|
247 |
void vPortFree( void *pv ) |
---|
248 |
{ |
---|
249 |
unsigned portCHAR *puc = ( unsigned portCHAR * ) pv; |
---|
250 |
xBlockLink *pxLink; |
---|
251 |
|
---|
252 |
if( pv ) |
---|
253 |
{ |
---|
254 |
/* The memory being freed will have an xBlockLink structure immediately |
---|
255 |
before it. */ |
---|
256 |
puc -= heapSTRUCT_SIZE; |
---|
257 |
|
---|
258 |
/* This casting is to keep the compiler from issuing warnings. */ |
---|
259 |
pxLink = ( void * ) puc; |
---|
260 |
|
---|
261 |
vTaskSuspendAll(); |
---|
262 |
{ |
---|
263 |
/* Add this block to the list of free blocks. */ |
---|
264 |
prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) ); |
---|
265 |
} |
---|
266 |
xTaskResumeAll(); |
---|
267 |
} |
---|
268 |
} |
---|
269 |
/*-----------------------------------------------------------*/ |
---|
270 |
|
---|