void TestMemoryAllocatorFree(void) { MemoryInit(); CMemoryAllocator cAlloc; CConstructorTestClass* pcTest; gcConstructors.Add<CConstructorTestClass>("CConstructorTestClass"); cAlloc.Init(); AssertInt(0, cAlloc.GetMemory()->NumElements()); pcTest = (CConstructorTestClass*)gcConstructors.Construct("CConstructorTestClass", &cAlloc); AssertInt(1, cAlloc.GetMemory()->NumElements()); pcTest->k = 7; AssertInt(3, pcTest->Method1()); AssertInt(7, pcTest->k); cAlloc.Free(pcTest); AssertInt(0, cAlloc.GetMemory()->NumElements()); cAlloc.Kill(); MemoryKill(); }
void TestTrackingAllocatorSize(void) { CTrackingAllocator cTracking; CMemoryAllocator cMemory; void* pv1; void* pv2; void* pv3; void* pv4; cMemory.Init(); cTracking.Init(&cMemory); AssertInt(0, cTracking.AllocatedSize()); pv1 = cTracking.Malloc(100); AssertInt(100, cTracking.AllocatedSize()); pv2 = cTracking.Malloc(200); AssertInt(300, cTracking.AllocatedSize()); pv3 = cTracking.Malloc(50); AssertInt(350, cTracking.AllocatedSize()); pv4 = cTracking.Realloc(pv3, 1000); AssertInt(1300, cTracking.AllocatedSize()); cTracking.Free(pv1); AssertInt(1200, cTracking.AllocatedSize()); cTracking.Free(pv2); AssertInt(1000, cTracking.AllocatedSize()); cTracking.Free(pv4); AssertInt(0, cTracking.AllocatedSize()); cTracking.Kill(); cMemory.Kill(); }
void TestIndexTreeMemoryRemoveResize(void) { CIndexTreeMemory cIndex; long long li; CMemoryAllocator cMemoryAlloc; CCountingAllocator cTrackingAlloc; int iExpectedRootSize; cMemoryAlloc.Init(); cTrackingAlloc.Init(&cMemoryAlloc); cIndex.Init(&cTrackingAlloc); AssertInt(1, cIndex.CountAllocatedNodes()); AssertInt(0, cIndex.RecurseSize()); AssertInt(16, cIndex.SizeofNode()); AssertInt(4, cIndex.SizeofNodePtr()); iExpectedRootSize = cIndex.CalculateRootNodeSize(); AssertInt(1040, iExpectedRootSize); AssertInt(1040, cTrackingAlloc.AllocatedUserSize()); li = 0x77LL; cIndex.Put("M", &li, sizeof(long long)); AssertInt(2, cIndex.CountAllocatedNodes()); AssertInt(1, cIndex.RecurseSize()); AssertInt(1064, iExpectedRootSize + sizeof(CIndexTreeNodeMemory) + sizeof(long long)); AssertInt(1064, cTrackingAlloc.AllocatedUserSize()); li = 0x88LL; cIndex.Put("MA", &li, sizeof(long long)); AssertInt(1092, cTrackingAlloc.AllocatedUserSize()); li = 0x99LL; cIndex.Put("MC", &li, sizeof(long long)); AssertInt(4, cIndex.CountAllocatedNodes()); AssertInt(3, cIndex.RecurseSize()); AssertInt(1124, cTrackingAlloc.AllocatedUserSize()); li = 0xaaLL; cIndex.Put("MB", &li, sizeof(long long)); AssertInt(5, cIndex.CountAllocatedNodes()); AssertInt(4, cIndex.RecurseSize()); AssertInt(1148, cTrackingAlloc.AllocatedUserSize()); li = 0xbbLL; cIndex.Put("MBP", &li, sizeof(long long)); AssertInt(6, cIndex.CountAllocatedNodes()); AssertInt(5, cIndex.RecurseSize()); AssertInt(5, cIndex.NumElements()); AssertInt(1176, cTrackingAlloc.AllocatedUserSize()); AssertLongLongInt(0xaaLL, *((long long*)cIndex.Get("MB"))); cIndex.Remove("MB"); AssertInt(6, cIndex.CountAllocatedNodes()); AssertInt(4, cIndex.RecurseSize()); AssertInt(4, cIndex.NumElements()); AssertInt(1176, cTrackingAlloc.AllocatedUserSize()); AssertNull(cIndex.Get("MB")); AssertLongLongInt(0xbbLL, *((long long*)cIndex.Get("MBP"))); cIndex.Remove("MBP"); AssertInt(4, cIndex.CountAllocatedNodes()); AssertInt(3, cIndex.RecurseSize()); AssertInt(3, cIndex.NumElements()); AssertInt(1124, cTrackingAlloc.AllocatedUserSize()); AssertNull(cIndex.Get("MBP")); AssertLongLongInt(0x99LL, *((long long*)cIndex.Get("MC"))); AssertLongLongInt(0x88LL, *((long long*)cIndex.Get("MA"))); cIndex.Remove("MA"); AssertInt(3, cIndex.CountAllocatedNodes()); AssertInt(2, cIndex.RecurseSize()); AssertInt(2, cIndex.NumElements()); AssertInt(1092, cTrackingAlloc.AllocatedUserSize()); AssertNull(cIndex.Get("MA")); AssertLongLongInt(0x99LL, *((long long*)cIndex.Get("MC"))); cIndex.Remove("MC"); AssertInt(2, cIndex.CountAllocatedNodes()); AssertInt(1, cIndex.RecurseSize()); AssertInt(1, cIndex.NumElements()); AssertInt(1064, cTrackingAlloc.AllocatedUserSize()); AssertNull(cIndex.Get("MC")); AssertLongLongInt(0x77LL, *((long long*)cIndex.Get("M"))); cIndex.Remove("M"); AssertInt(1, cIndex.CountAllocatedNodes()); AssertInt(0, cIndex.RecurseSize()); AssertInt(0, cIndex.NumElements()); AssertInt(1040, cTrackingAlloc.AllocatedUserSize()); AssertNull(cIndex.Get("M")); cIndex.Kill(); cTrackingAlloc.Kill(); cMemoryAlloc.Kill(); }
void TestMemoryAllocatorReadWrite(void) { MemoryInit(); CMemoryAllocator cAlloc; CFileBasic cFile; CMemoryAllocator cAllocIn; int i; SMemoryParams sParams; SMemoryFreeListParams* psBlockParams; cFile.Init(MemoryFile()); cFile.Open(EFM_ReadWrite_Create); cFile.WriteInt(789); cAlloc.Init(16, FALSE); cAlloc.GetMemory()->AddParamBlock(24 , 16 , 32*32); cAlloc.GetMemory()->AddParamBlock(32 , 24 , 28*32); cAlloc.GetMemory()->AddParamBlock(40 , 32 , 24*32); AssertTrue(cAlloc.Write(&cFile)); cFile.WriteInt(124); cFile.Close(); cAlloc.Kill(); cFile.Open(EFM_Read); cFile.ReadInt(&i); AssertInt(789, i); AssertTrue(cAllocIn.Read(&cFile)); cFile.ReadInt(&i); AssertInt(124, i); AssertInt(16, cAllocIn.GetMemory()->ByteSize()); cAllocIn.GetMemory()->GetParams(&sParams); AssertInt(16, sParams.iDefaultAlignment); AssertInt(56, sParams.uiFreeListSizeLimit); AssertInt(3, sParams.iFreeListParams); psBlockParams = cAllocIn.GetMemory()->GetFreeListParams(0); AssertNotNull(psBlockParams); AssertInt(32*32, psBlockParams->iChunkSize); AssertInt(17, psBlockParams->iMinElementSize); AssertInt(24, psBlockParams->iMaxElementSize); psBlockParams = cAllocIn.GetMemory()->GetFreeListParams(1); AssertNotNull(psBlockParams); AssertInt(28*32, psBlockParams->iChunkSize); AssertInt(25, psBlockParams->iMinElementSize); AssertInt(32, psBlockParams->iMaxElementSize); psBlockParams = cAllocIn.GetMemory()->GetFreeListParams(2); AssertNotNull(psBlockParams); AssertInt(24*32, psBlockParams->iChunkSize); AssertInt(33, psBlockParams->iMinElementSize); AssertInt(40, psBlockParams->iMaxElementSize); psBlockParams = cAllocIn.GetMemory()->GetFreeListParams(3); AssertNull(psBlockParams); MemoryKill(); }