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