OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Source/] [portable/] [MemMang/] [heap_2.c] - Blame information for rev 604

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 572 jeremybenn
/*
2
    FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
3
 
4
    ***************************************************************************
5
    *                                                                         *
6
    * If you are:                                                             *
7
    *                                                                         *
8
    *    + New to FreeRTOS,                                                   *
9
    *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
10
    *    + Looking for basic training,                                        *
11
    *    + Wanting to improve your FreeRTOS skills and productivity           *
12
    *                                                                         *
13
    * then take a look at the FreeRTOS books - available as PDF or paperback  *
14
    *                                                                         *
15
    *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
16
    *                  http://www.FreeRTOS.org/Documentation                  *
17
    *                                                                         *
18
    * A pdf reference manual is also available.  Both are usually delivered   *
19
    * to your inbox within 20 minutes to two hours when purchased between 8am *
20
    * and 8pm GMT (although please allow up to 24 hours in case of            *
21
    * exceptional circumstances).  Thank you for your support!                *
22
    *                                                                         *
23
    ***************************************************************************
24
 
25
    This file is part of the FreeRTOS distribution.
26
 
27
    FreeRTOS is free software; you can redistribute it and/or modify it under
28
    the terms of the GNU General Public License (version 2) as published by the
29
    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30
    ***NOTE*** The exception to the GPL is included to allow you to distribute
31
    a combined work that includes FreeRTOS without being obliged to provide the
32
    source code for proprietary components outside of the FreeRTOS kernel.
33
    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
36
    more details. You should have received a copy of the GNU General Public
37
    License and the FreeRTOS license exception along with FreeRTOS; if not it
38
    can be viewed here: http://www.freertos.org/a00114.html and also obtained
39
    by writing to Richard Barry, contact details for whom are available on the
40
    FreeRTOS WEB site.
41
 
42
    1 tab == 4 spaces!
43
 
44
    http://www.FreeRTOS.org - Documentation, latest information, license and
45
    contact details.
46
 
47
    http://www.SafeRTOS.com - A version that is certified for use in safety
48
    critical systems.
49
 
50
    http://www.OpenRTOS.com - Commercial support, development, porting,
51
    licensing and training services.
52
*/
53
 
54
/*
55
 * A sample implementation of pvPortMalloc() and vPortFree() that permits
56
 * allocated blocks to be freed, but does not combine adjacent free blocks
57
 * into a single larger block.
58
 *
59
 * See heap_1.c and heap_3.c for alternative implementations, and the memory
60
 * management pages of http://www.FreeRTOS.org for more information.
61
 */
62
#include <stdlib.h>
63
 
64
/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
65
all the API functions to use the MPU wrappers.  That should only be done when
66
task.h is included from an application file. */
67
#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
68
 
69
#include "FreeRTOS.h"
70
#include "task.h"
71
 
72
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
73
 
74
/* Allocate the memory for the heap.  The struct is used to force byte
75
alignment without using any non-portable code. */
76
static union xRTOS_HEAP
77
{
78
        #if portBYTE_ALIGNMENT == 8
79
                volatile portDOUBLE dDummy;
80
        #else
81
                volatile unsigned long ulDummy;
82
        #endif
83
        unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];
84
} xHeap;
85
 
86
/* Define the linked list structure.  This is used to link free blocks in order
87
of their size. */
88
typedef struct A_BLOCK_LINK
89
{
90
        struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */
91
        size_t xBlockSize;                                              /*<< The size of the free block. */
92
} xBlockLink;
93
 
94
 
95
static const unsigned short  heapSTRUCT_SIZE    = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );
96
#define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
97
 
98
/* Create a couple of list links to mark the start and end of the list. */
99
static xBlockLink xStart, xEnd;
100
 
101
/* Keeps track of the number of free bytes remaining, but says nothing about
102
fragmentation. */
103
static size_t xFreeBytesRemaining = configTOTAL_HEAP_SIZE;
104
 
105
/* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
106
 
107
/*
108
 * Insert a block into the list of free blocks - which is ordered by size of
109
 * the block.  Small blocks at the start of the list and large blocks at the end
110
 * of the list.
111
 */
112
#define prvInsertBlockIntoFreeList( pxBlockToInsert )                                                           \
113
{                                                                                                                                                                       \
114
xBlockLink *pxIterator;                                                                                                                         \
115
size_t xBlockSize;                                                                                                                                      \
116
                                                                                                                                                                        \
117
        xBlockSize = pxBlockToInsert->xBlockSize;                                                                               \
118
                                                                                                                                                                        \
119
        /* Iterate through the list until a block is found that has a larger size */    \
120
        /* than the block we are inserting. */                                                                                  \
121
        for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock )     \
122
        {                                                                                                                                                               \
123
                /* There is nothing to do here - just iterate to the correct position. */       \
124
        }                                                                                                                                                               \
125
                                                                                                                                                                        \
126
        /* Update the list to include the block being inserted in the correct */                \
127
        /* position. */                                                                                                                                 \
128
        pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;                                 \
129
        pxIterator->pxNextFreeBlock = pxBlockToInsert;                                                                  \
130
}
131
/*-----------------------------------------------------------*/
132
 
133
#define prvHeapInit()                                                                                                                           \
134
{                                                                                                                                                                       \
135
xBlockLink *pxFirstFreeBlock;                                                                                                           \
136
                                                                                                                                                                        \
137
        /* xStart is used to hold a pointer to the first item in the list of free */    \
138
        /* blocks.  The void cast is used to prevent compiler warnings. */                              \
139
        xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap;                                                               \
140
        xStart.xBlockSize = ( size_t ) 0;                                                                                                \
141
                                                                                                                                                                        \
142
        /* xEnd is used to mark the end of the list of free blocks. */                                  \
143
        xEnd.xBlockSize = configTOTAL_HEAP_SIZE;                                                                                \
144
        xEnd.pxNextFreeBlock = NULL;                                                                                                    \
145
                                                                                                                                                                        \
146
        /* To start with there is a single free block that is sized to take up the              \
147
        entire heap space. */                                                                                                                   \
148
        pxFirstFreeBlock = ( void * ) xHeap.ucHeap;                                                                             \
149
        pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE;                                                   \
150
        pxFirstFreeBlock->pxNextFreeBlock = &xEnd;                                                                              \
151
}
152
/*-----------------------------------------------------------*/
153
 
154
void *pvPortMalloc( size_t xWantedSize )
155
{
156
xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
157
static portBASE_TYPE xHeapHasBeenInitialised = pdFALSE;
158
void *pvReturn = NULL;
159
 
160
        vTaskSuspendAll();
161
        {
162
                /* If this is the first call to malloc then the heap will require
163
                initialisation to setup the list of free blocks. */
164
                if( xHeapHasBeenInitialised == pdFALSE )
165
                {
166
                        prvHeapInit();
167
                        xHeapHasBeenInitialised = pdTRUE;
168
                }
169
 
170
                /* The wanted size is increased so it can contain a xBlockLink
171
                structure in addition to the requested amount of bytes. */
172
                if( xWantedSize > 0 )
173
                {
174
                        xWantedSize += heapSTRUCT_SIZE;
175
 
176
                        /* Ensure that blocks are always aligned to the required number of bytes. */
177
                        if( xWantedSize & portBYTE_ALIGNMENT_MASK )
178
                        {
179
                                /* Byte alignment required. */
180
                                xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
181
                        }
182
                }
183
 
184
                if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_HEAP_SIZE ) )
185
                {
186
                        /* Blocks are stored in byte order - traverse the list from the start
187
                        (smallest) block until one of adequate size is found. */
188
                        pxPreviousBlock = &xStart;
189
                        pxBlock = xStart.pxNextFreeBlock;
190
                        while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock ) )
191
                        {
192
                                pxPreviousBlock = pxBlock;
193
                                pxBlock = pxBlock->pxNextFreeBlock;
194
                        }
195
 
196
                        /* If we found the end marker then a block of adequate size was not found. */
197
                        if( pxBlock != &xEnd )
198
                        {
199
                                /* Return the memory space - jumping over the xBlockLink structure
200
                                at its start. */
201
                                pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
202
 
203
                                /* This block is being returned for use so must be taken our of the
204
                                list of free blocks. */
205
                                pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
206
 
207
                                /* If the block is larger than required it can be split into two. */
208
                                if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
209
                                {
210
                                        /* This block is to be split into two.  Create a new block
211
                                        following the number of bytes requested. The void cast is
212
                                        used to prevent byte alignment warnings from the compiler. */
213
                                        pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );
214
 
215
                                        /* Calculate the sizes of two blocks split from the single
216
                                        block. */
217
                                        pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
218
                                        pxBlock->xBlockSize = xWantedSize;
219
 
220
                                        /* Insert the new block into the list of free blocks. */
221
                                        prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
222
                                }
223
 
224
                                xFreeBytesRemaining -= pxBlock->xBlockSize;
225
                        }
226
                }
227
        }
228
        xTaskResumeAll();
229
 
230
        #if( configUSE_MALLOC_FAILED_HOOK == 1 )
231
        {
232
                if( pvReturn == NULL )
233
                {
234
                        extern void vApplicationMallocFailedHook( void );
235
                        vApplicationMallocFailedHook();
236
                }
237
        }
238
        #endif
239
 
240
        return pvReturn;
241
}
242
/*-----------------------------------------------------------*/
243
 
244
void vPortFree( void *pv )
245
{
246
unsigned char *puc = ( unsigned char * ) pv;
247
xBlockLink *pxLink;
248
 
249
        if( pv )
250
        {
251
                /* The memory being freed will have an xBlockLink structure immediately
252
                before it. */
253
                puc -= heapSTRUCT_SIZE;
254
 
255
                /* This casting is to keep the compiler from issuing warnings. */
256
                pxLink = ( void * ) puc;
257
 
258
                vTaskSuspendAll();
259
                {
260
                        /* Add this block to the list of free blocks. */
261
                        prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
262
                        xFreeBytesRemaining += pxLink->xBlockSize;
263
                }
264
                xTaskResumeAll();
265
        }
266
}
267
/*-----------------------------------------------------------*/
268
 
269
size_t xPortGetFreeHeapSize( void )
270
{
271
        return xFreeBytesRemaining;
272
}
273
/*-----------------------------------------------------------*/
274
 
275
void vPortInitialiseBlocks( void )
276
{
277
        /* This just exists to keep the linker quiet. */
278
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.