sl@0
|
1 |
// Copyright (c) 1995-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 |
// e32test\buffer\t_bma.cpp
|
sl@0
|
15 |
// Overview:
|
sl@0
|
16 |
// Test the bitmap allocation abilities of the CBitMapAllocator class.
|
sl@0
|
17 |
// API Information:
|
sl@0
|
18 |
// CBitMapAllocator.
|
sl@0
|
19 |
// Details:
|
sl@0
|
20 |
// - Create an instance of CBitMapAllocator class with positive size using New and NewL methods,
|
sl@0
|
21 |
// verify that object is created and deleted successfully, test the heap allocation failure.
|
sl@0
|
22 |
// - Verify that the heap has not been corrupted by the test.
|
sl@0
|
23 |
// - Test Alloc, AllocFromTop, AllocAt, Free, and AllocFromTopFrom methods of
|
sl@0
|
24 |
// CBitMapAllocator class are as expected.
|
sl@0
|
25 |
// - Allocate all available memory using Alloc, AllocFromTop, AllocFromTopFrom
|
sl@0
|
26 |
// and check that available free space is zero.
|
sl@0
|
27 |
// - Allocate more than available memory using Alloc, AllocFromTop,
|
sl@0
|
28 |
// AllocFromTopFrom and check the return value is KErrorNoMemory.
|
sl@0
|
29 |
// - Free the memory and check that available free space is equal to the size.
|
sl@0
|
30 |
// - Allocate at specified blocks, check the allocation and available free block
|
sl@0
|
31 |
// is as expected.
|
sl@0
|
32 |
// - Free the block and check the available space is as expected.
|
sl@0
|
33 |
// - Check the alignment of blocks after allocation is as expected.
|
sl@0
|
34 |
// - Perform all of the above tests for CBitMapAllocator size of 1, 4, 32, 33, 68, 96, 64, 65 and 63 bits.
|
sl@0
|
35 |
// - Allocate some contiguous pages of RAM from the kernel's free page pool with pattern of
|
sl@0
|
36 |
// increasingly large gaps and test that the pages are allocated as specified.
|
sl@0
|
37 |
// - Check KErrorNoMemory is returned when extracting a page beyond the available space.
|
sl@0
|
38 |
// - Perform a test specifically for defect EXT-5AMDKP, Alloc, Free and ExtractRamPages. Test for
|
sl@0
|
39 |
// expected results.
|
sl@0
|
40 |
// - Test whether the heap has been corrupted by any of the tests.
|
sl@0
|
41 |
// Platforms/Drives/Compatibility:
|
sl@0
|
42 |
// All
|
sl@0
|
43 |
// Assumptions/Requirement/Pre-requisites:
|
sl@0
|
44 |
// Failures and causes:
|
sl@0
|
45 |
// Base Port information:
|
sl@0
|
46 |
//
|
sl@0
|
47 |
//
|
sl@0
|
48 |
|
sl@0
|
49 |
#include <e32test.h>
|
sl@0
|
50 |
#include <e32base.h>
|
sl@0
|
51 |
#include <e32base_private.h>
|
sl@0
|
52 |
#include <e32def.h>
|
sl@0
|
53 |
#include <e32def_private.h>
|
sl@0
|
54 |
|
sl@0
|
55 |
const TInt KMaxAllocations=50;
|
sl@0
|
56 |
|
sl@0
|
57 |
LOCAL_D RTest test(_L("T_BMA"));
|
sl@0
|
58 |
|
sl@0
|
59 |
LOCAL_C void testNew(TInt aSize)
|
sl@0
|
60 |
//
|
sl@0
|
61 |
// Test New
|
sl@0
|
62 |
//
|
sl@0
|
63 |
{
|
sl@0
|
64 |
|
sl@0
|
65 |
test.Start(_L("New"));
|
sl@0
|
66 |
__UHEAP_MARK;
|
sl@0
|
67 |
CBitMapAllocator* pBitMapAllocator=CBitMapAllocator::New(aSize);
|
sl@0
|
68 |
test(pBitMapAllocator!=NULL);
|
sl@0
|
69 |
test(pBitMapAllocator->Size()==pBitMapAllocator->Avail());
|
sl@0
|
70 |
delete pBitMapAllocator;
|
sl@0
|
71 |
__UHEAP_CHECK(0);
|
sl@0
|
72 |
for (TInt i=1;i<KMaxAllocations;i++)
|
sl@0
|
73 |
{
|
sl@0
|
74 |
test.Printf(_L("Try %d\n"),i);
|
sl@0
|
75 |
__UHEAP_SETFAIL(RHeap::EDeterministic,i);
|
sl@0
|
76 |
pBitMapAllocator=CBitMapAllocator::New(aSize);
|
sl@0
|
77 |
if (pBitMapAllocator!=NULL)
|
sl@0
|
78 |
break;
|
sl@0
|
79 |
__UHEAP_CHECK(0);
|
sl@0
|
80 |
}
|
sl@0
|
81 |
delete pBitMapAllocator;
|
sl@0
|
82 |
__UHEAP_MARKEND;
|
sl@0
|
83 |
__UHEAP_RESET;
|
sl@0
|
84 |
test.End();
|
sl@0
|
85 |
}
|
sl@0
|
86 |
|
sl@0
|
87 |
LOCAL_C void testNewL(TInt aSize)
|
sl@0
|
88 |
//
|
sl@0
|
89 |
// Test NewL
|
sl@0
|
90 |
//
|
sl@0
|
91 |
{
|
sl@0
|
92 |
|
sl@0
|
93 |
test.Start(_L("NewL"));
|
sl@0
|
94 |
__UHEAP_MARK;
|
sl@0
|
95 |
CBitMapAllocator* pBitMapAllocator=CBitMapAllocator::NewL(aSize);
|
sl@0
|
96 |
test(pBitMapAllocator!=NULL);
|
sl@0
|
97 |
test(pBitMapAllocator->Size()==pBitMapAllocator->Avail());
|
sl@0
|
98 |
delete pBitMapAllocator;
|
sl@0
|
99 |
__UHEAP_CHECK(0);
|
sl@0
|
100 |
test.Next(_L("Repetitive NewL"));
|
sl@0
|
101 |
for (TInt i=1;i<KMaxAllocations;i++)
|
sl@0
|
102 |
{
|
sl@0
|
103 |
test.Printf(_L("Try %d\n"),i);
|
sl@0
|
104 |
__UHEAP_SETFAIL(RHeap::EDeterministic,i);
|
sl@0
|
105 |
TRAPD(r,pBitMapAllocator=CBitMapAllocator::NewL(aSize));
|
sl@0
|
106 |
if (r==KErrNone)
|
sl@0
|
107 |
break;
|
sl@0
|
108 |
__UHEAP_CHECK(0);
|
sl@0
|
109 |
}
|
sl@0
|
110 |
delete pBitMapAllocator;
|
sl@0
|
111 |
__UHEAP_MARKEND;
|
sl@0
|
112 |
__UHEAP_RESET;
|
sl@0
|
113 |
test.End();
|
sl@0
|
114 |
}
|
sl@0
|
115 |
|
sl@0
|
116 |
LOCAL_C void testAlloc(TInt aSize)
|
sl@0
|
117 |
//
|
sl@0
|
118 |
// Test Alloc, AllocFromTop, AllocAt, and Free, and AllocFromTopFrom
|
sl@0
|
119 |
//
|
sl@0
|
120 |
{
|
sl@0
|
121 |
|
sl@0
|
122 |
CBitMapAllocator* pBitMapAllocator=CBitMapAllocator::New(aSize);
|
sl@0
|
123 |
test(pBitMapAllocator!=NULL);
|
sl@0
|
124 |
test.Start(_L("Alloc all available"));
|
sl@0
|
125 |
TInt available=pBitMapAllocator->Avail();
|
sl@0
|
126 |
TInt i=0;
|
sl@0
|
127 |
for (;i<available;i++)
|
sl@0
|
128 |
{
|
sl@0
|
129 |
TInt j=pBitMapAllocator->Alloc();
|
sl@0
|
130 |
test(j==i);
|
sl@0
|
131 |
}
|
sl@0
|
132 |
test(pBitMapAllocator->Avail()==0);
|
sl@0
|
133 |
//
|
sl@0
|
134 |
test.Next(_L("Try to alloc more than available"));
|
sl@0
|
135 |
i=pBitMapAllocator->Alloc();
|
sl@0
|
136 |
test(i==KErrNoMemory);
|
sl@0
|
137 |
//
|
sl@0
|
138 |
test.Next(_L("Free"));
|
sl@0
|
139 |
for (i=0;i<available;i++)
|
sl@0
|
140 |
pBitMapAllocator->Free(i);
|
sl@0
|
141 |
test(pBitMapAllocator->Avail()==pBitMapAllocator->Size());
|
sl@0
|
142 |
//
|
sl@0
|
143 |
test.Next(_L("AllocFromTop"));
|
sl@0
|
144 |
for (i=available-1;i>=0;i--)
|
sl@0
|
145 |
{
|
sl@0
|
146 |
TInt j=pBitMapAllocator->AllocFromTop();
|
sl@0
|
147 |
test(j==i);
|
sl@0
|
148 |
}
|
sl@0
|
149 |
test(pBitMapAllocator->Avail()==0);
|
sl@0
|
150 |
//
|
sl@0
|
151 |
test.Next(_L("Try to AllocFromTop more than available"));
|
sl@0
|
152 |
i=pBitMapAllocator->AllocFromTop();
|
sl@0
|
153 |
test(i==KErrNoMemory);
|
sl@0
|
154 |
//
|
sl@0
|
155 |
test.Next(_L("Free (again)"));
|
sl@0
|
156 |
for (i=0;i<available;i++)
|
sl@0
|
157 |
pBitMapAllocator->Free(i);
|
sl@0
|
158 |
test(pBitMapAllocator->Avail()==pBitMapAllocator->Size());
|
sl@0
|
159 |
//
|
sl@0
|
160 |
test.Next(_L("AllocAt"));
|
sl@0
|
161 |
pBitMapAllocator->AllocAt(aSize-1);
|
sl@0
|
162 |
test(pBitMapAllocator->Avail()==pBitMapAllocator->Size()-1);
|
sl@0
|
163 |
//
|
sl@0
|
164 |
// test.Next(_L("AllocAt an already allocated cell")); // this test should cause a Panic.
|
sl@0
|
165 |
// pBitMapAllocator->AllocAt(aSize-1);
|
sl@0
|
166 |
// test(pBitMapAllocator->Avail()==pBitMapAllocator->Size()-1);
|
sl@0
|
167 |
//
|
sl@0
|
168 |
test.Next(_L("Free (again)"));
|
sl@0
|
169 |
pBitMapAllocator->Free(aSize-1);
|
sl@0
|
170 |
test(pBitMapAllocator->Avail()==pBitMapAllocator->Size());
|
sl@0
|
171 |
//
|
sl@0
|
172 |
test.Next(_L("AllocFromTopFrom"));
|
sl@0
|
173 |
TInt x;
|
sl@0
|
174 |
for (x=available-1;x>0;x--)
|
sl@0
|
175 |
{
|
sl@0
|
176 |
for (i=x;i>=0;i--)
|
sl@0
|
177 |
{
|
sl@0
|
178 |
TInt j=pBitMapAllocator->AllocFromTopFrom(x);
|
sl@0
|
179 |
test(j==i);
|
sl@0
|
180 |
test(!pBitMapAllocator->IsFree(j));
|
sl@0
|
181 |
}
|
sl@0
|
182 |
test(pBitMapAllocator->Avail()==available-x-1);
|
sl@0
|
183 |
|
sl@0
|
184 |
test.Next(_L("Try to AllocFromTopFrom more than available"));
|
sl@0
|
185 |
i=pBitMapAllocator->AllocFromTopFrom(x);
|
sl@0
|
186 |
test(i==KErrNoMemory);
|
sl@0
|
187 |
//
|
sl@0
|
188 |
TInt y;
|
sl@0
|
189 |
for (y=0;y<=x;y++)
|
sl@0
|
190 |
{
|
sl@0
|
191 |
for (i=0;i<=x;i++)
|
sl@0
|
192 |
{
|
sl@0
|
193 |
if (pBitMapAllocator->Avail()<=available-x-1)
|
sl@0
|
194 |
pBitMapAllocator->Free(y);
|
sl@0
|
195 |
TInt j=pBitMapAllocator->AllocFromTopFrom(i);
|
sl@0
|
196 |
if (i<y)
|
sl@0
|
197 |
test(j==KErrNoMemory);
|
sl@0
|
198 |
else
|
sl@0
|
199 |
{
|
sl@0
|
200 |
test(j==y);
|
sl@0
|
201 |
test(!pBitMapAllocator->IsFree(j));
|
sl@0
|
202 |
}
|
sl@0
|
203 |
}
|
sl@0
|
204 |
}
|
sl@0
|
205 |
|
sl@0
|
206 |
//
|
sl@0
|
207 |
test.Next(_L("Free (again)"));
|
sl@0
|
208 |
for (i=0;i<=x;i++)
|
sl@0
|
209 |
pBitMapAllocator->Free(i);
|
sl@0
|
210 |
test(pBitMapAllocator->Avail()==pBitMapAllocator->Size());
|
sl@0
|
211 |
}
|
sl@0
|
212 |
//
|
sl@0
|
213 |
for (x=available-1;x>0;x--)
|
sl@0
|
214 |
{
|
sl@0
|
215 |
for (i=x;i>=0;i--)
|
sl@0
|
216 |
{
|
sl@0
|
217 |
TInt j=pBitMapAllocator->AllocFromTopFrom(x);
|
sl@0
|
218 |
test(j==i);
|
sl@0
|
219 |
}
|
sl@0
|
220 |
test(pBitMapAllocator->Avail()==available-x-1);
|
sl@0
|
221 |
|
sl@0
|
222 |
test.Next(_L("Try to AllocFromTopFrom more than available"));
|
sl@0
|
223 |
i=pBitMapAllocator->AllocFromTopFrom(x);
|
sl@0
|
224 |
test(i==KErrNoMemory);
|
sl@0
|
225 |
//
|
sl@0
|
226 |
test.Next(_L("Free (again)"));
|
sl@0
|
227 |
for (i=0;i<=x;i++)
|
sl@0
|
228 |
pBitMapAllocator->Free(i);
|
sl@0
|
229 |
test(pBitMapAllocator->Avail()==pBitMapAllocator->Size());
|
sl@0
|
230 |
}
|
sl@0
|
231 |
test.End();
|
sl@0
|
232 |
delete pBitMapAllocator;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
|
sl@0
|
235 |
LOCAL_C void testBlock(TInt aSize)
|
sl@0
|
236 |
//
|
sl@0
|
237 |
// Test Alloc(TInt, TInt&), AllocAligned, AllocAlignedBlock, AllocAt(TInt, TInt),
|
sl@0
|
238 |
// IsFree(TInt, TInt), Free(TInt, TInt)
|
sl@0
|
239 |
//
|
sl@0
|
240 |
{
|
sl@0
|
241 |
CBitMapAllocator* pB=CBitMapAllocator::New(aSize);
|
sl@0
|
242 |
test(pB!=NULL);
|
sl@0
|
243 |
test.Start(_L("AllocAt block, Free block, IsFree block"));
|
sl@0
|
244 |
TInt available=pB->Avail();
|
sl@0
|
245 |
test(available==aSize);
|
sl@0
|
246 |
TInt start, len;
|
sl@0
|
247 |
for(start=0; start<available; start++)
|
sl@0
|
248 |
{
|
sl@0
|
249 |
for(len=1; len<=available-start; len++)
|
sl@0
|
250 |
{
|
sl@0
|
251 |
pB->AllocAt(start,len);
|
sl@0
|
252 |
test(pB->Avail()==available-len);
|
sl@0
|
253 |
for(TInt i=0; i<available; i++)
|
sl@0
|
254 |
{
|
sl@0
|
255 |
if (i>=start && i<start+len)
|
sl@0
|
256 |
{
|
sl@0
|
257 |
if(pB->IsFree(i))
|
sl@0
|
258 |
test(0);
|
sl@0
|
259 |
}
|
sl@0
|
260 |
else
|
sl@0
|
261 |
{
|
sl@0
|
262 |
if(!pB->IsFree(i))
|
sl@0
|
263 |
test(0);
|
sl@0
|
264 |
}
|
sl@0
|
265 |
}
|
sl@0
|
266 |
if (start)
|
sl@0
|
267 |
test(pB->IsFree(0,start));
|
sl@0
|
268 |
test(!pB->IsFree(0,start+1));
|
sl@0
|
269 |
if (start+len<available)
|
sl@0
|
270 |
{
|
sl@0
|
271 |
test(pB->IsFree(start+len,available-(start+len)));
|
sl@0
|
272 |
test(!pB->IsFree(start+len-1,available-(start+len-1)));
|
sl@0
|
273 |
}
|
sl@0
|
274 |
pB->Free(start,len);
|
sl@0
|
275 |
test(pB->Avail()==available);
|
sl@0
|
276 |
test(pB->IsFree(start,len));
|
sl@0
|
277 |
test(pB->IsFree(0,available));
|
sl@0
|
278 |
}
|
sl@0
|
279 |
}
|
sl@0
|
280 |
test.End();
|
sl@0
|
281 |
test.Start(_L("Alloc consecutive block"));
|
sl@0
|
282 |
TInt askfor, init, pos, consec;
|
sl@0
|
283 |
for(askfor=1; askfor<=available; askfor++)
|
sl@0
|
284 |
{
|
sl@0
|
285 |
test.Printf(_L("Ask for %d\n"),askfor);
|
sl@0
|
286 |
for(init=0; init<available; init++)
|
sl@0
|
287 |
{
|
sl@0
|
288 |
if (init)
|
sl@0
|
289 |
pB->AllocAt(0,init);
|
sl@0
|
290 |
for(pos=init+1; pos<available; pos++)
|
sl@0
|
291 |
{
|
sl@0
|
292 |
pB->AllocAt(pos);
|
sl@0
|
293 |
TInt firstfree=pB->Alloc(askfor, consec);
|
sl@0
|
294 |
if (firstfree!=init)
|
sl@0
|
295 |
test(0);
|
sl@0
|
296 |
TInt number=(pos-init>askfor)?askfor:pos-init;
|
sl@0
|
297 |
if (consec!=number)
|
sl@0
|
298 |
test(0);
|
sl@0
|
299 |
if (number<pos-init)
|
sl@0
|
300 |
{
|
sl@0
|
301 |
firstfree=pB->Alloc(pos-init-number,consec);
|
sl@0
|
302 |
if(firstfree!=init+number)
|
sl@0
|
303 |
test(0);
|
sl@0
|
304 |
if(consec!=pos-init-number)
|
sl@0
|
305 |
test(0);
|
sl@0
|
306 |
}
|
sl@0
|
307 |
test(pB->Avail()==available-pos-1);
|
sl@0
|
308 |
TInt freeto=available;
|
sl@0
|
309 |
if (pos<available-1)
|
sl@0
|
310 |
{
|
sl@0
|
311 |
firstfree=pB->Alloc(askfor,consec);
|
sl@0
|
312 |
number=(available-pos-1>askfor)?askfor:available-pos-1;
|
sl@0
|
313 |
if (firstfree!=pos+1)
|
sl@0
|
314 |
test(0);
|
sl@0
|
315 |
if (consec!=number)
|
sl@0
|
316 |
test(0);
|
sl@0
|
317 |
freeto=pos+1+number;
|
sl@0
|
318 |
}
|
sl@0
|
319 |
test(pB->Avail()==available-freeto);
|
sl@0
|
320 |
if (available==freeto)
|
sl@0
|
321 |
{
|
sl@0
|
322 |
firstfree=pB->Alloc(1,consec);
|
sl@0
|
323 |
if (firstfree!=KErrNoMemory)
|
sl@0
|
324 |
test(0);
|
sl@0
|
325 |
if (consec!=0)
|
sl@0
|
326 |
test(0);
|
sl@0
|
327 |
}
|
sl@0
|
328 |
pB->Free(init,freeto-init);
|
sl@0
|
329 |
}
|
sl@0
|
330 |
if (init)
|
sl@0
|
331 |
pB->Free(0,init);
|
sl@0
|
332 |
test(pB->Avail()==available);
|
sl@0
|
333 |
}
|
sl@0
|
334 |
}
|
sl@0
|
335 |
test.End();
|
sl@0
|
336 |
test.Start(_L("AllocAligned"));
|
sl@0
|
337 |
TInt alignment, alignstep;
|
sl@0
|
338 |
for(alignment=0, alignstep=1; alignstep<available; alignment++, alignstep<<=1 )
|
sl@0
|
339 |
{
|
sl@0
|
340 |
TInt numaligned=(available+alignstep-1)/alignstep;
|
sl@0
|
341 |
TInt next=0;
|
sl@0
|
342 |
TInt r;
|
sl@0
|
343 |
do {
|
sl@0
|
344 |
r=pB->AllocAligned(alignment);
|
sl@0
|
345 |
if (r>=0)
|
sl@0
|
346 |
{
|
sl@0
|
347 |
if (r!=next)
|
sl@0
|
348 |
test(0);
|
sl@0
|
349 |
next+=alignstep;
|
sl@0
|
350 |
}
|
sl@0
|
351 |
else if (r!=KErrNoMemory)
|
sl@0
|
352 |
test(0);
|
sl@0
|
353 |
} while(r>=0);
|
sl@0
|
354 |
if (pB->Avail()!=available-numaligned)
|
sl@0
|
355 |
test(0);
|
sl@0
|
356 |
for(TInt i=0; i<available; i++)
|
sl@0
|
357 |
{
|
sl@0
|
358 |
if (i==((i>>alignment)<<alignment) )
|
sl@0
|
359 |
{
|
sl@0
|
360 |
if (pB->IsFree(i))
|
sl@0
|
361 |
test(0);
|
sl@0
|
362 |
pB->Free(i);
|
sl@0
|
363 |
}
|
sl@0
|
364 |
else
|
sl@0
|
365 |
{
|
sl@0
|
366 |
if (!pB->IsFree(i))
|
sl@0
|
367 |
test(0);
|
sl@0
|
368 |
}
|
sl@0
|
369 |
}
|
sl@0
|
370 |
test(pB->Avail()==available);
|
sl@0
|
371 |
}
|
sl@0
|
372 |
test.End();
|
sl@0
|
373 |
test.Start(_L("AllocAlignedBlock"));
|
sl@0
|
374 |
for(alignment=0, alignstep=1; alignstep<available; alignment++, alignstep<<=1 )
|
sl@0
|
375 |
{
|
sl@0
|
376 |
TInt numalignedblocks=available/alignstep;
|
sl@0
|
377 |
TInt next=0;
|
sl@0
|
378 |
TInt r;
|
sl@0
|
379 |
do {
|
sl@0
|
380 |
r=pB->AllocAlignedBlock(alignment);
|
sl@0
|
381 |
if (r>=0)
|
sl@0
|
382 |
{
|
sl@0
|
383 |
if (r!=next)
|
sl@0
|
384 |
test(0);
|
sl@0
|
385 |
next+=alignstep;
|
sl@0
|
386 |
}
|
sl@0
|
387 |
else if (r!=KErrNoMemory)
|
sl@0
|
388 |
test(0);
|
sl@0
|
389 |
} while(r>=0);
|
sl@0
|
390 |
if (pB->Avail()!=available-numalignedblocks*alignstep)
|
sl@0
|
391 |
test(0);
|
sl@0
|
392 |
if (pB->Avail()!=0)
|
sl@0
|
393 |
{
|
sl@0
|
394 |
if ( !pB->IsFree(numalignedblocks*alignstep,pB->Avail()) )
|
sl@0
|
395 |
test(0);
|
sl@0
|
396 |
r=pB->Alloc();
|
sl@0
|
397 |
if (r!=numalignedblocks*alignstep)
|
sl@0
|
398 |
test(0);
|
sl@0
|
399 |
pB->Free(r);
|
sl@0
|
400 |
}
|
sl@0
|
401 |
pB->Free(0,numalignedblocks*alignstep);
|
sl@0
|
402 |
if (pB->Avail()!=available)
|
sl@0
|
403 |
test(0);
|
sl@0
|
404 |
TInt freepos, blockpos, c;
|
sl@0
|
405 |
for (freepos=0; freepos<available; freepos+=alignstep)
|
sl@0
|
406 |
{
|
sl@0
|
407 |
for (blockpos=0; blockpos<alignstep; blockpos++)
|
sl@0
|
408 |
{
|
sl@0
|
409 |
c=0;
|
sl@0
|
410 |
for(TInt i=blockpos; i<freepos; i+=alignstep)
|
sl@0
|
411 |
{
|
sl@0
|
412 |
pB->AllocAt(i);
|
sl@0
|
413 |
c++;
|
sl@0
|
414 |
}
|
sl@0
|
415 |
if (pB->Avail()!=available-c)
|
sl@0
|
416 |
test(0);
|
sl@0
|
417 |
r=pB->AllocAlignedBlock(alignment);
|
sl@0
|
418 |
if (available-freepos<alignstep)
|
sl@0
|
419 |
{
|
sl@0
|
420 |
if (r!=KErrNoMemory)
|
sl@0
|
421 |
test(0);
|
sl@0
|
422 |
if (pB->Avail()!=available-c)
|
sl@0
|
423 |
test(0);
|
sl@0
|
424 |
}
|
sl@0
|
425 |
else
|
sl@0
|
426 |
{
|
sl@0
|
427 |
if (r!=freepos)
|
sl@0
|
428 |
test(0);
|
sl@0
|
429 |
if (pB->Avail()!=available-c-alignstep)
|
sl@0
|
430 |
test(0);
|
sl@0
|
431 |
pB->Free(freepos,alignstep);
|
sl@0
|
432 |
if (pB->Avail()!=available-c)
|
sl@0
|
433 |
test(0);
|
sl@0
|
434 |
}
|
sl@0
|
435 |
for(TInt j=blockpos; j<freepos; j+=alignstep)
|
sl@0
|
436 |
pB->Free(j);
|
sl@0
|
437 |
if (pB->Avail()!=available)
|
sl@0
|
438 |
test(0);
|
sl@0
|
439 |
}
|
sl@0
|
440 |
}
|
sl@0
|
441 |
}
|
sl@0
|
442 |
delete pB;
|
sl@0
|
443 |
test.End();
|
sl@0
|
444 |
}
|
sl@0
|
445 |
|
sl@0
|
446 |
LOCAL_C void testContiguousAllocation(TInt aSize)
|
sl@0
|
447 |
{//test RemoveRamPages()
|
sl@0
|
448 |
//set up bitmap with pattern of increasingly large gaps -
|
sl@0
|
449 |
//page 1 - in use,page 2 - free
|
sl@0
|
450 |
//pages 3,4 - in use,pages 5,6 - free
|
sl@0
|
451 |
//pages 7,8,9 - in use,pages 10,11,12 - free ...etc
|
sl@0
|
452 |
test.Start(_L("Create swiss cheese effect..."));
|
sl@0
|
453 |
|
sl@0
|
454 |
CBitMapAllocator* pB=CBitMapAllocator::New(aSize);
|
sl@0
|
455 |
test(pB!=NULL);
|
sl@0
|
456 |
|
sl@0
|
457 |
TInt available=pB->Avail();
|
sl@0
|
458 |
test(available==aSize);
|
sl@0
|
459 |
|
sl@0
|
460 |
TInt i=0;
|
sl@0
|
461 |
TInt j=0;
|
sl@0
|
462 |
TInt k=1;
|
sl@0
|
463 |
while(k<46)
|
sl@0
|
464 |
{
|
sl@0
|
465 |
for(j=0;j<k;j++)
|
sl@0
|
466 |
{
|
sl@0
|
467 |
pB->AllocAt(i+j);
|
sl@0
|
468 |
test(!pB->IsFree(i+j));
|
sl@0
|
469 |
}
|
sl@0
|
470 |
i+=2*k;
|
sl@0
|
471 |
k++;
|
sl@0
|
472 |
}
|
sl@0
|
473 |
|
sl@0
|
474 |
TInt ret=KErrNone;
|
sl@0
|
475 |
TInt pageNo=0;
|
sl@0
|
476 |
for(i=1;i<45;i++)
|
sl@0
|
477 |
{
|
sl@0
|
478 |
ret=pB->ExtractRamPages(i,pageNo); //look for a gap of size i pages and allocate it
|
sl@0
|
479 |
test(pageNo==i*i); //test the right page no is returned
|
sl@0
|
480 |
test.Printf(_L("OK -pageNo is :%d\r\n"),pageNo);
|
sl@0
|
481 |
for(j=i*i;j<i*i + i;j++) //test that the pages are actually allocated
|
sl@0
|
482 |
test(!pB->IsFree(j));
|
sl@0
|
483 |
}
|
sl@0
|
484 |
|
sl@0
|
485 |
ret=pB->ExtractRamPages(45,pageNo);//there's not a big enough space in the bitmap for this to succeed
|
sl@0
|
486 |
test(ret==KErrNoMemory);
|
sl@0
|
487 |
delete pB;
|
sl@0
|
488 |
test.End();
|
sl@0
|
489 |
}
|
sl@0
|
490 |
|
sl@0
|
491 |
LOCAL_C void testAll(TInt aSize)
|
sl@0
|
492 |
//
|
sl@0
|
493 |
// Test all BMA functions using a BMA of size aSize
|
sl@0
|
494 |
//
|
sl@0
|
495 |
{
|
sl@0
|
496 |
|
sl@0
|
497 |
TBuf<0x40> b;
|
sl@0
|
498 |
b.Format(_L("BitMapAllocator size = %d"),aSize);
|
sl@0
|
499 |
test.Start(b);
|
sl@0
|
500 |
//
|
sl@0
|
501 |
testNew(aSize);
|
sl@0
|
502 |
testNewL(aSize);
|
sl@0
|
503 |
testAlloc(aSize);
|
sl@0
|
504 |
testBlock(aSize);
|
sl@0
|
505 |
//
|
sl@0
|
506 |
test.End();
|
sl@0
|
507 |
}
|
sl@0
|
508 |
|
sl@0
|
509 |
GLDEF_C TInt E32Main()
|
sl@0
|
510 |
//
|
sl@0
|
511 |
// Test bitmap allocator
|
sl@0
|
512 |
//
|
sl@0
|
513 |
{
|
sl@0
|
514 |
test.Title();
|
sl@0
|
515 |
__UHEAP_MARK;
|
sl@0
|
516 |
//
|
sl@0
|
517 |
test.Start(_L("1 bit"));
|
sl@0
|
518 |
testAll(1);
|
sl@0
|
519 |
|
sl@0
|
520 |
test.Next(_L("4 bit"));
|
sl@0
|
521 |
testAll(4);
|
sl@0
|
522 |
//
|
sl@0
|
523 |
test.Next(_L("32 bit"));
|
sl@0
|
524 |
testAll(32);
|
sl@0
|
525 |
//
|
sl@0
|
526 |
test.Next(_L("33 bit"));
|
sl@0
|
527 |
testAll(33);
|
sl@0
|
528 |
//
|
sl@0
|
529 |
test.Next(_L("68 bit"));
|
sl@0
|
530 |
testAll(68);
|
sl@0
|
531 |
//
|
sl@0
|
532 |
test.Next(_L("96 bit"));
|
sl@0
|
533 |
testAll(96);
|
sl@0
|
534 |
//
|
sl@0
|
535 |
test.Next(_L("64 bit"));
|
sl@0
|
536 |
testAll(64);
|
sl@0
|
537 |
//
|
sl@0
|
538 |
test.Next(_L("65 bit"));
|
sl@0
|
539 |
testAll(65);
|
sl@0
|
540 |
//
|
sl@0
|
541 |
test.Next(_L("63 bit"));
|
sl@0
|
542 |
testAll(63);
|
sl@0
|
543 |
|
sl@0
|
544 |
testContiguousAllocation(2048);
|
sl@0
|
545 |
|
sl@0
|
546 |
test.Next(_L("test defect EXT-5AMDKP"));
|
sl@0
|
547 |
|
sl@0
|
548 |
CBitMapAllocator* pB = CBitMapAllocator::New(64);
|
sl@0
|
549 |
test(pB != NULL);
|
sl@0
|
550 |
pB->AllocAt(0, 32);
|
sl@0
|
551 |
pB->Free(2, 2);
|
sl@0
|
552 |
pB->Free(5, 2);
|
sl@0
|
553 |
pB->Free(8, 2);
|
sl@0
|
554 |
pB->Free(12, 3);
|
sl@0
|
555 |
pB->Free(30, 2);
|
sl@0
|
556 |
TInt page = -1;
|
sl@0
|
557 |
pB->ExtractRamPages(3, page);
|
sl@0
|
558 |
test(page == 12);
|
sl@0
|
559 |
pB->ExtractRamPages(4, page);
|
sl@0
|
560 |
test(page == 30);
|
sl@0
|
561 |
pB->ExtractRamPages(1, page);
|
sl@0
|
562 |
test(page == 2);
|
sl@0
|
563 |
pB->ExtractRamPages(5, page);
|
sl@0
|
564 |
test(page == 34);
|
sl@0
|
565 |
pB->ExtractRamPages(2, page);
|
sl@0
|
566 |
test(page == 5);
|
sl@0
|
567 |
delete pB;
|
sl@0
|
568 |
|
sl@0
|
569 |
__UHEAP_MARKEND;
|
sl@0
|
570 |
test.End();
|
sl@0
|
571 |
return(KErrNone);
|
sl@0
|
572 |
}
|
sl@0
|
573 |
|