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 |
|
|
|
38 |
|
|
Changes between V2.5.1 and V2.5.1
|
39 |
|
|
|
40 |
|
|
+ The memory pool has been defined within a struct to ensure correct memory
|
41 |
|
|
alignment on 32bit systems.
|
42 |
|
|
|
43 |
|
|
Changes between V2.6.1 and V3.0.0
|
44 |
|
|
|
45 |
|
|
+ An overflow check has been added to ensure the next free byte variable
|
46 |
|
|
does not wrap around.
|
47 |
|
|
*/
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
/*
|
51 |
|
|
* The simplest possible implementation of pvPortMalloc(). Note that this
|
52 |
|
|
* implementation does NOT allow allocated memory to be freed again.
|
53 |
|
|
*
|
54 |
|
|
* See heap_2.c and heap_3.c for alternative implementations, and the memory
|
55 |
|
|
* management pages of http://www.FreeRTOS.org for more information.
|
56 |
|
|
*/
|
57 |
|
|
#include <stdlib.h>
|
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 struct xRTOS_HEAP
|
86 |
|
|
{
|
87 |
|
|
unsigned portLONG ulDummy;
|
88 |
|
|
unsigned portCHAR ucHeap[configTOTAL_HEAP_SIZE];
|
89 |
|
|
} xHeap;
|
90 |
|
|
|
91 |
|
|
static size_t xNextFreeByte = (size_t) 0;
|
92 |
|
|
/*-----------------------------------------------------------*/
|
93 |
|
|
|
94 |
|
|
void *
|
95 |
|
|
pvPortMalloc (size_t xWantedSize)
|
96 |
|
|
{
|
97 |
|
|
void *pvReturn = NULL;
|
98 |
|
|
|
99 |
|
|
/* Ensure that blocks are always aligned to the required number of bytes. */
|
100 |
|
|
#if portBYTE_ALIGNMENT != 1
|
101 |
|
|
if (xWantedSize & heapBYTE_ALIGNMENT_MASK)
|
102 |
|
|
{
|
103 |
|
|
/* Byte alignment required. */
|
104 |
|
|
xWantedSize +=
|
105 |
|
|
(portBYTE_ALIGNMENT - (xWantedSize & heapBYTE_ALIGNMENT_MASK));
|
106 |
|
|
}
|
107 |
|
|
#endif
|
108 |
|
|
|
109 |
|
|
vTaskSuspendAll ();
|
110 |
|
|
{
|
111 |
|
|
/* Check there is enough room left for the allocation. */
|
112 |
|
|
if (((xNextFreeByte + xWantedSize) < configTOTAL_HEAP_SIZE) && ((xNextFreeByte + xWantedSize) > xNextFreeByte)) /* Check for overflow. */
|
113 |
|
|
{
|
114 |
|
|
/* Return the next free byte then increment the index past this
|
115 |
|
|
block. */
|
116 |
|
|
pvReturn = &(xHeap.ucHeap[xNextFreeByte]);
|
117 |
|
|
xNextFreeByte += xWantedSize;
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
xTaskResumeAll ();
|
121 |
|
|
|
122 |
|
|
return pvReturn;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/*-----------------------------------------------------------*/
|
126 |
|
|
|
127 |
|
|
void
|
128 |
|
|
vPortFree (void *pv)
|
129 |
|
|
{
|
130 |
|
|
/* Memory cannot be freed using this scheme. See heap_2.c and heap_3.c
|
131 |
|
|
for alternative implementations, and the memory management pages of
|
132 |
|
|
http://www.FreeRTOS.org for more information. */
|
133 |
|
|
(void) pv;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
/*-----------------------------------------------------------*/
|
137 |
|
|
|
138 |
|
|
void
|
139 |
|
|
vPortInitialiseBlocks (void)
|
140 |
|
|
{
|
141 |
|
|
/* Only required when static memory is not cleared. */
|
142 |
|
|
xNextFreeByte = (size_t) 0;
|
143 |
|
|
}
|