VirtualBox

Changeset 65220 in vbox for trunk/src/VBox/Devices


Ignore:
Timestamp:
Jan 10, 2017 11:22:14 AM (8 years ago)
Author:
vboxsync
Message:

Devices/Storage/IOBufMgmt: Some code which helped debugging the previously fixed issue

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Storage/IOBufMgmt.cpp

    r65219 r65220  
    1515 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
    1616 */
     17#define LOG_GROUP LOG_GROUP_IOBUFMGMT
    1718#include <VBox/cdefs.h>
    1819#include <VBox/err.h>
     20#include <VBox/log.h>
    1921#include <iprt/assert.h>
    2022#include <iprt/critsect.h>
     
    2224#include <iprt/memsafer.h>
    2325#include <iprt/sg.h>
     26#include <iprt/string.h>
    2427#include <iprt/asm.h>
     28
     29/** Set to verify the allocations for distinct memory areas. */
     30//#define IOBUFMGR_VERIFY_ALLOCATIONS 1
    2531
    2632/** The minimum bin size to create - power of two!. */
     
    8389    /** Array of bins. */
    8490    PIOBUFMGRBIN        paBins;
     91#ifdef IOBUFMGR_VERIFY_ALLOCATIONS
     92    /** Pointer to the object state (allocated/free) bitmap. */
     93    void                *pbmObjState;
     94#endif
    8595    /** Array of pointer entries for the various bins - variable in size. */
    8696    void               *apvObj[1];
     
    131141}
    132142
     143DECLINLINE(void) iobufMgrBinObjAdd(PIOBUFMGRBIN pBin, void *pvObj)
     144{
     145    LogFlowFunc(("pBin=%#p{.iFree=%u} pvObj=%#p\n", pBin, pBin->iFree, pvObj));
     146    AssertPtr(pvObj);
     147    pBin->papvFree[pBin->iFree] = pvObj;
     148    pBin->iFree++;
     149    LogFlowFunc(("return pBin=%#p{.iFree=%u}\n", pBin, pBin->iFree));
     150}
     151
     152DECLINLINE(void *) iobufMgrBinObjRemove(PIOBUFMGRBIN pBin)
     153{
     154    LogFlowFunc(("pBin=%#p{.iFree=%u}\n", pBin, pBin->iFree));
     155    Assert(pBin->iFree > 0);
     156
     157    pBin->iFree--;
     158    void *pvObj = pBin->papvFree[pBin->iFree];
     159    AssertPtr(pvObj);
     160
     161    LogFlowFunc(("returns pvObj=%#p pBin=%#p{.iFree=%u}\n", pvObj, pBin, pBin->iFree));
     162    return pvObj;
     163}
     164
    133165/**
    134166 * Resets the bins to factory default (memory resigin in the largest bin).
     
    157189            while (cbMax)
    158190            {
    159                 pBin->papvFree[pBin->iFree] = pbMem;
     191                iobufMgrBinObjAdd(pBin, pbMem);
    160192                cbMax -= cbBin;
    161193                pbMem += cbBin;
    162                 pBin->iFree++;
    163194
    164195                if (cbMax < cbBin) /** @todo Populate smaller bins and don't waste memory. */
     
    216247            if (pBinCur->iFree != 0)
    217248            {
    218                 pBinCur->iFree--;
    219                 uint8_t *pbMem = (uint8_t *)pBinCur->papvFree[pBinCur->iFree];
     249                uint8_t *pbMem = (uint8_t *)iobufMgrBinObjRemove(pBinCur);
    220250                AssertPtr(pbMem);
    221251
     
    225255                    iBinCur--;
    226256                    pBinCur = &pThis->paBins[iBinCur];
    227                     pBinCur->papvFree[pBinCur->iFree] = pbMem + (size_t)RT_BIT(iBinCur + pThis->u32OrderMin); /* (RT_BIT causes weird MSC warning without cast) */
    228                     pBinCur->iFree++;
     257                    iobufMgrBinObjAdd(pBinCur, pbMem + (size_t)RT_BIT(iBinCur + pThis->u32OrderMin)); /* (RT_BIT causes weird MSC warning without cast) */
    229258                }
    230259
    231260                /* For the last bin we will get two new memory blocks. */
    232                 pBinCur->papvFree[pBinCur->iFree] = pbMem;
    233                 pBinCur->iFree++;
     261                iobufMgrBinObjAdd(pBinCur, pbMem);
    234262                Assert(pBin == pBinCur);
    235263                break;
     
    270298    else if (pBin->iFree != 0)
    271299    {
    272         pBin->iFree--;
    273         pSeg->pvSeg = pBin->papvFree[pBin->iFree];
     300        pSeg->pvSeg = iobufMgrBinObjRemove(pBin);
    274301        pSeg->cbSeg = (size_t)RT_BIT_32(u32Order);
    275302        cbAlloc = pSeg->cbSeg;
     
    277304
    278305        pThis->cbFree -= cbAlloc;
     306
     307#ifdef IOBUFMGR_VERIFY_ALLOCATIONS
     308        /* Mark the objects as allocated. */
     309        uint32_t iBinStart = ((uintptr_t)pSeg->pvSeg - (uintptr_t)pThis->pvMem) / IOBUFMGR_BIN_SIZE_MIN;
     310        Assert(   !(((uintptr_t)pSeg->pvSeg - (uintptr_t)pThis->pvMem) % IOBUFMGR_BIN_SIZE_MIN)
     311               && !(pSeg->cbSeg % IOBUFMGR_BIN_SIZE_MIN));
     312        uint32_t iBinEnd = iBinStart + (pSeg->cbSeg / IOBUFMGR_BIN_SIZE_MIN);
     313        while (iBinStart < iBinEnd)
     314        {
     315            bool fState = ASMBitTestAndSet(pThis->pbmObjState, iBinStart);
     316            //LogFlowFunc(("iBinStart=%u fState=%RTbool -> true\n", iBinStart, fState));
     317            AssertMsg(!fState, ("Trying to allocate an already allocated object\n"));
     318            iBinStart++;
     319        }
     320#endif
    279321    }
    280322
     
    304346        pThis->paBins = (PIOBUFMGRBIN)((uint8_t *)pThis + RT_OFFSETOF(IOBUFMGRINT, apvObj[cObjs]));
    305347
    306         rc = RTCritSectInit(&pThis->CritSectAlloc);
     348#ifdef IOBUFMGR_VERIFY_ALLOCATIONS
     349        pThis->pbmObjState = RTMemAllocZ((cbMax / IOBUFMGR_BIN_SIZE_MIN / 8) + 1);
     350        if (!pThis->pbmObjState)
     351            rc = VERR_NO_MEMORY;
     352#endif
     353
     354        if (RT_SUCCESS(rc))
     355            rc = RTCritSectInit(&pThis->CritSectAlloc);
    307356        if (RT_SUCCESS(rc))
    308357        {
     
    351400                RTMemPageFree(pThis->pvMem, RT_ALIGN_Z(pThis->cbMax, _4K));
    352401
     402#ifdef IOBUFMGR_VERIFY_ALLOCATIONS
     403            AssertPtr(pThis->pbmObjState);
     404            RTMemFree(pThis->pbmObjState);
     405            pThis->pbmObjState = NULL;
     406#endif
     407
    353408            RTCritSectLeave(&pThis->CritSectAlloc);
    354409            RTCritSectDelete(&pThis->CritSectAlloc);
     
    369424{
    370425    PIOBUFMGRINT pThis = hIoBufMgr;
     426
     427    LogFlowFunc(("pThis=%#p pIoBufDesc=%#p cbIoBuf=%zu pcbIoBufAllocated=%#p\n",
     428                 pThis, pIoBufDesc, cbIoBuf, pcbIoBufAllocated));
    371429
    372430    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     
    418476    PIOBUFMGRINT pThis = pIoBufDesc->Int.pIoBufMgr;
    419477
     478    LogFlowFunc(("pIoBufDesc=%#p{.cSegsUsed=%u}\n", pIoBufDesc, pIoBufDesc->Int.cSegsUsed));
     479
    420480    AssertPtr(pThis);
    421481
     
    434494            Assert(iBin < pThis->cBins);
    435495            PIOBUFMGRBIN pBin = &pThis->paBins[iBin];
    436             pBin->papvFree[pBin->iFree] = pSeg->pvSeg;
    437             pBin->iFree++;
     496            iobufMgrBinObjAdd(pBin, pSeg->pvSeg);
    438497            pThis->cbFree += pSeg->cbSeg;
     498
     499#ifdef IOBUFMGR_VERIFY_ALLOCATIONS
     500            /* Mark the objects as free. */
     501            uint32_t iBinStart = ((uintptr_t)pSeg->pvSeg - (uintptr_t)pThis->pvMem) / IOBUFMGR_BIN_SIZE_MIN;
     502            Assert(   !(((uintptr_t)pSeg->pvSeg - (uintptr_t)pThis->pvMem) % IOBUFMGR_BIN_SIZE_MIN)
     503                   && !(pSeg->cbSeg % IOBUFMGR_BIN_SIZE_MIN));
     504            uint32_t iBinEnd = iBinStart + (pSeg->cbSeg / IOBUFMGR_BIN_SIZE_MIN);
     505            while (iBinStart < iBinEnd)
     506            {
     507                bool fState = ASMBitTestAndClear(pThis->pbmObjState, iBinStart);
     508                //LogFlowFunc(("iBinStart=%u fState=%RTbool -> false\n", iBinStart, fState));
     509                AssertMsg(fState, ("Trying to free a non allocated object\n"));
     510                iBinStart++;
     511            }
     512
     513            /* Poison the state. */
     514            pSeg->cbSeg = ~0;
     515            pSeg->pvSeg = (void *)~(uintptr_t)0;
     516#endif
    439517        }
    440518
     
    450528
    451529    pIoBufDesc->Int.cSegsUsed = 0;
    452 }
    453 
     530#ifdef IOBUFMGR_VERIFY_ALLOCATIONS
     531    memset(&pIoBufDesc->SgBuf, 0xff, sizeof(pIoBufDesc->SgBuf));
     532#endif
     533}
     534
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette