//+------------------------------------------------------------------------- // // Microsoft Windows // Copyright (C) Microsoft Corporation, 1996 - 1999. // // File: tmem.cxx // // Contents: tmem - test mem allocators for big tables. // //-------------------------------------------------------------------------- #include "pch.cxx" #pragma hdrstop DECLARE_INFOLEVEL(tb) #include "tblalloc.hxx" BYTE MemPool[4096]; //BYTE* PoolPtrs[409]; //ULONG PoolOffs[409]; BOOL fInMemPool = TRUE; // // ReportHeap - report heap blocks // void ReportHeap( void* pBlock, USHORT cbSize, USHORT fFree ) { BYTE* pbBlock = (BYTE*) pBlock; printf("%08x %d%s\n", pbBlock, cbSize, fFree? " FREE": ""); if (fInMemPool && (pbBlock < &MemPool[0] || pbBlock + cbSize > &MemPool[ sizeof MemPool ])) { printf("\007***\tpBlock outside mem pool\t***\007"); } } // // FreeBlocks -- free some of the allocated blocks // void FreeBlocks( BYTE* apbPtrs[], unsigned cPtrs, int fVerbose, CWindowDataAllocator& Alloc ) { unsigned i; // Free every third allocated block for (i = 2; i < cPtrs; i += 3) { if (apbPtrs[i]) { Alloc.Free((void*)apbPtrs[i]); apbPtrs[i] = 0; } } if (fVerbose) { printf("\nAfter freeing every third allocation\n"); Alloc.WalkHeap(ReportHeap); } // Free every other allocated block, will cause some coalescing for (i = 0; i < cPtrs; i += 2) { if (apbPtrs[i]) { Alloc.Free((void*)apbPtrs[i]); apbPtrs[i] = 0; } } if (fVerbose) { printf("\nAfter freeing every other allocation\n"); Alloc.WalkHeap(ReportHeap); } // Finally, Free every fourth allocated block, will cause more coalescing for (i = 3; i < cPtrs; i += 4) { if (apbPtrs[i]) { Alloc.Free((void*)apbPtrs[i]); apbPtrs[i] = 0; } } if (fVerbose) { printf("\nAfter freeing every other allocation\n"); Alloc.WalkHeap(ReportHeap); } } __cdecl main(int argc, char** argv) { BYTE* pMem = MemPool; int fVerbose = 0; unsigned i; for (i=1; i<(unsigned)argc; i++) { if (argv[i][0] == '-' && tolower(argv[i][1]) == 'v') fVerbose++; else { printf("Usage: %s [-v]\n", argv[0]); } } // Test the simple allocator with forward allocation. memset(pMem, 0xD5, sizeof MemPool); CVarBufferAllocator Alloc1(pMem, sizeof MemPool); BYTE* pbuf; BOOL fBreak = FALSE; for (i=0; i<0xFFFFFFFF && !fBreak; i++) { TRY { pbuf = 0; pbuf = (BYTE *)Alloc1.Allocate(10); } CATCH (CException, e) { if (e.GetErrorCode() == E_OUTOFMEMORY) fBreak = TRUE; else RETHROW(); } END_CATCH if (pbuf) memset(pbuf, '0' + i%10, 10); } Win4Assert(i == sizeof MemPool/10); for (i=0; iWalkHeap(ReportHeap); } if (pVAlloc) { for (i=0; i