1 #ifndef CYGONCE_MEMALLOC_MEMJOIN_INL
2 #define CYGONCE_MEMALLOC_MEMJOIN_INL
4 //==========================================================================
8 // Pseudo memory pool used to join together other memory pools
10 //==========================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
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 version.
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //==========================================================================
44 //#####DESCRIPTIONBEGIN####
46 // Author(s): jlarmour
49 // Purpose: Implement joined up memory pool class interface
50 // Description: Inline class for constructing a pseudo allocator that contains
51 // multiple other allocators. It caters solely to the requirements
52 // of the malloc implementation.
53 // Usage: #include <cyg/memalloc/memjoin.hxx>
56 //####DESCRIPTIONEND####
58 //==========================================================================
62 #include <pkgconf/memalloc.h>
66 #include <cyg/infra/cyg_type.h> // types
67 #include <cyg/infra/cyg_ass.h> // assertion macros
68 #include <cyg/infra/cyg_trac.h> // tracing macros
69 #include <cyg/memalloc/memjoin.hxx> // header for this file just in case
75 // -------------------------------------------------------------------------
76 // find_pool_for_ptr returns the pool that ptr came from
80 Cyg_Mempool_Joined<T>::find_pool_for_ptr( const cyg_uint8 *ptr )
84 for ( i=0; i < poolcount; i++ ) {
85 if ( ptr >= pools[i].startaddr &&
86 ptr < pools[i].endaddr ) {
91 } // Cyg_Mempool_Joined<T>::find_pool_for_ptr()
94 // -------------------------------------------------------------------------
98 Cyg_Mempool_Joined<T>::Cyg_Mempool_Joined( cyg_uint8 num_heaps, T *heaps[] )
100 Cyg_Mempool_Status stat;
103 CYG_REPORT_FUNCTION();
104 CYG_REPORT_FUNCARG2( "num_heaps=%u, heaps=%08x", (int)num_heaps, heaps );
106 CYG_CHECK_DATA_PTRC( heaps );
108 poolcount = num_heaps;
110 // allocate internal structures - this should work because we should be
111 // the first allocation for this pool; and if there isn't enough space
112 // for these teeny bits, what hope is there!
113 for (i=0; i<num_heaps; i++) {
114 pools = (struct pooldesc *)
115 heaps[i]->try_alloc( num_heaps * sizeof(struct pooldesc) );
120 CYG_ASSERT( pools != NULL,
121 "Couldn't allocate internal structures from any pools!");
123 // now set up internal structures
124 for (i=0; i<num_heaps; i++) {
125 pools[i].pool = heaps[i];
126 heaps[i]->get_status( CYG_MEMPOOL_STAT_ARENABASE|
127 CYG_MEMPOOL_STAT_ARENASIZE,
130 CYG_ASSERT( stat.arenabase != (const cyg_uint8 *)-1,
131 "pool returns valid pool base" );
132 CYG_CHECK_DATA_PTR( stat.arenabase, "Bad arena location" );
133 CYG_ASSERT( stat.arenasize > 0, "pool returns valid pool size" );
135 pools[i].startaddr = stat.arenabase;
136 pools[i].endaddr = stat.arenabase + stat.arenasize;
140 } // Cyg_Mempool_Joined<T>::Cyg_Mempool_Joined()
144 // -------------------------------------------------------------------------
148 Cyg_Mempool_Joined<T>::~Cyg_Mempool_Joined()
150 CYG_REPORT_FUNCTION();
151 CYG_REPORT_FUNCARGVOID();
155 freestat = free( (cyg_uint8 *)pools, poolcount * sizeof(struct pooldesc) );
156 CYG_ASSERT( freestat, "free failed!");
158 } // Cyg_Mempool_Joined<T>::~Cyg_Mempool_Joined()
162 // -------------------------------------------------------------------------
163 // get some memory, return NULL if none available
166 Cyg_Mempool_Joined<T>::try_alloc( cyg_int32 size )
171 CYG_REPORT_FUNCTYPE( "returning memory at addr %08x" );
172 CYG_REPORT_FUNCARG1DV( size );
174 for (i=0; i<poolcount; i++) {
175 ptr = pools[i].pool->try_alloc( size );
180 CYG_REPORT_RETVAL( ptr );
182 CYG_MEMALLOC_FAIL_TEST(ptr==NULL, size);
185 } // Cyg_Mempool_Joined<T>::try_alloc()
188 // -------------------------------------------------------------------------
189 // resize existing allocation, if oldsize is non-NULL, previous
190 // allocation size is placed into it. If previous size not available,
191 // it is set to 0. NB previous allocation size may have been rounded up.
192 // Occasionally the allocation can be adjusted *backwards* as well as,
193 // or instead of forwards, therefore the address of the resized
194 // allocation is returned, or NULL if no resizing was possible.
195 // Note that this differs from ::realloc() in that no attempt is
196 // made to call malloc() if resizing is not possible - that is left
197 // to higher layers. The data is copied from old to new though.
198 // The effects of alloc_ptr==NULL or newsize==0 are undefined
201 Cyg_Mempool_Joined<T>::resize_alloc( cyg_uint8 *alloc_ptr, cyg_int32 newsize,
207 CYG_REPORT_FUNCTYPE( "success=" );
208 CYG_REPORT_FUNCARG3( "alloc_ptr=%08x, newsize=%d, &oldsize=%08x",
209 alloc_ptr, newsize, oldsize );
210 CYG_CHECK_DATA_PTRC( alloc_ptr );
211 if (NULL != oldsize )
212 CYG_CHECK_DATA_PTRC( oldsize );
214 pool = find_pool_for_ptr( alloc_ptr );
215 CYG_ASSERT( NULL != pool, "Couldn't find pool for pointer!" );
217 ret = pool->resize_alloc( alloc_ptr, newsize, oldsize );
219 CYG_REPORT_RETVAL( ret );
222 } // Cyg_Mempool_Joined<T>::resize_alloc()
225 // -------------------------------------------------------------------------
226 // free the memory back to the pool
227 // returns true on success
230 Cyg_Mempool_Joined<T>::free( cyg_uint8 *ptr, cyg_int32 size )
235 CYG_REPORT_FUNCTYPE("success=");
236 CYG_REPORT_FUNCARG2( "ptr=%08x, size=%d", ptr, size );
237 CYG_CHECK_DATA_PTRC( ptr );
239 pool = find_pool_for_ptr( ptr );
240 CYG_ASSERT( NULL != pool, "Couldn't find pool for pointer!" );
242 ret = pool->free( ptr, size );
244 CYG_REPORT_RETVAL( ret );
246 } // Cyg_Mempool_Joined<T>::free()
249 // -------------------------------------------------------------------------
250 // Get memory pool status
251 // flags is a bitmask of requested fields to fill in. The flags are
252 // defined in common.hxx
255 Cyg_Mempool_Joined<T>::get_status( cyg_mempool_status_flag_t flags,
256 Cyg_Mempool_Status &status )
259 Cyg_Mempool_Status tmpstat;
261 status.arenasize = status.freeblocks = 0;
262 status.totalallocated = status.totalfree = 0;
263 status.maxfree = status.origsize = 0;
265 for ( i=0; i<poolcount; i++ ) {
266 if ( status.arenasize >= 0 ) {
267 if ( 0 != (flags & CYG_MEMPOOL_STAT_ARENASIZE) ) {
268 pools[i].pool->get_status( CYG_MEMPOOL_STAT_ARENASIZE,
270 if ( tmpstat.arenasize > 0)
271 status.arenasize += tmpstat.arenasize;
273 status.arenasize = -1;
277 if ( status.freeblocks >= 0 ) {
278 if ( 0 != (flags & CYG_MEMPOOL_STAT_FREEBLOCKS) ) {
279 pools[i].pool->get_status( CYG_MEMPOOL_STAT_FREEBLOCKS,
281 if ( tmpstat.freeblocks > 0 )
282 status.freeblocks += tmpstat.freeblocks;
284 status.freeblocks = -1;
288 if ( status.totalallocated >= 0 ) {
289 if ( 0 != (flags & CYG_MEMPOOL_STAT_TOTALALLOCATED) ) {
290 pools[i].pool->get_status( CYG_MEMPOOL_STAT_TOTALALLOCATED,
292 if ( tmpstat.totalallocated > 0 )
293 status.totalallocated += tmpstat.totalallocated;
295 status.totalallocated = -1;
299 if ( status.totalfree >= 0 ) {
300 if ( 0 != (flags & CYG_MEMPOOL_STAT_TOTALFREE) ) {
301 pools[i].pool->get_status( CYG_MEMPOOL_STAT_TOTALFREE,
303 if ( tmpstat.totalfree > 0 )
304 status.totalfree += tmpstat.totalfree;
306 status.totalfree = -1;
310 if ( status.maxfree >= 0 ) {
311 if ( 0 != (flags & CYG_MEMPOOL_STAT_MAXFREE) ) {
312 pools[i].pool->get_status( CYG_MEMPOOL_STAT_MAXFREE, tmpstat );
313 if ( tmpstat.maxfree < 0 )
315 else if ( tmpstat.maxfree > status.maxfree )
316 status.maxfree = tmpstat.maxfree;
320 if ( status.origsize >= 0 ) {
321 if ( 0 != (flags & CYG_MEMPOOL_STAT_ORIGSIZE) ) {
322 pools[i].pool->get_status( CYG_MEMPOOL_STAT_ORIGSIZE, tmpstat );
323 if ( tmpstat.origsize > 0 )
324 status.origsize += tmpstat.origsize;
326 status.origsize = -1;
330 if ( status.maxoverhead >= 0 ) {
331 if ( 0 != (flags & CYG_MEMPOOL_STAT_MAXOVERHEAD) ) {
332 pools[i].pool->get_status( CYG_MEMPOOL_STAT_MAXOVERHEAD,
334 if ( tmpstat.maxoverhead < 0 )
335 status.maxoverhead = -1;
336 else if ( tmpstat.maxoverhead > status.maxoverhead )
337 status.maxoverhead = tmpstat.maxoverhead;
341 } // Cyg_Mempool_Joined<T>::get_status()
344 // -------------------------------------------------------------------------
346 #endif // ifndef CYGONCE_MEMALLOC_MEMJOIN_INL