tstRTHeapOffset.cpp revision 10d6b59caa606eb4603637d6796949d50e99c258
/* $Id$ */
/** @file
* IPRT Testcase - Offset Based Heap.
*/
/*
* Copyright (C) 2006-2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
{
/*
* Init runtime.
*/
if (rc)
return rc;
/*
* Create a heap.
*/
if (RT_FAILURE(rc))
return RTTestSummaryAndDestroy(hTest);
/*
* Try allocate.
*/
static struct TstHeapOffsetOps
{
unsigned uAlignment;
void *pvAlloc;
unsigned iFreeOrder;
} s_aOps[] =
{
};
uint32_t i;
RTHeapOffsetDump(Heap, (PFNRTHEAPOFFSETPRINTF)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
static char const s_szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* allocate */
for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
{
RTTESTI_CHECK_MSG(s_aOps[i].pvAlloc, ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
return RTTestSummaryAndDestroy(hTest);
RTTESTI_CHECK_MSG(RT_ALIGN_P(s_aOps[i].pvAlloc, (s_aOps[i].uAlignment ? s_aOps[i].uAlignment : 8)) == s_aOps[i].pvAlloc,
return RTTestSummaryAndDestroy(hTest);
}
/* free and allocate the same node again. */
for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
{
continue;
//RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, s_aOps[i].pvAlloc,
// s_aOps[i].cb, s_aOps[i].uAlignment, RTHeapOffsetSize(Heap, s_aOps[i].pvAlloc));
void *pv;
RTTESTI_CHECK_MSG(pv, ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
if (!pv)
return RTTestSummaryAndDestroy(hTest);
//RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapOffsetSize(Heap, pv),
// cbBeforeSub, cbAfterSubFree, RTHeapOffsetGetFreeSize(Heap));
RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, s_aOps[i].pvAlloc, i);
if (cbBeforeSub != cbAfterSubAlloc)
{
RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
//return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
}
}
/* make a copy of the heap and the to-be-freed list. */
/* free it in a specific order. */
int cFreed = 0;
for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
{
unsigned j;
for (j = 0; j < RT_ELEMENTS(s_aOps); j++)
{
if ( s_aOps[j].iFreeOrder != i
continue;
//RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapOffsetGetFreeSize(Heap), s_aOps[j].cb, s_aOps[j].pvAlloc);
cFreed++;
}
}
/* check that we're back at the right amount of free memory. */
{
"Warning: Either we've split out an alignment chunk at the start, or we've got\n"
}
/* relocate and free the bits in heap2 now. */
/* free it in a specific order. */
int cFreed2 = 0;
for (i = 0; i < RT_ELEMENTS(s_aOpsCopy); i++)
{
unsigned j;
for (j = 0; j < RT_ELEMENTS(s_aOpsCopy); j++)
{
if ( s_aOpsCopy[j].iFreeOrder != i
|| !s_aOpsCopy[j].pvAlloc)
continue;
//RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapOffsetGetFreeSize(hHeapCopy), s_aOpsCopy[j].cb, s_aOpsCopy[j].pvAlloc);
cFreed2++;
}
}
/* check that we're back at the right amount of free memory. */
/*
* Use random allocation pattern
*/
if (RT_FAILURE(rc))
return RTTestSummaryAndDestroy(hTest);
if (RT_FAILURE(rc))
return RTTestSummaryAndDestroy(hTest);
#if 0
#else
#endif
static struct
{
void *pv;
} s_aHistory[1536];
{
if (!s_aHistory[i].pv)
{
if (!s_aHistory[i].pv)
{
}
if (s_aHistory[i].pv)
}
else
{
}
{
/* exhaust the heap */
if (!s_aHistory[i].pv)
{
}
{
if (!s_aHistory[i].pv)
{
}
if (s_aHistory[i].pv)
}
}
{
/* free all */
for (i = 0; i < RT_ELEMENTS(s_aHistory); i++)
{
}
}
}
/* free the rest. */
for (i = 0; i < RT_ELEMENTS(s_aHistory); i++)
{
}
/* check that we're back at the right amount of free memory. */
return RTTestSummaryAndDestroy(hTest);
}