VirtualBox

Changeset 25066 in vbox for trunk/src/VBox/Runtime/common


Ignore:
Timestamp:
Nov 28, 2009 2:30:08 AM (15 years ago)
Author:
vboxsync
Message:

rtHeapOffsetAllocBlock: Rewrote the alignment handling so it doesn't blow up nor leaks memory.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/alloc/heapoffset.cpp

    r25065 r25066  
    537537        if (offAlign)
    538538        {
    539 #define DONT_DONATE_ALIGNMENT
    540 #ifdef DONT_DONATE_ALIGNMENT
    541539            PRTHEAPOFFSETFREE pPrev;
    542540
     
    575573            ASSERT_BLOCK_FREE(pHeapInt, pPrev);
    576574            ASSERT_BLOCK_FREE(pHeapInt, pFree);
    577 
    578 #else  /* !DONT_DONATE_ALIGNMENT */
    579             RTHEAPOFFSETFREE Free;
    580             PRTHEAPOFFSETBLOCK pPrev;
    581 
    582             offAlign = uAlignment - offAlign;
    583             if (pFree->cb - offAlign < cb)
    584                 continue;
    585 
    586             /*
    587              * Make a stack copy of the free block header and adjust the pointer.
    588              */
    589             Free = *pFree;
    590             pFree = (PRTHEAPOFFSETFREE)((uintptr_t)pFree + offAlign);
    591 
    592             /*
    593              * Donate offAlign bytes to the node in front of us.
    594              * If we're the head node, we'll have to create a fake node. We'll
    595              * mark it USED for simplicity.
    596              *
    597              * (Should this policy of donating memory to the guy in front of us
    598              * cause big 'leaks', we could create a new free node if there is room
    599              * for that.)
    600              */
    601             pPrev = RTHEAPOFF_TO_PTR_N(pHeapInt, Free.Core.offPrev, PRTHEAPOFFSETBLOCK);
    602             if (pPrev)
    603             {
    604                 AssertMsg(!RTHEAPOFFSETBLOCK_IS_FREE(pPrev), ("Impossible!\n"));
    605                 pPrev->offNext = RTHEAPOFF_TO_OFF(pHeapInt, pFree);
    606             }
    607             else
    608             {
    609                 pPrev = (PRTHEAPOFFSETBLOCK)(pHeapInt + 1);
    610                 Assert(pPrev == &pFree->Core);
    611                 pPrev->offPrev = 0;
    612                 pPrev->offNext = RTHEAPOFF_TO_OFF(pHeapInt, pFree);
    613                 pPrev->offSelf = RTHEAPOFF_TO_OFF(pHeapInt, pPrev);
    614                 pPrev->fFlags = RTHEAPOFFSETBLOCK_FLAGS_MAGIC;
    615             }
    616             pHeapInt->cbFree -= offAlign;
    617 
    618             /*
    619              * Recreate pFree in the new position and adjust the neighbors.
    620              */
    621             *pFree = Free;
    622             pFree->Core.offSelf = RTHEAPOFF_TO_OFF(pHeapInt, pFree);
    623 
    624             /* the core */
    625             if (pFree->Core.offNext)
    626                 RTHEAPOFF_TO_PTR(pHeapInt, pFree->Core.offNext, PRTHEAPOFFSETBLOCK)->offPrev = pFree->Core.offSelf;
    627             pFree->Core.offPrev = RTHEAPOFF_TO_OFF(pHeapInt, pPrev);
    628 
    629             /* the free part */
    630             pFree->cb -= offAlign;
    631             if (pFree->offNext)
    632                 RTHEAPOFF_TO_PTR(pHeapInt, pFree->offNext, PRTHEAPOFFSETFREE)->offPrev = pFree->Core.offSelf;
    633             else
    634                 pHeapInt->offFreeTail = pFree->Core.offSelf;
    635             if (pFree->offPrev)
    636                 RTHEAPOFF_TO_PTR(pHeapInt, pFree->offPrev, PRTHEAPOFFSETFREE)->offNext = pFree->Core.offSelf;
    637             else
    638                 pHeapInt->offFreeHead = pFree->Core.offSelf;
    639             ASSERT_BLOCK_FREE(pHeapInt, pFree);
    640             ASSERT_BLOCK_USED(pHeapInt, pPrev);
    641 #endif /* !DONT_DONATE_ALIGNMENT */
    642575        }
    643576
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