Changeset 101151 in vbox for trunk/src/VBox/Runtime/r3/posix
- Timestamp:
- Sep 18, 2023 2:29:04 PM (20 months ago)
- svn:sync-xref-src-repo-rev:
- 159124
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/posix/rtmempage-exec-mmap-heap-posix.cpp
r101150 r101151 65 65 *********************************************************************************************************************************/ 66 66 /** Threshold at which to we switch to simply calling mmap. */ 67 #define RTMEMPAGE POSIX_MMAP_THRESHOLD_1M67 #define RTMEMPAGE_NATIVE_THRESHOLD _1M 68 68 /** The size of a heap block (power of two) - in bytes. */ 69 #define RTMEMPAGEPOSIX_BLOCK_SIZE _2M 70 71 AssertCompile(RTMEMPAGEPOSIX_BLOCK_SIZE == (RTMEMPAGEPOSIX_BLOCK_SIZE / PAGE_SIZE) * PAGE_SIZE); 69 #define RTMEMPAGE_BLOCK_SIZE _4M 70 72 71 /** The number of pages per heap block. */ 73 #define RTMEMPAGEPOSIX_BLOCK_PAGE_COUNT (RTMEMPAGEPOSIX_BLOCK_SIZE / PAGE_SIZE) 72 #define RTMEMPAGE_BLOCK_PAGE_COUNT (RTMEMPAGE_BLOCK_SIZE / PAGE_SIZE) 73 AssertCompile(RTMEMPAGE_BLOCK_SIZE == RTMEMPAGE_BLOCK_PAGE_COUNT * PAGE_SIZE); 74 74 75 75 … … 128 128 PRTHEAPPAGE pHeap; 129 129 /** Allocation bitmap. Set bits marks allocated pages. */ 130 uint32_t bmAlloc[RTMEMPAGE POSIX_BLOCK_PAGE_COUNT / 32];130 uint32_t bmAlloc[RTMEMPAGE_BLOCK_PAGE_COUNT / 32]; 131 131 /** Allocation boundrary bitmap. Set bits marks the start of 132 132 * allocations. */ 133 uint32_t bmFirst[RTMEMPAGE POSIX_BLOCK_PAGE_COUNT / 32];133 uint32_t bmFirst[RTMEMPAGE_BLOCK_PAGE_COUNT / 32]; 134 134 /** Bitmap tracking pages where RTMEMPAGEALLOC_F_ADVISE_LOCKED has been 135 135 * successfully applied. */ 136 uint32_t bmLockedAdviced[RTMEMPAGE POSIX_BLOCK_PAGE_COUNT / 32];136 uint32_t bmLockedAdviced[RTMEMPAGE_BLOCK_PAGE_COUNT / 32]; 137 137 /** Bitmap tracking pages where RTMEMPAGEALLOC_F_ADVISE_NO_DUMP has been 138 138 * successfully applied. */ 139 uint32_t bmNoDumpAdviced[RTMEMPAGE POSIX_BLOCK_PAGE_COUNT / 32];139 uint32_t bmNoDumpAdviced[RTMEMPAGE_BLOCK_PAGE_COUNT / 32]; 140 140 } RTHEAPPAGEBLOCK; 141 141 … … 190 190 *********************************************************************************************************************************/ 191 191 /** Initialize once structure. */ 192 static RTONCE g_MemPage PosixInitOnce = RTONCE_INITIALIZER;192 static RTONCE g_MemPageHeapInitOnce = RTONCE_INITIALIZER; 193 193 /** The page heap. */ 194 static RTHEAPPAGE g_MemPage PosixHeap;194 static RTHEAPPAGE g_MemPageHeap; 195 195 /** The exec page heap. */ 196 static RTHEAPPAGE g_MemExec PosixHeap;196 static RTHEAPPAGE g_MemExecHeap; 197 197 198 198 … … 244 244 245 245 246 /** 247 * Native page allocator worker that applies advisory flags to the memory. 248 * 249 * @returns Set of flags succesfully applied 250 * @param pv The memory block address. 251 * @param cb The size of the memory block. 252 * @param fFlags The flags to apply (may include other flags too, ignore). 253 */ 246 254 DECLHIDDEN(uint32_t) rtMemPageNativeApplyFlags(void *pv, size_t cb, uint32_t fFlags) 247 255 { … … 274 282 275 283 284 /** 285 * Reverts flags previously applied by rtMemPageNativeApplyFlags(). 286 * 287 * @param pv The memory block address. 288 * @param cb The size of the memory block. 289 * @param fFlags The flags to revert. 290 */ 276 291 DECLHIDDEN(void) rtMemPageNativeRevertFlags(void *pv, size_t cb, uint32_t fFlags) 277 292 { … … 428 443 * @param fFlags RTMEMPAGEALLOC_F_XXX. 429 444 */ 430 DECLINLINE(uint32_t) rtMemPage PosixApplyFlags(void *pv, size_t cb, uint32_t fFlags)445 DECLINLINE(uint32_t) rtMemPageApplyFlags(void *pv, size_t cb, uint32_t fFlags) 431 446 { 432 447 uint32_t fHandled = 0; … … 465 480 if (fFlags) 466 481 { 467 uint32_t fHandled = rtMemPage PosixApplyFlags(pv, cPages << PAGE_SHIFT, fFlags);482 uint32_t fHandled = rtMemPageApplyFlags(pv, cPages << PAGE_SHIFT, fFlags); 468 483 Assert(!(fHandled & ~(RTMEMPAGEALLOC_F_ADVISE_LOCKED | RTMEMPAGEALLOC_F_ADVISE_NO_DUMP))); 469 484 if (fHandled & RTMEMPAGEALLOC_F_ADVISE_LOCKED) … … 512 527 if (pBlock->cFreePages >= cPages) 513 528 { 514 int iPage = ASMBitFirstClear(&pBlock->bmAlloc[0], RTMEMPAGE POSIX_BLOCK_PAGE_COUNT);529 int iPage = ASMBitFirstClear(&pBlock->bmAlloc[0], RTMEMPAGE_BLOCK_PAGE_COUNT); 515 530 Assert(iPage >= 0); 516 531 … … 523 538 524 539 while ( iPage >= 0 525 && (unsigned)iPage <= RTMEMPAGE POSIX_BLOCK_PAGE_COUNT - cPages)540 && (unsigned)iPage <= RTMEMPAGE_BLOCK_PAGE_COUNT - cPages) 526 541 { 527 542 if (rtHeapPageIsPageRangeFree(pBlock, iPage + 1, cPages - 1)) … … 532 547 533 548 /* next */ 534 iPage = ASMBitNextSet(&pBlock->bmAlloc[0], RTMEMPAGE POSIX_BLOCK_PAGE_COUNT, iPage);535 if (iPage < 0 || (unsigned)iPage >= RTMEMPAGE POSIX_BLOCK_PAGE_COUNT - 1)549 iPage = ASMBitNextSet(&pBlock->bmAlloc[0], RTMEMPAGE_BLOCK_PAGE_COUNT, iPage); 550 if (iPage < 0 || (unsigned)iPage >= RTMEMPAGE_BLOCK_PAGE_COUNT - 1) 536 551 break; 537 iPage = ASMBitNextClear(&pBlock->bmAlloc[0], RTMEMPAGE POSIX_BLOCK_PAGE_COUNT, iPage);552 iPage = ASMBitNextClear(&pBlock->bmAlloc[0], RTMEMPAGE_BLOCK_PAGE_COUNT, iPage); 538 553 } 539 554 } … … 620 635 621 636 void *pvPages = NULL; 622 rc = rtMemPageNativeAlloc(RTMEMPAGE POSIX_BLOCK_SIZE, pHeap->fExec ? RTMEMPAGEALLOC_F_EXECUTABLE : 0, &pvPages);637 rc = rtMemPageNativeAlloc(RTMEMPAGE_BLOCK_SIZE, pHeap->fExec ? RTMEMPAGEALLOC_F_EXECUTABLE : 0, &pvPages); 623 638 624 639 RTCritSectEnter(&pHeap->CritSect); … … 631 646 RT_ZERO(*pBlock); 632 647 pBlock->Core.Key = pvPages; 633 pBlock->Core.KeyLast = (uint8_t *)pvPages + RTMEMPAGE POSIX_BLOCK_SIZE - 1;634 pBlock->cFreePages = RTMEMPAGE POSIX_BLOCK_PAGE_COUNT;648 pBlock->Core.KeyLast = (uint8_t *)pvPages + RTMEMPAGE_BLOCK_SIZE - 1; 649 pBlock->cFreePages = RTMEMPAGE_BLOCK_PAGE_COUNT; 635 650 pBlock->pHeap = pHeap; 636 651 637 652 bool fRc = RTAvlrPVInsert(&pHeap->BlockTree, &pBlock->Core); Assert(fRc); NOREF(fRc); 638 pHeap->cFreePages += RTMEMPAGE POSIX_BLOCK_PAGE_COUNT;639 pHeap->cHeapPages += RTMEMPAGE POSIX_BLOCK_PAGE_COUNT;653 pHeap->cFreePages += RTMEMPAGE_BLOCK_PAGE_COUNT; 654 pHeap->cHeapPages += RTMEMPAGE_BLOCK_PAGE_COUNT; 640 655 641 656 /* … … 668 683 AssertPtrReturn(pHeap, VERR_INVALID_HANDLE); 669 684 AssertReturn(pHeap->u32Magic == RTHEAPPAGE_MAGIC, VERR_INVALID_HANDLE); 670 AssertMsgReturn(cPages < RTMEMPAGE POSIX_BLOCK_SIZE, ("%#zx\n", cPages), VERR_OUT_OF_RANGE);685 AssertMsgReturn(cPages < RTMEMPAGE_BLOCK_SIZE, ("%#zx\n", cPages), VERR_OUT_OF_RANGE); 671 686 672 687 /* … … 695 710 { 696 711 PRTHEAPPAGEBLOCK pBlock = RT_FROM_MEMBER(pNode, RTHEAPPAGEBLOCK, Core); 697 if (pBlock->cFreePages == RTMEMPAGE POSIX_BLOCK_PAGE_COUNT)712 if (pBlock->cFreePages == RTMEMPAGE_BLOCK_PAGE_COUNT) 698 713 { 699 714 *(PRTHEAPPAGEBLOCK *)pvUser = pBlock; … … 740 755 uint32_t const iPage = (uint32_t)(((uintptr_t)pv - (uintptr_t)pBlock->Core.Key) >> PAGE_SHIFT); 741 756 /* Check the range is within the block. */ 742 bool fOk = iPage + cPages <= RTMEMPAGE POSIX_BLOCK_PAGE_COUNT;757 bool fOk = iPage + cPages <= RTMEMPAGE_BLOCK_PAGE_COUNT; 743 758 /* Check that it's the start of an allocation. */ 744 759 fOk = fOk && ASMBitTest(&pBlock->bmFirst[0], iPage); 745 760 /* Check that the range ends at an allocation boundrary. */ 746 fOk = fOk && ( iPage + cPages == RTMEMPAGE POSIX_BLOCK_PAGE_COUNT761 fOk = fOk && ( iPage + cPages == RTMEMPAGE_BLOCK_PAGE_COUNT 747 762 || ASMBitTest(&pBlock->bmFirst[0], iPage + cPages) 748 763 || !ASMBitTest(&pBlock->bmAlloc[0], iPage + cPages)); … … 778 793 * Shrink the heap. Not very efficient because of the AVL tree. 779 794 */ 780 if ( pHeap->cFreePages >= RTMEMPAGE POSIX_BLOCK_PAGE_COUNT * 3795 if ( pHeap->cFreePages >= RTMEMPAGE_BLOCK_PAGE_COUNT * 3 781 796 && pHeap->cFreePages >= pHeap->cHeapPages / 2 /* 50% free */ 782 && pHeap->cFreeCalls - pHeap->uLastMinimizeCall > RTMEMPAGE POSIX_BLOCK_PAGE_COUNT797 && pHeap->cFreeCalls - pHeap->uLastMinimizeCall > RTMEMPAGE_BLOCK_PAGE_COUNT 783 798 ) 784 799 { … … 795 810 796 811 void *pv2 = RTAvlrPVRemove(&pHeap->BlockTree, pBlock->Core.Key); Assert(pv2); NOREF(pv2); 797 pHeap->cHeapPages -= RTMEMPAGE POSIX_BLOCK_PAGE_COUNT;798 pHeap->cFreePages -= RTMEMPAGE POSIX_BLOCK_PAGE_COUNT;812 pHeap->cHeapPages -= RTMEMPAGE_BLOCK_PAGE_COUNT; 813 pHeap->cFreePages -= RTMEMPAGE_BLOCK_PAGE_COUNT; 799 814 pHeap->pHint1 = NULL; 800 815 pHeap->pHint2 = NULL; 801 816 RTCritSectLeave(&pHeap->CritSect); 802 817 803 rtMemPageNativeFree(pBlock->Core.Key, RTMEMPAGE POSIX_BLOCK_SIZE);818 rtMemPageNativeFree(pBlock->Core.Key, RTMEMPAGE_BLOCK_SIZE); 804 819 pBlock->Core.Key = pBlock->Core.KeyLast = NULL; 805 820 pBlock->cFreePages = 0; … … 814 829 } 815 830 else 816 rc = VERR_NOT_FOUND; /* Distinct return code for this so rtMemPagePosixFree and others can try alternative heaps. */831 rc = VERR_NOT_FOUND; /* Distinct return code for this so RTMemPageFree and others can try alternative heaps. */ 817 832 818 833 RTCritSectLeave(&pHeap->CritSect); … … 829 844 * @param pvUser Unused. 830 845 */ 831 static DECLCALLBACK(int) rtMemPage PosixInitOnce(void *pvUser)846 static DECLCALLBACK(int) rtMemPageInitOnce(void *pvUser) 832 847 { 833 848 NOREF(pvUser); 834 int rc = RTHeapPageInit(&g_MemPage PosixHeap, false /*fExec*/);849 int rc = RTHeapPageInit(&g_MemPageHeap, false /*fExec*/); 835 850 if (RT_SUCCESS(rc)) 836 851 { 837 rc = RTHeapPageInit(&g_MemExec PosixHeap, true /*fExec*/);852 rc = RTHeapPageInit(&g_MemExecHeap, true /*fExec*/); 838 853 if (RT_SUCCESS(rc)) 839 854 return rc; 840 RTHeapPageDelete(&g_MemPage PosixHeap);855 RTHeapPageDelete(&g_MemPageHeap); 841 856 } 842 857 return rc; … … 853 868 * @param pHeap The heap to use. 854 869 */ 855 static void *rtMemPage PosixAlloc(size_t cb, const char *pszTag, uint32_t fFlags, PRTHEAPPAGE pHeap)870 static void *rtMemPageAllocInner(size_t cb, const char *pszTag, uint32_t fFlags, PRTHEAPPAGE pHeap) 856 871 { 857 872 /* … … 866 881 */ 867 882 void *pv = NULL; /* shut up gcc */ 868 if (cb >= RTMEMPAGE POSIX_MMAP_THRESHOLD)883 if (cb >= RTMEMPAGE_NATIVE_THRESHOLD) 869 884 { 870 885 int rc = rtMemPageNativeAlloc(cb, fFlags, &pv); … … 874 889 875 890 if (fFlags) 876 rtMemPage PosixApplyFlags(pv, cb, fFlags);891 rtMemPageApplyFlags(pv, cb, fFlags); 877 892 } 878 893 else … … 881 896 else 882 897 { 883 int rc = RTOnce(&g_MemPage PosixInitOnce, rtMemPagePosixInitOnce, NULL);898 int rc = RTOnce(&g_MemPageHeapInitOnce, rtMemPageInitOnce, NULL); 884 899 if (RT_SUCCESS(rc)) 885 900 rc = RTHeapPageAlloc(pHeap, cb >> PAGE_SHIFT, pszTag, fFlags, &pv); … … 892 907 893 908 894 /** 895 * Free memory allocated by rtMemPagePosixAlloc. 896 * 897 * @param pv The address of the memory to free. 898 * @param cb The size. 899 * @param pHeap1 The most probable heap. 900 * @param pHeap2 The less probable heap. 901 */ 902 static void rtMemPagePosixFree(void *pv, size_t cb, PRTHEAPPAGE pHeap1, PRTHEAPPAGE pHeap2) 909 RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF 910 { 911 return rtMemPageAllocInner(cb, pszTag, 0, &g_MemPageHeap); 912 } 913 914 915 RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF 916 { 917 return rtMemPageAllocInner(cb, pszTag, RTMEMPAGEALLOC_F_ZERO, &g_MemPageHeap); 918 } 919 920 921 RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF 922 { 923 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL); 924 return rtMemPageAllocInner(cb, pszTag, fFlags, 925 !(fFlags & RTMEMPAGEALLOC_F_EXECUTABLE) ? &g_MemPageHeap : &g_MemExecHeap); 926 } 927 928 929 RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF 903 930 { 904 931 /* … … 915 942 * If the allocation is relatively large, we used mmap/VirtualAlloc/DosAllocMem directly. 916 943 */ 917 if (cb >= RTMEMPAGE POSIX_MMAP_THRESHOLD)944 if (cb >= RTMEMPAGE_NATIVE_THRESHOLD) 918 945 rtMemPageNativeFree(pv, cb); 919 946 else 920 947 { 921 int rc = RTHeapPageFree( pHeap1, pv, cb >> PAGE_SHIFT);948 int rc = RTHeapPageFree(&g_MemPageHeap, pv, cb >> PAGE_SHIFT); 922 949 if (rc == VERR_NOT_FOUND) 923 rc = RTHeapPageFree( pHeap2, pv, cb >> PAGE_SHIFT);950 rc = RTHeapPageFree(&g_MemExecHeap, pv, cb >> PAGE_SHIFT); 924 951 AssertRC(rc); 925 952 } 926 953 } 927 954 928 929 930 931 932 RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF933 {934 return rtMemPagePosixAlloc(cb, pszTag, 0, &g_MemPagePosixHeap);935 }936 937 938 RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF939 {940 return rtMemPagePosixAlloc(cb, pszTag, RTMEMPAGEALLOC_F_ZERO, &g_MemPagePosixHeap);941 }942 943 944 RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF945 {946 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL);947 return rtMemPagePosixAlloc(cb, pszTag, fFlags,948 !(fFlags & RTMEMPAGEALLOC_F_EXECUTABLE) ? &g_MemPagePosixHeap : &g_MemExecPosixHeap);949 }950 951 952 RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF953 {954 rtMemPagePosixFree(pv, cb, &g_MemPagePosixHeap, &g_MemExecPosixHeap);955 }956
Note:
See TracChangeset
for help on using the changeset viewer.