tstRTMemCache.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
/* $Id$ */
/** @file
* IPRT Testcase - RTMemCache.
*/
/*
* Copyright (C) 2010 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/memcache.h>
#include <iprt/initterm.h>
#include <iprt/semaphore.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct TST3THREAD
{
uint64_t volatile cIterations;
bool fUseCache;
} TST3THREAD, *PTST3THREAD;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The test handle */
/** Global mem cache handle for use in some of the testcases. */
static RTMEMCACHE g_hMemCache;
/** Stop indicator for tst3 threads. */
static bool volatile g_fTst3Stop;
/**
* Basic API checks.
* We'll return if any of these fails.
*/
static void tst1(void)
{
RTTestISub("Basics");
/* Create one without constructor or destructor. */
RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&hMemCache, 256, cObjects, 32, NULL, NULL, NULL, 0 /*fFlags*/), VINF_SUCCESS);
/* Allocate a bit and free it again. */
/* Allocate everything and free it again, checking size constraints. */
{
/* Allocate everything. */
{
}
/* Check that we've got it all. */
int rc;
if (RT_SUCCESS(rc))
/* Free all the allocations. */
{
}
}
/* Destroy it. */
}
/** Constructor for tst2. */
{
if (*(bool *)pvUser)
return VERR_RESOURCE_BUSY;
return VINF_SUCCESS;
}
/** Destructor for tst2. Checks that it was constructed and used twice. */
{
}
/**
* Test constructor / destructor.
*/
static void tst2(void)
{
RTTestISub("Ctor/Dtor");
/* Create one without constructor or destructor. */
bool fFail = false;
RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&g_hMemCache, 256, cObjects, 32, tst2Ctor, tst2Dtor, &fFail, 0 /*fFlags*/), VINF_SUCCESS);
/* A failure run first. */
fFail = true;
void *pv = (void *)0x42;
fFail = false;
/* To two rounds where we allocate all the objects and free them again. */
{
{
if (iLoop == 0)
else
}
}
/* Cone, destroy the cache. */
}
/**
* Thread that allocates
* @returns
* @param hThreadSelf The thread.
* @param pvArg Pointer to fUseCache.
*/
{
uint64_t cIterations = 0;
/* wait for the kick-off */
/* allocate and free loop */
{
while (!g_fTst3Stop)
{
void *apv[64];
for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
{
}
for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
}
}
else
{
while (!g_fTst3Stop)
{
void *apv[64];
for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
{
}
for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
}
}
/* report back the status */
return VINF_SUCCESS;
}
/**
* Time constrained test with and unlimited N threads.
*/
{
iMethod == 0 ? "RTMemCache"
: "RTMemAlloc");
/*
* Create a cache with unlimited space, a start semaphore and line up
* the threads.
*/
RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&g_hMemCache, cbObject, 0 /*cbAlignment*/, UINT32_MAX, NULL, NULL, NULL, 0 /*fFlags*/), VINF_SUCCESS);
ASMAtomicWriteBool(&g_fTst3Stop, false);
{
aThreads[i].cIterations = 0;
}
/*
* Start the race.
*/
RTTimeNanoTS(); /* warmup */
ASMAtomicWriteBool(&g_fTst3Stop, true);
/*
* Sum up the counts.
*/
uint64_t cIterations = 0;
/* clean up */
}
{
}
{
if (rc)
return rc;
tst1();
tst2();
if (RTTestIErrorCount() == 0)
{
/* threads, cbObj, cSecs */
}
/*
* Summary.
*/
return RTTestSummaryAndDestroy(hTest);
}