VirtualBox

Ignore:
Timestamp:
Aug 14, 2009 11:00:51 PM (15 years ago)
Author:
vboxsync
Message:

IPRT: Added RTHeapSimpleRelocate for saving and restoring of the heap.

File:
1 edited

Legend:

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

    r21337 r22273  
    144144    /** The typical magic (RTHEAPSIMPLE_MAGIC). */
    145145    size_t                  uMagic;
    146     /** The heap size. (This structure is not included!) */
     146    /** The heap size. (This structure is included!) */
    147147    size_t                  cbHeap;
    148148    /** Pointer to the end of the heap. */
     
    285285 * Initializes the heap.
    286286 *
    287  * @returns IPRT status code on success.
     287 * @returns IPRT status code.
    288288 * @param   pHeap       Where to store the heap anchor block on success.
    289289 * @param   pvMemory    Pointer to the heap memory.
     
    346346}
    347347RT_EXPORT_SYMBOL(RTHeapSimpleInit);
     348
     349
     350/**
     351 * Relocater the heap internal structures after copying it to a new location.
     352 * 
     353 * This can be used when loading a saved heap.
     354 * 
     355 * @returns IPRT status code.
     356 * @param   hHeap       Heap handle that has already been adjusted by to the new
     357 *                      location.  That is to say, when calling
     358 *                      RTHeapSimpleInit, the caller must note the offset of the
     359 *                      returned heap handle into the heap memory.  This offset
     360 *                      must be used when calcuating the handle value for the
     361 *                      new location.  The offset may in some cases not be zero!
     362 * @param   offDelta    The delta between the new and old location, i.e. what
     363 *                      should be added to the internal pointers.
     364 */
     365RTDECL(int) RTHeapSimpleRelocate(RTHEAPSIMPLE hHeap, uintptr_t offDelta)
     366{
     367    PRTHEAPSIMPLEINTERNAL   pHeapInt = hHeap;
     368    PRTHEAPSIMPLEFREE       pCur;
     369
     370    /*
     371     * Validate input.
     372     */
     373    AssertPtrReturn(pHeapInt, VERR_INVALID_HANDLE);
     374    AssertReturn(pHeapInt->uMagic == RTHEAPSIMPLE_MAGIC, VERR_INVALID_HANDLE);
     375    AssertMsgReturn((uintptr_t)pHeapInt - (uintptr_t)pHeapInt->pvEnd + pHeapInt->cbHeap == offDelta,
     376                    ("offDelta=%p, expected=%p\n", offDelta, (uintptr_t)pHeapInt->pvEnd - pHeapInt->cbHeap - (uintptr_t)pHeapInt),
     377                    VERR_INVALID_PARAMETER);
     378
     379    /*
     380     * Relocate the heap anchor block.
     381     */
     382#define RELOCATE_IT(var, type, offDelta)    do { if (RT_UNLIKELY((var) != NULL)) { (var) = (type)((uintptr_t)(var) + offDelta); } } while (0)
     383    RELOCATE_IT(pHeapInt->pvEnd,     void *,            offDelta);
     384    RELOCATE_IT(pHeapInt->pFreeHead, PRTHEAPSIMPLEFREE, offDelta);
     385    RELOCATE_IT(pHeapInt->pFreeTail, PRTHEAPSIMPLEFREE, offDelta);
     386
     387    /*
     388     * Walk the heap blocks.
     389     */
     390    for (pCur = PRTHEAPSIMPLEFREE(pHeapInt + 1);
     391         pCur && (uintptr_t)pCur < (uintptr_t)pHeapInt->pvEnd;
     392         pCur = (PRTHEAPSIMPLEFREE)pCur->Core.pNext)
     393    {
     394        RELOCATE_IT(pCur->Core.pNext, PRTHEAPSIMPLEBLOCK,    offDelta);
     395        RELOCATE_IT(pCur->Core.pPrev, PRTHEAPSIMPLEBLOCK,    offDelta);
     396        RELOCATE_IT(pCur->Core.pHeap, PRTHEAPSIMPLEINTERNAL, offDelta);
     397        if (RTHEAPSIMPLEBLOCK_IS_FREE(&pCur->Core))
     398        {
     399            RELOCATE_IT(pCur->pNext, PRTHEAPSIMPLEFREE, offDelta);
     400            RELOCATE_IT(pCur->pPrev, PRTHEAPSIMPLEFREE, offDelta);
     401        }
     402    }
     403#undef RELOCATE_IT
     404
     405#ifdef RTHEAPSIMPLE_STRICT
     406    /*
     407     * Give it a once over before we return.
     408     */
     409    rtHeapSimpleAssertAll(pHeapInt);
     410#endif
     411    return VINF_SUCCESS;
     412}
     413RT_EXPORT_SYMBOL(RTHeapSimpleRelocate);
    348414
    349415
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