memcache.cpp revision 88dcc9be28281507be1764b09e862e5bc4e87805
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * IPRT - Memory Object Allocation Cache.
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2012 Oracle Corporation
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * available from http://www.virtualbox.org. This file is free software;
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * General Public License (GPL) as published by the Free Software
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * The contents of this file may alternatively be used under the terms
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * of the Common Development and Distribution License Version 1.0
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * VirtualBox OSE distribution, in which case the provisions of the
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * CDDL are applicable instead of those of the GPL.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * You may elect to license modified versions of this file under the
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * terms and conditions of either the GPL or the CDDL or both.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync/*******************************************************************************
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync* Header Files *
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync*******************************************************************************/
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync/*******************************************************************************
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync* Structures and Typedefs *
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync*******************************************************************************/
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync/** Pointer to a cache instance. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync/** Pointer to a cache page. */
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync * A free object.
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync * @remarks This only works if the objects don't have a constructor or
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync * destructor and are big enough.
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync /** Pointer to the next free object */
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync/** Pointer to a free object. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * A cache page.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * This is a page of memory that we split up in to a bunch object sized chunks
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * and hand out to the cache users. The bitmap is updated in an atomic fashion
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * so that we don't have to take any locks when freeing or allocating memory.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Pointer to the cache owning this page.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * This is used for validation purposes only. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Pointer to the next page.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * This is marked as volatile since we'll be adding new entries to the list
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * without taking any locks. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Bitmap tracking allocated blocks. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync void volatile *pbmAlloc;
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Bitmap tracking which blocks that has been thru the constructor. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync void volatile *pbmCtor;
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync /** Pointer to the object array. */
09e11be404479b989812ad9da85a16f3654ab320vboxsync /** The number of objects on this page. */
09e11be404479b989812ad9da85a16f3654ab320vboxsync /** Padding to force cFree into the next cache line. (ASSUMES CL = 64) */
09e11be404479b989812ad9da85a16f3654ab320vboxsync uint8_t abPadding[ARCH_BITS == 32 ? 64 - 6*4 : 64 - 5*8 - 4];
09e11be404479b989812ad9da85a16f3654ab320vboxsync /** The number of free objects. */
09e11be404479b989812ad9da85a16f3654ab320vboxsyncAssertCompileMemberOffset(RTMEMCACHEPAGE, cFree, 64);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Memory object cache instance.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Magic value (RTMEMCACHE_MAGIC). */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** The object size. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Object alignment. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** The per page object count. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Number of bits in the bitmap.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * @remarks This is higher or equal to cPerPage and it is aligned such that
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * the search operation will be most efficient on x86/AMD64. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** The maximum number of objects. */
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync /** Whether to the use the free list or not. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Head of the page list. */
704377ab2f9056b54fb01a500448f54b38bb3001vboxsync /** Poiner to the insertion point in the page list. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Constructor callback. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Destructor callback. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Callback argument. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /** Critical section serializing page allocation and similar. */
09e11be404479b989812ad9da85a16f3654ab320vboxsync /** The total object count. */
09e11be404479b989812ad9da85a16f3654ab320vboxsync /** The number of free objects. */
09e11be404479b989812ad9da85a16f3654ab320vboxsync /** This may point to a page with free entries. */
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync /** Stack of free items.
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync * These are marked as used in the allocation bitmaps.
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync * @todo This doesn't scale well when several threads are beating on the
36f105528f08a78e2eff142de3d160b66dffd496vboxsync * cache. Also, it totally doesn't work when the objects are too
36f105528f08a78e2eff142de3d160b66dffd496vboxsync * small. */
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync/*******************************************************************************
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync* Internal Functions *
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync*******************************************************************************/
96daf9b797345655297c03a44adb2a2cfd66538cvboxsyncstatic void rtMemCacheFreeList(RTMEMCACHEINT *pThis, PRTMEMCACHEFREEOBJ pHead);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsyncRTDECL(int) RTMemCacheCreate(PRTMEMCACHE phMemCache, size_t cbObject, size_t cbAlignment, uint32_t cMaxObjects,
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync PFNMEMCACHECTOR pfnCtor, PFNMEMCACHEDTOR pfnDtor, void *pvUser, uint32_t fFlags)
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync AssertReturn(!pfnDtor || pfnCtor, VERR_INVALID_PARAMETER);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync AssertReturn(cbObject > 0, VERR_INVALID_PARAMETER);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync AssertReturn(cbObject <= PAGE_SIZE / 8, VERR_INVALID_PARAMETER);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync AssertReturn(!((cbAlignment - 1) & cbAlignment), VERR_NOT_POWER_OF_TWO);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync AssertReturn(cbAlignment <= 64, VERR_OUT_OF_RANGE);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Allocate and initialize the instance memory.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync RTMEMCACHEINT *pThis = (RTMEMCACHEINT *)RTMemAlloc(sizeof(*pThis));
0aa150e34ed49f14aaa37368c2e6999ec89e5f43vboxsync pThis->cbObject = (uint32_t)RT_ALIGN_Z(cbObject, cbAlignment);
0aa150e34ed49f14aaa37368c2e6999ec89e5f43vboxsync pThis->cPerPage = (uint32_t)((PAGE_SIZE - RT_ALIGN_Z(sizeof(RTMEMCACHEPAGE), cbAlignment)) / pThis->cbObject);
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync pThis->fUseFreeList = cbObject >= sizeof(RTMEMCACHEFREEOBJ)
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync AssertReturn(pThis->u32Magic == RTMEMCACHE_MAGIC, VERR_INVALID_HANDLE);
704377ab2f9056b54fb01a500448f54b38bb3001vboxsync#if 0 /*def RT_STRICT - don't require eveything to be freed. Caches are very convenient for lazy cleanup. */
21a96ae495634bdc8bbcc6ed7d8d727a1b9430f5vboxsync for (PRTMEMCACHEFREEOBJ pFree = pThis->pFreeTop; pFree && cFree < pThis->cTotal + 5; pFree = pFree->pNext)
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync AssertMsg(cFree == pThis->cTotal, ("cFree=%u cTotal=%u\n", cFree, pThis->cTotal));
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Destroy it.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTMEMCACHE_MAGIC_DEAD, RTMEMCACHE_MAGIC), VERR_INVALID_HANDLE);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync while (iObj-- > 0)
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync pThis->pfnDtor(hMemCache, pPage->pbObjects + iObj * pThis->cbObject, pThis->pvUser);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Grows the cache.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * @returns IPRT status code.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * @param pThis The memory cache instance.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Enter the critical section here to avoid allocation races leading to
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * wasted memory (++) and make it easier to link in the new page.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Allocate and initialize the new page.
09e11be404479b989812ad9da85a16f3654ab320vboxsync * We put the constructor bitmap at the lower end right after cFree.
09e11be404479b989812ad9da85a16f3654ab320vboxsync * We then push the object array to the end of the page and place the
09e11be404479b989812ad9da85a16f3654ab320vboxsync * allocation bitmap below it. The hope is to increase the chance that
09e11be404479b989812ad9da85a16f3654ab320vboxsync * the allocation bitmap is in a different cache line than cFree since
09e11be404479b989812ad9da85a16f3654ab320vboxsync * this increases performance markably when lots of threads are beating
09e11be404479b989812ad9da85a16f3654ab320vboxsync * on the cache.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync PRTMEMCACHEPAGE pPage = (PRTMEMCACHEPAGE)RTMemPageAlloc(PAGE_SIZE);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync uint32_t const cObjects = RT_MIN(pThis->cPerPage, pThis->cMax - pThis->cTotal);
09e11be404479b989812ad9da85a16f3654ab320vboxsync pb = (uint8_t *)pPage + PAGE_SIZE - pThis->cbObject * cObjects;
09e11be404479b989812ad9da85a16f3654ab320vboxsync pPage->pbObjects = pb; Assert(RT_ALIGN_P(pb, pThis->cbAlignment) == pb);
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync Assert((uintptr_t)pPage->pbmCtor + pThis->cBits / 8 <= (uintptr_t)pPage->pbmAlloc);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /* Mark the bitmap padding and any unused objects as allocated. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync for (uint32_t iBit = cObjects; iBit < pThis->cBits; iBit++)
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /* Make it the hint. */
704377ab2f9056b54fb01a500448f54b38bb3001vboxsync /* Link the page in at the end of the list. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync /* Add it to the page counts. */
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Grabs a an object in a page.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * @returns New cFree value on success (0 or higher), -1 on failure.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * @param pPage Pointer to the page.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsyncDECL_FORCE_INLINE(int32_t) rtMemCacheGrabObj(PRTMEMCACHEPAGE pPage)
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsyncRTDECL(int) RTMemCacheAllocEx(RTMEMCACHE hMemCache, void **ppvObj)
5a7561300d631625bc381bcc85dd2087f34d0bf9vboxsync AssertReturn(pThis->u32Magic == RTMEMCACHE_MAGIC, VERR_INVALID_PARAMETER);
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync * Try grab a free object from the stack.
c7ff622115966b69b482bd2896662e40d823b22fvboxsync PRTMEMCACHEFREEOBJ pObj = ASMAtomicUoReadPtrT(&pThis->pFreeTop, PRTMEMCACHEFREEOBJ);
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync pObj = ASMAtomicXchgPtrT(&pThis->pFreeTop, NULL, PRTMEMCACHEFREEOBJ);
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync PRTMEMCACHEFREEOBJ pAllocRace = ASMAtomicXchgPtrT(&pThis->pFreeTop, pObj->pNext, PRTMEMCACHEFREEOBJ);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Try grab a free object at the cache level.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync uint32_t cTotal = ASMAtomicUoReadU32(&pThis->cTotal);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Grab a free object at the page level.
88dcc9be28281507be1764b09e862e5bc4e87805vboxsync PRTMEMCACHEPAGE pPage = ASMAtomicUoReadPtrT(&pThis->pPageHint, PRTMEMCACHEPAGE);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync int32_t iObj = pPage ? rtMemCacheGrabObj(pPage) : -1;
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync for (pPage = pThis->pPageHead; pPage; pPage = pPage->pNext)
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Find a free object in the allocation bitmap. Use the new cFree count
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * as a hint.
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync iObj = ASMBitFirstClear(pPage->pbmAlloc, pThis->cBits);
09e11be404479b989812ad9da85a16f3654ab320vboxsync if (!ASMAtomicBitTestAndSet(pPage->pbmAlloc, iObj))
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync void *pvObj = &pPage->pbObjects[iObj * pThis->cbObject];
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync Assert((uintptr_t)pvObj - (uintptr_t)pPage < PAGE_SIZE);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync * Call the constructor?
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync int rc = pThis->pfnCtor(hMemCache, pvObj, pThis->pvUser);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsyncRTDECL(void *) RTMemCacheAlloc(RTMEMCACHE hMemCache)
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync * Really frees one object.
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync * @param pThis The memory cache.
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync * @param pvObj The memory object to free.
96daf9b797345655297c03a44adb2a2cfd66538cvboxsyncstatic void rtMemCacheFreeOne(RTMEMCACHEINT *pThis, void *pvObj)
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync /* Note: Do *NOT* attempt to poison the object! */
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync * Find the cache page. The page structure is at the start of the page.
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync PRTMEMCACHEPAGE pPage = (PRTMEMCACHEPAGE)(((uintptr_t)pvObj) & ~(uintptr_t)PAGE_OFFSET_MASK);
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync Assert(ASMAtomicUoReadS32(&pPage->cFree) < (int32_t)pThis->cPerPage);
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync * Clear the bitmap bit and update the two object counter. Order matters!
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync uintptr_t offObj = (uintptr_t)pvObj - (uintptr_t)pPage->pbObjects;
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync AssertReturnVoid(ASMAtomicBitTestAndClear(pPage->pbmAlloc, iObj));
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync * Really frees a list of 'freed' object.
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync * @param pThis The memory cache.
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync * @param pHead The head of the list.
96daf9b797345655297c03a44adb2a2cfd66538cvboxsyncstatic void rtMemCacheFreeList(RTMEMCACHEINT *pThis, PRTMEMCACHEFREEOBJ pHead)
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsyncRTDECL(void) RTMemCacheFree(RTMEMCACHE hMemCache, void *pvObj)
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync AssertReturnVoid(pThis->u32Magic == RTMEMCACHE_MAGIC);
66b15150e35f27f9499bb0a8399e452d6a04895dvboxsync Assert(RT_ALIGN_P(pvObj, pThis->cbAlignment) == pvObj);
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync /* This is the same as the other branch, except it's not actually freed. */
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync PRTMEMCACHEPAGE pPage = (PRTMEMCACHEPAGE)(((uintptr_t)pvObj) & ~(uintptr_t)PAGE_OFFSET_MASK);
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync Assert(ASMAtomicUoReadS32(&pPage->cFree) < (int32_t)pThis->cPerPage);
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync uintptr_t offObj = (uintptr_t)pvObj - (uintptr_t)pPage->pbObjects;
1f963f01f4f9a3848e72d4748c4f36feb715dba3vboxsync AssertReturnVoid(ASMBitTest(pPage->pbmAlloc, (int32_t)iObj));
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync * Push it onto the free stack.
db2893e30b129fbadd201b8f56cd7bc1974b395dvboxsync PRTMEMCACHEFREEOBJ pObj = (PRTMEMCACHEFREEOBJ)pvObj;
96daf9b797345655297c03a44adb2a2cfd66538cvboxsync pObj->pNext = ASMAtomicXchgPtrT(&pThis->pFreeTop, NULL, PRTMEMCACHEFREEOBJ);