tstRTMemCache.cpp revision db2893e30b129fbadd201b8f56cd7bc1974b395d
45e9809aff7304721fddb95654901b32195c9c7avboxsync * IPRT Testcase - RTMemCache.
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Copyright (C) 2010 Sun Microsystems, Inc.
45e9809aff7304721fddb95654901b32195c9c7avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
45e9809aff7304721fddb95654901b32195c9c7avboxsync * available from http://www.virtualbox.org. This file is free software;
45e9809aff7304721fddb95654901b32195c9c7avboxsync * you can redistribute it and/or modify it under the terms of the GNU
45e9809aff7304721fddb95654901b32195c9c7avboxsync * General Public License (GPL) as published by the Free Software
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
45e9809aff7304721fddb95654901b32195c9c7avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
45e9809aff7304721fddb95654901b32195c9c7avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
45e9809aff7304721fddb95654901b32195c9c7avboxsync * The contents of this file may alternatively be used under the terms
45e9809aff7304721fddb95654901b32195c9c7avboxsync * of the Common Development and Distribution License Version 1.0
45e9809aff7304721fddb95654901b32195c9c7avboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
45e9809aff7304721fddb95654901b32195c9c7avboxsync * VirtualBox OSE distribution, in which case the provisions of the
45e9809aff7304721fddb95654901b32195c9c7avboxsync * CDDL are applicable instead of those of the GPL.
45e9809aff7304721fddb95654901b32195c9c7avboxsync * You may elect to license modified versions of this file under the
45e9809aff7304721fddb95654901b32195c9c7avboxsync * terms and conditions of either the GPL or the CDDL or both.
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
45e9809aff7304721fddb95654901b32195c9c7avboxsync * additional information or have any questions.
45e9809aff7304721fddb95654901b32195c9c7avboxsync/*******************************************************************************
45e9809aff7304721fddb95654901b32195c9c7avboxsync* Header Files *
45e9809aff7304721fddb95654901b32195c9c7avboxsync*******************************************************************************/
45e9809aff7304721fddb95654901b32195c9c7avboxsync/*******************************************************************************
45e9809aff7304721fddb95654901b32195c9c7avboxsync* Structures and Typedefs *
45e9809aff7304721fddb95654901b32195c9c7avboxsync*******************************************************************************/
45e9809aff7304721fddb95654901b32195c9c7avboxsynctypedef struct TST3THREAD
45e9809aff7304721fddb95654901b32195c9c7avboxsync/*******************************************************************************
45e9809aff7304721fddb95654901b32195c9c7avboxsync* Global Variables *
45e9809aff7304721fddb95654901b32195c9c7avboxsync*******************************************************************************/
45e9809aff7304721fddb95654901b32195c9c7avboxsync/** The test handle */
45e9809aff7304721fddb95654901b32195c9c7avboxsync/** Global mem cache handle for use in some of the testcases. */
45e9809aff7304721fddb95654901b32195c9c7avboxsync/** For testcase 3. */
45e9809aff7304721fddb95654901b32195c9c7avboxsync/** Stop indicator for tst3 threads. */
45e9809aff7304721fddb95654901b32195c9c7avboxsyncstatic bool volatile g_fTst3Stop;
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Basic API checks.
45e9809aff7304721fddb95654901b32195c9c7avboxsync * We'll return if any of these fails.
45e9809aff7304721fddb95654901b32195c9c7avboxsyncstatic void tst1(void)
45e9809aff7304721fddb95654901b32195c9c7avboxsync /* Create one without constructor or destructor. */
45e9809aff7304721fddb95654901b32195c9c7avboxsync RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&hMemCache, 256, cObjects, 32, NULL, NULL, NULL, 0 /*fFlags*/), VINF_SUCCESS);
45e9809aff7304721fddb95654901b32195c9c7avboxsync /* Allocate a bit and free it again. */
45e9809aff7304721fddb95654901b32195c9c7avboxsync RTTESTI_CHECK_RC_RETV(RTMemCacheAllocEx(hMemCache, &pv), VINF_SUCCESS);
45e9809aff7304721fddb95654901b32195c9c7avboxsync RTTESTI_CHECK((pv = RTMemCacheAlloc(hMemCache)) != NULL);
45e9809aff7304721fddb95654901b32195c9c7avboxsync /* Allocate everything and free it again, checking size constraints. */
45e9809aff7304721fddb95654901b32195c9c7avboxsync /* Allocate everything. */
45e9809aff7304721fddb95654901b32195c9c7avboxsync RTTESTI_CHECK_RC(RTMemCacheAllocEx(hMemCache, &apv[i]), VINF_SUCCESS);
int rc;
if (*(bool *)pvUser)
return VERR_RESOURCE_BUSY;
return VINF_SUCCESS;
static void tst2(void)
bool fFail = false;
RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&g_hMemCache, 256, cObjects, 32, tst2Ctor, tst2Dtor, &fFail, 0 /*fFlags*/), VINF_SUCCESS);
fFail = true;
fFail = false;
if (iLoop == 0)
while (!g_fTst3Stop)
while (!g_fTst3Stop)
while (!g_fTst3Stop)
return VINF_SUCCESS;
RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&g_hMemCache, cbObject, 0 /*cbAlignment*/, UINT32_MAX, NULL, NULL, NULL, 0 /*fFlags*/), VINF_SUCCESS);
RTTESTI_CHECK_RC_RETV(RTCacheCreate(&g_pOldCacheTst3, 0, cbObject, RTOBJCACHE_PROTECT_INSERT | RTOBJCACHE_PROTECT_REQUEST), VINF_SUCCESS);
if (rc)
return rc;
tst1();
tst2();
if (RTTestIErrorCount() == 0)