handletablesimple.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * IPRT - Handle Tables.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2008 Oracle Corporation
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * available from http://www.virtualbox.org. This file is free software;
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * you can redistribute it and/or modify it under the terms of the GNU
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * General Public License (GPL) as published by the Free Software
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * The contents of this file may alternatively be used under the terms
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * of the Common Development and Distribution License Version 1.0
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * VirtualBox OSE distribution, in which case the provisions of the
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * CDDL are applicable instead of those of the GPL.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * You may elect to license modified versions of this file under the
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * terms and conditions of either the GPL or the CDDL or both.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync/*******************************************************************************
cc66247640b520463f925a5533fc9e5de06aa982vboxsync* Header Files *
cc66247640b520463f925a5533fc9e5de06aa982vboxsync*******************************************************************************/
cc66247640b520463f925a5533fc9e5de06aa982vboxsyncRTDECL(int) RTHandleTableAlloc(RTHANDLETABLE hHandleTable, void *pvObj, uint32_t *ph)
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* validate the input */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
cc66247640b520463f925a5533fc9e5de06aa982vboxsync AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, VERR_INVALID_HANDLE);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync AssertReturn(!(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT), VERR_INVALID_FUNCTION);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync AssertReturn(!RTHT_IS_FREE(pvObj), VERR_INVALID_PARAMETER);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Allocation loop.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Try grab a free entry from the head of the free list.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync PRTHTENTRYFREE pFree = (PRTHTENTRYFREE)rtHandleTableLookupSimpleIdx(pThis, i);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync pThis->iFreeTail = pThis->iFreeHead = NIL_RTHT_INDEX;
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Setup the entry and return.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Must expand the handle table, unless it's full.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Do we have to expand the 1st level table too?
cc66247640b520463f925a5533fc9e5de06aa982vboxsync uint32_t const iLevel1 = pThis->cCur / RTHT_LEVEL2_ENTRIES;
cc66247640b520463f925a5533fc9e5de06aa982vboxsync Assert(!cLevel1 || pThis->cMax / RTHT_LEVEL2_ENTRIES >= RTHT_LEVEL1_DYN_ALLOC_THRESHOLD);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* leave the lock (never do fancy stuff from behind a spinlock). */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Do the allocation(s).
cc66247640b520463f925a5533fc9e5de06aa982vboxsync papvLevel1 = (void **)RTMemAlloc(sizeof(void *) * cLevel1);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync PRTHTENTRY paTable = (PRTHTENTRY)RTMemAlloc(sizeof(*paTable) * RTHT_LEVEL2_ENTRIES);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* re-enter the lock. */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Insert the new bits, but be a bit careful as someone might have
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * raced us expanding the table.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* deal with the 1st level lookup expansion first */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* Replace the 1st level table. */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync memcpy(papvLevel1, pThis->papvLevel1, sizeof(void *) * pThis->cLevel1);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync memset(&papvLevel1[pThis->cLevel1], 0, sizeof(void *) * (cLevel1 - pThis->cLevel1));
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* free the obsolete one (outside the lock of course) */
8429669b672301e12d6ddab8bc9ce0618d930d2evboxsync /* insert the table we allocated. */
8429669b672301e12d6ddab8bc9ce0618d930d2evboxsync uint32_t iLevel1New = pThis->cCur / RTHT_LEVEL2_ENTRIES;
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* link all entries into a free list. */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync RTHT_SET_FREE_IDX((PRTHTENTRYFREE)&paTable[i], i + 1 + pThis->cCur);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync RTHT_SET_FREE_IDX((PRTHTENTRYFREE)&paTable[RTHT_LEVEL2_ENTRIES - 1], NIL_RTHT_INDEX);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* join the free list with the other. */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync PRTHTENTRYFREE pPrev = (PRTHTENTRYFREE)rtHandleTableLookupSimpleIdx(pThis, pThis->iFreeTail);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync pThis->iFreeTail = pThis->cCur + RTHT_LEVEL2_ENTRIES - 1;
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* free the table (raced someone, and we lost). */
cc66247640b520463f925a5533fc9e5de06aa982vboxsyncRTDECL(void *) RTHandleTableLookup(RTHANDLETABLE hHandleTable, uint32_t h)
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* validate the input */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
cc66247640b520463f925a5533fc9e5de06aa982vboxsync AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, NULL);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync AssertReturn(!(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT), NULL);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* acquire the lock */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Perform the lookup and retaining.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync PRTHTENTRY pEntry = rtHandleTableLookupSimple(pThis, h);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync int rc = pThis->pfnRetain(hHandleTable, pEntry->pvObj, NULL, pThis->pvRetainUser);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* release the lock */
cc66247640b520463f925a5533fc9e5de06aa982vboxsyncRTDECL(void *) RTHandleTableFree(RTHANDLETABLE hHandleTable, uint32_t h)
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* validate the input */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
cc66247640b520463f925a5533fc9e5de06aa982vboxsync AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, NULL);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync AssertReturn(!(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT), NULL);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* acquire the lock */
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Perform the lookup and retaining.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync PRTHTENTRY pEntry = rtHandleTableLookupSimple(pThis, h);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync int rc = pThis->pfnRetain(hHandleTable, pEntry->pvObj, NULL, pThis->pvRetainUser);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync * Link it into the free list.
cc66247640b520463f925a5533fc9e5de06aa982vboxsync PRTHTENTRYFREE pPrev = (PRTHTENTRYFREE)rtHandleTableLookupSimpleIdx(pThis, pThis->iFreeTail);
cc66247640b520463f925a5533fc9e5de06aa982vboxsync /* release the lock */