handletablectx.cpp revision 8429669b672301e12d6ddab8bc9ce0618d930d2e
/* $Id$ */
/** @file
* IPRT - Handle Tables.
*/
/*
* Copyright (C) 2008 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/handletable.h>
#include <iprt/spinlock.h>
#include "handletable.h"
RTDECL(int) RTHandleTableAllocWithCtx(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, uint32_t *ph)
{
/* validate the input */
/*
* Allocation loop.
*/
int rc;
do
{
/*
* Try grab a free entry from the head of the free list.
*/
if (i != NIL_RTHT_INDEX)
{
else
pThis->cCurAllocated++;
/*
* Setup the entry and return.
*/
rc = VINF_SUCCESS;
}
/*
* Must expand the handle table, unless it's full.
*/
{
}
else
{
/*
* Do we have to expand the 1st level table too?
*/
: 0;
/* leave the lock (never do fancy stuff from behind a spinlock). */
/*
* Do the allocation(s).
*/
rc = VERR_TRY_AGAIN;
void **papvLevel1 = NULL;
if (cLevel1)
{
if (!papvLevel1)
return VERR_NO_MEMORY;
}
if (!paTable)
{
return VERR_NO_MEMORY;
}
/* re-enter the lock. */
/*
* Insert the new bits, but be a bit careful as someone might have
* raced us expanding the table.
*/
/* deal with the 1st level lookup expansion first */
if (cLevel1)
{
{
/* Replace the 1st level table. */
}
/* free the obsolete one (outside the lock of course) */
}
/* insert the table we allocated. */
{
/* link all entries into a free list. */
{
}
/* join the free list with the other. */
else
{
}
}
else
{
/* free the table (raced someone, and we lost). */
}
rc = VERR_TRY_AGAIN;
}
} while (rc == VERR_TRY_AGAIN);
return rc;
}
{
/* validate the input */
/* acquire the lock */
/*
* Perform the lookup and retaining.
*/
{
if (!RTHT_IS_FREE(pvObj))
{
{
if (RT_FAILURE(rc))
}
}
else
}
/* release the lock */
return pvObj;
}
{
/* validate the input */
/* acquire the lock */
/*
* Perform the lookup and retaining.
*/
{
if (!RTHT_IS_FREE(pvObj))
{
{
if (RT_FAILURE(rc))
}
/*
* Link it into the free list.
*/
if (pvObj)
{
else
{
RTHT_SET_FREE_IDX(pPrev, i);
}
pThis->cCurAllocated--;
}
}
else
}
/* release the lock */
return pvObj;
}