Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\buffer\t_tbma.cpp
18 #define __E32TEST_EXTENSION__
21 #include <e32atomics.h>
23 RTest test(_L("T_TBMA"));
26 /**************************************
28 **************************************/
30 TBmaList* TBmaList::New(TInt aNumBmas)
32 TBmaList* pL=new TBmaList;
35 pL->iNumBmas=aNumBmas;
36 pL->iBmaList=(TBitMapAllocator**)User::Alloc(aNumBmas*sizeof(TBitMapAllocator*));
38 Mem::FillZ(pL->iBmaList, aNumBmas*sizeof(TBitMapAllocator*));
39 pL->iBaseList=(TInt*)User::Alloc((aNumBmas+1)*sizeof(TInt));
40 if (!pL->iBmaList || !pL->iBaseList)
49 TBmaList* TBmaList::New(const TBitMapAllocator& aBma, TInt aNumSplits, VA_LIST aList)
51 TBmaList* pL=TBmaList::New(aNumSplits+1);
56 for (i=1; i<=aNumSplits; ++i)
57 pL->iBaseList[i]=VA_ARG(aList,TInt);
58 pL->iBaseList[aNumSplits+1]=aBma.iSize;
59 for (i=0; i<=aNumSplits; ++i)
61 TInt sz=pL->iBaseList[i+1]-pL->iBaseList[i];
62 __ASSERT_ALWAYS(sz>0, TBMA_FAULT());
63 pL->iBmaList[i]=TBitMapAllocator::New(sz,EFalse);
72 if (aBma.NotAllocated(j+pL->iBaseList[i],1))
73 pL->iBmaList[i]->Free(j);
89 for (i=0; i<iNumBmas; ++i)
92 User::Free(iBaseList);
95 * Extended first fit allocator
97 TInt TBmaList::AllocConsecutiveFF(TInt aLength)
99 TInt base=KErrNotFound;
102 TBitMapAllocator** ppA=iBmaList; // pointer to list of TBitMapAllocator*
103 TBitMapAllocator** pE=ppA+iNumBmas;
105 for (; ppA<pE; ++ppA, ++pB)
107 TBitMapAllocator* pA=*ppA;
108 if (*pB!=base+bmalen)
110 // this BMA is not contiguous with previous one
116 TInt r=pA->AllocAligned(aLength,0,0,EFalse,carry,l);
124 * Extended best fit allocator
126 TInt TBmaList::AllocConsecutiveBF(TInt aLength)
130 TInt minrun=KMaxTInt;
131 TInt minrunpos=KErrNotFound;
132 TBitMapAllocator** ppA=iBmaList; // pointer to list of TBitMapAllocator*
133 TBitMapAllocator** pE=ppA+iNumBmas;
136 for (; ppA<pE; ++ppA, ++pB)
138 TBitMapAllocator* pA=*ppA;
139 if (*pB!=base+bmalen)
141 // this BMA is not contiguous with previous one
142 // check final run of previous BMA
146 minrunpos=base+bmalen-carry;
154 TInt r=pA->AllocAligned(aLength,0,0,ETrue,carry,l);
157 // check shortest run in this BMA
161 minrunpos=r ? (base+r) : (base-oldc);
163 break; // exact match so finish
167 // check final run of last BMA
168 if (ppA==pE && carry>=aLength && carry<minrun)
169 minrunpos=base+bmalen-carry;
174 * Extended first fit aligned allocator
176 TInt TBmaList::AllocAlignedFF(TInt aLength, TInt aAlign)
178 TUint32 alignsize=1<<aAlign;
179 TUint32 alignmask=alignsize-1;
180 TInt base=KErrNotFound;
183 TBitMapAllocator** ppA=iBmaList; // pointer to list of TBitMapAllocator*
184 TBitMapAllocator** pE=ppA+iNumBmas;
186 for (; ppA<pE; ++ppA, ++pB)
188 TBitMapAllocator* pA=*ppA;
189 if (*pB!=base+bmalen)
191 // this BMA is not contiguous with previous one
197 TInt r=pA->AllocAligned(aLength,aAlign,base,EFalse,carry,l);
199 return (base+r-carry+alignmask)&~alignmask;
205 * Extended best fit aligned allocator
207 TInt TBmaList::AllocAlignedBF(TInt aLength, TInt aAlign)
211 TInt minrun=KMaxTInt;
212 TInt minrunpos=KErrNotFound;
213 TUint32 alignsize=1<<aAlign;
214 TUint32 alignmask=alignsize-1;
215 TBitMapAllocator** ppA=iBmaList; // pointer to list of TBitMapAllocator*
216 TBitMapAllocator** pE=ppA+iNumBmas;
219 for (; ppA<pE; ++ppA, ++pB)
221 TBitMapAllocator* pA=*ppA;
222 if (*pB!=base+bmalen)
224 // this BMA is not contiguous with previous one
225 // check final run of previous BMA
228 TInt fpos=base+bmalen-carry;
229 TInt lost=((fpos+base+alignmask)&~alignmask)-base-fpos;
230 if (carry-lost>=aLength)
242 TInt r=pA->AllocAligned(aLength,aAlign,base,ETrue,carry,l);
245 // check shortest run in this BMA
249 minrunpos=r ? (base+r) : (base-oldc);
251 break; // exact match so finish
255 // check final run of last BMA
256 if (ppA==pE && carry<minrun)
258 TInt fpos=base+bmalen-carry;
259 TInt lost=((fpos+alignmask)&~alignmask)-fpos;
260 if (carry-lost>=aLength)
266 return (minrunpos<0) ? minrunpos : ((minrunpos+alignmask)&~alignmask);
276 void Display(TBitMapAllocator* aBma)
278 test.Printf(_L("Free %d FirstCheck %08x Size %d Map %08x\n"),aBma->iAvail,aBma->iCheckFirst,aBma->iSize,aBma->iMap);
281 for (i=0; i<((aBma->iSize+31)>>5); i++)
288 TUint32 x=aBma->iMap[i];
290 buf.NumFixedWidth(x,EBinary,32);
291 buf.Append(_L("\n"));
297 void Check(TBitMapAllocator& a)
309 n+=__e32_bit_count_32(w);
312 test(first?(a.iCheckFirst<=first):(a.iCheckFirst>=a.iMap && a.iCheckFirst<=a.iMap+l));
315 void TestConstruct(TInt aSize)
317 test.Printf(_L("TestConstruct %d\n"),aSize);
318 TBitMapAllocator* pA;
319 pA=TBitMapAllocator::New(aSize, EFalse);
321 test(pA->Avail()==0);
324 pA=TBitMapAllocator::New(aSize, ETrue);
326 test(pA->Avail()==aSize);
331 void TestAlloc1(TInt aSize)
333 test.Printf(_L("TestAlloc1 %d\n"),aSize);
334 TBitMapAllocator* pA=TBitMapAllocator::New(aSize, ETrue);
336 test(pA->Avail()==aSize);
339 for (i=0; i<aSize; ++i)
343 test(pA->Avail()==aSize-i-1);
344 test(pA->iCheckFirst==pA->iMap+i/32);
351 void TestFree1(TInt aSize)
353 test.Printf(_L("TestFree1 %d\n"),aSize);
354 TBitMapAllocator* pA=TBitMapAllocator::New(aSize, EFalse);
356 test(pA->Avail()==0);
358 for (i=aSize-1; i>=0; --i)
361 test(pA->Avail()==aSize-i);
362 test(pA->Alloc()==i);
364 test(pA->iCheckFirst==pA->iMap+i/32);
370 void TestBlockAlloc(TInt aSize)
372 test.Printf(_L("TestBlockAlloc %d\n"),aSize);
373 const TInt begin[]={0,1,2,7,16,29,31,32,33,63,64,65,83,128};
374 TBitMapAllocator* pA=TBitMapAllocator::New(aSize, ETrue);
376 test(pA->Avail()==aSize);
378 test(pA->Avail()==0);
381 test(pA->Avail()==aSize);
384 for (i=0; i<(TInt)(sizeof(begin)/sizeof(TInt)); ++i)
390 for (l=1; l<=aSize-j; ++l)
392 // test.Printf(_L("j=%d, l=%d, s=%d\n"),j,l,aSize);
394 test(pA->Avail()==aSize-l);
395 test(!pA->NotAllocated(j,l));
397 test(pA->NotAllocated(j,l+1));
399 test(pA->NotAllocated(j-1,l));
419 test(pA->Avail()==aSize);
426 void TestBlockFree(TInt aSize)
428 test.Printf(_L("TestBlockFree %d\n"),aSize);
429 const TInt begin[]={0,1,2,7,16,29,31,32,33,63,64,65,83,128};
430 TBitMapAllocator* pA=TBitMapAllocator::New(aSize, EFalse);
432 test(pA->Avail()==0);
434 for (i=0; i<(TInt)(sizeof(begin)/sizeof(TInt)); ++i)
440 for (l=1; l<=aSize-j; ++l)
442 // test.Printf(_L("j=%d, l=%d, s=%d\n"),j,l,aSize);
444 test(pA->Avail()==l);
445 test(!pA->NotFree(j,l));
447 test(pA->NotFree(j,l+1));
449 test(pA->NotFree(j-1,l));
454 test(pA->Avail()==0);
461 void TestNotFree(TInt aSize)
463 test.Printf(_L("TestNotFree %d\n"),aSize);
464 TBitMapAllocator* pA=TBitMapAllocator::New(aSize, ETrue);
466 test(pA->Avail()==aSize);
467 test(!pA->NotFree(0,aSize));
469 for (i=0; i<aSize; ++i)
472 test(pA->NotFree(0,aSize));
474 for (j=1; j*j<=i || j*j+i<=aSize; ++j)
477 TInt b=Min(i+j*j,aSize);
478 test(pA->NotFree(a,b-a));
481 test(!pA->NotFree(0,aSize));
483 const TInt begin[]={0,1,2,7,16,29,31,32,33,63,64,65,83,128};
484 const TInt size[]={2,3,7,16,23,31,32,33,63,64,65,89,128};
485 const TInt* pB=begin;
486 const TInt* pBE=pB+sizeof(begin)/sizeof(TInt);
488 const TInt* pSE=pS+sizeof(size)/sizeof(TInt);
501 for (j=1; j<aSize; ++j)
504 test(!pA->NotFree(0,j));
506 test(pA->NotFree(0,j));
508 test(!pA->NotFree(aSize-j,j));
510 test(pA->NotFree(aSize-j,j));
518 void TestNotAllocated(TInt aSize)
520 test.Printf(_L("TestNotAllocated %d\n"),aSize);
521 TBitMapAllocator* pA=TBitMapAllocator::New(aSize, EFalse);
523 test(pA->Avail()==0);
524 test(!pA->NotAllocated(0,aSize));
526 for (i=0; i<aSize; ++i)
529 test(pA->NotAllocated(0,aSize));
531 for (j=1; j*j<=i || j*j+i<=aSize; ++j)
534 TInt b=Min(i+j*j,aSize);
535 test(pA->NotAllocated(a,b-a));
538 test(!pA->NotAllocated(0,aSize));
540 const TInt begin[]={0,1,2,7,16,29,31,32,33,63,64,65,83,128};
541 const TInt size[]={2,3,7,16,23,31,32,33,63,64,65,89,128};
542 const TInt* pB=begin;
543 const TInt* pBE=pB+sizeof(begin)/sizeof(TInt);
545 const TInt* pSE=pS+sizeof(size)/sizeof(TInt);
558 for (j=1; j<aSize; ++j)
561 test(!pA->NotAllocated(0,j));
563 test(pA->NotAllocated(0,j));
565 test(!pA->NotAllocated(aSize-j,j));
567 test(pA->NotAllocated(aSize-j,j));
575 void TestAllocList(TInt aSize)
577 test.Printf(_L("TestAllocList %d\n"),aSize);
578 TBitMapAllocator* pA=TBitMapAllocator::New(aSize, EFalse);
580 test(pA->Avail()==0);
583 for (i=0; i<aSize; ++i)
586 test(pA->AllocList(128,list)==1);
588 test(pA->Avail()==0);
591 for (i=0; i<aSize-1; ++i)
593 for (j=i+1; j<aSize; ++j)
597 test(pA->AllocList(1,list)==1);
599 test(pA->Avail()==1);
601 test(pA->AllocList(128,list)==2);
602 test(list[0]==i && list[1]==j);
603 test(pA->Avail()==0);
611 for (i=l+1; i<=aSize-l; ++i)
615 test(pA->Avail()==2*l);
617 for (l2=Max(l-1,1); l2<=2*l; ++l2)
619 TInt r=pA->AllocList(l2,list);
620 // test.Printf(_L("l2=%d r=%d\n"),l2,r);
622 for (j=0; j<Min(l2,l); j++)
625 test(list[j]==j-l+i);
628 pA->SelectiveFree(0,l);
629 pA->SelectiveFree(i,l);
630 test(pA->Avail()==2*l);
640 void TestSelectiveFree(TInt aSize)
642 test.Printf(_L("TestSelectiveFree %d\n"),aSize);
643 TBitMapAllocator* pA=TBitMapAllocator::New(aSize, ETrue);
645 test(pA->Avail()==aSize);
651 for (l=1; l<=aSize; ++l)
653 new (pA) TBitMapAllocator(aSize, ETrue);
654 for (j=0; j<aSize; j+=i)
656 TInt orig=pA->Avail();
657 test(orig==aSize-(aSize+i-1)/i);
658 pA->SelectiveFree(0,l);
659 TInt freed=pA->Avail()-orig;
660 test(freed==(l+i-1)/i);
664 for (i=0; i<=Min(32,aSize-1); ++i)
666 for (l=1; l<=aSize-i; ++l)
668 for (j=1; j<=aSize; ++j)
670 new (pA) TBitMapAllocator(aSize, ETrue);
672 test(pA->Avail()==aSize-l);
673 pA->SelectiveFree(0,j);
674 test(pA->Avail()==aSize-l+Max(0,Min(i+l,j)-i));
675 test(!pA->NotFree(0,j));
677 test(pA->NotFree(0,j+1));
685 TBitMapAllocator* DoSetupBMA(TInt aSize, VA_LIST aList)
687 TBitMapAllocator* pA=TBitMapAllocator::New(aSize, EFalse);
689 test(pA->Avail()==0);
693 TInt i=VA_ARG(aList,TInt);
696 TInt l=VA_ARG(aList,TInt);
700 test(pA->Avail()==totalfree);
704 TBitMapAllocator* SetupBMA(TInt aSize, ...)
707 VA_START(list,aSize);
708 return DoSetupBMA(aSize,list);
711 void PopulateRangeArray(RArray<SRange>& aArray, VA_LIST aList)
718 rng.iBase=VA_ARG(aList,TInt);
721 rng.iLength=VA_ARG(aList,TInt);
725 test(aArray.Append(rng)==KErrNone);
729 TInt FirstFitPos(RArray<SRange>& aArray, TInt aLength)
731 TInt c=aArray.Count();
732 SRange* pR=&aArray[0];
736 TInt l=pR->iLength>>8;
739 // test.Printf(_L("FFP %d = %d\n"),aLength,pR->iBase);
743 // test.Printf(_L("FFP %d = -1\n"),aLength);
747 TInt AlignedFirstFitPos(RArray<SRange>& aArray, TInt aSize, TInt aLength, TInt aAlign, TInt aBase, TInt aOffset=0, TBool aBestFit=EFalse)
749 TInt alignSize=1<<aAlign;
750 TInt alignMask=alignSize-1;
753 TBool runFound = EFalse;
754 TInt c=aArray.Count();
755 SRange* pR=&aArray[0];
756 // Best fit mode should ignore any final run TBitMapAllocator will
757 // always ignore the final run in best fit mode and rely on carry being
758 // checked by the caller.
759 SRange* pE = pR + c - 1;
760 if (!aBestFit || aSize > pE->iBase + (pE->iLength >> 8))
765 TInt l=pR->iLength>>8;
769 aOffset = ((aOffset + aBase + alignMask) & ~alignMask) - aBase;
770 if (aOffset + aLength - 1 >= b + l)
771 {// The offset is after the end of this region.
774 l -= (aOffset <= b)? 0 : aOffset - b;
775 b += (aOffset <= b)? 0 : aOffset - b; // Start the search from aOffset
777 TInt ab=((b+aBase+alignMask)&~alignMask)-aBase;
778 TInt al = l + b - ab;
781 if (!aBestFit || l == aLength)
783 // test.Printf(_L("AFFP %d %d %d = %d\n"),aLength,aAlign,aBase,ab);
786 if (!runFound || minRun > l)
798 // test.Printf(_L("AFFP %d %d %d = -1\n"),aLength,aAlign,aBase);
802 void DoConsecTest(TInt aSize, ...)
804 test.Printf(_L("DoConsecTest %d\n"),aSize);
807 VA_START(list,aSize);
808 VA_START(list2,aSize);
809 TBitMapAllocator* pA=DoSetupBMA(aSize,list2);
810 RArray<SRange> rangeArray(8,_FOFF(SRange,iLength));
811 PopulateRangeArray(rangeArray, list);
813 for (n=1; n<=aSize; ++n)
815 TInt r1=pA->AllocConsecutive(n,EFalse);
816 TInt r2=FirstFitPos(rangeArray,n);
817 // test.Printf(_L("ALC(%d,0) = %d [%d]\n"),n,r1,r2);
820 rangeArray.SortUnsigned(); // sort array in ascending order of length
821 for (n=1; n<=aSize; ++n)
823 TInt r1=pA->AllocConsecutive(n,ETrue);
824 TInt r2=FirstFitPos(rangeArray,n);
825 // test.Printf(_L("ALC(%d,1) = %d [%d]\n"),n,r1,r2);
832 void DoAlignedTest(TInt aSize, ...)
834 test.Printf(_L("DoAlignedTest %d\n"),aSize);
837 VA_START(list,aSize);
838 VA_START(list2,aSize);
839 TBitMapAllocator* pA=DoSetupBMA(aSize,list2);
840 RArray<SRange> rangeArray(8,_FOFF(SRange,iLength));
841 PopulateRangeArray(rangeArray, list);
842 TInt finalRunLength = 0;
843 SRange& lastRun = rangeArray[rangeArray.Count() - 1];
844 if (lastRun.iBase + (lastRun.iLength>>8) == aSize)
845 {// The last run is at the end of the bma.
846 finalRunLength = lastRun.iLength >> 8;
852 for (a=0; ((1<<a)<=aSize); ++a)
855 TInt alignmask=alignsize-1;
856 for (b=0; b<(1<<a); ++b)
858 // test.Printf(_L("size %d a=%d b=%d First\n"),aSize,a,b);
859 for (n=1; n<=aSize; ++n)
861 for (offset = 1; offset < (TUint)aSize; offset <<= 1)
865 TInt r1=pA->AllocAligned(n,a,b,EFalse, carry, runLength, offset);
866 TInt r2=AlignedFirstFitPos(rangeArray,aSize, n,a,b, offset);
867 if (r2 < 0 && pA->iSize == pA->iAvail)
868 {// Totally empty bmas return KErrOverflow on failure.
871 // test.Printf(_L("ALA %d %d %d %d 0 = %d [%d]\n"),n,a,b,offset,r1,r2);
872 test( (r1<0) || ((r1+b)&alignmask)==0 );
873 test( (r1<0) || !pA->NotFree(r1,n));
874 test( (r1<0) || runLength >= n);
880 for (a=0; ((1<<a)<=aSize); ++a)
883 TInt alignmask=alignsize-1;
884 for (b=0; b<(1<<a); ++b)
886 // test.Printf(_L("size %d a=%d b=%d Best\n"),aSize,a,b);
887 for (n=1; n<=aSize; ++n)
888 {// test for with offset=0 as that has screwed best fit in past.
889 for (offset = 0; offset < (TUint)aSize; offset <<= 1)
893 TInt r1=pA->AllocAligned(n,a,b,ETrue, carry, runLength, offset);
894 TInt r2=AlignedFirstFitPos(rangeArray,aSize, n,a,b, offset, ETrue);
895 if (pA->iSize == pA->iAvail)
896 {// Totally empty bmas return KErrOverflow always on best fit mode.
899 // test.Printf(_L("ALA %d %d %d 1 = %d [%d]\n"),n,a,b,r1,r2);
900 test( (r1<0) || ((r1+b)&alignmask)==0 );
901 test( (r1<0) || !pA->NotFree(r1,n));
902 test( (r1<0) || runLength >= n);
904 // No carry in so if run found then carry should be zero.
905 // If no run found then carry should set to the length of
906 // any run at the end of the bma minus the aligned offset.
908 TInt alignOffset = ((offset + b + alignmask) & ~alignmask) - b;
909 if (finalRunLength && offset && lastRun.iBase < alignOffset)
910 {// This search had started past the start of the final run
911 // so the final run length found will be shorter than its
913 if (alignOffset < aSize)
915 lost = Min(alignOffset - lastRun.iBase, finalRunLength);
917 else // alignedOffset starts past end of bma.
918 lost = finalRunLength;
920 test((r1>=0 && carry == 0) || carry == finalRunLength - lost);
921 offset = (offset)? offset : 1;
930 void Clone(TAny* aDest, const TBitMapAllocator* aSrc)
932 TInt nmapw=(aSrc->iSize+31)>>5;
933 TInt memsz=sizeof(TBitMapAllocator)+(nmapw-1)*sizeof(TUint32);
934 Mem::Move(aDest,aSrc,memsz);
937 void TestAllocConsecutive()
939 test.Printf(_L("TestAllocConsecutive\n"));
940 DoConsecTest(256, 0,4 , 20,8 , 38,1 , 58,6 , 65,10, 78,16 , 127,72, 222,19 , 244,12 , -1);
941 DoConsecTest(255, 0,2 , 3,2 , 6,3 , 10,3 , 14,5 , 20,5 , 26,7 , 34,7 , 42,11 , 54,11 , 66,13 , 80,37,
942 118,19 , 138,23 , 162,47 , 254,1 , -1);
943 DoConsecTest(1023, 0,2 , 32,32 , 65,31 , 99,30 , 144,64 , 256,519 , 776,1, 778,245 , -1);
945 DoAlignedTest(256, 0,4 , 20,8 , 38,1 , 58,6 , 65,10, 78,16 , 127,72, 222,19 , 244,12 , -1);
946 DoAlignedTest(255, 0,2 , 3,2 , 6,3 , 10,3 , 14,5 , 20,5 , 26,7 , 34,7 , 42,11 , 54,11 , 66,13 , 80,37,
947 118,19 , 138,23 , 162,47 , 254,1 , -1);
948 DoAlignedTest(1023, 0,2 , 32,32 , 65,31 , 99,30 , 144,64 , 256,519 , 776,1, 778,245 , -1);
949 // Test some completely free bmas
950 DoAlignedTest(255, 0,255, -1);
951 DoAlignedTest(256, 0,256, -1);
952 DoAlignedTest(1023, 0,1023, -1);
953 DoAlignedTest(1024, 0,1024, -1);
956 void DoTestChain(const TBitMapAllocator& aBma, TInt aNumSplits, ...)
958 test.Printf(_L("DoTestChain %d %d\n"),aBma.iSize,aNumSplits);
960 VA_START(list,aNumSplits);
962 TBmaList* pL=TBmaList::New(aBma,aNumSplits,list);
966 for (n=1; n<=aBma.iSize; ++n)
968 TInt r1=aBma.AllocConsecutive(n,EFalse);
969 TInt r2=pL->AllocConsecutiveFF(n);
970 // test.Printf(_L("CHAIN C FF %d: r1=%d r2=%d\n"),n,r1,r2);
973 for (n=1; n<=aBma.iSize; ++n)
975 TInt r1=aBma.AllocConsecutive(n,ETrue);
976 TInt r2=pL->AllocConsecutiveBF(n);
977 // test.Printf(_L("CHAIN C BF %d: r1=%d r2=%d\n"),n,r1,r2);
982 for (a=0; ((1<<a)<=aBma.iSize); ++a)
984 for (n=1; n<=aBma.iSize; ++n)
991 TInt r1=aBma.AllocAligned(n,a,0,EFalse);
992 TInt r2=pL->AllocAlignedFF(n,a);
993 // test.Printf(_L("CHAIN A FF %d,%d: r1=%d r2=%d\n"),n,a,r1,r2);
997 for (a=0; ((1<<a)<=aBma.iSize); ++a)
999 for (n=1; n<=aBma.iSize; ++n)
1006 TInt r1=aBma.AllocAligned(n,a,0,ETrue);
1007 TInt r2=pL->AllocAlignedBF(n,a);
1008 // test.Printf(_L("CHAIN A BF %d,%d: r1=%d r2=%d\n"),n,a,r1,r2);
1018 test.Printf(_L("TestChain\n"));
1019 TBitMapAllocator* pA;
1020 pA=SetupBMA(1023, 0,2 , 32,32 , 65,31 , 99,30 , 144,64 , 256,519 , 776,1, 778,245 , -1);
1022 DoTestChain(*pA, 2, 300, 700);
1023 DoTestChain(*pA, 3, 64, 301, 702);
1025 pA=SetupBMA(512, 0,2 , 20,10 , 32,32 , 65,31 , 144,64 , 399,113 , -1);
1027 DoTestChain(*pA, 2, 256, 384);
1028 DoTestChain(*pA, 3, 128, 256, 384);
1029 DoTestChain(*pA, 3, 80, 208, 384);
1030 DoTestChain(*pA, 3, 80, 208, 400);
1036 test.Next(_L("Bit operations (32 bit)"));
1037 test(__e32_find_ls1_32(0x00000000)==-1);
1040 for (i=0; i<=31; ++i)
1041 test(__e32_find_ls1_32(1u<<i)==i);
1043 for (i=0; i<1000; ++i)
1049 TUint y = ((x|1)<<bit);
1051 test(__e32_find_ls1_32(y) == bit);
1054 test(__e32_find_ms1_32(0x00000000)==-1);
1055 for (i=0; i<=31; ++i)
1056 test(__e32_find_ms1_32(1u<<i)==i);
1057 for (i=0; i<1000; ++i)
1063 TUint y = ((x|0x80000000u)>>bit);
1065 test(__e32_find_ms1_32(y) == 31-bit);
1068 test(__e32_bit_count_32(0)==0);
1069 test(__e32_bit_count_32(0xffffffff)==32);
1070 for (i=0; i<32; ++i)
1072 TUint32 y = 0xffffffffu << i;
1073 TUint32 z = 0xffffffffu >> i;
1074 test(__e32_bit_count_32(y) == 32-i);
1075 test(__e32_bit_count_32(z) == 32-i);
1076 test(__e32_bit_count_32(~y) == i);
1077 test(__e32_bit_count_32(~z) == i);
1079 for (i=0; i<32; ++i)
1080 for (j=0; j<32; ++j)
1081 for (k=0; k<32; ++k)
1086 TUint32 y = b0 | b1 | b2;
1088 if (i==j && j==k) n=1;
1089 else if (i==j || j==k || i==k) n=2;
1091 test(__e32_bit_count_32(y) == n);
1092 test(__e32_bit_count_32(~y) == 32-n);
1095 test.Printf(_L("%d iterations done\n"), count);
1096 for (i=0; i<=31; ++i)
1098 test(__e32_bit_count_32(0xaaaaaaaau<<i)==16-(i+1)/2);
1099 test(__e32_bit_count_32(0x55555555u<<i)==16-i/2);
1101 test(__e32_bit_count_32(0x33333333u)==16);
1102 test(__e32_bit_count_32(0x88888888u)==8);
1104 test.Next(_L("Bit operations (64 bit)"));
1105 test(__e32_find_ls1_64(0x00000000)==-1);
1106 for (i=0; i<=63; ++i)
1110 test(__e32_find_ls1_64(x)==i);
1113 for (i=0; i<1000; ++i)
1122 TUint64 y = MAKE_TUINT64(xh,xl);
1124 test(__e32_find_ls1_64(y) == bit);
1127 test(__e32_find_ms1_64(0x00000000)==-1);
1128 for (i=0; i<=63; ++i)
1132 test(__e32_find_ms1_64(x)==i);
1135 for (i=0; i<1000; ++i)
1143 TUint32 xh = x|0x80000000u;
1144 TUint64 y = MAKE_TUINT64(xh,xl);
1146 test(__e32_find_ms1_64(y) == 63-bit);
1149 test(__e32_bit_count_64(0)==0);
1150 test(__e32_bit_count_64(MAKE_TUINT64(0x00000000,0xffffffff))==32);
1151 test(__e32_bit_count_64(MAKE_TUINT64(0xffffffff,0x00000000))==32);
1152 test(__e32_bit_count_64(MAKE_TUINT64(0xffffffff,0xffffffff))==64);
1153 for (i=0; i<64; ++i)
1155 TUint64 y = MAKE_TUINT64(0xffffffff,0xffffffff);
1158 test(__e32_bit_count_64(y) == 64-i);
1159 test(__e32_bit_count_64(z) == 64-i);
1160 test(__e32_bit_count_64(~y) == i);
1161 test(__e32_bit_count_64(~z) == i);
1164 for (i=0; i<64; ++i)
1165 for (j=0; j<64; ++j)
1166 for (k=0; k<64; ++k)
1174 TUint64 y = b0 | b1 | b2;
1177 if (i==j && j==k) n=1;
1178 else if (i==j || j==k || i==k) n=2;
1180 test(__e32_bit_count_64(y) == n);
1181 test(__e32_bit_count_64(z) == 64-n);
1184 test.Printf(_L("%d iterations done\n"), count);
1185 for (i=0; i<64; ++i)
1187 TUint64 y = MAKE_TUINT64(0xaaaaaaaa,0xaaaaaaaa);
1189 test(__e32_bit_count_64(y<<i)==32-(i+1)/2);
1190 test(__e32_bit_count_64(z<<i)==32-i/2);
1192 test(__e32_bit_count_64(MAKE_TUINT64(0x33333333u,0x33333333u))==32);
1193 test(__e32_bit_count_64(MAKE_TUINT64(0x88888888u,0x88888888u))==16);
1196 GLDEF_C TInt E32Main()
1200 test.Start(_L("TBitMapAllocator tests"));
1221 TestBlockAlloc(256);
1222 TestBlockAlloc(179);
1234 TestNotAllocated(3);
1235 TestNotAllocated(31);
1236 TestNotAllocated(256);
1237 TestNotAllocated(149);
1244 TestSelectiveFree(3);
1245 TestSelectiveFree(31);
1246 TestSelectiveFree(128);
1247 TestSelectiveFree(149);
1249 TestAllocConsecutive();