VirtualBox

Ignore:
Timestamp:
Nov 27, 2009 3:45:49 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
55393
Message:

iprt/heap.h: Prototypes an offset based variation of the simple heap. Docs in the header only. Cleanup before code fork.

File:
1 edited

Legend:

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

    r22281 r25055  
    282282
    283283
    284 /**
    285  * Initializes the heap.
    286  *
    287  * @returns IPRT status code.
    288  * @param   pHeap       Where to store the heap anchor block on success.
    289  * @param   pvMemory    Pointer to the heap memory.
    290  * @param   cbMemory    The size of the heap memory.
    291  */
    292 RTDECL(int) RTHeapSimpleInit(PRTHEAPSIMPLE pHeap, void *pvMemory, size_t cbMemory)
     284RTDECL(int) RTHeapSimpleInit(PRTHEAPSIMPLE phHeap, void *pvMemory, size_t cbMemory)
    293285{
    294286    PRTHEAPSIMPLEINTERNAL pHeapInt;
     
    338330    pFree->cb = pHeapInt->cbFree;
    339331
    340     *pHeap = pHeapInt;
     332    *phHeap = pHeapInt;
    341333
    342334#ifdef RTHEAPSIMPLE_STRICT
     
    348340
    349341
    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  */
    365342RTDECL(int) RTHeapSimpleRelocate(RTHEAPSIMPLE hHeap, uintptr_t offDelta)
    366343{
     
    373350    AssertPtrReturn(pHeapInt, VERR_INVALID_HANDLE);
    374351    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), 
     352    AssertMsgReturn((uintptr_t)pHeapInt - (uintptr_t)pHeapInt->pvEnd + pHeapInt->cbHeap == offDelta,
     353                    ("offDelta=%p, expected=%p\n", offDelta, (uintptr_t)pHeapInt->pvEnd - pHeapInt->cbHeap - (uintptr_t)pHeapInt),
    377354                    VERR_INVALID_PARAMETER);
    378355
     
    388365     * Walk the heap blocks.
    389366     */
    390     for (pCur = (PRTHEAPSIMPLEFREE)(pHeapInt + 1); 
    391          pCur && (uintptr_t)pCur < (uintptr_t)pHeapInt->pvEnd; 
     367    for (pCur = (PRTHEAPSIMPLEFREE)(pHeapInt + 1);
     368         pCur && (uintptr_t)pCur < (uintptr_t)pHeapInt->pvEnd;
    392369         pCur = (PRTHEAPSIMPLEFREE)pCur->Core.pNext)
    393370    {
     
    409386    rtHeapSimpleAssertAll(pHeapInt);
    410387#endif
    411     return VINF_SUCCESS; 
     388    return VINF_SUCCESS;
    412389}
    413390RT_EXPORT_SYMBOL(RTHeapSimpleRelocate);
    414391
    415392
    416 
    417 /**
    418  * Allocates memory from the specified simple heap.
    419  *
    420  * @returns Pointer to the allocated memory block on success.
    421  * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.)
    422  *
    423  * @param   Heap        The heap to allocate the memory on.
    424  * @param   cb          The requested heap block size.
    425  * @param   cbAlignment The requested heap block alignment. Pass 0 for default alignment.
    426  *                      Must be a power of 2.
    427  */
    428 RTDECL(void *) RTHeapSimpleAlloc(RTHEAPSIMPLE Heap, size_t cb, size_t cbAlignment)
    429 {
    430     PRTHEAPSIMPLEINTERNAL pHeapInt = Heap;
     393RTDECL(void *) RTHeapSimpleAlloc(RTHEAPSIMPLE hHeap, size_t cb, size_t cbAlignment)
     394{
     395    PRTHEAPSIMPLEINTERNAL pHeapInt = hHeap;
    431396    PRTHEAPSIMPLEBLOCK pBlock;
    432397
     
    463428
    464429
    465 /**
    466  * Allocates zeroed memory from the specified simple heap.
    467  *
    468  * @returns Pointer to the allocated memory block on success.
    469  * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.)
    470  *
    471  * @param   Heap        The heap to allocate the memory on.
    472  * @param   cb          The requested heap block size.
    473  * @param   cbAlignment The requested heap block alignment. Pass 0 for default alignment.
    474  *                      Must be a power of 2.
    475  */
    476 RTDECL(void *) RTHeapSimpleAllocZ(RTHEAPSIMPLE Heap, size_t cb, size_t cbAlignment)
    477 {
    478     PRTHEAPSIMPLEINTERNAL pHeapInt = Heap;
     430RTDECL(void *) RTHeapSimpleAllocZ(RTHEAPSIMPLE hHeap, size_t cb, size_t cbAlignment)
     431{
     432    PRTHEAPSIMPLEINTERNAL pHeapInt = hHeap;
    479433    PRTHEAPSIMPLEBLOCK pBlock;
    480434
     
    519473 * @returns Pointer to the allocated block.
    520474 * @returns NULL on failure.
     475 *
    521476 * @param   pHeapInt    The heap.
    522477 * @param   cb          Size of the memory block to allocate.
     
    686641
    687642
    688 
    689 
    690 /**
    691  * Frees memory allocated from a simple heap.
    692  *
    693  * @param   Heap    The heap. This is optional and will only be used for strict assertions.
    694  * @param   pv      The heap block returned by RTHeapSimple
    695  */
    696 RTDECL(void) RTHeapSimpleFree(RTHEAPSIMPLE Heap, void *pv)
     643RTDECL(void) RTHeapSimpleFree(RTHEAPSIMPLE hHeap, void *pv)
    697644{
    698645    PRTHEAPSIMPLEINTERNAL pHeapInt;
     
    714661    ASSERT_BLOCK_USED(pHeapInt, pBlock);
    715662    ASSERT_ANCHOR(pHeapInt);
    716     Assert(pHeapInt == (PRTHEAPSIMPLEINTERNAL)Heap || !Heap);
     663    Assert(pHeapInt == (PRTHEAPSIMPLEINTERNAL)hHeap || !hHeap);
    717664
    718665#ifdef RTHEAPSIMPLE_FREE_POISON
     
    887834
    888835
    889 /**
    890  * Gets the size of the specified heap block.
    891  *
    892  * @returns The actual size of the heap block.
    893  * @returns 0 if \a pv is NULL or it doesn't point to a valid heap block. An invalid \a pv
    894  *          can also cause traps or trigger assertions.
    895  * @param   Heap    The heap. This is optional and will only be used for strict assertions.
    896  * @param   pv      The heap block returned by RTHeapSimple
    897  */
    898 RTDECL(size_t) RTHeapSimpleSize(RTHEAPSIMPLE Heap, void *pv)
     836RTDECL(size_t) RTHeapSimpleSize(RTHEAPSIMPLE hHeap, void *pv)
    899837{
    900838    PRTHEAPSIMPLEINTERNAL pHeapInt;
     
    917855    ASSERT_BLOCK_USED(pHeapInt, pBlock);
    918856    ASSERT_ANCHOR(pHeapInt);
    919     Assert(pHeapInt == (PRTHEAPSIMPLEINTERNAL)Heap || !Heap);
     857    Assert(pHeapInt == (PRTHEAPSIMPLEINTERNAL)hHeap || !hHeap);
    920858
    921859    /*
     
    929867
    930868
    931 /**
    932  * Gets the size of the heap.
    933  *
    934  * This size includes all the internal heap structures. So, even if the heap is
    935  * empty the RTHeapSimpleGetFreeSize() will never reach the heap size returned
    936  * by this function.
    937  *
    938  * @returns The heap size.
    939  * @returns 0 if heap was safely detected as being bad.
    940  * @param   Heap    The heap.
    941  */
    942 RTDECL(size_t) RTHeapSimpleGetHeapSize(RTHEAPSIMPLE Heap)
     869RTDECL(size_t) RTHeapSimpleGetHeapSize(RTHEAPSIMPLE hHeap)
    943870{
    944871    PRTHEAPSIMPLEINTERNAL pHeapInt;
    945872
    946     if (Heap == NIL_RTHEAPSIMPLE)
     873    if (hHeap == NIL_RTHEAPSIMPLE)
    947874        return 0;
    948875
    949     pHeapInt = Heap;
     876    pHeapInt = hHeap;
    950877    AssertPtrReturn(pHeapInt, 0);
    951878    ASSERT_ANCHOR(pHeapInt);
     
    955882
    956883
    957 /**
    958  * Returns the sum of all free heap blocks.
    959  *
    960  * This is the amount of memory you can theoretically allocate
    961  * if you do allocations exactly matching the free blocks.
    962  *
    963  * @returns The size of the free blocks.
    964  * @returns 0 if heap was safely detected as being bad.
    965  * @param   Heap    The heap.
    966  */
    967 RTDECL(size_t) RTHeapSimpleGetFreeSize(RTHEAPSIMPLE Heap)
     884RTDECL(size_t) RTHeapSimpleGetFreeSize(RTHEAPSIMPLE hHeap)
    968885{
    969886    PRTHEAPSIMPLEINTERNAL pHeapInt;
    970887
    971     if (Heap == NIL_RTHEAPSIMPLE)
     888    if (hHeap == NIL_RTHEAPSIMPLE)
    972889        return 0;
    973890
    974     pHeapInt = Heap;
     891    pHeapInt = hHeap;
    975892    AssertPtrReturn(pHeapInt, 0);
    976893    ASSERT_ANCHOR(pHeapInt);
     
    980897
    981898
    982 /**
    983  * Dumps the hypervisor heap.
    984  *
    985  * @param   Heap        The heap handle.
    986  * @param   pfnPrintf   Printf like function that groks IPRT formatting.
    987  */
    988 RTDECL(void) RTHeapSimpleDump(RTHEAPSIMPLE Heap, PFNRTHEAPSIMPLEPRINTF pfnPrintf)
    989 {
    990     PRTHEAPSIMPLEINTERNAL pHeapInt = (PRTHEAPSIMPLEINTERNAL)Heap;
     899RTDECL(void) RTHeapSimpleDump(RTHEAPSIMPLE hHeap, PFNRTHEAPSIMPLEPRINTF pfnPrintf)
     900{
     901    PRTHEAPSIMPLEINTERNAL pHeapInt = (PRTHEAPSIMPLEINTERNAL)hHeap;
    991902    PRTHEAPSIMPLEFREE pBlock;
    992903
    993904    pfnPrintf("**** Dumping Heap %p - cbHeap=%zx cbFree=%zx ****\n",
    994               Heap, pHeapInt->cbHeap, pHeapInt->cbFree);
     905              hHeap, pHeapInt->cbHeap, pHeapInt->cbFree);
    995906
    996907    for (pBlock = (PRTHEAPSIMPLEFREE)(pHeapInt + 1);
     
    1008919                      pBlock, (uintptr_t)pBlock - (uintptr_t)(pHeapInt + 1), pBlock->Core.pNext, pBlock->Core.pPrev, pBlock->Core.fFlags, cb);
    1009920    }
    1010     pfnPrintf("**** Done dumping Heap %p ****\n", Heap);
     921    pfnPrintf("**** Done dumping Heap %p ****\n", hHeap);
    1011922}
    1012923RT_EXPORT_SYMBOL(RTHeapSimpleDump);
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