Changeset 78334 in vbox for trunk/src/VBox
- Timestamp:
- Apr 26, 2019 7:53:32 PM (6 years ago)
- Location:
- trunk/src/VBox/Runtime/r3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/posix/rtmempage-exec-mmap-heap-posix.cpp
r76553 r78334 130 130 /** Non-null on success. */ 131 131 void *pvAlloc; 132 /** Whether the pages should be zeroed or not. */133 bool fZero;132 /** RTMEMPAGEALLOC_F_XXX. */ 133 uint32_t fFlags; 134 134 } RTHEAPPAGEALLOCARGS; 135 135 … … 234 234 235 235 /** 236 * Applies flags to an allocation. 237 * 238 * @param pv The allocation. 239 * @param cb The size of the allocation (page aligned). 240 * @param fFlags RTMEMPAGEALLOC_F_XXX. 241 */ 242 DECLINLINE(void) rtMemPagePosixApplyFlags(void *pv, size_t cb, uint32_t fFlags) 243 { 244 #ifndef RT_OS_OS2 245 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED) 246 { 247 int rc = mlock(pv, cb); 248 AssertMsg(rc == 0, ("mlock %p LB %#zx -> %d errno=%d\n", pv, cb, rc, errno)); 249 NOREF(rc); 250 } 251 252 # ifdef MADV_DONTDUMP 253 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_NO_DUMP) 254 { 255 int rc = madvise(pv, cb, MADV_DONTDUMP); 256 AssertMsg(rc == 0, ("madvice %p LB %#zx MADV_DONTDUMP -> %d errno=%d\n", pv, cb, rc, errno)); 257 NOREF(rc); 258 } 259 # endif 260 #endif 261 262 if (fFlags & RTMEMPAGEALLOC_F_ZERO) 263 RT_BZERO(pv, cb); 264 } 265 266 267 /** 236 268 * Avoids some gotos in rtHeapPageAllocFromBlock. 237 269 * … … 240 272 * @param iPage The page to start allocating at. 241 273 * @param cPages The number of pages. 242 * @param f Zero Whether to clear them.274 * @param fFlags RTMEMPAGEALLOC_F_XXX. 243 275 * @param ppv Where to return the allocation address. 244 276 */ 245 DECLINLINE(int) rtHeapPageAllocFromBlockSuccess(PRTHEAPPAGEBLOCK pBlock, uint32_t iPage, size_t cPages, bool fZero, void **ppv)277 DECLINLINE(int) rtHeapPageAllocFromBlockSuccess(PRTHEAPPAGEBLOCK pBlock, uint32_t iPage, size_t cPages, uint32_t fFlags, void **ppv) 246 278 { 247 279 PRTHEAPPAGE pHeap = pBlock->pHeap; … … 256 288 void *pv = (uint8_t *)pBlock->Core.Key + (iPage << PAGE_SHIFT); 257 289 *ppv = pv; 258 if (fZero) 259 RT_BZERO(pv, cPages << PAGE_SHIFT); 290 291 if (fFlags) 292 rtMemPagePosixApplyFlags(pv, cPages << PAGE_SHIFT, fFlags); 260 293 261 294 return VINF_SUCCESS; … … 291 324 * @param pBlock The block to allocate from. 292 325 * @param cPages The size of the allocation. 293 * @param f Zero Whether it should be zeroed or not.326 * @param fFlags RTMEMPAGEALLOC_F_XXX. 294 327 * @param ppv Where to return the allocation address on success. 295 328 */ 296 DECLINLINE(int) rtHeapPageAllocFromBlock(PRTHEAPPAGEBLOCK pBlock, size_t cPages, bool fZero, void **ppv)329 DECLINLINE(int) rtHeapPageAllocFromBlock(PRTHEAPPAGEBLOCK pBlock, size_t cPages, uint32_t fFlags, void **ppv) 297 330 { 298 331 if (pBlock->cFreePages >= cPages) … … 305 338 { 306 339 ASMBitSet(&pBlock->bmAlloc[0], iPage); 307 return rtHeapPageAllocFromBlockSuccess(pBlock, iPage, cPages, f Zero, ppv);340 return rtHeapPageAllocFromBlockSuccess(pBlock, iPage, cPages, fFlags, ppv); 308 341 } 309 342 … … 314 347 { 315 348 ASMBitSetRange(&pBlock->bmAlloc[0], iPage, iPage + cPages); 316 return rtHeapPageAllocFromBlockSuccess(pBlock, iPage, cPages, f Zero, ppv);349 return rtHeapPageAllocFromBlockSuccess(pBlock, iPage, cPages, fFlags, ppv); 317 350 } 318 351 … … 340 373 PRTHEAPPAGEBLOCK pBlock = RT_FROM_MEMBER(pNode, RTHEAPPAGEBLOCK, Core); 341 374 RTHEAPPAGEALLOCARGS *pArgs = (RTHEAPPAGEALLOCARGS *)pvUser; 342 int rc = rtHeapPageAllocFromBlock(pBlock, pArgs->cPages, pArgs->f Zero, &pArgs->pvAlloc);375 int rc = rtHeapPageAllocFromBlock(pBlock, pArgs->cPages, pArgs->fFlags, &pArgs->pvAlloc); 343 376 return RT_SUCCESS(rc) ? 1 : 0; 344 377 } … … 349 382 * 350 383 * @returns IPRT status code 351 * @param pHeap 352 * @param cPages 353 * @param pszTag 354 * @param f Zero Whether to zero the memory.355 * @param ppv 356 * 357 */ 358 static int rtHeapPageAllocLocked(PRTHEAPPAGE pHeap, size_t cPages, const char *pszTag, bool fZero, void **ppv)384 * @param pHeap The heap - locked. 385 * @param cPages The page count. 386 * @param pszTag The tag. 387 * @param fFlags RTMEMPAGEALLOC_F_XXX. 388 * @param ppv Where to return the address of the allocation 389 * on success. 390 */ 391 static int rtHeapPageAllocLocked(PRTHEAPPAGE pHeap, size_t cPages, const char *pszTag, uint32_t fFlags, void **ppv) 359 392 { 360 393 int rc; … … 366 399 if (pHeap->pHint1) 367 400 { 368 rc = rtHeapPageAllocFromBlock(pHeap->pHint1, cPages, f Zero, ppv);401 rc = rtHeapPageAllocFromBlock(pHeap->pHint1, cPages, fFlags, ppv); 369 402 if (rc != VERR_NO_MEMORY) 370 403 return rc; … … 372 405 if (pHeap->pHint2) 373 406 { 374 rc = rtHeapPageAllocFromBlock(pHeap->pHint2, cPages, f Zero, ppv);407 rc = rtHeapPageAllocFromBlock(pHeap->pHint2, cPages, fFlags, ppv); 375 408 if (rc != VERR_NO_MEMORY) 376 409 return rc; … … 388 421 Args.cPages = cPages; 389 422 Args.pvAlloc = NULL; 390 Args.f Zero = fZero;423 Args.fFlags = fFlags; 391 424 RTAvlrPVDoWithAll(&pHeap->BlockTree, true /*fFromLeft*/, rtHeapPageAllocCallback, &Args); 392 425 if (Args.pvAlloc) … … 442 475 * Grab memory from the new block (cannot fail). 443 476 */ 444 rc = rtHeapPageAllocFromBlock(pBlock, cPages, f Zero, ppv);477 rc = rtHeapPageAllocFromBlock(pBlock, cPages, fFlags, ppv); 445 478 Assert(rc == VINF_SUCCESS); 446 479 … … 456 489 * @param cPages The number of pages to allocate. 457 490 * @param pszTag The allocation tag. 458 * @param f Zero Set if the pages should be zeroed or not.491 * @param fFlags RTMEMPAGEALLOC_F_XXX. 459 492 * @param ppv Where to return the pointer to the pages. 460 493 */ 461 int RTHeapPageAlloc(PRTHEAPPAGE pHeap, size_t cPages, const char *pszTag, bool fZero, void **ppv)494 int RTHeapPageAlloc(PRTHEAPPAGE pHeap, size_t cPages, const char *pszTag, uint32_t fFlags, void **ppv) 462 495 { 463 496 /* … … 476 509 if (RT_SUCCESS(rc)) 477 510 { 478 rc = rtHeapPageAllocLocked(pHeap, cPages, pszTag, f Zero, ppv);511 rc = rtHeapPageAllocLocked(pHeap, cPages, pszTag, fFlags, ppv); 479 512 RTCritSectLeave(&pHeap->CritSect); 480 513 } … … 561 594 pHeap->pHint1 = pBlock; 562 595 596 /** @todo Add bitmaps for tracking madvice and mlock so we can undo those. */ 597 563 598 /* 564 599 * Shrink the heap. Not very efficient because of the AVL tree. … … 641 676 * @param cb The number of bytes to allocate. 642 677 * @param pszTag The tag. 643 * @param f Zero Whether to zero the memory or not.678 * @param fFlags RTMEMPAGEALLOC_F_XXX. 644 679 * @param pHeap The heap to use. 645 680 */ 646 static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, bool fZero, PRTHEAPPAGE pHeap)681 static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, uint32_t fFlags, PRTHEAPPAGE pHeap) 647 682 { 648 683 /* … … 667 702 { 668 703 AssertPtr(pv); 669 if (fZero) 670 RT_BZERO(pv, cb); 704 705 if (fFlags) 706 rtMemPagePosixApplyFlags(pv, cb, fFlags); 671 707 } 672 708 else … … 677 713 int rc = RTOnce(&g_MemPagePosixInitOnce, rtMemPagePosixInitOnce, NULL); 678 714 if (RT_SUCCESS(rc)) 679 rc = RTHeapPageAlloc(pHeap, cb >> PAGE_SHIFT, pszTag, f Zero, &pv);715 rc = RTHeapPageAlloc(pHeap, cb >> PAGE_SHIFT, pszTag, fFlags, &pv); 680 716 if (RT_FAILURE(rc)) 681 717 pv = NULL; … … 726 762 RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF 727 763 { 728 return rtMemPagePosixAlloc(cb, pszTag, false /*fZero*/, &g_MemPagePosixHeap);764 return rtMemPagePosixAlloc(cb, pszTag, 0, &g_MemPagePosixHeap); 729 765 } 730 766 … … 732 768 RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF 733 769 { 734 return rtMemPagePosixAlloc(cb, pszTag, true /*fZero*/, &g_MemPagePosixHeap); 770 return rtMemPagePosixAlloc(cb, pszTag, RTMEMPAGEALLOC_F_ZERO, &g_MemPagePosixHeap); 771 } 772 773 774 RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF 775 { 776 AssertReturn(!(fFlags & RTMEMPAGEALLOC_F_VALID_MASK), NULL); 777 return rtMemPagePosixAlloc(cb, pszTag, fFlags, 0); 735 778 } 736 779 … … 747 790 RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF 748 791 { 749 return rtMemPagePosixAlloc(cb, pszTag, false /*fZero*/, &g_MemExecPosixHeap);792 return rtMemPagePosixAlloc(cb, pszTag, 0, &g_MemExecPosixHeap); 750 793 } 751 794 -
trunk/src/VBox/Runtime/r3/posix/rtmempage-exec-mmap-posix.cpp
r76553 r78334 47 47 48 48 /** 49 * Applies flags to an allocation. 50 * 51 * @param pv The allocation. 52 * @param cb The size of the allocation (page aligned). 53 * @param fFlags RTMEMPAGEALLOC_F_XXX. 54 */ 55 DECLINLINE(void) rtMemPagePosixApplyFlags(void *pv, size_t cb, uint32_t fFlags) 56 { 57 #ifndef RT_OS_OS2 58 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED) 59 { 60 int rc = mlock(pv, cb); 61 AssertMsg(rc == 0, ("mlock %p LB %#zx -> %d errno=%d\n", pv, cb, rc, errno)); 62 NOREF(rc); 63 } 64 65 # ifdef MADV_DONTDUMP 66 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_NO_DUMP) 67 { 68 int rc = madvise(pv, cb, MADV_DONTDUMP); 69 AssertMsg(rc == 0, ("madvice %p LB %#zx MADV_DONTDUMP -> %d errno=%d\n", pv, cb, rc, errno)); 70 NOREF(rc); 71 } 72 # endif 73 #endif 74 75 if (fFlags & RTMEMPAGEALLOC_F_ZERO) 76 RT_BZERO(pv, cb); 77 } 78 79 80 /** 49 81 * Allocates memory from the specified heap. 50 82 * … … 52 84 * @param cb The number of bytes to allocate. 53 85 * @param pszTag The tag. 54 * @param f Zero Whether to zero the memory or not.86 * @param fFlags RTMEMPAGEALLOC_F_XXX. 55 87 * @param fProtExec PROT_EXEC or 0. 56 88 */ 57 static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, bool fZero, int fProtExec)89 static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, uint32_t fFlags, int fProtExec) 58 90 { 59 91 /* … … 73 105 { 74 106 AssertPtr(pv); 75 if (fZero) 76 RT_BZERO(pv, cb); 107 108 if (fFlags) 109 rtMemPagePosixApplyFlags(pv, cb, fFlags); 77 110 } 78 111 else … … 114 147 RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF 115 148 { 116 return rtMemPagePosixAlloc(cb, pszTag, false /*fZero*/, 0);149 return rtMemPagePosixAlloc(cb, pszTag, 0, 0); 117 150 } 118 151 … … 120 153 RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF 121 154 { 122 return rtMemPagePosixAlloc(cb, pszTag, true /*fZero*/, 0); 155 return rtMemPagePosixAlloc(cb, pszTag, RTMEMPAGEALLOC_F_ZERO, 0); 156 } 157 158 159 RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF 160 { 161 AssertReturn(!(fFlags & RTMEMPAGEALLOC_F_VALID_MASK), NULL); 162 return rtMemPagePosixAlloc(cb, pszTag, fFlags, 0); 123 163 } 124 164 … … 135 175 RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF 136 176 { 137 return rtMemPagePosixAlloc(cb, pszTag, false /*fZero*/, PROT_EXEC);177 return rtMemPagePosixAlloc(cb, pszTag, 0, PROT_EXEC); 138 178 } 139 179 -
trunk/src/VBox/Runtime/r3/win/alloc-win.cpp
r76553 r78334 96 96 97 97 98 RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF 99 { 100 size_t const cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE); 101 RT_NOREF_PV(pszTag); 102 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL); 103 104 #ifdef USE_VIRTUAL_ALLOC 105 void *pv = VirtualAlloc(NULL, cbAligned, MEM_COMMIT, PAGE_READWRITE); 106 #else 107 void *pv = _aligned_malloc(cbAligned, PAGE_SIZE); 108 #endif 109 AssertMsgReturn(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()), NULL); 110 111 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED) 112 { 113 BOOL const fOkay = VirtualLock(pv, cbAligned); 114 AssertMsg(fOkay, ("pv=%p cb=%d lasterr=%d\n", pv, cb, GetLastError())); 115 NOREF(fOkay); 116 } 117 118 if (fFlags & RTMEMPAGEALLOC_F_ZERO) 119 RT_BZERO(pv, cbAligned); 120 121 return pv; 122 } 123 124 98 125 RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF 99 126 {
Note:
See TracChangeset
for help on using the changeset viewer.