tstHandleTable.cpp revision 230bd8589bba39933ac5ec21482d6186d675e604
/* $Id$ */
/** @file
* IPRT Testcase - Handle Tables.
*/
/*
* Copyright (C) 2008 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/handletable.h>
#include <iprt/initterm.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static unsigned g_cErrors;
static DECLCALLBACK(void) tstHandleTableTest1Delete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser)
{
(*pcCalls)++;
}
static DECLCALLBACK(int) tstHandleTableTest1Retain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
{
(*pcCalls)++;
return VINF_SUCCESS;
}
static int tstHandleTableTest1(uint32_t uBase, uint32_t cMax, uint32_t cDelta, uint32_t cUnitsPerDot, bool fCallbacks, uint32_t fFlags)
{
uint32_t cRetainerCalls = 0;
int rc;
RTPrintf("tstHandleTable: TESTING RTHandleTableCreateEx(, 0");
if (RT_FAILURE(rc))
{
return 1;
}
/* fill it */
for (;; i++)
{
uint32_t h;
else
if (RT_SUCCESS(rc))
{
if (h != i)
{
g_cErrors++;
}
}
else if (rc == VERR_NO_MORE_HANDLES)
{
if (i < cMax)
{
g_cErrors++;
}
break;
}
else
{
g_cErrors++;
}
if (!(i % cUnitsPerDot))
{
RTPrintf(".");
}
}
uint32_t const c = i;
RTPrintf(" c=%#x\n", c);
if (fCallbacks && cRetainerCalls != 0)
{
RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected 0!\n", __LINE__, i, cRetainerCalls);
g_cErrors++;
}
/* look up all the entries */
cRetainerCalls = 0;
for (i = uBase; i < c; i++)
{
void *pvObj;
else
if (!pvObj)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s failed!\n", __LINE__, i, pszWithCtx);
g_cErrors++;
}
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
g_cErrors++;
}
if (!(i % cUnitsPerDot))
{
RTPrintf(".");
}
}
RTPrintf("\n");
{
RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
g_cErrors++;
}
/* remove all the entries (in order) */
cRetainerCalls = 0;
for (i = uBase; i < c; i++)
{
void *pvObj;
else
if (!pvObj)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s failed!\n", __LINE__, i, pszWithCtx);
g_cErrors++;
}
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
g_cErrors++;
}
else if ( fFlags & RTHANDLETABLE_FLAGS_CONTEXT
: RTHandleTableLookup(hHT, i))
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s succeeded after free!\n", __LINE__, i, pszWithCtx);
g_cErrors++;
}
if (!(i % cUnitsPerDot))
{
RTPrintf(".");
}
}
RTPrintf("\n");
{
RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
g_cErrors++;
}
/* do a mix of alloc, lookup and free where there is a constant of cDelta handles in the table. */
RTPrintf("tstHandleTable: TESTING Alloc,Lookup,Free mix [cDelta=%#x]..", cDelta); RTStrmFlush(g_pStdOut);
for (i = uBase; i < c * 2; i++)
{
/* alloc */
uint32_t h;
else
if (RT_FAILURE(rc))
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAlloc%s: rc=%Rrc!\n", __LINE__, i, pszWithCtx, rc);
g_cErrors++;
}
else if (h != hExpect)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAlloc%s: h=%u hExpect=%u! - abort sub-test\n", __LINE__, i, pszWithCtx, h, hExpect);
g_cErrors++;
break;
}
{
/* lookup */
{
void *pvObj;
else
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookup%s(,%u,): pvObj=%p expected %p!\n",
g_cErrors++;
}
else if ( (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookupWithCtx: succeeded with bad context\n",
g_cErrors++;
}
}
/* free */
void *pvObj;
else
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableFree%s: pvObj=%p expected %p!\n",
g_cErrors++;
}
else if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup/Free%s: succeeded after free\n",
__LINE__, i, pszWithCtx);
g_cErrors++;
}
}
if (!(i % (cUnitsPerDot * 2)))
{
RTPrintf(".");
}
}
RTPrintf("\n");
/* finally, destroy the table (note that there are 128 entries in it). */
cRetainerCalls = 0;
uint32_t cDeleteCalls = 0;
if (RT_FAILURE(rc))
{
g_cErrors++;
}
return 0;
}
typedef struct TSTHTTEST2ARGS
{
/** The handle table. */
/** The thread handle. */
/** Thread index. */
/** the max number of handles the thread should allocate. */
{
if (!pah)
{
RTPrintf("tstHandleTable: FAILURE (%d) - failed to allocate %zu bytes\n", __LINE__, sizeof(uint32_t) * cMax);
return VERR_NO_MEMORY;
}
/*
* Allocate our quota.
*/
{
if (RT_FAILURE(rc))
{
RTPrintf("tstHandleTable: FAILURE (%d) - t=%d i=%d: RTHandleTableAllocWithCtx failed, rc=%Rrc\n",
return rc;
}
}
/*
* Look them up.
*/
{
{
RTPrintf("tstHandleTable: FAILURE (%d) - t=%d i=%d: RTHandleTableLookupWithCtx failed, pvObj=%p\n",
return VERR_INTERNAL_ERROR;
}
}
/*
* Free them all.
*/
{
{
RTPrintf("tstHandleTable: FAILURE (%d) - t=%d i=%d: RTHandleTableFreeWithCtx failed, pvObj=%p\n",
return VERR_INTERNAL_ERROR;
}
}
return VINF_SUCCESS;
}
{
/*
* Create the table.
*/
int rc = RTHandleTableCreateEx(&hHT, RTHANDLETABLE_FLAGS_LOCKED | RTHANDLETABLE_FLAGS_CONTEXT, uBase, cMax, NULL, NULL);
if (RT_FAILURE(rc))
{
return 1;
}
/// @todo there must be a race somewhere in the thread code, I keep hitting a duplicate insert id here...
// Or perhaps it just barcelona B2 bugs?
RTThreadSleep(50);
/*
* Spawn the threads.
*/
{
}
{
char szName[32];
rc = RTThreadCreate(&paThread[i].hThread, tstHandleTableTest2Thread, &paThread[i], 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, szName);
if (RT_FAILURE(rc))
{
g_cErrors++;
break;
}
}
/*
* Wait for them to complete.
*/
do /** @todo Remove when RTSemEventWait (linux) has been fixed. */
{
RTThreadSleep(10);
cRunning = 0;
{
if (RT_SUCCESS(rc))
else
cRunning++;
}
} while (cRunning);
/*
* Destroy the handle table.
*/
if (RT_FAILURE(rc))
{
g_cErrors++;
}
return 0;
}
{
/*
* Init the runtime and parse the arguments.
*/
static RTGETOPTDEF const s_aOptions[] =
{
};
int ch;
switch (ch)
{
case 'b':
break;
case 'm':
break;
case 't':
if (!cThreads)
cThreads = 1;
break;
case 'h':
RTPrintf("syntax: tstHandleTable [-b <base>] [-m <max>] [-t <threads>]\n");
return 1;
case 'V':
RTPrintf("$Revision: $\n");
return 0;
default:
}
/*
* If any argument was specified, run the requested test setup.
* Otherwise run a bunch of default tests.
*/
{
if (!cMax)
cMax = 65535;
if (!cThreads)
tstHandleTableTest1(uBase, cMax, 128, cMax / 32, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
else
}
else
{
/*
* Do a simple warmup / smoke test first.
*/
tstHandleTableTest1(1, 65534, 63, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
/* Test that the retain and delete functions work. */
tstHandleTableTest1(1, 1024, 256, 256, true, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
/* check that the base works. */
tstHandleTableTest1(0x7ffff000, 65534, 4, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
tstHandleTableTest1(0xeffff000, 65534, 4, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
tstHandleTableTest1(0, 4097, 4, 256, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
tstHandleTableTest1(0, 1024, 4, 128, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
/* For testing 1st level expansion / reallocation. */
/*
* Threaded tests.
*/
}
/*
* Summary.
*/
if (!g_cErrors)
RTPrintf("tstHandleTable: SUCCESS\n");
else
return !!g_cErrors;
}