OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [services/] [memalloc/] [common/] [current/] [include/] [memjoin.inl] - Blame information for rev 819

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

Line No. Rev Author Line
1 786 skrzyp
#ifndef CYGONCE_MEMALLOC_MEMJOIN_INL
2
#define CYGONCE_MEMALLOC_MEMJOIN_INL
3
 
4
//==========================================================================
5
//
6
//      memjoin.inl
7
//
8
//      Pseudo memory pool used to join together other memory pools
9
//
10
//==========================================================================
11
// ####ECOSGPLCOPYRIGHTBEGIN####
12
// -------------------------------------------
13
// This file is part of eCos, the Embedded Configurable Operating System.
14
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
15
//
16
// eCos is free software; you can redistribute it and/or modify it under
17
// the terms of the GNU General Public License as published by the Free
18
// Software Foundation; either version 2 or (at your option) any later
19
// version.
20
//
21
// eCos is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License
27
// along with eCos; if not, write to the Free Software Foundation, Inc.,
28
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
29
//
30
// As a special exception, if other files instantiate templates or use
31
// macros or inline functions from this file, or you compile this file
32
// and link it with other works to produce a work based on this file,
33
// this file does not by itself cause the resulting work to be covered by
34
// the GNU General Public License. However the source code for this file
35
// must still be made available in accordance with section (3) of the GNU
36
// General Public License v2.
37
//
38
// This exception does not invalidate any other reasons why a work based
39
// on this file might be covered by the GNU General Public License.
40
// -------------------------------------------
41
// ####ECOSGPLCOPYRIGHTEND####
42
//==========================================================================
43
//#####DESCRIPTIONBEGIN####
44
//
45
// Author(s):    jlarmour
46
// Contributors:
47
// Date:         2000-06-12
48
// Purpose:      Implement joined up memory pool class interface
49
// Description:  Inline class for constructing a pseudo allocator that contains
50
//               multiple other allocators. It caters solely to the requirements
51
//               of the malloc implementation.
52
// Usage:        #include 
53
//
54
//
55
//####DESCRIPTIONEND####
56
//
57
//==========================================================================
58
 
59
// CONFIGURATION
60
 
61
#include 
62
 
63
// INCLUDES
64
 
65
#include         // types
66
#include          // assertion macros
67
#include         // tracing macros
68
#include     // header for this file just in case
69
 
70
 
71
// FUNCTIONS
72
 
73
 
74
// -------------------------------------------------------------------------
75
// find_pool_for_ptr returns the pool that ptr came from
76
 
77
template 
78
inline T *
79
Cyg_Mempool_Joined::find_pool_for_ptr( const cyg_uint8 *ptr )
80
{
81
    cyg_uint8 i;
82
 
83
    for ( i=0; i < poolcount; i++ ) {
84
        if ( ptr >= pools[i].startaddr &&
85
             ptr < pools[i].endaddr ) {
86
            return pools[i].pool;
87
        } // if
88
    } // for
89
    return NULL;
90
} // Cyg_Mempool_Joined::find_pool_for_ptr()
91
 
92
 
93
// -------------------------------------------------------------------------
94
// Constructor
95
template 
96
inline
97
Cyg_Mempool_Joined::Cyg_Mempool_Joined( cyg_uint8 num_heaps, T *heaps[] )
98
{
99
    Cyg_Mempool_Status stat;
100
    cyg_uint8 i;
101
 
102
    CYG_REPORT_FUNCTION();
103
    CYG_REPORT_FUNCARG2( "num_heaps=%u, heaps=%08x", (int)num_heaps, heaps );
104
 
105
    CYG_CHECK_DATA_PTRC( heaps );
106
 
107
    poolcount = num_heaps;
108
 
109
    // allocate internal structures - this should work because we should be
110
    // the first allocation for this pool; and if there isn't enough space
111
    // for these teeny bits, what hope is there!
112
    for (i=0; i
113
        pools = (struct pooldesc *)
114
            heaps[i]->try_alloc( num_heaps * sizeof(struct pooldesc) );
115
        if ( NULL != pools )
116
            break;
117
    } // for
118
 
119
    CYG_ASSERT( pools != NULL,
120
                "Couldn't allocate internal structures from any pools!");
121
 
122
    // now set up internal structures
123
    for (i=0; i
124
        pools[i].pool = heaps[i];
125
        heaps[i]->get_status( CYG_MEMPOOL_STAT_ARENABASE|
126
                              CYG_MEMPOOL_STAT_ARENASIZE,
127
                              stat );
128
 
129
        CYG_ASSERT( stat.arenabase != (const cyg_uint8 *)-1,
130
                    "pool returns valid pool base" );
131
        CYG_CHECK_DATA_PTR( stat.arenabase, "Bad arena location" );
132
        CYG_ASSERT( stat.arenasize > 0, "pool returns valid pool size" );
133
 
134
        pools[i].startaddr = stat.arenabase;
135
        pools[i].endaddr = stat.arenabase + stat.arenasize;
136
    } // for
137
 
138
    CYG_REPORT_RETURN();
139
} // Cyg_Mempool_Joined::Cyg_Mempool_Joined()
140
 
141
 
142
 
143
// -------------------------------------------------------------------------
144
// Destructor
145
template 
146
inline
147
Cyg_Mempool_Joined::~Cyg_Mempool_Joined()
148
{
149
    CYG_REPORT_FUNCTION();
150
    CYG_REPORT_FUNCARGVOID();
151
 
152
    cyg_bool freestat;
153
 
154
    freestat = free( (cyg_uint8 *)pools, poolcount * sizeof(struct pooldesc) );
155
    CYG_ASSERT( freestat, "free failed!");
156
    CYG_REPORT_RETURN();
157
} // Cyg_Mempool_Joined::~Cyg_Mempool_Joined()
158
 
159
 
160
 
161
// -------------------------------------------------------------------------
162
// get some memory, return NULL if none available
163
template 
164
inline cyg_uint8 *
165
Cyg_Mempool_Joined::try_alloc( cyg_int32 size )
166
{
167
    cyg_uint8 i;
168
    cyg_uint8 *ptr=NULL;
169
 
170
    CYG_REPORT_FUNCTYPE( "returning memory at addr %08x" );
171
    CYG_REPORT_FUNCARG1DV( size );
172
 
173
    for (i=0; i
174
        ptr = pools[i].pool->try_alloc( size );
175
        if ( NULL != ptr )
176
            break;
177
    }
178
 
179
    CYG_REPORT_RETVAL( ptr );
180
 
181
    CYG_MEMALLOC_FAIL_TEST(ptr==NULL, size);
182
 
183
    return ptr;
184
} // Cyg_Mempool_Joined::try_alloc()
185
 
186
 
187
// -------------------------------------------------------------------------
188
// resize existing allocation, if oldsize is non-NULL, previous
189
// allocation size is placed into it. If previous size not available,
190
// it is set to 0. NB previous allocation size may have been rounded up.
191
// Occasionally the allocation can be adjusted *backwards* as well as,
192
// or instead of forwards, therefore the address of the resized
193
// allocation is returned, or NULL if no resizing was possible.
194
// Note that this differs from ::realloc() in that no attempt is
195
// made to call malloc() if resizing is not possible - that is left
196
// to higher layers. The data is copied from old to new though.
197
// The effects of alloc_ptr==NULL or newsize==0 are undefined
198
template 
199
inline cyg_uint8 *
200
Cyg_Mempool_Joined::resize_alloc( cyg_uint8 *alloc_ptr, cyg_int32 newsize,
201
                                     cyg_int32 *oldsize )
202
{
203
    T *pool;
204
    cyg_uint8 * ret;
205
 
206
    CYG_REPORT_FUNCTYPE( "success=" );
207
    CYG_REPORT_FUNCARG3( "alloc_ptr=%08x, newsize=%d, &oldsize=%08x",
208
                        alloc_ptr, newsize, oldsize );
209
    CYG_CHECK_DATA_PTRC( alloc_ptr );
210
    if (NULL != oldsize )
211
        CYG_CHECK_DATA_PTRC( oldsize );
212
 
213
    pool = find_pool_for_ptr( alloc_ptr );
214
    CYG_ASSERT( NULL != pool, "Couldn't find pool for pointer!" );
215
 
216
    ret = pool->resize_alloc( alloc_ptr, newsize, oldsize );
217
 
218
    CYG_REPORT_RETVAL( ret );
219
 
220
    return ret;
221
} // Cyg_Mempool_Joined::resize_alloc()
222
 
223
 
224
// -------------------------------------------------------------------------
225
// free the memory back to the pool
226
// returns true on success
227
template 
228
inline cyg_bool
229
Cyg_Mempool_Joined::free( cyg_uint8 *ptr, cyg_int32 size )
230
{
231
    T *pool;
232
    cyg_bool ret;
233
 
234
    CYG_REPORT_FUNCTYPE("success=");
235
    CYG_REPORT_FUNCARG2( "ptr=%08x, size=%d", ptr, size );
236
    CYG_CHECK_DATA_PTRC( ptr );
237
 
238
    pool = find_pool_for_ptr( ptr );
239
    CYG_ASSERT( NULL != pool, "Couldn't find pool for pointer!" );
240
 
241
    ret = pool->free( ptr, size );
242
 
243
    CYG_REPORT_RETVAL( ret );
244
    return ret;
245
} // Cyg_Mempool_Joined::free()
246
 
247
 
248
// -------------------------------------------------------------------------
249
// Get memory pool status
250
// flags is a bitmask of requested fields to fill in. The flags are
251
// defined in common.hxx
252
template 
253
inline void
254
Cyg_Mempool_Joined::get_status( cyg_mempool_status_flag_t flags,
255
                                Cyg_Mempool_Status &status )
256
{
257
    cyg_uint8 i;
258
    Cyg_Mempool_Status tmpstat;
259
 
260
    status.arenasize      = status.freeblocks = 0;
261
    status.totalallocated = status.totalfree  = 0;
262
    status.maxfree        = status.origsize   = 0;
263
 
264
    for ( i=0; i
265
        if ( status.arenasize >= 0 ) {
266
            if ( 0 != (flags & CYG_MEMPOOL_STAT_ARENASIZE) ) {
267
                pools[i].pool->get_status( CYG_MEMPOOL_STAT_ARENASIZE,
268
                                           tmpstat );
269
                if ( tmpstat.arenasize > 0)
270
                    status.arenasize += tmpstat.arenasize;
271
                else
272
                    status.arenasize = -1;
273
            } // if
274
        } // if
275
 
276
        if ( status.freeblocks >= 0 ) {
277
            if ( 0 != (flags & CYG_MEMPOOL_STAT_FREEBLOCKS) ) {
278
                pools[i].pool->get_status( CYG_MEMPOOL_STAT_FREEBLOCKS,
279
                                           tmpstat );
280
                if ( tmpstat.freeblocks > 0 )
281
                    status.freeblocks += tmpstat.freeblocks;
282
                else
283
                    status.freeblocks = -1;
284
            } // if
285
        } // if
286
 
287
        if ( status.totalallocated >= 0 ) {
288
            if ( 0 != (flags & CYG_MEMPOOL_STAT_TOTALALLOCATED) ) {
289
                pools[i].pool->get_status( CYG_MEMPOOL_STAT_TOTALALLOCATED,
290
                                           tmpstat );
291
                if ( tmpstat.totalallocated > 0 )
292
                    status.totalallocated += tmpstat.totalallocated;
293
                else
294
                    status.totalallocated = -1;
295
            } // if
296
        } // if
297
 
298
        if ( status.totalfree >= 0 ) {
299
            if ( 0 != (flags & CYG_MEMPOOL_STAT_TOTALFREE) ) {
300
                pools[i].pool->get_status( CYG_MEMPOOL_STAT_TOTALFREE,
301
                                           tmpstat );
302
                if ( tmpstat.totalfree > 0 )
303
                    status.totalfree += tmpstat.totalfree;
304
                else
305
                    status.totalfree = -1;
306
            } // if
307
        } // if
308
 
309
        if ( status.maxfree >= 0 ) {
310
            if ( 0 != (flags & CYG_MEMPOOL_STAT_MAXFREE) ) {
311
                pools[i].pool->get_status( CYG_MEMPOOL_STAT_MAXFREE, tmpstat );
312
                if ( tmpstat.maxfree < 0 )
313
                    status.maxfree = -1;
314
                else if ( tmpstat.maxfree > status.maxfree )
315
                    status.maxfree = tmpstat.maxfree;
316
            } // if
317
        } // if
318
 
319
        if ( status.origsize >= 0 ) {
320
            if ( 0 != (flags & CYG_MEMPOOL_STAT_ORIGSIZE) ) {
321
                pools[i].pool->get_status( CYG_MEMPOOL_STAT_ORIGSIZE, tmpstat );
322
                if ( tmpstat.origsize > 0 )
323
                    status.origsize += tmpstat.origsize;
324
                else
325
                    status.origsize = -1;
326
            } // if
327
        } // if
328
 
329
        if ( status.maxoverhead >= 0 ) {
330
            if ( 0 != (flags & CYG_MEMPOOL_STAT_MAXOVERHEAD) ) {
331
                pools[i].pool->get_status( CYG_MEMPOOL_STAT_MAXOVERHEAD,
332
                                           tmpstat );
333
                if ( tmpstat.maxoverhead < 0 )
334
                    status.maxoverhead = -1;
335
                else if ( tmpstat.maxoverhead > status.maxoverhead )
336
                    status.maxoverhead = tmpstat.maxoverhead;
337
            } // if
338
        } // if
339
    } // for
340
} // Cyg_Mempool_Joined::get_status()
341
 
342
 
343
// -------------------------------------------------------------------------
344
 
345
#endif // ifndef CYGONCE_MEMALLOC_MEMJOIN_INL
346
// EOF memjoin.inl

powered by: WebSVN 2.1.0

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