handletable.cpp revision 09034c53c75eab7ea04021a749b98e7f78fae85a
/* $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>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The number of entries in the 2nd level lookup table. */
#define RTHT_LEVEL2_ENTRIES 2048
/** The number of (max) 1st level entries requiring dynamic allocation of the
* 1st level table. If the max number is below this threshold, the 1st level
* table will be allocated as part of the handle table structure. */
#define RTHT_LEVEL1_DYN_ALLOC_THRESHOLD 256
/** Checks whether a object pointer is really a free entry or not. */
/** Sets RTHTENTRYFREE::iNext. */
do { \
} while (0)
/** Gets the index part of RTHTENTRYFREE::iNext. */
/** @def NIL_RTHT_INDEX
* The NIL handle index for use in the free list. (The difference between
* 32-bit and 64-bit hosts here comes down to the shifting performed for
* RTHTENTRYFREE::iNext.) */
#if ARCH_BITS == 32
#else
# error "Missing or unsupported ARCH_BITS."
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Handle table entry, simple variant.
*/
typedef struct RTHTENTRY
{
/** The object. */
void *pvObj;
} RTHTENTRY;
/** Pointer to a handle table entry, simple variant. */
typedef RTHTENTRY *PRTHTENTRY;
/**
* Handle table entry, context variant.
*/
typedef struct RTHTENTRYCTX
{
/** The object. */
void *pvObj;
/** The context. */
void *pvCtx;
} RTHTENTRYCTX;
/** Pointer to a handle table entry, context variant. */
typedef RTHTENTRYCTX *PRTHTENTRYCTX;
/**
* Free handle table entry, shared by all variants.
*/
typedef struct RTHTENTRYFREE
{
/** The index of the next handle, special format.
* In order to distinguish free and used handle table entries we exploit
* the heap alignment and use the lower two bits to do this. Used entries
* will have these bits set to 0, while free entries will have tem set
* to 3. Use the RTHT_GET_FREE_IDX and RTHT_SET_FREE_IDX macros to access
* this field. */
/** Pointer to a free handle table entry. */
typedef RTHTENTRYFREE *PRTHTENTRYFREE;
/**
* Internal handle table structure.
*/
typedef struct RTHANDLETABLEINT
{
/** Magic value (RTHANDLETABLE_MAGIC). */
/** The handle table flags specified to RTHandleTableCreateEx. */
/** The base handle value (i.e. the first handle). */
/** The current number of handle table entries. */
/** The spinlock handle (NIL if RTHANDLETABLE_FLAGS_LOCKED wasn't used). */
/** The level one lookup table. */
void **papvLevel1;
/** The retainer callback. Can be NULL. */
/** The user argument to the retainer. */
void *pvRetainUser;
/** The max number of handles. */
/** The number of handles currently allocated. (for optimizing destruction) */
/** The current number of 1st level entries. */
/** Head of the list of free handle entires (index). */
/** Tail of the list of free handle entires (index). */
/** Pointer to an handle table structure. */
typedef RTHANDLETABLEINT *PRTHANDLETABLEINT;
/**
* Looks up a simple index.
*
* @returns Pointer to the handle table entry on success, NULL on failure.
* @param pThis The handle table structure.
* @param i The index to look up.
*/
{
{
if (paTable)
return &paTable[i % RTHT_LEVEL2_ENTRIES];
}
return NULL;
}
/**
* Looks up a simple handle.
*
* @returns Pointer to the handle table entry on success, NULL on failure.
* @param pThis The handle table structure.
* @param h The handle to look up.
*/
{
}
/**
* Looks up a context index.
*
* @returns Pointer to the handle table entry on success, NULL on failure.
* @param pThis The handle table structure.
* @param i The index to look up.
*/
{
{
if (paTable)
return &paTable[i % RTHT_LEVEL2_ENTRIES];
}
return NULL;
}
/**
* Looks up a context handle.
*
* @returns Pointer to the handle table entry on success, NULL on failure.
* @param pThis The handle table structure.
* @param h The handle to look up.
*/
{
}
/**
* Locks the handle table.
*
* @param pThis The handle table structure.
* @param pTmp The spinlock temp variable.
*/
{
{
}
}
/**
* Locks the handle table.
*
* @param pThis The handle table structure.
* @param pTmp The spinlock temp variable.
*/
{
}
RTDECL(int) RTHandleTableCreateEx(PRTHANDLETABLE phHandleTable, uint32_t fFlags, uint32_t uBase, uint32_t cMax,
{
/*
* Validate input.
*/
/*
* Adjust the cMax value so it is a multiple of the 2nd level tables.
*/
/*
* Allocate the structure, include the 1st level lookup table
* if it's below the threshold size.
*/
if (!pThis)
return VERR_NO_MEMORY;
/*
* Initialize it.
*/
else
pThis->cCurAllocated = 0;
if (fFlags & RTHANDLETABLE_FLAGS_LOCKED)
{
if (RT_FAILURE(rc))
{
return rc;
}
}
*phHandleTable = pThis;
return VINF_SUCCESS;
}
{
}
RTDECL(int) RTHandleTableDestroy(RTHANDLETABLE hHandleTable, PFNRTHANDLETABLEDELETE pfnDelete, void *pvUser)
{
/*
* Validate input, quitely ignore the NIL handle.
*/
if (hHandleTable == NIL_RTHANDLETABLE)
return VINF_SUCCESS;
/*
* Mark the thing as invalid / deleted.
* Then kill the lock.
*/
{
}
if (pfnDelete)
{
/*
* Walk all the tables looking for used handles.
*/
{
{
if (paTable)
for (uint32_t i = 0; i < RTHT_LEVEL2_ENTRIES; i++)
{
cLeft--;
}
}
}
else
{
{
if (paTable)
for (uint32_t i = 0; i < RTHT_LEVEL2_ENTRIES; i++)
{
cLeft--;
}
}
}
}
/*
* Free the memory.
*/
{
}
return VINF_SUCCESS;
}
/**
* Allocates a handle from the handle table.
*
* @returns IPRT status code, almost any.
* @retval VINF_SUCCESS on success.
* @retval VERR_NO_MEMORY if we failed to extend the handle table.
* @retval VERR_NO_MORE_HANDLES if we're out of handles.
*
* @param hHandleTable The handle to the handle table.
* @param pvObj The object to associate with the new handle.
* @param ph Where to return the handle on success.
*
* @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
*/
/**
* Looks up a handle.
*
* @returns The object pointer on success. NULL on failure.
*
* @param hHandleTable The handle to the handle table.
* @param h The handle to lookup.
*
* @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
*/
/**
* Looks up and frees a handle.
*
* @returns The object pointer on success. NULL on failure.
*
* @param hHandleTable The handle to the handle table.
* @param h The handle to lookup.
*
* @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
*/
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;
}