]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3S811_KEIL/heap/heap_1.c
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@50 1d2547de-c912-0410-9cb9...
[freertos] / Demo / CORTEX_LM3S811_KEIL / heap / heap_1.c
1 /*\r
2         FreeRTOS.org V4.1.2 - Copyright (C) 2003-2006 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 */\r
32 \r
33 \r
34 \r
35 /*\r
36  * The simplest possible implementation of pvPortMalloc().  Note that this\r
37  * implementation does NOT allow allocated memory to be freed again.\r
38  *\r
39  * See heap_2.c and heap_3.c for alternative implementations, and the memory\r
40  * management pages of http://www.FreeRTOS.org for more information.\r
41  */\r
42 #include <stdlib.h>\r
43 #include "FreeRTOS.h"\r
44 #include "task.h"\r
45 \r
46 /* Setup the correct byte alignment mask for the defined byte alignment. */\r
47 #if portBYTE_ALIGNMENT == 4\r
48         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0003 )\r
49 #endif\r
50 \r
51 #if portBYTE_ALIGNMENT == 2\r
52         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0001 )\r
53 #endif\r
54 \r
55 #if portBYTE_ALIGNMENT == 1 \r
56         #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0000 )\r
57 #endif\r
58 \r
59 #ifndef heapBYTE_ALIGNMENT_MASK\r
60         #error "Invalid portBYTE_ALIGNMENT definition"\r
61 #endif\r
62 \r
63 /* Allocate the memory for the heap.  The struct is used to force byte\r
64 alignment without using any non-portable code. */\r
65 extern struct xRTOS_HEAP\r
66 {\r
67         unsigned portLONG ulDummy;\r
68         unsigned portCHAR ucHeap[ configTOTAL_HEAP_SIZE ];\r
69 } xHeap;\r
70 \r
71 static size_t xNextFreeByte = ( size_t ) 0;\r
72 /*-----------------------------------------------------------*/\r
73 \r
74 void *pvPortMalloc( size_t xWantedSize )\r
75 {\r
76 void *pvReturn = NULL; \r
77 \r
78         /* Ensure that blocks are always aligned to the required number of bytes. */\r
79         #if portBYTE_ALIGNMENT != 1\r
80                 if( xWantedSize & heapBYTE_ALIGNMENT_MASK )\r
81                 {\r
82                         /* Byte alignment required. */\r
83                         xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & heapBYTE_ALIGNMENT_MASK ) );\r
84                 }\r
85         #endif\r
86 \r
87         vTaskSuspendAll();\r
88         {\r
89                 /* Check there is enough room left for the allocation. */\r
90                 if( ( ( xNextFreeByte + xWantedSize ) < configTOTAL_HEAP_SIZE ) &&\r
91                         ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte )     )/* Check for overflow. */\r
92                 {\r
93                         /* Return the next free byte then increment the index past this\r
94                         block. */\r
95                         pvReturn = &( xHeap.ucHeap[ xNextFreeByte ] );\r
96                         xNextFreeByte += xWantedSize;                   \r
97                 }       \r
98         }\r
99         xTaskResumeAll();\r
100 \r
101         return pvReturn;\r
102 }\r
103 /*-----------------------------------------------------------*/\r
104 \r
105 void vPortFree( void *pv )\r
106 {\r
107         /* Memory cannot be freed using this scheme.  See heap_2.c and heap_3.c \r
108         for alternative implementations, and the memory management pages of \r
109         http://www.FreeRTOS.org for more information. */\r
110         ( void ) pv;\r
111 }\r
112 /*-----------------------------------------------------------*/\r
113 \r
114 void vPortInitialiseBlocks( void )\r
115 {\r
116         /* Only required when static memory is not cleared. */\r
117         xNextFreeByte = ( size_t ) 0;\r
118 }\r
119 \r
120 \r