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