]> git.sur5r.net Git - freertos/blob - Source/portable/MemMang/heap_1.c
Update to V4.7.0.
[freertos] / Source / portable / MemMang / heap_1.c
1 /*\r
2         FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com a version that has been certified for use\r
32         in safety critical systems, plus commercial licensing, development and\r
33         support options.\r
34         ***************************************************************************\r
35 */\r
36 \r
37 /* \r
38 \r
39 Changes between V2.5.1 and V2.5.1\r
40 \r
41         + The memory pool has been defined within a struct to ensure correct memory\r
42           alignment on 32bit systems.\r
43 \r
44 Changes between V2.6.1 and V3.0.0\r
45 \r
46         + An overflow check has been added to ensure the next free byte variable \r
47           does not wrap around.\r
48 */\r
49 \r
50 \r
51 /*\r
52  * The simplest possible implementation of pvPortMalloc().  Note that this\r
53  * implementation does NOT allow allocated memory to be freed again.\r
54  *\r
55  * See heap_2.c and heap_3.c for alternative implementations, and the memory\r
56  * management pages of http://www.FreeRTOS.org for more information.\r
57  */\r
58 #include <stdlib.h>\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 \r
62 /* Setup the correct byte alignment mask for the defined byte alignment. */\r
63 \r
64 #if portBYTE_ALIGNMENT == 8\r
65         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0007 )\r
66 #endif\r
67 \r
68 #if portBYTE_ALIGNMENT == 4\r
69         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0003 )\r
70 #endif\r
71 \r
72 #if portBYTE_ALIGNMENT == 2\r
73         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0001 )\r
74 #endif\r
75 \r
76 #if portBYTE_ALIGNMENT == 1 \r
77         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0000 )\r
78 #endif\r
79 \r
80 #ifndef heapBYTE_ALIGNMENT_MASK\r
81         #error "Invalid portBYTE_ALIGNMENT definition"\r
82 #endif\r
83 \r
84 /* Allocate the memory for the heap.  The struct is used to force byte\r
85 alignment without using any non-portable code. */\r
86 static struct xRTOS_HEAP\r
87 {\r
88         unsigned portLONG ulDummy;\r
89         unsigned portCHAR ucHeap[ configTOTAL_HEAP_SIZE ];\r
90 } xHeap;\r
91 \r
92 static size_t xNextFreeByte = ( size_t ) 0;\r
93 /*-----------------------------------------------------------*/\r
94 \r
95 void *pvPortMalloc( size_t xWantedSize )\r
96 {\r
97 void *pvReturn = NULL; \r
98 \r
99         /* Ensure that blocks are always aligned to the required number of bytes. */\r
100         #if portBYTE_ALIGNMENT != 1\r
101                 if( xWantedSize & heapBYTE_ALIGNMENT_MASK )\r
102                 {\r
103                         /* Byte alignment required. */\r
104                         xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & heapBYTE_ALIGNMENT_MASK ) );\r
105                 }\r
106         #endif\r
107 \r
108         vTaskSuspendAll();\r
109         {\r
110                 /* Check there is enough room left for the allocation. */\r
111                 if( ( ( xNextFreeByte + xWantedSize ) < configTOTAL_HEAP_SIZE ) &&\r
112                         ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte )     )/* Check for overflow. */\r
113                 {\r
114                         /* Return the next free byte then increment the index past this\r
115                         block. */\r
116                         pvReturn = &( xHeap.ucHeap[ xNextFreeByte ] );\r
117                         xNextFreeByte += xWantedSize;                   \r
118                 }       \r
119         }\r
120         xTaskResumeAll();\r
121 \r
122         return pvReturn;\r
123 }\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 void vPortFree( void *pv )\r
127 {\r
128         /* Memory cannot be freed using this scheme.  See heap_2.c and heap_3.c \r
129         for alternative implementations, and the memory management pages of \r
130         http://www.FreeRTOS.org for more information. */\r
131         ( void ) pv;\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 void vPortInitialiseBlocks( void )\r
136 {\r
137         /* Only required when static memory is not cleared. */\r
138         xNextFreeByte = ( size_t ) 0;\r
139 }\r
140 \r
141 \r