]> git.sur5r.net Git - freertos/blob - Source/portable/MemMang/heap_1.c
Update to V4.3.0 as described in http://www.FreeRTOS.org/History.txt
[freertos] / Source / portable / MemMang / heap_1.c
1 /*\r
2         FreeRTOS.org V4.3.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 for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /* \r
37 \r
38 Changes between V2.5.1 and V2.5.1\r
39 \r
40         + The memory pool has been defined within a struct to ensure correct memory\r
41           alignment on 32bit systems.\r
42 \r
43 Changes between V2.6.1 and V3.0.0\r
44 \r
45         + An overflow check has been added to ensure the next free byte variable \r
46           does not wrap around.\r
47 */\r
48 \r
49 \r
50 /*\r
51  * The simplest possible implementation of pvPortMalloc().  Note that this\r
52  * implementation does NOT allow allocated memory to be freed again.\r
53  *\r
54  * See heap_2.c and heap_3.c for alternative implementations, and the memory\r
55  * management pages of http://www.FreeRTOS.org for more information.\r
56  */\r
57 #include <stdlib.h>\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 \r
61 /* Setup the correct byte alignment mask for the defined byte alignment. */\r
62 \r
63 #if portBYTE_ALIGNMENT == 8\r
64         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0007 )\r
65 #endif\r
66 \r
67 #if portBYTE_ALIGNMENT == 4\r
68         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0003 )\r
69 #endif\r
70 \r
71 #if portBYTE_ALIGNMENT == 2\r
72         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0001 )\r
73 #endif\r
74 \r
75 #if portBYTE_ALIGNMENT == 1 \r
76         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0000 )\r
77 #endif\r
78 \r
79 #ifndef heapBYTE_ALIGNMENT_MASK\r
80         #error "Invalid portBYTE_ALIGNMENT definition"\r
81 #endif\r
82 \r
83 /* Allocate the memory for the heap.  The struct is used to force byte\r
84 alignment without using any non-portable code. */\r
85 static struct xRTOS_HEAP\r
86 {\r
87         unsigned portLONG ulDummy;\r
88         unsigned portCHAR ucHeap[ configTOTAL_HEAP_SIZE ];\r
89 } xHeap;\r
90 \r
91 static size_t xNextFreeByte = ( size_t ) 0;\r
92 /*-----------------------------------------------------------*/\r
93 \r
94 void *pvPortMalloc( size_t xWantedSize )\r
95 {\r
96 void *pvReturn = NULL; \r
97 \r
98         /* Ensure that blocks are always aligned to the required number of bytes. */\r
99         #if portBYTE_ALIGNMENT != 1\r
100                 if( xWantedSize & heapBYTE_ALIGNMENT_MASK )\r
101                 {\r
102                         /* Byte alignment required. */\r
103                         xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & heapBYTE_ALIGNMENT_MASK ) );\r
104                 }\r
105         #endif\r
106 \r
107         vTaskSuspendAll();\r
108         {\r
109                 /* Check there is enough room left for the allocation. */\r
110                 if( ( ( xNextFreeByte + xWantedSize ) < configTOTAL_HEAP_SIZE ) &&\r
111                         ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte )     )/* Check for overflow. */\r
112                 {\r
113                         /* Return the next free byte then increment the index past this\r
114                         block. */\r
115                         pvReturn = &( xHeap.ucHeap[ xNextFreeByte ] );\r
116                         xNextFreeByte += xWantedSize;                   \r
117                 }       \r
118         }\r
119         xTaskResumeAll();\r
120 \r
121         return pvReturn;\r
122 }\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 void vPortFree( void *pv )\r
126 {\r
127         /* Memory cannot be freed using this scheme.  See heap_2.c and heap_3.c \r
128         for alternative implementations, and the memory management pages of \r
129         http://www.FreeRTOS.org for more information. */\r
130         ( void ) pv;\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 void vPortInitialiseBlocks( void )\r
135 {\r
136         /* Only required when static memory is not cleared. */\r
137         xNextFreeByte = ( size_t ) 0;\r
138 }\r
139 \r
140 \r