1 |
786 |
skrzyp |
#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 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): hmt
|
46 |
|
|
// Contributors: jlarmour
|
47 |
|
|
// Date: 2000-06-12
|
48 |
|
|
// Purpose: Define Mfiximpl class interface
|
49 |
|
|
// Description: Inline class for constructing a fixed block allocator
|
50 |
|
|
// Usage: #include
|
51 |
|
|
//
|
52 |
|
|
//
|
53 |
|
|
//####DESCRIPTIONEND####
|
54 |
|
|
//
|
55 |
|
|
//==========================================================================
|
56 |
|
|
|
57 |
|
|
#include
|
58 |
|
|
#include // HAL_LSBIT_INDEX magic asm code
|
59 |
|
|
#include
|
60 |
|
|
|
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 |
|
|
CYG_MEMALLOC_FAIL(size);
|
127 |
|
|
return NULL;
|
128 |
|
|
}
|
129 |
|
|
cyg_int32 i = firstfree;
|
130 |
|
|
cyg_uint8 *p = NULL;
|
131 |
|
|
do {
|
132 |
|
|
if ( 0xffffffff != bitmap[ i ] ) {
|
133 |
|
|
// then there is a free block in this bucket
|
134 |
|
|
register cyg_uint32 j, k;
|
135 |
|
|
k = ~bitmap[ i ]; // look for a 1 in complement
|
136 |
|
|
HAL_LSBIT_INDEX( j, k );
|
137 |
|
|
CYG_ASSERT( 0 <= j && j <= 31, "Bad bit index" );
|
138 |
|
|
CYG_ASSERT( 0 == (bitmap[ i ] & (1 << j)), "Found bit not clear" );
|
139 |
|
|
bitmap[ i ] |= (1 << j); // set it allocated
|
140 |
|
|
firstfree = i;
|
141 |
|
|
freeblocks--;
|
142 |
|
|
CYG_ASSERT( freeblocks >= 0, "allocated too many" );
|
143 |
|
|
p = &mempool[ ((32 * i) + j) * blocksize ];
|
144 |
|
|
break;
|
145 |
|
|
}
|
146 |
|
|
if ( ++i >= maptop )
|
147 |
|
|
i = 0; // wrap if at top
|
148 |
|
|
} while ( i != firstfree ); // prevent hang if internal error
|
149 |
|
|
CYG_ASSERT( NULL != p, "Should have a block here" );
|
150 |
|
|
CYG_ASSERT( mempool <= p && p <= top, "alloc mem escaped" );
|
151 |
|
|
return p;
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
// -------------------------------------------------------------------------
|
155 |
|
|
// supposedly resize existing allocation. This is defined in the
|
156 |
|
|
// fixed block allocator purely for API consistency. It will return
|
157 |
|
|
// an error (false) for all values, except for the blocksize
|
158 |
|
|
// returns true on success
|
159 |
|
|
|
160 |
|
|
inline cyg_uint8 *
|
161 |
|
|
Cyg_Mempool_Fixed_Implementation::resize_alloc( cyg_uint8 *alloc_ptr,
|
162 |
|
|
cyg_int32 newsize,
|
163 |
|
|
cyg_int32 *oldsize )
|
164 |
|
|
{
|
165 |
|
|
CYG_CHECK_DATA_PTRC( alloc_ptr );
|
166 |
|
|
if ( NULL != oldsize )
|
167 |
|
|
CYG_CHECK_DATA_PTRC( oldsize );
|
168 |
|
|
|
169 |
|
|
CYG_ASSERT( alloc_ptr >= mempool && alloc_ptr < top,
|
170 |
|
|
"alloc_ptr outside pool" );
|
171 |
|
|
|
172 |
|
|
if ( NULL != oldsize )
|
173 |
|
|
*oldsize = blocksize;
|
174 |
|
|
|
175 |
|
|
if (newsize == blocksize)
|
176 |
|
|
return alloc_ptr;
|
177 |
|
|
else {
|
178 |
|
|
CYG_MEMALLOC_FAIL(newsize);
|
179 |
|
|
return NULL;
|
180 |
|
|
}
|
181 |
|
|
} // resize_alloc()
|
182 |
|
|
|
183 |
|
|
|
184 |
|
|
// -------------------------------------------------------------------------
|
185 |
|
|
|
186 |
|
|
inline cyg_bool
|
187 |
|
|
Cyg_Mempool_Fixed_Implementation::free( cyg_uint8 *p, cyg_int32 size )
|
188 |
|
|
{
|
189 |
|
|
// size parameter is not used
|
190 |
|
|
CYG_UNUSED_PARAM( cyg_int32, size );
|
191 |
|
|
if ( p < mempool || p >= top )
|
192 |
|
|
return false; // address way out of bounds
|
193 |
|
|
cyg_int32 i = p - mempool;
|
194 |
|
|
i = i / blocksize;
|
195 |
|
|
if ( &mempool[ i * blocksize ] != p )
|
196 |
|
|
return false; // address not aligned
|
197 |
|
|
cyg_int32 j = i / 32;
|
198 |
|
|
CYG_ASSERT( 0 <= j && j < maptop, "map index escaped" );
|
199 |
|
|
i = i - 32 * j;
|
200 |
|
|
CYG_ASSERT( 0 <= i && i < 32, "map bit index escaped" );
|
201 |
|
|
if ( ! ((1 << i) & bitmap[ j ] ) )
|
202 |
|
|
return false; // block was not allocated
|
203 |
|
|
bitmap[ j ] &=~(1 << i); // clear the bit
|
204 |
|
|
freeblocks++; // count the block
|
205 |
|
|
CYG_ASSERT( freeblocks <= numblocks, "freeblocks overflow" );
|
206 |
|
|
return true;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
// -------------------------------------------------------------------------
|
210 |
|
|
|
211 |
|
|
inline void
|
212 |
|
|
Cyg_Mempool_Fixed_Implementation::get_status(
|
213 |
|
|
cyg_mempool_status_flag_t flags,
|
214 |
|
|
Cyg_Mempool_Status &status )
|
215 |
|
|
{
|
216 |
|
|
// as quick or quicker to just set it, rather than test flag first
|
217 |
|
|
status.arenabase = (const cyg_uint8 *)bitmap;
|
218 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_ARENASIZE) )
|
219 |
|
|
status.arenasize = top - (cyg_uint8 *)bitmap;
|
220 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_FREEBLOCKS) )
|
221 |
|
|
status.freeblocks = freeblocks;
|
222 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_TOTALALLOCATED) )
|
223 |
|
|
status.totalallocated = blocksize * numblocks;
|
224 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_TOTALFREE) )
|
225 |
|
|
status.totalfree = blocksize * freeblocks;
|
226 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_BLOCKSIZE) )
|
227 |
|
|
status.blocksize = blocksize;
|
228 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_MAXFREE) ) {
|
229 |
|
|
status.maxfree = freeblocks > 0 ? blocksize : 0;
|
230 |
|
|
}
|
231 |
|
|
// as quick or quicker to just set it, rather than test flag first
|
232 |
|
|
status.origbase = (const cyg_uint8 *)bitmap;
|
233 |
|
|
if ( 0 != (flags & CYG_MEMPOOL_STAT_ORIGSIZE) )
|
234 |
|
|
status.origsize = top - (cyg_uint8 *)bitmap;
|
235 |
|
|
// quicker to just set it, rather than test flag first
|
236 |
|
|
status.maxoverhead = 0;
|
237 |
|
|
|
238 |
|
|
} // get_status()
|
239 |
|
|
|
240 |
|
|
// -------------------------------------------------------------------------
|
241 |
|
|
#endif // ifndef CYGONCE_MEMALLOC_MFIXIMPL_INL
|
242 |
|
|
// EOF mfiximpl.inl
|