VirtualBox

Changeset 25059 in vbox for trunk/src/VBox/Runtime/testcase


Ignore:
Timestamp:
Nov 27, 2009 6:17:44 PM (15 years ago)
Author:
vboxsync
Message:

RTHeapOffset: Initial conversion of RTHeapSimple.

Location:
trunk/src/VBox/Runtime/testcase
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/Makefile.kmk

    r25057 r25059  
    6868        tstGetOpt \
    6969        tstHandleTable \
     70        tstRTHeapOffset \
    7071        tstRTHeapSimple \
    7172        tstInlineAsm \
     
    201202tstHandleTable_SOURCES = tstHandleTable.cpp
    202203
     204tstRTHeapOffset_TEMPLATE = VBOXR3TSTEXE
     205tstRTHeapOffset_SOURCES = tstRTHeapOffset.cpp
     206
    203207tstRTHeapSimple_TEMPLATE = VBOXR3TSTEXE
    204208tstRTHeapSimple_SOURCES = tstRTHeapSimple.cpp
  • trunk/src/VBox/Runtime/testcase/tstRTHeapOffset.cpp

    r25057 r25059  
    11/* $Id$ */
    22/** @file
    3  * IPRT Testcase - Simple Heap.
     3 * IPRT Testcase - Offset Based Heap.
    44 */
    55
    66/*
    7  * Copyright (C) 2006-2007 Sun Microsystems, Inc.
     7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    4949     */
    5050    RTTEST hTest;
    51     int rc = RTTestInitAndCreate("tstRTHeapSimple", &hTest);
     51    int rc = RTTestInitAndCreate("tstRTHeapOffset", &hTest);
    5252    if (rc)
    5353        return rc;
     
    5959    RTTestSub(hTest, "Basics");
    6060    static uint8_t s_abMem[128*1024];
    61     RTHEAPSIMPLE Heap;
    62     RTTESTI_CHECK_RC(rc = RTHeapSimpleInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1), VINF_SUCCESS);
    63     if (RT_FAILURE(rc)) 
     61    RTHEAPOFFSET Heap;
     62    RTTESTI_CHECK_RC(rc = RTHeapOffsetInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1), VINF_SUCCESS);
     63    if (RT_FAILURE(rc))
    6464        return RTTestSummaryAndDestroy(hTest);
    6565
     
    6767     * Try allocate.
    6868     */
    69     static struct TstHeapSimpleOps
     69    static struct TstHeapOffsetOps
    7070    {
    7171        size_t      cb;
     
    9898        {        16,          0,    NULL,  7 },
    9999    };
    100     unsigned i;                                   
    101     RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
    102     size_t cbBefore = RTHeapSimpleGetFreeSize(Heap);
     100    unsigned i;
     101    RTHeapOffsetDump(Heap, (PFNRTHEAPOFFSETPRINTF)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
     102    size_t cbBefore = RTHeapOffsetGetFreeSize(Heap);
    103103    static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    104104
     
    106106    for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
    107107    {
    108         s_aOps[i].pvAlloc = RTHeapSimpleAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
    109         RTTESTI_CHECK_MSG(s_aOps[i].pvAlloc, ("RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
     108        s_aOps[i].pvAlloc = RTHeapOffsetAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
     109        RTTESTI_CHECK_MSG(s_aOps[i].pvAlloc, ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
    110110        if (!s_aOps[i].pvAlloc)
    111111            return RTTestSummaryAndDestroy(hTest);
     
    113113        memset(s_aOps[i].pvAlloc, szFill[i], s_aOps[i].cb);
    114114        RTTESTI_CHECK_MSG(RT_ALIGN_P(s_aOps[i].pvAlloc, (s_aOps[i].uAlignment ? s_aOps[i].uAlignment : 8)) == s_aOps[i].pvAlloc,
    115                           ("RTHeapSimpleAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
     115                          ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
    116116        if (!s_aOps[i].pvAlloc)
    117117            return RTTestSummaryAndDestroy(hTest);
     
    124124            continue;
    125125        //RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, s_aOps[i].pvAlloc,
    126         //         s_aOps[i].cb, s_aOps[i].uAlignment, RTHeapSimpleSize(Heap, s_aOps[i].pvAlloc));
    127         size_t cbBeforeSub = RTHeapSimpleGetFreeSize(Heap);
    128         RTHeapSimpleFree(Heap, s_aOps[i].pvAlloc);
    129         size_t cbAfterSubFree = RTHeapSimpleGetFreeSize(Heap);
     126        //         s_aOps[i].cb, s_aOps[i].uAlignment, RTHeapOffsetSize(Heap, s_aOps[i].pvAlloc));
     127        size_t cbBeforeSub = RTHeapOffsetGetFreeSize(Heap);
     128        RTHeapOffsetFree(Heap, s_aOps[i].pvAlloc);
     129        size_t cbAfterSubFree = RTHeapOffsetGetFreeSize(Heap);
    130130
    131131        void *pv;
    132         pv = RTHeapSimpleAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
    133         RTTESTI_CHECK_MSG(pv, ("RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
     132        pv = RTHeapOffsetAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
     133        RTTESTI_CHECK_MSG(pv, ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
    134134        if (!pv)
    135135            return RTTestSummaryAndDestroy(hTest);
    136         //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapSimpleSize(Heap, pv),
    137         //         cbBeforeSub, cbAfterSubFree, RTHeapSimpleGetFreeSize(Heap));
     136        //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapOffsetSize(Heap, pv),
     137        //         cbBeforeSub, cbAfterSubFree, RTHeapOffsetGetFreeSize(Heap));
     138
    138139        if (pv != s_aOps[i].pvAlloc)
    139140            RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, s_aOps[i].pvAlloc, i);
    140141        s_aOps[i].pvAlloc = pv;
    141         size_t cbAfterSubAlloc = RTHeapSimpleGetFreeSize(Heap);
     142        size_t cbAfterSubAlloc = RTHeapOffsetGetFreeSize(Heap);
    142143        if (cbBeforeSub != cbAfterSubAlloc)
    143144        {
     
    147148        }
    148149    }
    149    
     150
    150151    /* make a copy of the heap and the to-be-freed list. */
    151152    static uint8_t s_abMemCopy[sizeof(s_abMem)];
    152153    memcpy(s_abMemCopy, s_abMem, sizeof(s_abMem));
    153154    uintptr_t    offDelta  = (uintptr_t)&s_abMemCopy[0] - (uintptr_t)&s_abMem[0];
    154     RTHEAPSIMPLE hHeapCopy = (RTHEAPSIMPLE)((uintptr_t)Heap + offDelta);
    155     static struct TstHeapSimpleOps s_aOpsCopy[RT_ELEMENTS(s_aOps)];
     155    RTHEAPOFFSET hHeapCopy = (RTHEAPOFFSET)((uintptr_t)Heap + offDelta);
     156    static struct TstHeapOffsetOps s_aOpsCopy[RT_ELEMENTS(s_aOps)];
    156157    memcpy(&s_aOpsCopy[0], &s_aOps[0], sizeof(s_aOps));
    157158
     
    166167                ||  !s_aOps[j].pvAlloc)
    167168                continue;
    168             //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(Heap), s_aOps[j].cb, s_aOps[j].pvAlloc);
    169             RTHeapSimpleFree(Heap, s_aOps[j].pvAlloc);
     169            //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapOffsetGetFreeSize(Heap), s_aOps[j].cb, s_aOps[j].pvAlloc);
     170            RTHeapOffsetFree(Heap, s_aOps[j].pvAlloc);
    170171            s_aOps[j].pvAlloc = NULL;
    171172            cFreed++;
     
    173174    }
    174175    RTTESTI_CHECK(cFreed == RT_ELEMENTS(s_aOps));
    175     RTTestIPrintf(RTTESTLVL_ALWAYS, "i=done free=%d\n", RTHeapSimpleGetFreeSize(Heap));
     176    RTTestIPrintf(RTTESTLVL_ALWAYS, "i=done free=%d\n", RTHeapOffsetGetFreeSize(Heap));
    176177
    177178    /* check that we're back at the right amount of free memory. */
    178     size_t cbAfter = RTHeapSimpleGetFreeSize(Heap);
     179    size_t cbAfter = RTHeapOffsetGetFreeSize(Heap);
    179180    if (cbBefore != cbAfter)
    180181    {
    181         RTTestIPrintf(RTTESTLVL_ALWAYS, 
     182        RTTestIPrintf(RTTESTLVL_ALWAYS,
    182183                      "Warning: Either we've split out an alignment chunk at the start, or we've got\n"
    183184                      "         an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
    184         RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf);
     185        RTHeapOffsetDump(Heap, (PFNRTHEAPOFFSETPRINTF)RTPrintf);
    185186    }
    186187
    187188    /* relocate and free the bits in heap2 now. */
    188     RTTestSub(hTest, "RTHeapSimpleRelocate");
    189     rc = RTHeapSimpleRelocate(hHeapCopy, offDelta);
    190     RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
    191     if (RT_FAILURE(rc))
    192     {
    193         /* free it in a specific order. */
    194         int cFreed2 = 0;
    195         for (i = 0; i < RT_ELEMENTS(s_aOpsCopy); i++)
     189    RTTestSub(hTest, "Relocated Heap");
     190    /* free it in a specific order. */
     191    int cFreed2 = 0;
     192    for (i = 0; i < RT_ELEMENTS(s_aOpsCopy); i++)
     193    {
     194        unsigned j;
     195        for (j = 0; j < RT_ELEMENTS(s_aOpsCopy); j++)
    196196        {
    197             unsigned j;
    198             for (j = 0; j < RT_ELEMENTS(s_aOpsCopy); j++)
    199             {
    200                 if (    s_aOpsCopy[j].iFreeOrder != i
    201                     ||  !s_aOpsCopy[j].pvAlloc)
    202                     continue;
    203                 //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(hHeapCopy), s_aOpsCopy[j].cb, s_aOpsCopy[j].pvAlloc);
    204                 RTHeapSimpleFree(hHeapCopy, (uint8_t *)s_aOpsCopy[j].pvAlloc + offDelta);
    205                 s_aOpsCopy[j].pvAlloc = NULL;
    206                 cFreed++;
    207             }
     197            if (    s_aOpsCopy[j].iFreeOrder != i
     198                ||  !s_aOpsCopy[j].pvAlloc)
     199                continue;
     200            //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapOffsetGetFreeSize(hHeapCopy), s_aOpsCopy[j].cb, s_aOpsCopy[j].pvAlloc);
     201            RTHeapOffsetFree(hHeapCopy, (uint8_t *)s_aOpsCopy[j].pvAlloc + offDelta);
     202            s_aOpsCopy[j].pvAlloc = NULL;
     203            cFreed2++;
    208204        }
    209         RTTESTI_CHECK(cFreed == RT_ELEMENTS(s_aOpsCopy));
    210    
    211         /* check that we're back at the right amount of free memory. */
    212         size_t cbAfterCopy = RTHeapSimpleGetFreeSize(hHeapCopy);
    213         RTTESTI_CHECK_MSG(cbAfterCopy == cbAfter, ("cbAfterCopy=%zu cbAfter=%zu\n", cbAfterCopy, cbAfter));
    214     }
     205    }
     206    RTTESTI_CHECK(cFreed2 == RT_ELEMENTS(s_aOpsCopy));
     207
     208    /* check that we're back at the right amount of free memory. */
     209    size_t cbAfterCopy = RTHeapOffsetGetFreeSize(hHeapCopy);
     210    RTTESTI_CHECK_MSG(cbAfterCopy == cbAfter, ("cbAfterCopy=%zu cbAfter=%zu\n", cbAfterCopy, cbAfter));
    215211
    216212
  • trunk/src/VBox/Runtime/testcase/tstRTHeapSimple.cpp

    r25057 r25059  
    6161    RTHEAPSIMPLE Heap;
    6262    RTTESTI_CHECK_RC(rc = RTHeapSimpleInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1), VINF_SUCCESS);
    63     if (RT_FAILURE(rc)) 
     63    if (RT_FAILURE(rc))
    6464        return RTTestSummaryAndDestroy(hTest);
    6565
     
    9898        {        16,          0,    NULL,  7 },
    9999    };
    100     unsigned i;                                   
     100    unsigned i;
    101101    RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
    102102    size_t cbBefore = RTHeapSimpleGetFreeSize(Heap);
     
    147147        }
    148148    }
    149    
     149
    150150    /* make a copy of the heap and the to-be-freed list. */
    151151    static uint8_t s_abMemCopy[sizeof(s_abMem)];
     
    179179    if (cbBefore != cbAfter)
    180180    {
    181         RTTestIPrintf(RTTESTLVL_ALWAYS, 
     181        RTTestIPrintf(RTTESTLVL_ALWAYS,
    182182                      "Warning: Either we've split out an alignment chunk at the start, or we've got\n"
    183183                      "         an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
     
    189189    rc = RTHeapSimpleRelocate(hHeapCopy, offDelta);
    190190    RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
    191     if (RT_FAILURE(rc))
     191    if (RT_SUCCESS(rc))
    192192    {
    193193        /* free it in a specific order. */
     
    204204                RTHeapSimpleFree(hHeapCopy, (uint8_t *)s_aOpsCopy[j].pvAlloc + offDelta);
    205205                s_aOpsCopy[j].pvAlloc = NULL;
    206                 cFreed++;
     206                cFreed2++;
    207207            }
    208208        }
    209         RTTESTI_CHECK(cFreed == RT_ELEMENTS(s_aOpsCopy));
    210    
     209        RTTESTI_CHECK(cFreed2 == RT_ELEMENTS(s_aOpsCopy));
     210
    211211        /* check that we're back at the right amount of free memory. */
    212212        size_t cbAfterCopy = RTHeapSimpleGetFreeSize(hHeapCopy);
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