]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/memalloc/common/v2_0/include/memjoin.inl
Initial revision
[karo-tx-redboot.git] / packages / services / memalloc / common / v2_0 / include / memjoin.inl
1 #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 Red Hat, 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 version.
19 //
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
23 // for more details.
24 //
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.
28 //
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.
35 //
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.
38 //
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####
45 //
46 // Author(s):    jlarmour
47 // Contributors: 
48 // Date:         2000-06-12
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>
54 //              
55 //
56 //####DESCRIPTIONEND####
57 //
58 //==========================================================================
59
60 // CONFIGURATION
61
62 #include <pkgconf/memalloc.h>
63
64 // INCLUDES
65
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
70
71
72 // FUNCTIONS
73
74
75 // -------------------------------------------------------------------------
76 // find_pool_for_ptr returns the pool that ptr came from
77
78 template <class T>
79 inline T *
80 Cyg_Mempool_Joined<T>::find_pool_for_ptr( const cyg_uint8 *ptr )
81 {
82     cyg_uint8 i;
83
84     for ( i=0; i < poolcount; i++ ) {
85         if ( ptr >= pools[i].startaddr &&
86              ptr < pools[i].endaddr ) {
87             return pools[i].pool;
88         } // if
89     } // for
90     return NULL;
91 } // Cyg_Mempool_Joined<T>::find_pool_for_ptr()
92
93
94 // -------------------------------------------------------------------------
95 // Constructor
96 template <class T>
97 inline
98 Cyg_Mempool_Joined<T>::Cyg_Mempool_Joined( cyg_uint8 num_heaps, T *heaps[] )
99 {
100     Cyg_Mempool_Status stat;
101     cyg_uint8 i;
102
103     CYG_REPORT_FUNCTION();
104     CYG_REPORT_FUNCARG2( "num_heaps=%u, heaps=%08x", (int)num_heaps, heaps );
105
106     CYG_CHECK_DATA_PTRC( heaps );
107
108     poolcount = num_heaps;
109
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) );
116         if ( NULL != pools )
117             break;
118     } // for
119
120     CYG_ASSERT( pools != NULL,
121                 "Couldn't allocate internal structures from any pools!");
122
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,
128                               stat );
129
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" );
134         
135         pools[i].startaddr = stat.arenabase;
136         pools[i].endaddr = stat.arenabase + stat.arenasize;
137     } // for
138
139     CYG_REPORT_RETURN();
140 } // Cyg_Mempool_Joined<T>::Cyg_Mempool_Joined()
141
142
143
144 // -------------------------------------------------------------------------
145 // Destructor
146 template <class T>
147 inline
148 Cyg_Mempool_Joined<T>::~Cyg_Mempool_Joined()
149 {
150     CYG_REPORT_FUNCTION();
151     CYG_REPORT_FUNCARGVOID();
152
153     cyg_bool freestat;
154     
155     freestat = free( (cyg_uint8 *)pools, poolcount * sizeof(struct pooldesc) );
156     CYG_ASSERT( freestat, "free failed!");
157     CYG_REPORT_RETURN();
158 } // Cyg_Mempool_Joined<T>::~Cyg_Mempool_Joined()
159
160
161
162 // -------------------------------------------------------------------------
163 // get some memory, return NULL if none available
164 template <class T>
165 inline cyg_uint8 *
166 Cyg_Mempool_Joined<T>::try_alloc( cyg_int32 size )
167 {
168     cyg_uint8 i;
169     cyg_uint8 *ptr=NULL;
170
171     CYG_REPORT_FUNCTYPE( "returning memory at addr %08x" );
172     CYG_REPORT_FUNCARG1DV( size );
173
174     for (i=0; i<poolcount; i++) {
175         ptr = pools[i].pool->try_alloc( size );
176         if ( NULL != ptr )
177             break;
178     }
179
180     CYG_REPORT_RETVAL( ptr );
181
182     CYG_MEMALLOC_FAIL_TEST(ptr==NULL, size);
183
184     return ptr;
185 } // Cyg_Mempool_Joined<T>::try_alloc()
186
187
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
199 template <class T>
200 inline cyg_uint8 *
201 Cyg_Mempool_Joined<T>::resize_alloc( cyg_uint8 *alloc_ptr, cyg_int32 newsize,
202                                      cyg_int32 *oldsize )
203 {
204     T *pool;
205     cyg_uint8 * ret;
206     
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 );
213
214     pool = find_pool_for_ptr( alloc_ptr );
215     CYG_ASSERT( NULL != pool, "Couldn't find pool for pointer!" );
216
217     ret = pool->resize_alloc( alloc_ptr, newsize, oldsize );
218
219     CYG_REPORT_RETVAL( ret );
220
221     return ret;    
222 } // Cyg_Mempool_Joined<T>::resize_alloc()
223
224
225 // -------------------------------------------------------------------------
226 // free the memory back to the pool
227 // returns true on success
228 template <class T>
229 inline cyg_bool
230 Cyg_Mempool_Joined<T>::free( cyg_uint8 *ptr, cyg_int32 size )
231 {
232     T *pool;
233     cyg_bool ret;
234
235     CYG_REPORT_FUNCTYPE("success=");
236     CYG_REPORT_FUNCARG2( "ptr=%08x, size=%d", ptr, size );
237     CYG_CHECK_DATA_PTRC( ptr );
238
239     pool = find_pool_for_ptr( ptr );
240     CYG_ASSERT( NULL != pool, "Couldn't find pool for pointer!" );
241
242     ret = pool->free( ptr, size );
243
244     CYG_REPORT_RETVAL( ret );
245     return ret;    
246 } // Cyg_Mempool_Joined<T>::free()
247
248
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
253 template <class T>
254 inline void
255 Cyg_Mempool_Joined<T>::get_status( cyg_mempool_status_flag_t flags,
256                                 Cyg_Mempool_Status &status )
257 {
258     cyg_uint8 i;
259     Cyg_Mempool_Status tmpstat;
260
261     status.arenasize      = status.freeblocks = 0;
262     status.totalallocated = status.totalfree  = 0;
263     status.maxfree        = status.origsize   = 0;
264
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,
269                                            tmpstat );
270                 if ( tmpstat.arenasize > 0)
271                     status.arenasize += tmpstat.arenasize;
272                 else
273                     status.arenasize = -1;
274             } // if
275         } // if
276
277         if ( status.freeblocks >= 0 ) {
278             if ( 0 != (flags & CYG_MEMPOOL_STAT_FREEBLOCKS) ) {
279                 pools[i].pool->get_status( CYG_MEMPOOL_STAT_FREEBLOCKS,
280                                            tmpstat );
281                 if ( tmpstat.freeblocks > 0 )
282                     status.freeblocks += tmpstat.freeblocks;
283                 else
284                     status.freeblocks = -1;
285             } // if
286         } // if
287
288         if ( status.totalallocated >= 0 ) {
289             if ( 0 != (flags & CYG_MEMPOOL_STAT_TOTALALLOCATED) ) {
290                 pools[i].pool->get_status( CYG_MEMPOOL_STAT_TOTALALLOCATED,
291                                            tmpstat );
292                 if ( tmpstat.totalallocated > 0 )
293                     status.totalallocated += tmpstat.totalallocated;
294                 else
295                     status.totalallocated = -1;
296             } // if
297         } // if
298
299         if ( status.totalfree >= 0 ) {
300             if ( 0 != (flags & CYG_MEMPOOL_STAT_TOTALFREE) ) {
301                 pools[i].pool->get_status( CYG_MEMPOOL_STAT_TOTALFREE,
302                                            tmpstat );
303                 if ( tmpstat.totalfree > 0 )
304                     status.totalfree += tmpstat.totalfree;
305                 else
306                     status.totalfree = -1;
307             } // if
308         } // if
309
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 )
314                     status.maxfree = -1;
315                 else if ( tmpstat.maxfree > status.maxfree )
316                     status.maxfree = tmpstat.maxfree;
317             } // if
318         } // if
319
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;
325                 else
326                     status.origsize = -1;
327             } // if
328         } // if
329
330         if ( status.maxoverhead >= 0 ) {
331             if ( 0 != (flags & CYG_MEMPOOL_STAT_MAXOVERHEAD) ) {
332                 pools[i].pool->get_status( CYG_MEMPOOL_STAT_MAXOVERHEAD,
333                                            tmpstat );
334                 if ( tmpstat.maxoverhead < 0 )
335                     status.maxoverhead = -1;
336                 else if ( tmpstat.maxoverhead > status.maxoverhead )
337                     status.maxoverhead = tmpstat.maxoverhead;
338             } // if
339         } // if
340     } // for
341 } // Cyg_Mempool_Joined<T>::get_status()
342
343
344 // -------------------------------------------------------------------------
345
346 #endif // ifndef CYGONCE_MEMALLOC_MEMJOIN_INL
347 // EOF memjoin.inl