1 |
27 |
unneback |
#ifndef CYGONCE_MEMALLOC_MFIXIMPL_INL
|
2 |
|
|
#define CYGONCE_MEMALLOC_MFIXIMPL_INL
|
3 |
|
|
|
4 |
|
|
//==========================================================================
|
5 |
|
|
//
|
6 |
|
|
// mfiximpl.inl
|
7 |
|
|
//
|
8 |
|
|
// Memory pool with fixed block class declarations
|
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): hmt
|
47 |
|
|
// Contributors: jlarmour
|
48 |
|
|
// Date: 2000-06-12
|
49 |
|
|
// Purpose: Define Mfiximpl class interface
|
50 |
|
|
// Description: Inline class for constructing a fixed block allocator
|
51 |
|
|
// Usage: #include
|
52 |
|
|
//
|
53 |
|
|
//
|
54 |
|
|
//####DESCRIPTIONEND####
|
55 |
|
|
//
|
56 |
|
|
//==========================================================================
|
57 |
|
|
|
58 |
|
|
#include
|
59 |
|
|
#include // HAL_LSBIT_INDEX magic asm code
|
60 |
|
|
#include
|
61 |
|
|
|
62 |
|
|
// -------------------------------------------------------------------------
|
63 |
|
|
|
64 |
|
|
inline
|
65 |
|
|
Cyg_Mempool_Fixed_Implementation::Cyg_Mempool_Fixed_Implementation(
|
66 |
|
|
cyg_uint8 *base,
|
67 |
|
|
cyg_int32 size,
|
68 |
|
|
CYG_ADDRWORD alloc_unit )
|
69 |
|
|
{
|
70 |
|
|
cyg_int32 i;
|
71 |
|
|
bitmap = (cyg_uint32 *)base;
|
72 |
|
|
blocksize = alloc_unit;
|
73 |
|
|
|
74 |
|
|
CYG_ASSERT( blocksize > 0, "Bad blocksize" );
|
75 |
|
|
CYG_ASSERT( size > 2, "Bad blocksize" );
|
76 |
|
|
CYG_ASSERT( blocksize < size, "blocksize, size bad" );
|
77 |
|
|
|
78 |
|
|
numblocks = size / blocksize;
|
79 |
|
|
top = base + size;
|
80 |
|
|
|
81 |
|
|
CYG_ASSERT( numblocks >= 2, "numblocks bad" );
|
82 |
|
|
|
83 |
|
|
i = (numblocks + 31)/32; // number of words to map blocks
|
84 |
|
|
while ( (i * 4 + numblocks * blocksize) > size ) {
|
85 |
|
|
numblocks --; // steal one block for admin
|
86 |
|
|
i = (numblocks + 31)/32; // number of words to map blocks
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
CYG_ASSERT( 0 < i, "Bad word count for bitmap after fitment" );
|
90 |
|
|
CYG_ASSERT( 0 < numblocks, "Bad block count after fitment" );
|
91 |
|
|
|
92 |
|
|
maptop = i;
|
93 |
|
|
// this should leave space for the bitmap and maintain alignment
|
94 |
|
|
mempool = top - (numblocks * blocksize);
|
95 |
|
|
CYG_ASSERT( base < mempool && mempool < top, "mempool escaped" );
|
96 |
|
|
CYG_ASSERT( (cyg_uint8 *)(&bitmap[ maptop ]) <= mempool,
|
97 |
|
|
"mempool overwrites bitmap" );
|
98 |
|
|
CYG_ASSERT( &mempool[ numblocks * blocksize ] <= top,
|
99 |
|
|
"mempool overflows top" );
|
100 |
|
|
freeblocks = numblocks;
|
101 |
|
|
firstfree = 0;
|
102 |
|
|
|
103 |
|
|
// clear out the bitmap; no blocks allocated yet
|
104 |
|
|
for ( i = 0; i < maptop; i++ )
|
105 |
|
|
bitmap[ i ] = 0;
|
106 |
|
|
// apart from the non-existent ones at the top
|
107 |
|
|
for ( i = ((numblocks-1)&31) + 1; i < 32; i++ )
|
108 |
|
|
bitmap[ maptop - 1 ] |= ( 1 << i );
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
// -------------------------------------------------------------------------
|
112 |
|
|
|
113 |
|
|
inline
|
114 |
|
|
Cyg_Mempool_Fixed_Implementation::~Cyg_Mempool_Fixed_Implementation()
|
115 |
|
|
{
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
// -------------------------------------------------------------------------
|
119 |
|
|
|
120 |
|
|
inline cyg_uint8 *
|
121 |
|
|
Cyg_Mempool_Fixed_Implementation::try_alloc( cyg_int32 size )
|
122 |
|
|
{
|
123 |
|
|
// size parameter is not used
|
124 |
|
|
CYG_UNUSED_PARAM( cyg_int32, size );
|
125 |
|
|
if ( 0 >= freeblocks )
|
126 |
|
|
return NULL;
|
127 |
|
|
cyg_int32 i = firstfree;
|
128 |
|
|
cyg_uint8 *p = NULL;
|
129 |
|
|
do {
|
130 |
|
|
if ( 0xffffffff != bitmap[ i ] ) {
|
131 |
|
|
// then there is a free block in this bucket
|
132 |
|
|
register cyg_uint32 j, k;
|
133 |
|
|
k = ~bitmap[ i ]; // look for a 1 in complement
|
134 |
|
|
HAL_LSBIT_INDEX( j, k );
|
135 |
|
|
CYG_ASSERT( 0 <= j && j <= 31, "Bad bit index" );
|
136 |
|
|
CYG_ASSERT( 0 == (bitmap[ i ] & (1 << j)), "Found bit not clear" );
|
137 |
|
|
bitmap[ i ] |= (1 << j); // set it allocated
|
138 |
|
|
firstfree = i;
|
139 |
|
|
freeblocks--;
|
140 |
|
|
CYG_ASSERT( freeblocks >= 0, "allocated too many" );
|
141 |
|
|
p = &mempool[ ((32 * i) + j) * blocksize ];
|
142 |
|
|
break;
|
143 |
|
|
}
|
144 |
|
|
if ( ++i >= maptop )
|
145 |
|
|
i = 0; // wrap if at top
|
146 |
|
|
} while ( i != firstfree ); // prevent hang if internal error
|
147 |
|
|
CYG_ASSERT( NULL != p, "Should have a block here" );
|
148 |
|
|
CYG_ASSERT( mempool <= p && p <= top, "alloc mem escaped" );
|
149 |
|
|
return p;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
// -------------------------------------------------------------------------
|
153 |
|
|
// supposedly resize existing allocation. This is defined in the
|
154 |
|
|
// fixed block allocator purely for API consistency. It will return
|
155 |
|
|
// an error (false) for all values, except for the blocksize
|
156 |
|
|
// returns true on success
|
157 |
|
|
|
158 |
|
|
inline cyg_uint8 *
|
159 |
|
|
Cyg_Mempool_Fixed_Implementation::resize_alloc( cyg_uint8 *alloc_ptr,
|
160 |
|
|
cyg_int32 newsize,
|
161 |
|
|
cyg_int32 *oldsize )
|
162 |
|
|
{
|
163 |
|
|
CYG_CHECK_DATA_PTRC( alloc_ptr );
|
164 |
|
|
if ( NULL != oldsize )
|
165 |
|
|
CYG_CHECK_DATA_PTRC( oldsize );
|
166 |
|
|
|
167 |
|
|
CYG_ASSERT( alloc_ptr >= mempool && alloc_ptr < top,
|
168 |
|
|
"alloc_ptr outside pool" );
|
169 |
|
|
|
170 |
|
|
if ( NULL != oldsize )
|
171 |
|
|
*oldsize = blocksize;
|
172 |
|
|
|
173 |
|
|
if (newsize == blocksize)
|
174 |
|
|
return alloc_ptr;
|
175 |
|
|
else
|
176 |
|
|
return NULL;
|
177 |
|
|
} // resize_alloc()
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
// -------------------------------------------------------------------------
|
181 |
|
|
|
182 |
|
|
inline cyg_bool
|
183 |
|
|
Cyg_Mempool_Fixed_Implementation::free( cyg_uint8 *p, cyg_int32 size )
|
184 |
|
|
{
|
185 |
|
|
// size parameter is not used
|
186 |
|
|
CYG_UNUSED_PARAM( cyg_int32, size );
|
187 |
|
|
if ( p < mempool || p >= top )
|
188 |
|
|
return false; // address way out of bounds
|
189 |
|
|
cyg_int32 i = p - mempool;
|
190 |
|
|
i = i / blocksize;
|
191 |
|
|
if ( &mempool[ i * blocksize ] != p )
|
192 |
|
|
return false; // address not aligned
|
193 |
|
|
cyg_int32 j = i / 32;
|
194 |
|
|
CYG_ASSERT( 0 <= j && j < maptop, "map index escaped" );
|
195 |
|
|
i = i - 32 * j;
|
196 |
|
|
CYG_ASSERT( 0 <= i && i < 32, "map bit index escaped" );
|
197 |
|
|
if ( ! ((1 << i) & bitmap[ j ] ) )
|
198 |
|
|
return false; // block was not allocated
|
199 |
|
|
bitmap[ j ] &=~(1 << i); // clear the bit
|
200 |
|
|
freeblocks++; // count the block
|
201 |
|
|
CYG_ASSERT( freeblocks <= numblocks, "freeblocks overflow" );
|
202 |
|
|
return true;
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
// -------------------------------------------------------------------------
|
206 |
|
|
|
207 |
|
|
inline void
|
208 |
|
|
Cyg_Mempool_Fixed_Implementation::get_status(
|
209 |
|
|
cyg_mempool_status_flag_t flags,
|
210 |
|
|
Cyg_Mempool_Status &status )
|
211 |
|
|
{
|
212 |
|
|
// as quick or quicker to just set it, rather than test flag first
|
213 |
|
|
status.arenabase = (const cyg_uint8 *)bitmap;
|
214 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_ARENASIZE) )
|
215 |
|
|
status.arenasize = top - (cyg_uint8 *)bitmap;
|
216 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_FREEBLOCKS) )
|
217 |
|
|
status.freeblocks = freeblocks;
|
218 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_TOTALALLOCATED) )
|
219 |
|
|
status.totalallocated = blocksize * numblocks;
|
220 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_TOTALFREE) )
|
221 |
|
|
status.totalfree = blocksize * freeblocks;
|
222 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_BLOCKSIZE) )
|
223 |
|
|
status.blocksize = blocksize;
|
224 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_MAXFREE) ) {
|
225 |
|
|
status.maxfree = freeblocks > 0 ? blocksize : 0;
|
226 |
|
|
}
|
227 |
|
|
// as quick or quicker to just set it, rather than test flag first
|
228 |
|
|
status.origbase = (const cyg_uint8 *)bitmap;
|
229 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_ORIGSIZE) )
|
230 |
|
|
status.origsize = top - (cyg_uint8 *)bitmap;
|
231 |
|
|
// quicker to just set it, rather than test flag first
|
232 |
|
|
status.maxoverhead = 0;
|
233 |
|
|
|
234 |
|
|
} // get_status()
|
235 |
|
|
|
236 |
|
|
// -------------------------------------------------------------------------
|
237 |
|
|
#endif // ifndef CYGONCE_MEMALLOC_MFIXIMPL_INL
|
238 |
|
|
// EOF mfiximpl.inl
|