author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Sat, 20 Feb 2010 00:10:51 +0200 | |
branch | RCL_3 |
changeset 19 | 4a8fed1c0ef6 |
parent 0 | a41df078684a |
child 43 | c1f20ce4abcf |
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\heap\t_heap.cpp |
|
15 |
// Overview: |
|
16 |
// Tests RHeap class. |
|
17 |
// API Information: |
|
18 |
// RHeap |
|
19 |
// Details: |
|
20 |
// - Test that the expected methods are in the DLL by calling each one. |
|
21 |
// - Test heap auto expansion and compression by calling Alloc and Compress |
|
22 |
// and verifying the results are as expected. |
|
23 |
// - Verify the heap dump Base, Size, MinLength, Top and len values. |
|
24 |
// - Test the RHeap AllocSize, Alloc, AllocLen, Count and Free methods. Verify |
|
25 |
// results are as expected. Check heap object and confirm Invariant status. |
|
26 |
// - For an RHeap object, test and verify the results of: allocate some cells, |
|
27 |
// free them with Reset, allocate some cells again, free them with Free, |
|
28 |
// allocate some cells again, free them backwards, allocate again, free the |
|
29 |
// odd cells then the even cells, allocate again, free one half then the other. |
|
30 |
// Check heap object and confirm Invariant status. |
|
31 |
// - For an RHeap object, test and verify the results of: attempt to resize a |
|
32 |
// block above the space available, resize the block to 0, resize positively, |
|
33 |
// allocate a block, fill with data, allocate another block or two then resize |
|
34 |
// the original block such that it has to be moved in memory, then check the |
|
35 |
// blocks' contents, test data was copied on reallocation, resize blocks and |
|
36 |
// verify data integrity, expand and shrink, verify data. |
|
37 |
// Check heap object and confirm Invariant status. |
|
38 |
// - For an RHeap object, test and verify the results of: Alloc some cells, |
|
39 |
// verify the Count, Check the object, Free some cells, verify the Count, |
|
40 |
// Check and Reset the object, corrupt the heap data and reset the object. |
|
41 |
// - Test the leaving methods: AllocL and ReAllocL. Verify the results are as |
|
42 |
// expected. |
|
43 |
// - Test the RHeap methods: Alloc, Count, Size, Free and Close. Verify results |
|
44 |
// are as expected. |
|
45 |
// - Test sharing a chunk heap between two separate threads. Each thread |
|
46 |
// accesses the shared heap in a timed loop, to ensure that some true |
|
47 |
// concurrency. |
|
48 |
// - Test sharing a chunk heap between two separate threads. Run each thread in |
|
49 |
// a timed loop, to ensure that some true concurrency. Each thread accesses |
|
50 |
// the shared heap and results are verified. The heap size is used to verify |
|
51 |
// no leaks and that the largest available space is still available. The heap |
|
52 |
// is checked to verify that no cells remain allocated after the tests are |
|
53 |
// complete. |
|
54 |
// - Test sharing a heap between two threads. The thread whose heap it was is |
|
55 |
// killed first. Each thread accesses the shared heap and results are |
|
56 |
// verified. |
|
57 |
// Platforms/Drives/Compatibility: |
|
58 |
// All |
|
59 |
// Assumptions/Requirement/Pre-requisites: |
|
60 |
// Failures and causes: |
|
61 |
// Base Port information: |
|
62 |
// |
|
63 |
// |
|
64 |
||
65 |
#include <e32test.h> |
|
66 |
#include <e32hal.h> |
|
67 |
#include <e32def.h> |
|
68 |
#include <e32def_private.h> |
|
69 |
||
70 |
// Sets data for Test6 |
|
71 |
#define SetData(size) pHeap->Reset();\ |
|
72 |
Cell1=pHeap->Alloc(size);\ |
|
73 |
Cell2=pHeap->Alloc(size);\ |
|
74 |
Cell3=pHeap->Alloc(size);\ |
|
75 |
for(pC=(TText8*)Cell1; pC<(TText8*)Cell1+pHeap->AllocLen(Cell1); *pC++='x');\ |
|
76 |
for(pC=(TText8*)Cell2; pC<(TText8*)Cell2+pHeap->AllocLen(Cell2); *pC++='y');\ |
|
77 |
for(pC=(TText8*)Cell3; pC<(TText8*)Cell3+pHeap->AllocLen(Cell3); *pC++='z');\ |
|
78 |
OrigLen=pHeap->AllocLen(Cell2); |
|
79 |
||
80 |
// Tests cell contents for Test6 |
|
81 |
#define TestCells(Cell2Len) for(pC=(TText8*)Cell1; pC<(TText8*)Cell1+pHeap->AllocLen(Cell1); test(*pC++=='x'));\ |
|
82 |
for(pC=(TText8*)Cell2; pC<(TText8*)Cell2+Cell2Len; test(*pC++=='y'));\ |
|
83 |
for(pC=(TText8*)Cell3; pC<(TText8*)Cell3+pHeap->AllocLen(Cell3); test(*pC++=='z'));\ |
|
84 |
pHeap->Check(); |
|
85 |
||
86 |
#ifdef __EABI__ |
|
87 |
IMPORT_D extern const TInt KHeapMinCellSize; |
|
88 |
#else |
|
89 |
const TInt KHeapMinCellSize = 0; |
|
90 |
#endif |
|
91 |
||
92 |
const TInt KHeadSize = (TInt)RHeap::EAllocCellSize; |
|
93 |
const TInt KAlign = _FOFF(RHeap::_s_align, d); |
|
94 |
const TInt KMinCellLength = _ALIGN_UP((KHeapMinCellSize + Max(TInt(RHeap::EFreeCellSize),TInt(RHeap::EAllocCellSize))),KAlign) - RHeap::EAllocCellSize; |
|
95 |
const TInt KMinFreeSize = _ALIGN_UP((KHeapMinCellSize + Max(TInt(RHeap::EFreeCellSize),TInt(RHeap::EAllocCellSize))),KAlign); |
|
96 |
||
97 |
TInt PageSize; |
|
98 |
||
99 |
class RTestHeap : public RHeap |
|
100 |
{ |
|
101 |
public: |
|
102 |
void __DbgTest(void* pRHeapDump) const; |
|
103 |
}; |
|
104 |
||
105 |
struct RHeapDump |
|
106 |
{ |
|
107 |
TUint iMinLength; |
|
108 |
RChunk iChunk; |
|
109 |
TUint8 *iBase; |
|
110 |
TUint8 *iTop; |
|
111 |
RHeap::SCell iFree; |
|
112 |
}; |
|
113 |
||
114 |
#pragma warning ( disable :4705 ) // statement has no effect |
|
115 |
RHeapDump OrigDump; |
|
116 |
#pragma warning ( default :4705 ) |
|
117 |
||
118 |
#if defined(_DEBUG) |
|
119 |
void RTestHeap::__DbgTest(void* aPtr) const |
|
120 |
{ |
|
121 |
RHeapDump& d = *(RHeapDump*)aPtr; |
|
122 |
d.iMinLength=iMinLength; |
|
123 |
d.iChunk.SetHandle(iChunkHandle); |
|
124 |
d.iBase=iBase; |
|
125 |
d.iTop=iTop; |
|
126 |
d.iFree=iFree; |
|
127 |
} |
|
128 |
#endif |
|
129 |
||
130 |
||
131 |
#if defined(_DEBUG) |
|
132 |
TBool Invariant(RHeap* aHeap) |
|
133 |
{ |
|
134 |
RHeapDump dump; |
|
135 |
((RTestHeap*)aHeap)->__DbgTest(&dump); |
|
136 |
if(dump.iMinLength!=OrigDump.iMinLength) return(EFalse); |
|
137 |
// Note: iChunk is a class |
|
138 |
if(dump.iBase!=OrigDump.iBase) return(EFalse); |
|
139 |
if(*dump.iBase!=*OrigDump.iBase) return(EFalse); |
|
140 |
if(dump.iTop!=OrigDump.iTop) return(EFalse); |
|
141 |
if(dump.iTop[-1]!=OrigDump.iTop[-1]) return(EFalse); |
|
142 |
if(dump.iFree.len!=OrigDump.iFree.len) return(EFalse); |
|
143 |
// iFree.Next changes during allocation/freeing etc. |
|
144 |
return(ETrue); |
|
145 |
} |
|
146 |
#define INV(x) x; |
|
147 |
#else |
|
148 |
#define INV(x) |
|
149 |
#endif |
|
150 |
||
151 |
LOCAL_D RTest test(_L("T_HEAP")); |
|
152 |
LOCAL_D TInt heapCount=1; |
|
153 |
LOCAL_D RHeap *gHeapPtr; |
|
154 |
LOCAL_D RHeap *gHeapPtr2; |
|
155 |
||
156 |
class TestRHeap |
|
157 |
{ |
|
158 |
public: |
|
159 |
void Test1(void); |
|
160 |
void Test2(void); |
|
161 |
void Test3(void); |
|
162 |
void Test4(void); |
|
163 |
void Test5(void); |
|
164 |
void Test7(void); |
|
165 |
void Test8(void); |
|
166 |
void TestCompressAll(void); |
|
167 |
void TestOffset(void); |
|
168 |
private: |
|
169 |
TInt RHeapCalcReduce(TInt aCellSize, TInt aGrowBy); |
|
170 |
}; |
|
171 |
||
172 |
LOCAL_C RHeap* allocHeap(TInt aSize) |
|
173 |
// |
|
174 |
// Allocate a chunk heap with max size aSize |
|
175 |
// |
|
176 |
{ |
|
177 |
||
178 |
TName n; |
|
179 |
n.Format(_L("TESTHEAP%d"),heapCount++); |
|
180 |
return(User::ChunkHeap(&n,aSize,aSize)); |
|
181 |
} |
|
182 |
||
183 |
//////////////////////////////////////////////////////////////////////////////////////// |
|
184 |
// Test that methods are in the DLL |
|
185 |
//////////////////////////////////////////////////////////////////////////////////////// |
|
186 |
void TestRHeap::Test1(void) |
|
187 |
{ |
|
188 |
TAny* aCell; |
|
189 |
TInt aVar; |
|
190 |
RHeap* pHeap=allocHeap(3000); // tests first constructor indirectly |
|
191 |
// constructor with Chunk not tested |
|
192 |
pHeap->Base(); |
|
193 |
pHeap->Size(); |
|
194 |
pHeap->Available(aVar); |
|
195 |
pHeap->Check(); |
|
196 |
pHeap->Count(); |
|
197 |
pHeap->Count(aVar); |
|
198 |
aCell=pHeap->Alloc(50); |
|
199 |
pHeap->Free(aCell); |
|
200 |
aCell=pHeap->AllocL(50); |
|
201 |
pHeap->AllocLen(aCell); |
|
202 |
pHeap->ReAlloc(aCell, 100); |
|
203 |
pHeap->ReAllocL(aCell, 150); |
|
204 |
pHeap->Reset(); |
|
205 |
pHeap->Close(); |
|
206 |
} |
|
207 |
||
208 |
/////////////////////////////////////////////////////////////////////////////// |
|
209 |
// Test Assorted Methods 1 |
|
210 |
////////////////////////////////////////////////////////////////////////////// |
|
211 |
void TestRHeap::Test2(void) |
|
212 |
{ |
|
213 |
#if defined(_DEBUG) |
|
214 |
RHeapDump dump; |
|
215 |
RHeap* pHeap=allocHeap(3000); |
|
216 |
||
217 |
((RTestHeap*)pHeap)->__DbgTest(&OrigDump); |
|
218 |
((RTestHeap*)pHeap)->__DbgTest(&dump); |
|
219 |
test(dump.iBase==pHeap->Base()); |
|
220 |
test((dump.iTop-dump.iBase)==pHeap->Size()); |
|
221 |
pHeap->Check(); |
|
222 |
test(Invariant(pHeap)); |
|
223 |
pHeap->Close(); |
|
224 |
#endif |
|
225 |
} |
|
226 |
||
227 |
/////////////////////////////////////////////////////////////////////////////// |
|
228 |
// Test Assorted Methods 2 |
|
229 |
////////////////////////////////////////////////////////////////////////////// |
|
230 |
void TestRHeap::Test3(void) |
|
231 |
{ |
|
232 |
TInt CellLen; |
|
233 |
TInt OrigBiggestBlock, BiggestBlock; |
|
234 |
TAny* aCell; |
|
235 |
TInt FreeCount, AllocCount, AllocSize; |
|
236 |
RHeap* pHeap=allocHeap(5000); |
|
237 |
||
238 |
#if defined(_DEBUG) |
|
239 |
((RTestHeap*)pHeap)->__DbgTest(&OrigDump); |
|
240 |
#endif |
|
241 |
||
242 |
// test AllocSize |
|
243 |
AllocCount=pHeap->Count(FreeCount); |
|
244 |
test(pHeap->AllocSize(AllocSize)==pHeap->Count()); |
|
245 |
test(AllocSize==0); |
|
246 |
test(AllocCount==pHeap->Count()); |
|
247 |
test(AllocCount==0); |
|
248 |
test(FreeCount==1); |
|
249 |
||
250 |
TAny* p1=pHeap->Alloc(1); |
|
251 |
test(pHeap->AllocSize(AllocSize)==1); |
|
252 |
test(AllocSize==pHeap->AllocLen(p1)); |
|
253 |
||
254 |
TAny* p2=pHeap->Alloc(8); |
|
255 |
test(pHeap->AllocSize(AllocSize)==2); |
|
256 |
test(AllocSize==pHeap->AllocLen(p1)+pHeap->AllocLen(p2)); |
|
257 |
||
258 |
TAny* p3=pHeap->Alloc(127); |
|
259 |
test(pHeap->AllocSize(AllocSize)==3); |
|
260 |
test(AllocSize==pHeap->AllocLen(p1)+pHeap->AllocLen(p2)+pHeap->AllocLen(p3)); |
|
261 |
||
262 |
pHeap->Free(p2); |
|
263 |
test(pHeap->AllocSize(AllocSize)==2); |
|
264 |
test(AllocSize==pHeap->AllocLen(p1)+pHeap->AllocLen(p3)); |
|
265 |
||
266 |
pHeap->Free(p1); |
|
267 |
test(pHeap->AllocSize(AllocSize)==1); |
|
268 |
test(AllocSize==pHeap->AllocLen(p3)); |
|
269 |
||
270 |
pHeap->Free(p3); |
|
271 |
test(pHeap->AllocSize(AllocSize)==0); |
|
272 |
test(AllocSize==0); |
|
273 |
||
274 |
pHeap->Available(OrigBiggestBlock); |
|
275 |
||
276 |
// Request too large a block |
|
277 |
test((aCell=pHeap->Alloc(OrigBiggestBlock+1))==NULL); |
|
278 |
AllocCount=pHeap->Count(FreeCount); |
|
279 |
test(AllocCount==0); |
|
280 |
test(FreeCount==1); |
|
281 |
||
282 |
||
283 |
// Request block same size as that available |
|
284 |
test((aCell=pHeap->Alloc(OrigBiggestBlock))!=NULL); |
|
285 |
test(pHeap->Available(BiggestBlock)==0); |
|
286 |
test(BiggestBlock==0); |
|
287 |
test(pHeap->AllocLen(aCell)==OrigBiggestBlock); |
|
288 |
AllocCount=pHeap->Count(FreeCount); |
|
289 |
test(AllocCount==pHeap->Count()); |
|
290 |
test(AllocCount==1); |
|
291 |
test(FreeCount==0); |
|
292 |
pHeap->Check(); |
|
293 |
// Free the block |
|
294 |
pHeap->FreeZ(aCell); |
|
295 |
test(aCell==NULL); |
|
296 |
pHeap->Available(BiggestBlock); |
|
297 |
test(BiggestBlock==OrigBiggestBlock); |
|
298 |
AllocCount=pHeap->Count(FreeCount); |
|
299 |
test(AllocCount==0); |
|
300 |
test(FreeCount==1); |
|
301 |
||
302 |
||
303 |
// Request a block much smaller than that available |
|
304 |
test((aCell=pHeap->Alloc(1))!=NULL); |
|
305 |
CellLen=pHeap->AllocLen(aCell); |
|
306 |
pHeap->Available(BiggestBlock); |
|
307 |
test(pHeap->Available(BiggestBlock)==BiggestBlock); |
|
308 |
test((BiggestBlock+CellLen+KHeadSize)==OrigBiggestBlock); |
|
309 |
// NOTE: if a block of 1000 was initially available, getting a cell of length 100 DOES NOT |
|
310 |
// leave 900 available as some of the 1000(KHeadSize) is used up storing the length of the |
|
311 |
// allocated block |
|
312 |
AllocCount=pHeap->Count(FreeCount); |
|
313 |
test(AllocCount==1); |
|
314 |
test(FreeCount==1); |
|
315 |
pHeap->Check(); |
|
316 |
// Free the block |
|
317 |
pHeap->Free(aCell); |
|
318 |
test(aCell!=NULL); |
|
319 |
pHeap->Available(BiggestBlock); |
|
320 |
test(BiggestBlock==OrigBiggestBlock); |
|
321 |
AllocCount=pHeap->Count(FreeCount); |
|
322 |
test(AllocCount==0); |
|
323 |
test(FreeCount==1); |
|
324 |
||
325 |
||
326 |
// Request a block only just smaller than that available |
|
327 |
test((aCell=pHeap->Alloc(OrigBiggestBlock-1))!=NULL); |
|
328 |
CellLen=pHeap->AllocLen(aCell); |
|
329 |
AllocCount=pHeap->Count(FreeCount); |
|
330 |
test(AllocCount==1); |
|
331 |
test(FreeCount==0); |
|
332 |
pHeap->Check(); |
|
333 |
// Free the block |
|
334 |
pHeap->Free(aCell); |
|
335 |
pHeap->Available(BiggestBlock); |
|
336 |
test(BiggestBlock==OrigBiggestBlock); |
|
337 |
AllocCount=pHeap->Count(FreeCount); |
|
338 |
test(AllocCount==0); |
|
339 |
test(FreeCount==1); |
|
340 |
||
341 |
||
342 |
//Request a block of 0 size Note: 0 may not necessarily be allocated (probably will be 4) |
|
343 |
test((aCell=pHeap->Alloc(0))!=NULL); |
|
344 |
pHeap->Available(BiggestBlock); |
|
345 |
AllocCount=pHeap->Count(FreeCount); |
|
346 |
test(AllocCount==1); |
|
347 |
test(FreeCount==1); |
|
348 |
pHeap->Check(); |
|
349 |
//Free the block |
|
350 |
pHeap->Free(aCell); |
|
351 |
pHeap->Available(BiggestBlock); |
|
352 |
test(BiggestBlock==OrigBiggestBlock); |
|
353 |
AllocCount=pHeap->Count(FreeCount); |
|
354 |
test(AllocCount==0); |
|
355 |
test(FreeCount==1); |
|
356 |
pHeap->Check(); |
|
357 |
INV(test(Invariant(pHeap))); |
|
358 |
||
359 |
// close heap so we don't exceed chunk limit |
|
360 |
pHeap->Close(); |
|
361 |
} |
|
362 |
||
363 |
/////////////////////////////////////////////////////////////////////////////// |
|
364 |
// Test Assorted Methods 3 - Here we go loopy loo, here we go loopy li |
|
365 |
////////////////////////////////////////////////////////////////////////////// |
|
366 |
void TestRHeap::Test4(void) |
|
367 |
{ |
|
368 |
TInt OrigBiggestBlock, BiggestBlock, FreeCount, AllocCount; |
|
369 |
RHeap* pHeap=allocHeap(5000); |
|
370 |
||
371 |
pHeap->Available(OrigBiggestBlock); |
|
372 |
#if defined(_DEBUG) |
|
373 |
((RTestHeap*)pHeap)->__DbgTest(&OrigDump); |
|
374 |
#endif |
|
375 |
||
376 |
for(TInt ArraySize=1; ArraySize<=100; ArraySize++) |
|
377 |
{ |
|
378 |
TAny** ArrayOfCells; |
|
379 |
ArrayOfCells= new TAny*[ArraySize]; |
|
380 |
TInt ArrayIndex; |
|
381 |
||
382 |
// Allocate some cells |
|
383 |
for(ArrayIndex=0; ArrayIndex<ArraySize;ArrayIndex++) |
|
384 |
ArrayOfCells[ArrayIndex]=pHeap->Alloc(OrigBiggestBlock/(ArraySize*3)); |
|
385 |
pHeap->Available(BiggestBlock); |
|
386 |
test(BiggestBlock!=OrigBiggestBlock); |
|
387 |
AllocCount=pHeap->Count(FreeCount); |
|
388 |
test((TInt)AllocCount==ArraySize); |
|
389 |
test(FreeCount==1); |
|
390 |
pHeap->Check(); |
|
391 |
// Now free them with Reset |
|
392 |
pHeap->Reset(); |
|
393 |
pHeap->Available(BiggestBlock); |
|
394 |
test(BiggestBlock==OrigBiggestBlock); |
|
395 |
AllocCount=pHeap->Count(FreeCount); |
|
396 |
test(AllocCount==0); |
|
397 |
test(FreeCount==1); |
|
398 |
||
399 |
||
400 |
// Allocate some cells again |
|
401 |
for(ArrayIndex=0; ArrayIndex<ArraySize;ArrayIndex++) |
|
402 |
ArrayOfCells[ArrayIndex]=pHeap->Alloc(OrigBiggestBlock/(ArraySize*3)); |
|
403 |
pHeap->Available(BiggestBlock); |
|
404 |
test(BiggestBlock!=OrigBiggestBlock); |
|
405 |
AllocCount=pHeap->Count(FreeCount); |
|
406 |
test((TInt)AllocCount==ArraySize); |
|
407 |
test(FreeCount==1); |
|
408 |
pHeap->Check(); |
|
409 |
// Free them with Free |
|
410 |
for(ArrayIndex=0; ArrayIndex<ArraySize;ArrayIndex++) |
|
411 |
pHeap->Free(ArrayOfCells[ArrayIndex]); |
|
412 |
pHeap->Available(BiggestBlock); |
|
413 |
test(BiggestBlock==OrigBiggestBlock); |
|
414 |
AllocCount=pHeap->Count(FreeCount); |
|
415 |
test(AllocCount==0); |
|
416 |
test(FreeCount==1); |
|
417 |
||
418 |
||
419 |
// Allocate some cells again |
|
420 |
for(ArrayIndex=0; ArrayIndex<ArraySize;ArrayIndex++) |
|
421 |
ArrayOfCells[ArrayIndex]=pHeap->Alloc(OrigBiggestBlock/(ArraySize*3)); |
|
422 |
pHeap->Available(BiggestBlock); |
|
423 |
test(BiggestBlock!=OrigBiggestBlock); |
|
424 |
AllocCount=pHeap->Count(FreeCount); |
|
425 |
test((TInt)AllocCount==ArraySize); |
|
426 |
test(FreeCount==1); |
|
427 |
pHeap->Check(); |
|
428 |
// Free them backwards |
|
429 |
for(ArrayIndex=ArraySize-1; ArrayIndex>=0; ArrayIndex--) |
|
430 |
pHeap->Free(ArrayOfCells[ArrayIndex]); |
|
431 |
pHeap->Available(BiggestBlock); |
|
432 |
test(BiggestBlock==OrigBiggestBlock); |
|
433 |
AllocCount=pHeap->Count(FreeCount); |
|
434 |
test(AllocCount==0); |
|
435 |
test(FreeCount==1); |
|
436 |
||
437 |
||
438 |
// Allocate some cells again |
|
439 |
for(ArrayIndex=0; ArrayIndex<ArraySize;ArrayIndex++) |
|
440 |
ArrayOfCells[ArrayIndex]=pHeap->Alloc(OrigBiggestBlock/(ArraySize*3)); |
|
441 |
pHeap->Available(BiggestBlock); |
|
442 |
test(BiggestBlock!=OrigBiggestBlock); |
|
443 |
AllocCount=pHeap->Count(FreeCount); |
|
444 |
test((TInt)AllocCount==ArraySize); |
|
445 |
test(FreeCount==1); |
|
446 |
pHeap->Check(); |
|
447 |
// Free the odd cells then the even cells |
|
448 |
for(ArrayIndex=0; ArrayIndex<ArraySize; ArrayIndex+=2) |
|
449 |
pHeap->Free(ArrayOfCells[ArrayIndex]); |
|
450 |
pHeap->Check(); |
|
451 |
for(ArrayIndex=1; ArrayIndex<ArraySize; ArrayIndex+=2) |
|
452 |
pHeap->Free(ArrayOfCells[ArrayIndex]); |
|
453 |
pHeap->Check(); |
|
454 |
pHeap->Available(BiggestBlock); |
|
455 |
test(BiggestBlock==OrigBiggestBlock); |
|
456 |
AllocCount=pHeap->Count(FreeCount); |
|
457 |
test(AllocCount==0); |
|
458 |
test(FreeCount==1); |
|
459 |
||
460 |
||
461 |
// Allocate some cells again |
|
462 |
for(ArrayIndex=0; ArrayIndex<ArraySize;ArrayIndex++) |
|
463 |
ArrayOfCells[ArrayIndex]=pHeap->Alloc(OrigBiggestBlock/(ArraySize*3)); |
|
464 |
pHeap->Available(BiggestBlock); |
|
465 |
test(BiggestBlock!=OrigBiggestBlock); |
|
466 |
AllocCount=pHeap->Count(FreeCount); |
|
467 |
test((TInt)AllocCount==ArraySize); |
|
468 |
test(FreeCount==1); |
|
469 |
pHeap->Check(); |
|
470 |
// Free one half then the other |
|
471 |
for(ArrayIndex=ArraySize-1; ArrayIndex>=ArraySize/2; ArrayIndex--) |
|
472 |
pHeap->Free(ArrayOfCells[ArrayIndex]); |
|
473 |
for(ArrayIndex=0; ArrayIndex<ArraySize/2; ArrayIndex++) |
|
474 |
pHeap->Free(ArrayOfCells[ArrayIndex]); |
|
475 |
AllocCount=pHeap->Count(FreeCount); |
|
476 |
test(AllocCount==0); |
|
477 |
test(FreeCount==1); |
|
478 |
||
479 |
delete [] ArrayOfCells; |
|
480 |
pHeap->Check(); |
|
481 |
INV(test(Invariant(pHeap))) |
|
482 |
} |
|
483 |
||
484 |
// close heap so we don't exceed chunk limit |
|
485 |
pHeap->Close(); |
|
486 |
} |
|
487 |
||
488 |
||
489 |
/////////////////////////////////////////////////////////////////////////////// |
|
490 |
// Test ReAlloc |
|
491 |
////////////////////////////////////////////////////////////////////////////// |
|
492 |
void TestRHeap::Test5(void) |
|
493 |
{ |
|
494 |
TInt BiggestBlock, CellSize; |
|
495 |
||
496 |
RHeap* pHeap=allocHeap(5000); |
|
497 |
#if defined(_DEBUG) |
|
498 |
((RTestHeap*)pHeap)->__DbgTest(&OrigDump); |
|
499 |
#endif |
|
500 |
pHeap->Available(BiggestBlock); |
|
501 |
TAny* aCell=pHeap->Alloc(BiggestBlock); |
|
502 |
||
503 |
// Attempt to resize the block above the space available |
|
504 |
test(pHeap->ReAlloc(aCell, BiggestBlock*2)==NULL); |
|
505 |
||
506 |
// Resize the block to 0 |
|
507 |
aCell=pHeap->ReAlloc(aCell, 0); |
|
508 |
CellSize=pHeap->AllocLen(aCell); // test? |
|
509 |
||
510 |
// Resize positively |
|
511 |
for(TInt aSize=0; aSize<=BiggestBlock; aSize++, pHeap->Available(BiggestBlock)) |
|
512 |
{ |
|
513 |
test(pHeap->ReAlloc(aCell, aSize)!=NULL); |
|
514 |
CellSize=pHeap->AllocLen(aCell); |
|
515 |
test(CellSize>=aSize); |
|
516 |
if (aSize<KMinCellLength) |
|
517 |
test(CellSize==KMinCellLength); |
|
518 |
else |
|
519 |
test(CellSize<aSize+KAlign); |
|
520 |
} |
|
521 |
||
522 |
// Note: when increasing a cell size the size is rounded up to the nearest 4 but when |
|
523 |
// decreasing a cell the size is rounded down to the nearest 8 - this is due to the fact |
|
524 |
// that when memory is released its size must be big enough to hold a free cell header which |
|
525 |
// is greater(8) than an allocated header(4) |
|
526 |
// i.e. size = 16, resize to 17 => result is 20. But resize to 15 stays as 16, resize to 9 |
|
527 |
// stays as 16 but resize as 8 will resize to 8 |
|
528 |
||
529 |
for(TInt aSize2=(TInt)pHeap->AllocLen(aCell); aSize2>=0; aSize2--) |
|
530 |
{ |
|
531 |
test(pHeap->ReAlloc(aCell, aSize2)!=NULL); |
|
19
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
532 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
533 |
test((TInt)pHeap->AllocLen(aCell)>=aSize2); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
534 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
535 |
TInt aTmpSize2 = Max(_ALIGN_UP(aSize2 + RHeap::EAllocCellSize, KAlign), KMinFreeSize); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
536 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
537 |
test((TInt)pHeap->AllocLen(aCell)<=aTmpSize2+KMinFreeSize); |
0 | 538 |
} |
539 |
||
540 |
pHeap->Check(); |
|
541 |
pHeap->Reset(); |
|
542 |
// Allocate a block, fill with data, allocate another block or two then resize the original |
|
543 |
// block such that it has to be moved in memory, then check the blocks' contents |
|
544 |
TAny* Cell1=pHeap->Alloc(16); |
|
545 |
TText8* pC; |
|
546 |
TInt Cell1Size=pHeap->AllocLen(Cell1); |
|
547 |
for(pC=(TText8*)Cell1; pC<(TText8*)Cell1+Cell1Size; *pC++='x') |
|
548 |
; |
|
549 |
TAny* Cell2=pHeap->Alloc(16); |
|
550 |
TInt Cell2Size=pHeap->AllocLen(Cell2); |
|
551 |
for(pC=(TText8*)Cell2; pC<(TText8*)Cell2+pHeap->AllocLen(Cell2); *pC++='y') |
|
552 |
; |
|
553 |
Cell1=pHeap->ReAlloc(Cell1, 128); |
|
554 |
// Test data was copied on reallocation |
|
555 |
for(pC=(TText8*)Cell1; pC<(TText8*)Cell1+Cell1Size; test(*pC++=='x')) |
|
556 |
; |
|
557 |
// Test other data wasn't corrupted |
|
558 |
for(pC=(TText8*)Cell2; pC<(TText8*)Cell2+pHeap->AllocLen(Cell2); test(*pC++=='y')) |
|
559 |
; |
|
560 |
||
561 |
// Allocate another block |
|
562 |
TAny* Cell3=pHeap->Alloc(8); |
|
563 |
for(pC=(TText8*)Cell3; pC<(TText8*)Cell3+pHeap->AllocLen(Cell3); *pC++='z') |
|
564 |
; |
|
565 |
// test existing blocks to be safe |
|
566 |
for(pC=(TText8*)Cell1; pC<(TText8*)Cell1+Cell1Size; test(*pC++=='x')) |
|
567 |
; |
|
568 |
for(pC=(TText8*)Cell2; pC<(TText8*)Cell2+Cell2Size; test(*pC++=='y')) |
|
569 |
; |
|
570 |
// Resize previous blocks |
|
571 |
Cell1=pHeap->ReAlloc(Cell1, 16); // Shrink previously expanded block |
|
572 |
Cell2=pHeap->ReAlloc(Cell2, 64); |
|
573 |
// Now test data |
|
574 |
for(pC=(TText8*)Cell1; pC<(TText8*)Cell1+Cell1Size; test(*pC++=='x')) |
|
575 |
; |
|
576 |
for(pC=(TText8*)Cell2; pC<(TText8*)Cell2+Cell2Size; test(*pC++=='y')) |
|
577 |
; |
|
578 |
for(pC=(TText8*)Cell3; pC<(TText8*)Cell3+pHeap->AllocLen(Cell3); test(*pC++=='z')) |
|
579 |
; |
|
580 |
||
581 |
// Re-expand Cell1 |
|
582 |
Cell1=pHeap->ReAlloc(Cell1, 1028); |
|
583 |
for(pC=(TText8*)Cell1; pC<(TText8*)Cell1+Cell1Size; test(*pC++=='x')) |
|
584 |
; |
|
585 |
for(pC=(TText8*)Cell2; pC<(TText8*)Cell2+Cell2Size; test(*pC++=='y')) |
|
586 |
; |
|
587 |
for(pC=(TText8*)Cell3; pC<(TText8*)Cell3+pHeap->AllocLen(Cell3); test(*pC++=='z')) |
|
588 |
; |
|
589 |
||
590 |
// Shrink cells back to original size |
|
591 |
Cell1=pHeap->ReAlloc(Cell1, Cell1Size); |
|
592 |
Cell2=pHeap->ReAlloc(Cell2, Cell2Size); |
|
593 |
for(pC=(TText8*)Cell1; pC<(TText8*)Cell1+Cell1Size; test(*pC++=='x')) |
|
594 |
; |
|
595 |
for(pC=(TText8*)Cell2; pC<(TText8*)Cell2+Cell2Size; test(*pC++=='y')) |
|
596 |
; |
|
597 |
for(pC=(TText8*)Cell3; pC<(TText8*)Cell3+pHeap->AllocLen(Cell3); test(*pC++=='z')) |
|
598 |
; |
|
599 |
||
600 |
pHeap->Check(); |
|
601 |
INV(test(Invariant(pHeap))); |
|
602 |
||
603 |
// close heap so we don't exceed chunk limit |
|
604 |
pHeap->Close(); |
|
605 |
} |
|
606 |
||
607 |
||
608 |
/////////////////////////////////////////////////////////////////////////////// |
|
609 |
// Test walking methods (more thoroughly than previously) |
|
610 |
////////////////////////////////////////////////////////////////////////////// |
|
611 |
void TestRHeap::Test7(void) |
|
612 |
{ |
|
613 |
TInt NumAllocated=0, NumFree=1, i; |
|
614 |
RHeap* pHeap=allocHeap(5000); |
|
615 |
||
616 |
TAny** ArrayOfCells; |
|
617 |
ArrayOfCells= new TAny*[100]; |
|
618 |
||
619 |
for(i=0; i<100; i++) |
|
620 |
{ |
|
621 |
ArrayOfCells[i]=pHeap->Alloc(8); |
|
622 |
NumAllocated++; |
|
623 |
test(NumAllocated==pHeap->Count(NumFree)); |
|
624 |
test(NumFree==1); |
|
625 |
} |
|
626 |
pHeap->Check(); |
|
627 |
||
628 |
for(i=0; i<100; i+=2) |
|
629 |
{ |
|
630 |
TInt temp; |
|
631 |
pHeap->Free(ArrayOfCells[i]); |
|
632 |
NumAllocated--; |
|
633 |
NumFree++; |
|
634 |
test(NumAllocated==pHeap->Count(temp)); |
|
635 |
test(NumFree==temp); |
|
636 |
} |
|
637 |
pHeap->Check(); |
|
638 |
pHeap->Reset(); |
|
639 |
||
640 |
||
641 |
/////////////////////////////////////////// |
|
642 |
// Corrupt data and see what happens |
|
643 |
/////////////////////////////////////////// |
|
644 |
// Corrupt allocated cell header |
|
645 |
ArrayOfCells[0]=pHeap->Alloc(32); |
|
646 |
TUint32* pC=(TUint32*)ArrayOfCells[0]-KHeadSize; |
|
647 |
*pC=0xa5a5a5a5u; |
|
648 |
// pHeap->Check(); |
|
649 |
||
650 |
// Corrupt free cell header |
|
651 |
pHeap->Reset(); |
|
652 |
ArrayOfCells[0]=pHeap->Alloc(32); |
|
653 |
pC=(TUint32*)ArrayOfCells[0]+(pHeap->AllocLen(ArrayOfCells[0])>>2); |
|
654 |
*pC=0xa1a1a1a1u; |
|
655 |
//pHeap->Check(); // Check doesn't pick it up but an access violation is generated |
|
656 |
||
657 |
// Write past end of heap |
|
658 |
pHeap->Reset(); |
|
659 |
TInt Avail; |
|
660 |
ArrayOfCells[0]=pHeap->Alloc(pHeap->Available(Avail)); |
|
661 |
pC=(TUint32*)ArrayOfCells[0]+(pHeap->AllocLen(ArrayOfCells[0])>>2); |
|
662 |
//*pC=0xa1a1a1a1u; // This line isn't picked up by Check (wouldn't expect it to) but the call |
|
663 |
//pHeap->Check(); // to delete below consequently crashes |
|
664 |
||
665 |
delete [] ArrayOfCells; |
|
666 |
||
667 |
// close heap so we don't exceed chunk limit |
|
668 |
pHeap->Close(); |
|
669 |
} |
|
670 |
||
671 |
////////////////////////////////////// |
|
672 |
// Test the leave methods |
|
673 |
////////////////////////////////////// |
|
674 |
void TestRHeap::Test8(void) |
|
675 |
{ |
|
676 |
||
677 |
TAny* aCell=NULL; |
|
678 |
RHeap* pHeap=allocHeap(1000); |
|
679 |
TRAPD(ret,aCell=pHeap->AllocL(100)) |
|
680 |
test(ret==KErrNone); |
|
681 |
TRAP(ret,aCell=pHeap->AllocL(PageSize)) |
|
682 |
test(ret==KErrNoMemory); |
|
683 |
TRAP(ret,aCell=pHeap->ReAllocL(aCell,32)) |
|
684 |
test(ret==KErrNone); |
|
685 |
TRAP(ret,aCell=pHeap->ReAllocL(NULL,10000)) |
|
686 |
test(ret==KErrNoMemory); |
|
687 |
||
688 |
// close heap so we don't exceed chunk limit |
|
689 |
pHeap->Close(); |
|
690 |
} |
|
691 |
||
692 |
class RMyHeap : public RHeap |
|
693 |
{ |
|
694 |
public: |
|
695 |
void MyCompressAll(){} |
|
696 |
private: |
|
697 |
RMyHeap(); |
|
698 |
}; |
|
699 |
||
700 |
#include "TestRHeapShrink.h" |
|
701 |
||
702 |
/** |
|
703 |
Calculates whether or not the heap with iGrowBy=aGrowBy will be reduced if a |
|
704 |
cell of size aCellSize bytes is the top free cell. |
|
705 |
It must be calculated as both the page size and min cell size could vary |
|
706 |
between different platforms/builds. Also, KHeapMinCellSize is 'patchdata' and can be |
|
707 |
different for particular ROM builds |
|
708 |
ASSUMPTIONS:- |
|
709 |
1 - The cell of aCellSize starts past the RHeap's iMinLength (i.e. all of it can be |
|
710 |
removed without the RHeap becoming smaller than iMinLength |
|
711 |
2 - The default value of aAlign was passed to RHeap contructor |
|
712 |
These should be safe as this is onl used by t_heap TestRHeap::CompressAll() |
|
713 |
@return The number of bytes the heap will be reduced by |
|
714 |
*/ |
|
715 |
TInt TestRHeap::RHeapCalcReduce(TInt aCellSize, TInt aGrowBy) |
|
716 |
{ |
|
717 |
TInt ret = 0; |
|
718 |
TInt pageSize = 0; |
|
719 |
test(UserHal::PageSizeInBytes(pageSize)==KErrNone); |
|
720 |
||
721 |
// adjust aGrowBy to match what RHeap would have aligned its iGrowBy to |
|
722 |
// see RHeap::RHeap() |
|
723 |
aGrowBy = _ALIGN_UP(aGrowBy, pageSize); |
|
724 |
if (aCellSize >= KHeapShrinkHysRatio*(aGrowBy>>8)) |
|
725 |
{ |
|
726 |
//calc for amount to reduce heap from RHeap::Reduce() |
|
727 |
// assumes that cell of aCellSize starts past the RHeap's iMinLength |
|
728 |
ret=_ALIGN_DOWN(aCellSize, pageSize); |
|
729 |
} |
|
730 |
return ret; |
|
731 |
} |
|
732 |
||
733 |
void TestRHeap::TestCompressAll() |
|
734 |
{ |
|
735 |
||
736 |
TPtrC myHeapName=_L("MyHeap"); |
|
737 |
// myHeap will have default GrowBy of KMinHeapGrowBy |
|
738 |
RMyHeap* myHeap=(RMyHeap*)User::ChunkHeap(&myHeapName,0x100,0x2000); |
|
739 |
const TInt KnormHeapGrowBy = 0x2000; |
|
740 |
RHeap* normHeap=User::ChunkHeap(NULL,0x100,0x20000,KnormHeapGrowBy); |
|
741 |
||
742 |
TAny* ptrMy1=myHeap->Alloc(0x102); |
|
743 |
test(ptrMy1!=NULL); |
|
744 |
TAny* ptrMy2=myHeap->Alloc(0x1001); |
|
745 |
test(ptrMy2!=NULL); |
|
746 |
TInt r=myHeap->Count(); |
|
747 |
test(r==2); |
|
748 |
||
749 |
TAny* ptrNorm1=normHeap->Alloc(0x8002); |
|
750 |
test(ptrNorm1!=NULL); |
|
751 |
TAny* ptrNorm2=normHeap->Alloc(0x12fff); |
|
752 |
test(ptrNorm2!=NULL); |
|
753 |
TAny* ptrNorm3=normHeap->Alloc(0x334f); |
|
754 |
test(ptrNorm3!=NULL); |
|
755 |
r=normHeap->Count(); |
|
756 |
test(r==3); |
|
757 |
||
758 |
TInt oldMyHeapSize=myHeap->Size(); |
|
759 |
TInt oldNormHeapSize=normHeap->Size(); |
|
760 |
||
761 |
myHeap->MyCompressAll(); |
|
762 |
||
763 |
r=myHeap->Count(); |
|
764 |
test(r==2); |
|
765 |
r=myHeap->Size(); |
|
766 |
test(r==oldMyHeapSize); |
|
767 |
r=normHeap->Count(); |
|
768 |
test(r==3); |
|
769 |
r=normHeap->Size(); |
|
770 |
test(r==oldNormHeapSize); |
|
771 |
||
772 |
// Remove the cell on the top of the normHeap |
|
773 |
normHeap->Free(ptrNorm3); |
|
774 |
// check myHeap unaffected |
|
775 |
r=myHeap->Count(); |
|
776 |
test(r==2); |
|
777 |
r=myHeap->Size(); |
|
778 |
test(r==oldMyHeapSize); |
|
779 |
//check normHeap updated after free of top cell |
|
780 |
r=normHeap->Count(); |
|
781 |
test(r==2); |
|
782 |
r=normHeap->Size(); |
|
783 |
||
784 |
// Calc the amount, if any, the overall size of normHeap will have been shrunk by |
|
785 |
// will depend on value of KHeapShrinkHysRatio. |
|
786 |
// 1st calc current total size of the allocated cells |
|
787 |
TInt normAllocdSize = normHeap->AllocLen(ptrNorm1)+RHeap::EAllocCellSize + |
|
788 |
normHeap->AllocLen(ptrNorm2)+RHeap::EAllocCellSize; |
|
789 |
TInt normReduce = RHeapCalcReduce(oldNormHeapSize-normAllocdSize,KnormHeapGrowBy); |
|
790 |
oldNormHeapSize -= normReduce; |
|
791 |
test(r==oldNormHeapSize); |
|
792 |
||
793 |
normHeap->Free(ptrNorm2); |
|
794 |
myHeap->Free(ptrMy2); |
|
795 |
r=myHeap->Count(); |
|
796 |
test(r==1); |
|
797 |
r=myHeap->Size(); |
|
798 |
||
799 |
// Calc the current total size of the allocated cells |
|
800 |
TInt myAllocdSize = myHeap->AllocLen(ptrMy1)+RHeap::EAllocCellSize; |
|
801 |
TInt myReduce=RHeapCalcReduce(oldMyHeapSize-myAllocdSize,1); |
|
802 |
oldMyHeapSize -= myReduce; |
|
803 |
test(r==oldMyHeapSize); |
|
804 |
||
805 |
r=normHeap->Count(); |
|
806 |
test(r==1); |
|
807 |
r=normHeap->Size(); |
|
808 |
||
809 |
// cell represented by ptrNorm3 may have already caused the heap |
|
810 |
// size to be reduced so ensure normReduce is factored into calcs |
|
811 |
test(r==oldNormHeapSize-(0x16000-normReduce)); |
|
812 |
||
813 |
myHeap->Close(); |
|
814 |
normHeap->Close(); |
|
815 |
} |
|
816 |
||
817 |
||
818 |
void TestRHeap::TestOffset() |
|
819 |
{ |
|
820 |
TInt size = 0x100000; |
|
821 |
const TInt offset = 0x8; |
|
822 |
const TUint8 magic = 0x74; // arbitrary magic value |
|
823 |
RChunk chunk; |
|
824 |
RHeap* heap; |
|
825 |
||
826 |
chunk.CreateLocal(0, size); |
|
827 |
size = chunk.MaxSize(); // X86 has 4MB chunk size |
|
828 |
||
829 |
// try and create a heap with a large offset - no room to make RHeap, should fail |
|
830 |
heap = UserHeap::OffsetChunkHeap(chunk, 0, size); |
|
831 |
test(heap==NULL); |
|
832 |
||
833 |
// write some magic numbers into the offset-reserved area |
|
834 |
chunk.Adjust(offset); |
|
835 |
TUint8* reserved = chunk.Base(); |
|
836 |
TUint8* limit = reserved + offset; |
|
837 |
for (; reserved<limit; reserved++) |
|
838 |
*reserved = magic; |
|
839 |
||
840 |
// make a heap with an offset |
|
841 |
heap = UserHeap::OffsetChunkHeap(chunk, 0, offset); |
|
842 |
test(heap!=NULL); |
|
843 |
test(chunk.Base() + offset == (TUint8*)heap); |
|
844 |
TInt origsize = heap->Size(); |
|
845 |
||
846 |
// force the heap to grow to the maximum size by allocating 1kb blocks |
|
847 |
// and then allocating whatever is left. Check this really is the end |
|
848 |
// of the chunk. |
|
849 |
TUint8* temp = NULL; |
|
850 |
TUint8* last = NULL; |
|
851 |
do |
|
852 |
{ |
|
853 |
last = temp; |
|
854 |
temp = (TUint8*)heap->Alloc(1024); |
|
855 |
} |
|
856 |
while (temp != NULL); |
|
857 |
TInt biggestblock, space; |
|
858 |
space = heap->Available(biggestblock); |
|
859 |
if (space>0) |
|
860 |
{ |
|
861 |
last = (TUint8*)heap->Alloc(space); |
|
862 |
test(last!=NULL); |
|
863 |
// Check that the last allocation doesn't pass the end of the chunk |
|
864 |
test(last+space <= chunk.Base()+size); |
|
865 |
// but that it is within the alignment requirement, as less than this |
|
866 |
// would be short of the end |
|
867 |
test(last+space > chunk.Base()+size-RHeap::ECellAlignment); |
|
868 |
} |
|
869 |
else |
|
870 |
{ |
|
871 |
test(last+1024 == chunk.Base()+size); |
|
872 |
} |
|
873 |
||
874 |
// try writing at the top end of it to make sure it's backed |
|
875 |
*(chunk.Base()+size-1) = 1; |
|
876 |
||
877 |
// test resetting the heap |
|
878 |
heap->Reset(); |
|
879 |
test(origsize == heap->Size()); |
|
880 |
||
881 |
// check reducing the heap works |
|
882 |
last = (TUint8*)heap->Alloc(size>>2); |
|
883 |
TInt midsize = heap->Size(); |
|
884 |
temp = (TUint8*)heap->Alloc(size>>2); |
|
885 |
heap->Free(temp); |
|
886 |
heap->Compress(); |
|
887 |
test(midsize == heap->Size()); |
|
888 |
heap->Free(last); |
|
889 |
heap->Compress(); |
|
890 |
test(origsize == heap->Size()); |
|
891 |
||
892 |
// check the magic numbers are still there |
|
893 |
for (reserved = chunk.Base(); reserved<limit; reserved++) |
|
894 |
test(*reserved==magic); |
|
895 |
||
896 |
heap->Close(); |
|
897 |
} |
|
898 |
||
899 |
||
900 |
RSemaphore sem; |
|
901 |
LOCAL_C void syncThreads(TAny* anArg) |
|
902 |
// |
|
903 |
// get the threads both running at the same time |
|
904 |
// |
|
905 |
{ |
|
906 |
if ((TInt)anArg==1) |
|
907 |
sem.Wait(); |
|
908 |
else |
|
909 |
sem.Signal(); |
|
910 |
} |
|
911 |
||
912 |
TInt comeInNumber=0; |
|
913 |
LOCAL_C TInt sharedHeapTest1(TAny* anArg) |
|
914 |
// |
|
915 |
// Shared heap test thread. |
|
916 |
// |
|
917 |
{ |
|
918 |
||
919 |
RHeap* pH = (RHeap*)&User::Allocator(); |
|
920 |
if (gHeapPtr && pH!=gHeapPtr) |
|
921 |
return(KErrGeneral); |
|
922 |
gHeapPtr2 = pH; |
|
923 |
||
924 |
syncThreads(anArg); |
|
925 |
||
926 |
TAny* a[0x100]; |
|
927 |
TInt mod=((TInt)anArg)*3; |
|
928 |
||
929 |
// Run in a timed loop, to ensure that we get some true concurrency |
|
930 |
RTimer timer; |
|
931 |
TTime now; |
|
932 |
TRequestStatus done; |
|
933 |
test(timer.CreateLocal()==KErrNone); |
|
934 |
now.HomeTime(); |
|
935 |
timer.At(done,now+TTimeIntervalSeconds(20)); |
|
936 |
||
937 |
while (done==KRequestPending && comeInNumber!=(TInt)anArg) |
|
938 |
{ |
|
939 |
TInt i=0; |
|
940 |
for (;i<0x100;i++) |
|
941 |
{ |
|
942 |
a[i]=User::Alloc(0x10); |
|
943 |
test(a[i]!=NULL); |
|
944 |
Mem::Fill(a[i],0x10,(((TInt)anArg)<<4)|(i&0x0F)); // marker |
|
945 |
if ((i%mod)==0) |
|
946 |
pH->Check(); |
|
947 |
} |
|
948 |
for (i=0;i<0x100;i++) |
|
949 |
{ |
|
950 |
User::Free(a[i]); |
|
951 |
if ((i%mod)==0) |
|
952 |
pH->Check(); |
|
953 |
} |
|
954 |
} |
|
955 |
timer.Cancel(); |
|
956 |
return((TInt)anArg); |
|
957 |
} |
|
958 |
||
959 |
LOCAL_C void bumpKernelGranularity() |
|
960 |
// |
|
961 |
// Push up the kernels granularities |
|
962 |
// |
|
963 |
{ |
|
964 |
||
965 |
RThread t[4]; |
|
966 |
TInt r; |
|
967 |
TUint i=0; |
|
968 |
for (;i<4;i++) |
|
969 |
{ |
|
970 |
TName n; |
|
971 |
n.Format(_L("Temp%d"),i); |
|
972 |
r=t[i].Create(n,sharedHeapTest1,KDefaultStackSize,NULL,NULL); |
|
973 |
test(r==KErrNone); |
|
974 |
} |
|
975 |
for (i=0;i<4;i++) |
|
976 |
{ |
|
977 |
t[i].Kill(KErrNone); |
|
978 |
t[i].Close(); |
|
979 |
} |
|
980 |
} |
|
981 |
||
982 |
LOCAL_C void createTestThreads(TThreadFunction aFunction,RHeap* aHeap) |
|
983 |
// |
|
984 |
// Create two test threads using the supplied entry point and heap |
|
985 |
// |
|
986 |
{ |
|
987 |
||
988 |
||
989 |
test.Next(_L("Create t1")); |
|
990 |
RThread t1; |
|
991 |
TInt r=t1.Create(_L("Shared1"),aFunction,KDefaultStackSize,aHeap,(TAny*)1); |
|
992 |
test(r==KErrNone); |
|
993 |
TRequestStatus tStat1; |
|
994 |
t1.Logon(tStat1); |
|
995 |
test(tStat1==KRequestPending); |
|
996 |
||
997 |
test.Next(_L("Create t2")); |
|
998 |
RThread t2; |
|
999 |
r=t2.Create(_L("Shared2"),aFunction,KDefaultStackSize,aHeap,(TAny*)2); |
|
1000 |
test(r==KErrNone); |
|
1001 |
TRequestStatus tStat2; |
|
1002 |
t2.Logon(tStat2); |
|
1003 |
test(tStat2==KRequestPending); |
|
1004 |
||
1005 |
test.Next(_L("Wait for t1 or t2 - approx 20 seconds")); |
|
1006 |
t1.Resume(); |
|
1007 |
t2.Resume(); |
|
1008 |
User::WaitForRequest(tStat1,tStat2); |
|
1009 |
User::WaitForRequest(tStat1==KRequestPending ? tStat1 : tStat2); |
|
1010 |
test(tStat1==1); |
|
1011 |
test(tStat2==2); |
|
1012 |
CLOSE_AND_WAIT(t1); |
|
1013 |
CLOSE_AND_WAIT(t2); |
|
1014 |
} |
|
1015 |
||
1016 |
LOCAL_C void SharedHeapTest1() |
|
1017 |
// |
|
1018 |
// Shared heap test using normal chunk heap |
|
1019 |
// |
|
1020 |
{ |
|
1021 |
||
1022 |
sem.CreateLocal(0); // create synchronisation semaphore |
|
1023 |
test.Start(_L("Create chunk to share")); |
|
1024 |
TPtrC sharedHeap=_L("SharedHeap"); |
|
1025 |
TInt minsize = ((RHeap&)User::Allocator()).Size(); |
|
1026 |
gHeapPtr=User::ChunkHeap(&sharedHeap,minsize/*0x20000*/,0x40000); |
|
1027 |
test(gHeapPtr!=NULL); |
|
1028 |
TInt count=gHeapPtr->Count(); |
|
1029 |
createTestThreads(sharedHeapTest1,gHeapPtr); |
|
1030 |
test(count==gHeapPtr->Count()); |
|
1031 |
gHeapPtr->Close(); |
|
1032 |
test.End(); |
|
1033 |
} |
|
1034 |
||
1035 |
LOCAL_C void SharedHeapTest2() |
|
1036 |
// |
|
1037 |
// Shared heap test using the current threads heap. Can test kernel |
|
1038 |
// cleanup since granularity will have been handled by running |
|
1039 |
// SharedHeapTest2(). |
|
1040 |
// |
|
1041 |
{ |
|
1042 |
||
1043 |
test.Start(_L("Current chunk to share")); |
|
1044 |
test.Next(_L("Bump up granularities")); |
|
1045 |
// |
|
1046 |
// First create a number of threads to push up the kernels granularities |
|
1047 |
// |
|
1048 |
bumpKernelGranularity(); |
|
1049 |
// |
|
1050 |
__KHEAP_MARK; |
|
1051 |
gHeapPtr = (RHeap*)&User::Allocator(); |
|
1052 |
TInt biggest1; |
|
1053 |
TInt avail1=gHeapPtr->Available(biggest1); |
|
1054 |
TInt size1=gHeapPtr->Size(); |
|
1055 |
||
1056 |
createTestThreads(sharedHeapTest1,NULL); |
|
1057 |
||
1058 |
TInt biggest2; |
|
1059 |
TInt avail2=gHeapPtr->Available(biggest2); |
|
1060 |
TInt size2=gHeapPtr->Size(); |
|
1061 |
test.Printf(_L("Before: size %d, %d available (biggest %d)\r\n"),size1,avail1,biggest1); |
|
1062 |
test.Printf(_L("After: size %d, %d available (biggest %d)\r\n"),size2,avail2,biggest2); |
|
1063 |
test((size1-avail1)==(size2-avail2)); // no leaks |
|
1064 |
if (avail1==biggest1) // if it was a single block of free space before |
|
1065 |
test(avail2==biggest2); // then it should still be a single block |
|
1066 |
__KHEAP_MARKEND; |
|
1067 |
test.End(); |
|
1068 |
} |
|
1069 |
||
1070 |
LOCAL_C void SharedHeapTest3() |
|
1071 |
// |
|
1072 |
// Shared heap test borrowing a thread's default heap and |
|
1073 |
// killing threads in different orders. |
|
1074 |
// |
|
1075 |
{ |
|
1076 |
||
1077 |
test.Start(_L("Create t1 whose heap will be shared")); |
|
1078 |
gHeapPtr = NULL; |
|
1079 |
RThread t1; |
|
1080 |
TInt r=t1.Create(_L("Owner_T1"),sharedHeapTest1,KDefaultStackSize,0x20000,0x40000,(TAny*)1); |
|
1081 |
test(r==KErrNone); |
|
1082 |
TRequestStatus tStat1; |
|
1083 |
t1.Logon(tStat1); |
|
1084 |
test(tStat1==KRequestPending); |
|
1085 |
t1.SetPriority(EPriorityMore); //t1 gets to wait on semaphore sem, before we start t2 |
|
1086 |
t1.Resume(); |
|
1087 |
test.Next(_L("Create t2 sharing t1's heap")); |
|
1088 |
RThread t2; |
|
1089 |
r=t2.Create(_L("Sharer_T2"),sharedHeapTest1,KDefaultStackSize,gHeapPtr2,(TAny*)2); |
|
1090 |
test(r==KErrNone); |
|
1091 |
TRequestStatus tStat2; |
|
1092 |
t2.Logon(tStat2); |
|
1093 |
test(tStat2==KRequestPending); |
|
1094 |
||
1095 |
test.Next(_L("Get t1 to exit while t2 continues running")); |
|
1096 |
test(tStat1==KRequestPending); |
|
1097 |
test(tStat2==KRequestPending); |
|
1098 |
t1.SetPriority(EPriorityNormal); //back to the same priority as t2 |
|
1099 |
t2.Resume(); |
|
1100 |
test(tStat1==KRequestPending); |
|
1101 |
test(tStat2==KRequestPending); |
|
1102 |
comeInNumber=1; |
|
1103 |
test.Next(_L("Wait for t1")); |
|
1104 |
User::WaitForRequest(tStat1); |
|
1105 |
test(tStat1==1); |
|
1106 |
test(t1.ExitType()==EExitKill); |
|
1107 |
test(t1.ExitReason()==1); |
|
1108 |
test(tStat2==KRequestPending); |
|
1109 |
test(t2.ExitType()==EExitPending); |
|
1110 |
test.Next(_L("Wait for t2")); |
|
1111 |
User::WaitForRequest(tStat2); |
|
1112 |
test(tStat2==2); |
|
1113 |
test(t2.ExitType()==EExitKill); |
|
1114 |
test(t2.ExitReason()==2); |
|
1115 |
CLOSE_AND_WAIT(t2); |
|
1116 |
CLOSE_AND_WAIT(t1); |
|
1117 |
test.End(); |
|
1118 |
} |
|
1119 |
||
1120 |
LOCAL_C void TestAuto() |
|
1121 |
// |
|
1122 |
// Test heap auto expansion and compression |
|
1123 |
// |
|
1124 |
{ |
|
1125 |
||
1126 |
test.Start(_L("Create chunk to")); |
|
1127 |
TPtrC autoHeap=_L("AutoHeap"); |
|
1128 |
gHeapPtr=User::ChunkHeap(&autoHeap,0x1800,0x6000); |
|
1129 |
test(gHeapPtr!=NULL); |
|
1130 |
TInt biggest; |
|
1131 |
TInt avail=gHeapPtr->Available(biggest); |
|
1132 |
test(avail==biggest); |
|
1133 |
TAny *p1=gHeapPtr->Alloc(biggest); |
|
1134 |
test(p1!=NULL); |
|
1135 |
TAny *p2=gHeapPtr->Alloc(biggest); |
|
1136 |
test(p2!=NULL); |
|
1137 |
TAny *p3=gHeapPtr->Alloc(biggest); |
|
1138 |
test(p3!=NULL); |
|
1139 |
TAny *p4=gHeapPtr->Alloc(biggest); |
|
1140 |
test(p4==NULL); |
|
1141 |
TInt comp=gHeapPtr->Compress(); |
|
1142 |
test(comp==0); |
|
1143 |
gHeapPtr->Free(p2); |
|
1144 |
comp=gHeapPtr->Compress(); |
|
1145 |
test(comp==0); |
|
1146 |
gHeapPtr->Free(p3); |
|
1147 |
comp=gHeapPtr->Compress(); |
|
1148 |
// stop wins compiler warning of constant expression as KHeapShrinkHysRatio |
|
1149 |
// isn't constant for non-emulator builds but ROM 'patchdata' |
|
1150 |
#pragma warning(disable : 4127) |
|
1151 |
// When hysteresis value > 4.0*GrowBy then Free() calls |
|
1152 |
// won't shrink heap but normally will shrink heap |
|
1153 |
if (KHeapShrinkHysRatio <= 1024) |
|
1154 |
test(comp==0); |
|
1155 |
else |
|
1156 |
test(comp==0x4000); |
|
1157 |
#pragma warning(default : 4127) |
|
1158 |
gHeapPtr->Free(p1); |
|
1159 |
comp=gHeapPtr->Compress(); |
|
1160 |
test(comp==0); |
|
1161 |
TInt biggest1; |
|
1162 |
TInt avail1=gHeapPtr->Available(biggest1); |
|
1163 |
test(avail1==avail1); |
|
1164 |
test(biggest==biggest1); |
|
1165 |
test(gHeapPtr->Count()==0); |
|
1166 |
gHeapPtr->Close(); |
|
1167 |
test.End(); |
|
1168 |
} |
|
1169 |
||
1170 |
||
1171 |
GLDEF_C TInt E32Main(void) |
|
1172 |
{ |
|
1173 |
||
1174 |
test.Title(); |
|
1175 |
||
1176 |
__KHEAP_MARK; |
|
1177 |
||
1178 |
test.Start(_L("Test 1")); |
|
1179 |
UserHal::PageSizeInBytes(PageSize); |
|
1180 |
TestRHeap T; |
|
1181 |
T.Test1(); |
|
1182 |
test.Next(_L("Test auto expand and compress")); |
|
1183 |
TestAuto(); |
|
1184 |
test.Next(_L("Test 2")); |
|
1185 |
T.Test2(); |
|
1186 |
test.Next(_L("Test 3")); |
|
1187 |
T.Test3(); |
|
1188 |
test.Next(_L("Test 4")); |
|
1189 |
T.Test4(); |
|
1190 |
test.Next(_L("Test 5")); |
|
1191 |
T.Test5(); |
|
1192 |
test.Next(_L("Test 7")); |
|
1193 |
T.Test7(); |
|
1194 |
test.Next(_L("Test 8")); |
|
1195 |
T.Test8(); |
|
1196 |
test.Next(_L("Test CompressAll()")); |
|
1197 |
T.TestCompressAll(); |
|
1198 |
test.Next(_L("Test offset heap")); |
|
1199 |
T.TestOffset(); |
|
1200 |
test.Next(_L("Shared heap test 1")); |
|
1201 |
SharedHeapTest1(); |
|
1202 |
test.Next(_L("Shared heap test 2")); |
|
1203 |
SharedHeapTest2(); |
|
1204 |
test.Next(_L("Shared heap test 3")); |
|
1205 |
SharedHeapTest3(); |
|
1206 |
sem.Close(); |
|
1207 |
||
1208 |
__KHEAP_CHECK(0); |
|
1209 |
__KHEAP_MARKEND; |
|
1210 |
// |
|
1211 |
test.End(); |
|
1212 |
return(0); |
|
1213 |
} |
|
1214 |