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