sl@0
|
1 |
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// e32\euser\cbase\ub_bma.cpp
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
#include "ub_std.h"
|
sl@0
|
19 |
|
sl@0
|
20 |
const TInt KBitsPerInt=32;
|
sl@0
|
21 |
const TInt KBitsPerIntMask=(KBitsPerInt-1);
|
sl@0
|
22 |
const TInt KBitsPerIntShift=5;
|
sl@0
|
23 |
|
sl@0
|
24 |
|
sl@0
|
25 |
inline TInt FindLeastSignificantZero(register TUint n)
|
sl@0
|
26 |
{
|
sl@0
|
27 |
register TInt i=0;
|
sl@0
|
28 |
n=~n;
|
sl@0
|
29 |
if (n<<16==0) n>>=16, i+=16;
|
sl@0
|
30 |
if (n<<24==0) n>>=8, i+=8;
|
sl@0
|
31 |
if (n<<28==0) n>>=4, i+=4;
|
sl@0
|
32 |
if (n<<30==0) n>>=2, i+=2;
|
sl@0
|
33 |
if (n<<31==0) n>>=1, i+=1;
|
sl@0
|
34 |
return i;
|
sl@0
|
35 |
}
|
sl@0
|
36 |
|
sl@0
|
37 |
inline TInt FindLeastSignificantZero(register TUint n, TUint aFrom)
|
sl@0
|
38 |
{
|
sl@0
|
39 |
n |= ((1<<aFrom)-1);
|
sl@0
|
40 |
return FindLeastSignificantZero(n);
|
sl@0
|
41 |
}
|
sl@0
|
42 |
|
sl@0
|
43 |
inline TInt FindLeastSignificantOne(register TUint n)
|
sl@0
|
44 |
{
|
sl@0
|
45 |
register TInt i=0;
|
sl@0
|
46 |
if (n<<16==0) n>>=16, i+=16;
|
sl@0
|
47 |
if (n<<24==0) n>>=8, i+=8;
|
sl@0
|
48 |
if (n<<28==0) n>>=4, i+=4;
|
sl@0
|
49 |
if (n<<30==0) n>>=2, i+=2;
|
sl@0
|
50 |
if (n<<31==0) n>>=1, i+=1;
|
sl@0
|
51 |
return i;
|
sl@0
|
52 |
}
|
sl@0
|
53 |
|
sl@0
|
54 |
inline TInt FindMostSignificantZero(register TUint n)
|
sl@0
|
55 |
{
|
sl@0
|
56 |
register TInt i=31;
|
sl@0
|
57 |
n=~n;
|
sl@0
|
58 |
if (n<0x00010000) n<<=16, i-=16;
|
sl@0
|
59 |
if (n<0x01000000) n<<=8, i-=8;
|
sl@0
|
60 |
if (n<0x10000000) n<<=4, i-=4;
|
sl@0
|
61 |
if (n<0x40000000) n<<=2, i-=2;
|
sl@0
|
62 |
if (n<0x80000000) n<<=1, i-=1;
|
sl@0
|
63 |
return i;
|
sl@0
|
64 |
}
|
sl@0
|
65 |
|
sl@0
|
66 |
EXPORT_C CBitMapAllocator::CBitMapAllocator(TInt aSize,TInt aLength)
|
sl@0
|
67 |
//
|
sl@0
|
68 |
// Constructor
|
sl@0
|
69 |
//
|
sl@0
|
70 |
: iAvail(aSize),iSize(aSize),iLength(aLength)
|
sl@0
|
71 |
{
|
sl@0
|
72 |
TInt rem=aSize&KBitsPerIntMask;
|
sl@0
|
73 |
if (rem)
|
sl@0
|
74 |
{
|
sl@0
|
75 |
TInt last=(aSize-1)>>KBitsPerIntShift;
|
sl@0
|
76 |
iMap[last]=0xFFFFFFFFu<<rem;
|
sl@0
|
77 |
}
|
sl@0
|
78 |
}
|
sl@0
|
79 |
|
sl@0
|
80 |
EXPORT_C CBitMapAllocator::~CBitMapAllocator()
|
sl@0
|
81 |
//
|
sl@0
|
82 |
// Destructor
|
sl@0
|
83 |
//
|
sl@0
|
84 |
{
|
sl@0
|
85 |
}
|
sl@0
|
86 |
|
sl@0
|
87 |
EXPORT_C CBitMapAllocator *CBitMapAllocator::New(TInt aSize)
|
sl@0
|
88 |
//
|
sl@0
|
89 |
// Create a new bit map allocator.
|
sl@0
|
90 |
//
|
sl@0
|
91 |
{
|
sl@0
|
92 |
__ASSERT_ALWAYS(aSize>0,Panic(EBmaSizeLessOrEqualToZero));
|
sl@0
|
93 |
TInt sz=((aSize+KBitsPerIntMask)>>KBitsPerIntShift)-1;
|
sl@0
|
94 |
return(new(sz*sizeof(TUint)) CBitMapAllocator(aSize,sz+1));
|
sl@0
|
95 |
}
|
sl@0
|
96 |
|
sl@0
|
97 |
EXPORT_C CBitMapAllocator *CBitMapAllocator::NewL(TInt aSize)
|
sl@0
|
98 |
//
|
sl@0
|
99 |
// Create a new bit map allocator. Leave on any error.
|
sl@0
|
100 |
//
|
sl@0
|
101 |
{
|
sl@0
|
102 |
CBitMapAllocator *pA=New(aSize);
|
sl@0
|
103 |
User::LeaveIfNull(pA);
|
sl@0
|
104 |
return(pA);
|
sl@0
|
105 |
}
|
sl@0
|
106 |
|
sl@0
|
107 |
EXPORT_C TInt CBitMapAllocator::Alloc()
|
sl@0
|
108 |
//
|
sl@0
|
109 |
// Allocate the next position.
|
sl@0
|
110 |
//
|
sl@0
|
111 |
{
|
sl@0
|
112 |
if (iAvail)
|
sl@0
|
113 |
{
|
sl@0
|
114 |
TUint *pS=iMap;
|
sl@0
|
115 |
TUint *pE=pS+iLength;
|
sl@0
|
116 |
do {
|
sl@0
|
117 |
register TUint n=*pS++;
|
sl@0
|
118 |
if (n!=0xFFFFFFFFu)
|
sl@0
|
119 |
{
|
sl@0
|
120 |
iAvail--;
|
sl@0
|
121 |
TInt bit=FindLeastSignificantZero(n);
|
sl@0
|
122 |
*--pS=n|(1<<bit);
|
sl@0
|
123 |
return((TInt(pS-iMap)<<KBitsPerIntShift)+bit);
|
sl@0
|
124 |
}
|
sl@0
|
125 |
} while(pS<pE);
|
sl@0
|
126 |
Panic(EBmaInconsistentState);
|
sl@0
|
127 |
}
|
sl@0
|
128 |
return(KErrNoMemory);
|
sl@0
|
129 |
}
|
sl@0
|
130 |
|
sl@0
|
131 |
EXPORT_C TInt CBitMapAllocator::AllocFrom(TInt aPos)
|
sl@0
|
132 |
//
|
sl@0
|
133 |
// Allocate the next position after aPos.
|
sl@0
|
134 |
//
|
sl@0
|
135 |
{
|
sl@0
|
136 |
__ASSERT_ALWAYS((aPos>=0 && aPos<iSize),Panic(EBmaAllocFromOutOfRange));
|
sl@0
|
137 |
if (iAvail)
|
sl@0
|
138 |
{
|
sl@0
|
139 |
TUint *pS=iMap+(aPos>>KBitsPerIntShift);
|
sl@0
|
140 |
TUint *pE=iMap+iLength;
|
sl@0
|
141 |
TInt start=aPos&KBitsPerIntMask;
|
sl@0
|
142 |
register TUint n;
|
sl@0
|
143 |
if (start)
|
sl@0
|
144 |
{
|
sl@0
|
145 |
n=*pS++ | ~(0xFFFFFFFFu<<start);
|
sl@0
|
146 |
if (n!=0xFFFFFFFFu)
|
sl@0
|
147 |
goto found;
|
sl@0
|
148 |
}
|
sl@0
|
149 |
while(pS<pE)
|
sl@0
|
150 |
{
|
sl@0
|
151 |
n=*pS++;
|
sl@0
|
152 |
if (n!=0xFFFFFFFFu)
|
sl@0
|
153 |
{
|
sl@0
|
154 |
found:
|
sl@0
|
155 |
iAvail--;
|
sl@0
|
156 |
TInt bit=FindLeastSignificantZero(n);
|
sl@0
|
157 |
*--pS |= (1<<bit);
|
sl@0
|
158 |
return((TInt(pS-iMap)<<KBitsPerIntShift)+bit);
|
sl@0
|
159 |
}
|
sl@0
|
160 |
}
|
sl@0
|
161 |
}
|
sl@0
|
162 |
return(KErrNoMemory);
|
sl@0
|
163 |
}
|
sl@0
|
164 |
|
sl@0
|
165 |
EXPORT_C TInt CBitMapAllocator::AllocFromTop()
|
sl@0
|
166 |
//
|
sl@0
|
167 |
// Allocate the next position.
|
sl@0
|
168 |
//
|
sl@0
|
169 |
{
|
sl@0
|
170 |
if (iAvail)
|
sl@0
|
171 |
{
|
sl@0
|
172 |
TUint *pS=iMap;
|
sl@0
|
173 |
TUint *pE=pS+iLength;
|
sl@0
|
174 |
do {
|
sl@0
|
175 |
register TUint n=*--pE;
|
sl@0
|
176 |
if (n!=0xFFFFFFFFu)
|
sl@0
|
177 |
{
|
sl@0
|
178 |
iAvail--;
|
sl@0
|
179 |
TInt bit=FindMostSignificantZero(n);
|
sl@0
|
180 |
*pE=n|(1<<bit);
|
sl@0
|
181 |
return((TInt(pE-pS)<<KBitsPerIntShift)+bit);
|
sl@0
|
182 |
}
|
sl@0
|
183 |
} while(pE>pS);
|
sl@0
|
184 |
Panic(EBmaInconsistentState);
|
sl@0
|
185 |
}
|
sl@0
|
186 |
return(KErrNoMemory);
|
sl@0
|
187 |
}
|
sl@0
|
188 |
|
sl@0
|
189 |
EXPORT_C TInt CBitMapAllocator::AllocFromTopFrom(TInt aPos)
|
sl@0
|
190 |
//
|
sl@0
|
191 |
// Allocate the next position after aPos.
|
sl@0
|
192 |
//
|
sl@0
|
193 |
{
|
sl@0
|
194 |
__ASSERT_ALWAYS((aPos>=0 && aPos<iSize),Panic(EBmaAllocFromTopFromOutOfRange));
|
sl@0
|
195 |
if (iAvail)
|
sl@0
|
196 |
{
|
sl@0
|
197 |
TUint *pS=iMap;
|
sl@0
|
198 |
TUint *pE=pS+((aPos+1)>>KBitsPerIntShift);
|
sl@0
|
199 |
TInt start=(aPos+1)&KBitsPerIntMask;
|
sl@0
|
200 |
register TUint n;
|
sl@0
|
201 |
if (start)
|
sl@0
|
202 |
{
|
sl@0
|
203 |
n=*pE | (0xFFFFFFFFu<<start);
|
sl@0
|
204 |
if (n!=0xFFFFFFFFu)
|
sl@0
|
205 |
goto found;
|
sl@0
|
206 |
}
|
sl@0
|
207 |
while(pE>pS)
|
sl@0
|
208 |
{
|
sl@0
|
209 |
n=*--pE;
|
sl@0
|
210 |
if (n!=0xFFFFFFFFu)
|
sl@0
|
211 |
{
|
sl@0
|
212 |
found:
|
sl@0
|
213 |
iAvail--;
|
sl@0
|
214 |
TInt bit=FindMostSignificantZero(n);
|
sl@0
|
215 |
*pE|=(1<<bit);
|
sl@0
|
216 |
return((TInt(pE-pS)<<KBitsPerIntShift)+bit);
|
sl@0
|
217 |
}
|
sl@0
|
218 |
}
|
sl@0
|
219 |
}
|
sl@0
|
220 |
return(KErrNoMemory);
|
sl@0
|
221 |
}
|
sl@0
|
222 |
|
sl@0
|
223 |
EXPORT_C TInt CBitMapAllocator::Alloc(TInt aCount, TInt& aConsecutive)
|
sl@0
|
224 |
{
|
sl@0
|
225 |
__ASSERT_ALWAYS((aCount>0),Panic(EBmaAllocCountNegative));
|
sl@0
|
226 |
TInt initPos;
|
sl@0
|
227 |
if (iAvail)
|
sl@0
|
228 |
{
|
sl@0
|
229 |
TUint *pS=iMap;
|
sl@0
|
230 |
TUint *pE=pS+iLength;
|
sl@0
|
231 |
register TUint n;
|
sl@0
|
232 |
do {
|
sl@0
|
233 |
n=*pS++;
|
sl@0
|
234 |
if (n!=0xFFFFFFFFu)
|
sl@0
|
235 |
goto found;
|
sl@0
|
236 |
} while(pS<pE);
|
sl@0
|
237 |
Panic(EBmaInconsistentState);
|
sl@0
|
238 |
found:
|
sl@0
|
239 |
register TInt c;
|
sl@0
|
240 |
pS--;
|
sl@0
|
241 |
TInt bit=FindLeastSignificantZero(n);
|
sl@0
|
242 |
initPos=(TInt(pS-iMap)<<KBitsPerIntShift)+bit;
|
sl@0
|
243 |
n>>=bit;
|
sl@0
|
244 |
if (n)
|
sl@0
|
245 |
{
|
sl@0
|
246 |
c=FindLeastSignificantOne(n);
|
sl@0
|
247 |
if (aCount<c) c=aCount;
|
sl@0
|
248 |
*pS |= ~(0xFFFFFFFFu<<c)<<bit;
|
sl@0
|
249 |
iAvail-=c;
|
sl@0
|
250 |
aConsecutive=c;
|
sl@0
|
251 |
return initPos;
|
sl@0
|
252 |
}
|
sl@0
|
253 |
c=KBitsPerInt-bit;
|
sl@0
|
254 |
if (c>=aCount)
|
sl@0
|
255 |
{
|
sl@0
|
256 |
c=aCount;
|
sl@0
|
257 |
if (c<KBitsPerInt)
|
sl@0
|
258 |
*pS |= ~(0xFFFFFFFFu<<c)<<bit;
|
sl@0
|
259 |
else
|
sl@0
|
260 |
*pS |= 0xFFFFFFFFu<<bit;
|
sl@0
|
261 |
iAvail-=c;
|
sl@0
|
262 |
aConsecutive=c;
|
sl@0
|
263 |
return initPos;
|
sl@0
|
264 |
}
|
sl@0
|
265 |
c=aCount-c;
|
sl@0
|
266 |
*pS=0xFFFFFFFFu;
|
sl@0
|
267 |
while(++pS<pE && (n=*pS)==0 && c>=KBitsPerInt)
|
sl@0
|
268 |
*pS=0xFFFFFFFFu, c-=KBitsPerInt;
|
sl@0
|
269 |
if (c && pS<pE && n!=0xFFFFFFFFu)
|
sl@0
|
270 |
{
|
sl@0
|
271 |
bit=n?FindLeastSignificantOne(n):KBitsPerInt;
|
sl@0
|
272 |
if (bit>c) bit=c;
|
sl@0
|
273 |
*pS |= ~(0xFFFFFFFFu<<bit);
|
sl@0
|
274 |
c-=bit;
|
sl@0
|
275 |
}
|
sl@0
|
276 |
aConsecutive=aCount-c;
|
sl@0
|
277 |
iAvail-=aConsecutive;
|
sl@0
|
278 |
return initPos;
|
sl@0
|
279 |
}
|
sl@0
|
280 |
aConsecutive=0;
|
sl@0
|
281 |
return KErrNoMemory;
|
sl@0
|
282 |
}
|
sl@0
|
283 |
|
sl@0
|
284 |
LOCAL_D const TUint AlignedSearchMask[] =
|
sl@0
|
285 |
{0x00000000,0xAAAAAAAA,0xEEEEEEEE,0xFEFEFEFE,0xFFFEFFFE};
|
sl@0
|
286 |
|
sl@0
|
287 |
EXPORT_C TInt CBitMapAllocator::AllocAligned(TInt anAlignment)
|
sl@0
|
288 |
{
|
sl@0
|
289 |
__ASSERT_ALWAYS((anAlignment>=0 && anAlignment<32),Panic(EBmaAllAlgnOutOfRange));
|
sl@0
|
290 |
if (iAvail==0)
|
sl@0
|
291 |
return KErrNoMemory;
|
sl@0
|
292 |
TUint mask;
|
sl@0
|
293 |
TInt step;
|
sl@0
|
294 |
if (anAlignment>=KBitsPerIntShift)
|
sl@0
|
295 |
{
|
sl@0
|
296 |
mask=0xFFFFFFFEu;
|
sl@0
|
297 |
step=1<<(anAlignment-KBitsPerIntShift);
|
sl@0
|
298 |
}
|
sl@0
|
299 |
else
|
sl@0
|
300 |
{
|
sl@0
|
301 |
mask=AlignedSearchMask[anAlignment];
|
sl@0
|
302 |
step=1;
|
sl@0
|
303 |
}
|
sl@0
|
304 |
TUint *pM=iMap;
|
sl@0
|
305 |
TUint *pE=pM+iLength;
|
sl@0
|
306 |
do {
|
sl@0
|
307 |
register TUint n=*pM | mask;
|
sl@0
|
308 |
if (n!=0xFFFFFFFFu)
|
sl@0
|
309 |
{
|
sl@0
|
310 |
iAvail--;
|
sl@0
|
311 |
TInt bit=(mask==0xFFFFFFFEu)?0:FindLeastSignificantZero(n);
|
sl@0
|
312 |
*pM |= (1<<bit);
|
sl@0
|
313 |
return((TInt(pM-iMap)<<KBitsPerIntShift)+bit);
|
sl@0
|
314 |
}
|
sl@0
|
315 |
pM+=step;
|
sl@0
|
316 |
} while(pM<pE);
|
sl@0
|
317 |
return KErrNoMemory;
|
sl@0
|
318 |
}
|
sl@0
|
319 |
|
sl@0
|
320 |
EXPORT_C TInt CBitMapAllocator::AllocAlignedBlock(TInt anAlignment)
|
sl@0
|
321 |
{
|
sl@0
|
322 |
__ASSERT_ALWAYS((anAlignment>=0 && anAlignment<32),Panic(EBmaAllAlgnBOutOfRange));
|
sl@0
|
323 |
if (iAvail==0)
|
sl@0
|
324 |
return KErrNoMemory;
|
sl@0
|
325 |
TInt blocksz=1<<anAlignment;
|
sl@0
|
326 |
TUint mask;
|
sl@0
|
327 |
TUint block;
|
sl@0
|
328 |
TInt step;
|
sl@0
|
329 |
if (anAlignment>=KBitsPerIntShift)
|
sl@0
|
330 |
{
|
sl@0
|
331 |
mask=0xFFFFFFFEu;
|
sl@0
|
332 |
step=1<<(anAlignment-KBitsPerIntShift);
|
sl@0
|
333 |
block=0xFFFFFFFFu;
|
sl@0
|
334 |
}
|
sl@0
|
335 |
else
|
sl@0
|
336 |
{
|
sl@0
|
337 |
mask=AlignedSearchMask[anAlignment];
|
sl@0
|
338 |
step=1;
|
sl@0
|
339 |
block=~(0xFFFFFFFFu<<blocksz);
|
sl@0
|
340 |
}
|
sl@0
|
341 |
TUint *pM=iMap;
|
sl@0
|
342 |
TUint *pE=pM+iLength;
|
sl@0
|
343 |
do {
|
sl@0
|
344 |
register TUint n=*pM | mask;
|
sl@0
|
345 |
if (n!=0xFFFFFFFFu)
|
sl@0
|
346 |
{
|
sl@0
|
347 |
if (blocksz>=KBitsPerInt)
|
sl@0
|
348 |
{
|
sl@0
|
349 |
n=0;
|
sl@0
|
350 |
TUint *pS=pM+step;
|
sl@0
|
351 |
if (pS<=pE)
|
sl@0
|
352 |
{
|
sl@0
|
353 |
do n|=*pM++; while(pM<pS);
|
sl@0
|
354 |
pM-=step;
|
sl@0
|
355 |
if (n==0)
|
sl@0
|
356 |
{
|
sl@0
|
357 |
iAvail-=blocksz;
|
sl@0
|
358 |
do *pM++=0xFFFFFFFFu; while(pM<pS);
|
sl@0
|
359 |
pM-=step;
|
sl@0
|
360 |
return (TInt(pM-iMap)<<KBitsPerIntShift);
|
sl@0
|
361 |
}
|
sl@0
|
362 |
}
|
sl@0
|
363 |
}
|
sl@0
|
364 |
else
|
sl@0
|
365 |
{
|
sl@0
|
366 |
TInt bit=FindLeastSignificantZero(n);
|
sl@0
|
367 |
mask=block<<bit;
|
sl@0
|
368 |
n=*pM;
|
sl@0
|
369 |
do {
|
sl@0
|
370 |
if ((n&mask)==0)
|
sl@0
|
371 |
{
|
sl@0
|
372 |
*pM |= mask;
|
sl@0
|
373 |
iAvail-=blocksz;
|
sl@0
|
374 |
return((TInt(pM-iMap)<<KBitsPerIntShift)+bit);
|
sl@0
|
375 |
}
|
sl@0
|
376 |
bit+=blocksz;
|
sl@0
|
377 |
mask<<=blocksz;
|
sl@0
|
378 |
} while(mask);
|
sl@0
|
379 |
}
|
sl@0
|
380 |
}
|
sl@0
|
381 |
pM+=step;
|
sl@0
|
382 |
} while(pM<pE);
|
sl@0
|
383 |
return KErrNoMemory;
|
sl@0
|
384 |
}
|
sl@0
|
385 |
|
sl@0
|
386 |
EXPORT_C void CBitMapAllocator::AllocAt(TInt aPos)
|
sl@0
|
387 |
//
|
sl@0
|
388 |
// Allocate a required position.
|
sl@0
|
389 |
//
|
sl@0
|
390 |
{
|
sl@0
|
391 |
__ASSERT_ALWAYS(aPos>=0 && aPos<iSize,Panic(EBmaAllocOutOfRange));
|
sl@0
|
392 |
TUint *pM=iMap+(aPos>>KBitsPerIntShift);
|
sl@0
|
393 |
TUint mask=1<<(aPos&KBitsPerIntMask);
|
sl@0
|
394 |
__ASSERT_ALWAYS(!(*pM&mask),Panic(EBmaAllocAtAlreadyAllocated));
|
sl@0
|
395 |
*pM |= mask;
|
sl@0
|
396 |
iAvail--;
|
sl@0
|
397 |
}
|
sl@0
|
398 |
|
sl@0
|
399 |
EXPORT_C void CBitMapAllocator::AllocAt(TInt aPos, TInt aCount)
|
sl@0
|
400 |
{
|
sl@0
|
401 |
__ASSERT_ALWAYS((aPos>=0 && (aPos+aCount)<=iSize),Panic(EBmaAllocBlkOutOfRange));
|
sl@0
|
402 |
TUint *pM=iMap+(aPos>>KBitsPerIntShift);
|
sl@0
|
403 |
TInt c=aPos&KBitsPerIntMask;
|
sl@0
|
404 |
TUint m;
|
sl@0
|
405 |
if (aCount<(KBitsPerInt-c))
|
sl@0
|
406 |
{
|
sl@0
|
407 |
m=~(0xFFFFFFFFu<<aCount)<<c;
|
sl@0
|
408 |
if (*pM & m)
|
sl@0
|
409 |
Panic(EBmaAllocBlkNotFree);
|
sl@0
|
410 |
*pM |= m;
|
sl@0
|
411 |
iAvail-=aCount;
|
sl@0
|
412 |
return;
|
sl@0
|
413 |
}
|
sl@0
|
414 |
m=0xFFFFFFFFu<<c;
|
sl@0
|
415 |
if (*pM & m)
|
sl@0
|
416 |
Panic(EBmaAllocBlkNotFree);
|
sl@0
|
417 |
*pM++ |= m;
|
sl@0
|
418 |
c=aCount-KBitsPerInt+c;
|
sl@0
|
419 |
while(c>=KBitsPerInt)
|
sl@0
|
420 |
{
|
sl@0
|
421 |
if (*pM)
|
sl@0
|
422 |
Panic(EBmaAllocBlkNotFree);
|
sl@0
|
423 |
*pM++=0xFFFFFFFFu;
|
sl@0
|
424 |
c-=KBitsPerInt;
|
sl@0
|
425 |
}
|
sl@0
|
426 |
if (c)
|
sl@0
|
427 |
{
|
sl@0
|
428 |
m=0xFFFFFFFFu>>(KBitsPerInt-c);
|
sl@0
|
429 |
if (*pM & m)
|
sl@0
|
430 |
Panic(EBmaAllocBlkNotFree);
|
sl@0
|
431 |
*pM++ |= m;
|
sl@0
|
432 |
}
|
sl@0
|
433 |
iAvail-=aCount;
|
sl@0
|
434 |
}
|
sl@0
|
435 |
|
sl@0
|
436 |
EXPORT_C TInt CBitMapAllocator::ExtractRamPages(TInt aConsecutive,TInt& aPageNo)
|
sl@0
|
437 |
{
|
sl@0
|
438 |
if(iAvail<aConsecutive)
|
sl@0
|
439 |
return KErrNoMemory;
|
sl@0
|
440 |
|
sl@0
|
441 |
TUint *pS=iMap;
|
sl@0
|
442 |
TUint *pE=pS+iLength;
|
sl@0
|
443 |
|
sl@0
|
444 |
do {
|
sl@0
|
445 |
register TUint n=*pS++;
|
sl@0
|
446 |
if (n!=0xFFFFFFFFu)
|
sl@0
|
447 |
{
|
sl@0
|
448 |
TInt x = 0;
|
sl@0
|
449 |
do
|
sl@0
|
450 |
{
|
sl@0
|
451 |
TInt bit=FindLeastSignificantZero(n,x);
|
sl@0
|
452 |
TInt pos=(TInt((pS-1)-iMap)<<KBitsPerIntShift)+bit;
|
sl@0
|
453 |
if(pos+aConsecutive > iSize)
|
sl@0
|
454 |
return KErrNoMemory;
|
sl@0
|
455 |
if(IsFree(pos,aConsecutive))
|
sl@0
|
456 |
{
|
sl@0
|
457 |
AllocAt(pos,aConsecutive);
|
sl@0
|
458 |
aPageNo=pos;
|
sl@0
|
459 |
return KErrNone;
|
sl@0
|
460 |
}
|
sl@0
|
461 |
else
|
sl@0
|
462 |
{
|
sl@0
|
463 |
x = bit+2;
|
sl@0
|
464 |
}
|
sl@0
|
465 |
}
|
sl@0
|
466 |
while (x < KBitsPerInt);
|
sl@0
|
467 |
}
|
sl@0
|
468 |
} while(pS<pE);
|
sl@0
|
469 |
return KErrNoMemory;
|
sl@0
|
470 |
}
|
sl@0
|
471 |
|
sl@0
|
472 |
EXPORT_C TBool CBitMapAllocator::IsFree(TInt aPos)
|
sl@0
|
473 |
//
|
sl@0
|
474 |
// Check a required position is available
|
sl@0
|
475 |
//
|
sl@0
|
476 |
{
|
sl@0
|
477 |
__ASSERT_ALWAYS(aPos>=0 && aPos<iSize,Panic(EBmaFreeOutOfRange));
|
sl@0
|
478 |
TUint n=iMap[aPos>>KBitsPerIntShift];
|
sl@0
|
479 |
return !(n>>(aPos&KBitsPerIntMask)&1);
|
sl@0
|
480 |
}
|
sl@0
|
481 |
|
sl@0
|
482 |
EXPORT_C TBool CBitMapAllocator::IsFree(TInt aPos, TInt aCount)
|
sl@0
|
483 |
{
|
sl@0
|
484 |
__ASSERT_ALWAYS((aPos>=0 && (aPos+aCount)<=iSize),Panic(EBmaChkBlkOutOfRange));
|
sl@0
|
485 |
TUint *pM=iMap+(aPos>>KBitsPerIntShift);
|
sl@0
|
486 |
TUint m=*pM++;
|
sl@0
|
487 |
TInt c=aPos&KBitsPerIntMask;
|
sl@0
|
488 |
m>>=c;
|
sl@0
|
489 |
if (aCount<(KBitsPerInt-c))
|
sl@0
|
490 |
{
|
sl@0
|
491 |
return !(m&~(0xFFFFFFFFu<<aCount));
|
sl@0
|
492 |
}
|
sl@0
|
493 |
aCount-=(KBitsPerInt-c);
|
sl@0
|
494 |
while(aCount>=KBitsPerInt)
|
sl@0
|
495 |
{
|
sl@0
|
496 |
m |= *pM++;
|
sl@0
|
497 |
aCount-=KBitsPerInt;
|
sl@0
|
498 |
}
|
sl@0
|
499 |
if (aCount)
|
sl@0
|
500 |
{
|
sl@0
|
501 |
m|=(*pM<<(KBitsPerInt-aCount));
|
sl@0
|
502 |
}
|
sl@0
|
503 |
return(!m);
|
sl@0
|
504 |
}
|
sl@0
|
505 |
|
sl@0
|
506 |
EXPORT_C void CBitMapAllocator::Free(TInt aPos)
|
sl@0
|
507 |
//
|
sl@0
|
508 |
// Free a position.
|
sl@0
|
509 |
//
|
sl@0
|
510 |
{
|
sl@0
|
511 |
__ASSERT_ALWAYS(aPos>=0 && aPos<iSize,Panic(EBmaFreeOutOfRange));
|
sl@0
|
512 |
TUint *pM=iMap+(aPos>>KBitsPerIntShift);
|
sl@0
|
513 |
TUint mask=1<<(aPos&KBitsPerIntMask);
|
sl@0
|
514 |
__ASSERT_ALWAYS((*pM&mask),Panic(EBmaFreeNotAllocated));
|
sl@0
|
515 |
*pM &= ~mask;
|
sl@0
|
516 |
iAvail++;
|
sl@0
|
517 |
}
|
sl@0
|
518 |
|
sl@0
|
519 |
EXPORT_C void CBitMapAllocator::Free(TInt aPos, TInt aCount)
|
sl@0
|
520 |
{
|
sl@0
|
521 |
__ASSERT_ALWAYS((aPos>=0 && (aPos+aCount)<=iSize),Panic(EBmaFreeBlkOutOfRange));
|
sl@0
|
522 |
TUint *pM=iMap+(aPos>>KBitsPerIntShift);
|
sl@0
|
523 |
TInt c=aPos&KBitsPerIntMask;
|
sl@0
|
524 |
TUint m;
|
sl@0
|
525 |
if (aCount<(KBitsPerInt-c))
|
sl@0
|
526 |
{
|
sl@0
|
527 |
m=~(0xFFFFFFFFu<<aCount)<<c;
|
sl@0
|
528 |
if ((*pM & m)!=m)
|
sl@0
|
529 |
Panic(EBmaFreeBlkNotAllocated);
|
sl@0
|
530 |
*pM &= ~m;
|
sl@0
|
531 |
iAvail+=aCount;
|
sl@0
|
532 |
return;
|
sl@0
|
533 |
}
|
sl@0
|
534 |
m=0xFFFFFFFFu<<c;
|
sl@0
|
535 |
if ((*pM & m)!=m)
|
sl@0
|
536 |
Panic(EBmaFreeBlkNotAllocated);
|
sl@0
|
537 |
*pM++ &= ~m;
|
sl@0
|
538 |
c=aCount-KBitsPerInt+c;
|
sl@0
|
539 |
while(c>=KBitsPerInt)
|
sl@0
|
540 |
{
|
sl@0
|
541 |
if (*pM!=0xFFFFFFFF)
|
sl@0
|
542 |
Panic(EBmaFreeBlkNotAllocated);
|
sl@0
|
543 |
*pM++=0;
|
sl@0
|
544 |
c-=KBitsPerInt;
|
sl@0
|
545 |
}
|
sl@0
|
546 |
if (c)
|
sl@0
|
547 |
{
|
sl@0
|
548 |
m=0xFFFFFFFFu>>(KBitsPerInt-c);
|
sl@0
|
549 |
if ((*pM & m)!=m)
|
sl@0
|
550 |
Panic(EBmaFreeBlkNotAllocated);
|
sl@0
|
551 |
*pM++ &= ~m;
|
sl@0
|
552 |
}
|
sl@0
|
553 |
iAvail+=aCount;
|
sl@0
|
554 |
}
|
sl@0
|
555 |
|
sl@0
|
556 |
EXPORT_C TInt CBitMapAllocator::Avail()
|
sl@0
|
557 |
//
|
sl@0
|
558 |
// Get the available blocks count.
|
sl@0
|
559 |
//
|
sl@0
|
560 |
{
|
sl@0
|
561 |
return(iAvail);
|
sl@0
|
562 |
}
|
sl@0
|
563 |
|
sl@0
|
564 |
EXPORT_C TInt CBitMapAllocator::Size()
|
sl@0
|
565 |
//
|
sl@0
|
566 |
// Get the size of all available blocks.
|
sl@0
|
567 |
//
|
sl@0
|
568 |
{
|
sl@0
|
569 |
return(iSize);
|
sl@0
|
570 |
}
|
sl@0
|
571 |
|