Changeset 65220 in vbox for trunk/src/VBox/Devices
- Timestamp:
- Jan 10, 2017 11:22:14 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/Storage/IOBufMgmt.cpp
r65219 r65220 15 15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. 16 16 */ 17 #define LOG_GROUP LOG_GROUP_IOBUFMGMT 17 18 #include <VBox/cdefs.h> 18 19 #include <VBox/err.h> 20 #include <VBox/log.h> 19 21 #include <iprt/assert.h> 20 22 #include <iprt/critsect.h> … … 22 24 #include <iprt/memsafer.h> 23 25 #include <iprt/sg.h> 26 #include <iprt/string.h> 24 27 #include <iprt/asm.h> 28 29 /** Set to verify the allocations for distinct memory areas. */ 30 //#define IOBUFMGR_VERIFY_ALLOCATIONS 1 25 31 26 32 /** The minimum bin size to create - power of two!. */ … … 83 89 /** Array of bins. */ 84 90 PIOBUFMGRBIN paBins; 91 #ifdef IOBUFMGR_VERIFY_ALLOCATIONS 92 /** Pointer to the object state (allocated/free) bitmap. */ 93 void *pbmObjState; 94 #endif 85 95 /** Array of pointer entries for the various bins - variable in size. */ 86 96 void *apvObj[1]; … … 131 141 } 132 142 143 DECLINLINE(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 152 DECLINLINE(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 133 165 /** 134 166 * Resets the bins to factory default (memory resigin in the largest bin). … … 157 189 while (cbMax) 158 190 { 159 pBin->papvFree[pBin->iFree] = pbMem;191 iobufMgrBinObjAdd(pBin, pbMem); 160 192 cbMax -= cbBin; 161 193 pbMem += cbBin; 162 pBin->iFree++;163 194 164 195 if (cbMax < cbBin) /** @todo Populate smaller bins and don't waste memory. */ … … 216 247 if (pBinCur->iFree != 0) 217 248 { 218 pBinCur->iFree--; 219 uint8_t *pbMem = (uint8_t *)pBinCur->papvFree[pBinCur->iFree]; 249 uint8_t *pbMem = (uint8_t *)iobufMgrBinObjRemove(pBinCur); 220 250 AssertPtr(pbMem); 221 251 … … 225 255 iBinCur--; 226 256 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) */ 229 258 } 230 259 231 260 /* For the last bin we will get two new memory blocks. */ 232 pBinCur->papvFree[pBinCur->iFree] = pbMem; 233 pBinCur->iFree++; 261 iobufMgrBinObjAdd(pBinCur, pbMem); 234 262 Assert(pBin == pBinCur); 235 263 break; … … 270 298 else if (pBin->iFree != 0) 271 299 { 272 pBin->iFree--; 273 pSeg->pvSeg = pBin->papvFree[pBin->iFree]; 300 pSeg->pvSeg = iobufMgrBinObjRemove(pBin); 274 301 pSeg->cbSeg = (size_t)RT_BIT_32(u32Order); 275 302 cbAlloc = pSeg->cbSeg; … … 277 304 278 305 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 279 321 } 280 322 … … 304 346 pThis->paBins = (PIOBUFMGRBIN)((uint8_t *)pThis + RT_OFFSETOF(IOBUFMGRINT, apvObj[cObjs])); 305 347 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); 307 356 if (RT_SUCCESS(rc)) 308 357 { … … 351 400 RTMemPageFree(pThis->pvMem, RT_ALIGN_Z(pThis->cbMax, _4K)); 352 401 402 #ifdef IOBUFMGR_VERIFY_ALLOCATIONS 403 AssertPtr(pThis->pbmObjState); 404 RTMemFree(pThis->pbmObjState); 405 pThis->pbmObjState = NULL; 406 #endif 407 353 408 RTCritSectLeave(&pThis->CritSectAlloc); 354 409 RTCritSectDelete(&pThis->CritSectAlloc); … … 369 424 { 370 425 PIOBUFMGRINT pThis = hIoBufMgr; 426 427 LogFlowFunc(("pThis=%#p pIoBufDesc=%#p cbIoBuf=%zu pcbIoBufAllocated=%#p\n", 428 pThis, pIoBufDesc, cbIoBuf, pcbIoBufAllocated)); 371 429 372 430 AssertPtrReturn(pThis, VERR_INVALID_HANDLE); … … 418 476 PIOBUFMGRINT pThis = pIoBufDesc->Int.pIoBufMgr; 419 477 478 LogFlowFunc(("pIoBufDesc=%#p{.cSegsUsed=%u}\n", pIoBufDesc, pIoBufDesc->Int.cSegsUsed)); 479 420 480 AssertPtr(pThis); 421 481 … … 434 494 Assert(iBin < pThis->cBins); 435 495 PIOBUFMGRBIN pBin = &pThis->paBins[iBin]; 436 pBin->papvFree[pBin->iFree] = pSeg->pvSeg; 437 pBin->iFree++; 496 iobufMgrBinObjAdd(pBin, pSeg->pvSeg); 438 497 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 439 517 } 440 518 … … 450 528 451 529 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.