root/webserver/example/freeRTOSexample/Source/portable/MemMang/heap_1.c

Revision 14, 4.9 kB (checked in by phil, 15 years ago)

added unmodified FreeRTOS package V5.4.1 with only web srv demo source for LPC2368 for CrossWorks?

Line 
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 /*
50  * The simplest possible implementation of pvPortMalloc().  Note that this
51  * implementation does NOT allow allocated memory to be freed again.
52  *
53  * See heap_2.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 #include "FreeRTOS.h"
58 #include "task.h"
59
60 /* Setup the correct byte alignment mask for the defined byte alignment. */
61
62 #if portBYTE_ALIGNMENT == 8
63         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0007 )
64 #endif
65
66 #if portBYTE_ALIGNMENT == 4
67         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0003 )
68 #endif
69
70 #if portBYTE_ALIGNMENT == 2
71         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0001 )
72 #endif
73
74 #if portBYTE_ALIGNMENT == 1
75         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0000 )
76 #endif
77
78 #ifndef heapBYTE_ALIGNMENT_MASK
79         #error "Invalid portBYTE_ALIGNMENT definition"
80 #endif
81
82 /* Allocate the memory for the heap.  The struct is used to force byte
83 alignment without using any non-portable code. */
84 static union xRTOS_HEAP
85 {
86         #if portBYTE_ALIGNMENT == 8
87                 volatile portDOUBLE dDummy;
88         #else
89                 volatile unsigned portLONG ulDummy;
90         #endif 
91         unsigned portCHAR ucHeap[ configTOTAL_HEAP_SIZE ];
92 } xHeap;
93
94 static size_t xNextFreeByte = ( size_t ) 0;
95 /*-----------------------------------------------------------*/
96
97 void *pvPortMalloc( size_t xWantedSize )
98 {
99 void *pvReturn = NULL;
100
101         /* Ensure that blocks are always aligned to the required number of bytes. */
102         #if portBYTE_ALIGNMENT != 1
103                 if( xWantedSize & heapBYTE_ALIGNMENT_MASK )
104                 {
105                         /* Byte alignment required. */
106                         xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & heapBYTE_ALIGNMENT_MASK ) );
107                 }
108         #endif
109
110         vTaskSuspendAll();
111         {
112                 /* Check there is enough room left for the allocation. */
113                 if( ( ( xNextFreeByte + xWantedSize ) < configTOTAL_HEAP_SIZE ) &&
114                         ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte )     )/* Check for overflow. */
115                 {
116                         /* Return the next free byte then increment the index past this
117                         block. */
118                         pvReturn = &( xHeap.ucHeap[ xNextFreeByte ] );
119                         xNextFreeByte += xWantedSize;                   
120                 }       
121         }
122         xTaskResumeAll();
123        
124         #if( configUSE_MALLOC_FAILED_HOOK == 1 )
125         {
126                 if( pvReturn == NULL )
127                 {
128                         extern void vApplicationMallocFailedHook( void );
129                         vApplicationMallocFailedHook();
130                 }
131         }
132         #endif 
133
134         return pvReturn;
135 }
136 /*-----------------------------------------------------------*/
137
138 void vPortFree( void *pv )
139 {
140         /* Memory cannot be freed using this scheme.  See heap_2.c and heap_3.c
141         for alternative implementations, and the memory management pages of
142         http://www.FreeRTOS.org for more information. */
143         ( void ) pv;
144 }
145 /*-----------------------------------------------------------*/
146
147 void vPortInitialiseBlocks( void )
148 {
149         /* Only required when static memory is not cleared. */
150         xNextFreeByte = ( size_t ) 0;
151 }
152
153
Note: See TracBrowser for help on using the browser.