VirtualBox

Changeset 36982 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
May 6, 2011 1:46:03 PM (14 years ago)
Author:
vboxsync
Message:

alloc-r0drv.cpp: Fixed bug in RTMemReallocTag when RTR0MEM_STRICT is defined - the case reusing the old block didn't take the fence into account. Fixed fence violation messages (cbReq and cb was mixed so we got the bits after the block instead of the broken fence).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r0drv/alloc-r0drv.cpp

    r36190 r36982  
    180180RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW
    181181{
    182     if (!cbNew)
     182    /* Free. */
     183    if (!cbNew && pvOld)
     184    {
    183185        RTMemFree(pvOld);
    184     else if (!pvOld)
     186        return NULL;
     187    }
     188
     189    /* Alloc. */
     190    if (!pvOld)
    185191        return RTMemAllocTag(cbNew, pszTag);
    186     else
    187     {
    188         PRTMEMHDR pHdrOld = (PRTMEMHDR)pvOld - 1;
    189         RT_ASSERT_PREEMPTIBLE();
    190 
    191         if (pHdrOld->u32Magic == RTMEMHDR_MAGIC)
     192
     193    /*
     194     * Realloc.
     195     */
     196    PRTMEMHDR pHdrOld = (PRTMEMHDR)pvOld - 1;
     197    RT_ASSERT_PREEMPTIBLE();
     198
     199    if (pHdrOld->u32Magic == RTMEMHDR_MAGIC)
     200    {
     201        PRTMEMHDR pHdrNew;
     202
     203        /* If there is sufficient space in the old block and we don't cause
     204           substantial internal fragmentation, reuse the old block. */
     205        if (   pHdrOld->cb >= cbNew + RTR0MEM_FENCE_EXTRA
     206            && pHdrOld->cb - (cbNew + RTR0MEM_FENCE_EXTRA) <= 128)
    192207        {
    193             PRTMEMHDR pHdrNew;
    194             if (pHdrOld->cb >= cbNew && pHdrOld->cb - cbNew <= 128)
    195                 return pvOld;
    196             pHdrNew = rtR0MemAlloc(cbNew + RTR0MEM_FENCE_EXTRA, 0);
    197             if (pHdrNew)
    198             {
    199                 size_t cbCopy = RT_MIN(pHdrOld->cb, pHdrNew->cb);
    200                 memcpy(pHdrNew + 1, pvOld, cbCopy);
    201 #ifdef RTR0MEM_STRICT
    202                 pHdrNew->cbReq = (uint32_t)cbNew; Assert(pHdrNew->cbReq == cbNew);
    203                 memcpy((uint8_t *)(pHdrNew + 1) + cbNew, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
    204                 AssertReleaseMsg(!memcmp((uint8_t *)(pHdrOld + 1) + pHdrOld->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
    205                                  ("pHdr=%p pvOld=%p cb=%zu cbNew=%zu\n"
    206                                   "fence:    %.*Rhxs\n"
    207                                   "expected: %.*Rhxs\n",
    208                                   pHdrOld, pvOld, pHdrOld->cb, cbNew,
    209                                   RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdrOld + 1) + pHdrOld->cb,
    210                                   RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
    211 #endif
    212                 rtR0MemFree(pHdrOld);
    213                 return pHdrNew + 1;
    214             }
     208            pHdrOld->cbReq = (uint32_t)cbNew; Assert(pHdrOld->cbReq == cbNew);
     209#ifdef RTR0MEM_STRICT
     210            memcpy((uint8_t *)(pHdrOld + 1) + cbNew, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
     211#endif
     212            return pvOld;
    215213        }
    216         else
    217             AssertMsgFailed(("pHdrOld->u32Magic=%RX32 pvOld=%p cbNew=%#zx\n", pHdrOld->u32Magic, pvOld, cbNew));
    218     }
     214
     215        /* Allocate a new block and copy over the content. */
     216        pHdrNew = rtR0MemAlloc(cbNew + RTR0MEM_FENCE_EXTRA, 0);
     217        if (pHdrNew)
     218        {
     219            size_t cbCopy = RT_MIN(pHdrOld->cb, pHdrNew->cb);
     220            memcpy(pHdrNew + 1, pvOld, cbCopy);
     221#ifdef RTR0MEM_STRICT
     222            pHdrNew->cbReq = (uint32_t)cbNew; Assert(pHdrNew->cbReq == cbNew);
     223            memcpy((uint8_t *)(pHdrNew + 1) + cbNew, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
     224            AssertReleaseMsg(!memcmp((uint8_t *)(pHdrOld + 1) + pHdrOld->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
     225                             ("pHdr=%p pvOld=%p cbReq=%u cb=%u cbNew=%zu fFlags=%#x\n"
     226                              "fence:    %.*Rhxs\n"
     227                              "expected: %.*Rhxs\n",
     228                              pHdrOld, pvOld, pHdrOld->cbReq, pHdrOld->cb, cbNew, pHdrOld->fFlags,
     229                              RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdrOld + 1) + pHdrOld->cbReq,
     230                              RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
     231#endif
     232            rtR0MemFree(pHdrOld);
     233            return pHdrNew + 1;
     234        }
     235    }
     236    else
     237        AssertMsgFailed(("pHdrOld->u32Magic=%RX32 pvOld=%p cbNew=%#zx\n", pHdrOld->u32Magic, pvOld, cbNew));
    219238
    220239    return NULL;
     
    237256#ifdef RTR0MEM_STRICT
    238257        AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
    239                          ("pHdr=%p pv=%p cb=%zu\n"
     258                         ("pHdr=%p pv=%p cbReq=%u cb=%u fFlags=%#x\n"
    240259                          "fence:    %.*Rhxs\n"
    241260                          "expected: %.*Rhxs\n",
    242                           pHdr, pv, pHdr->cb,
    243                           RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
     261                          pHdr, pv, pHdr->cbReq, pHdr->cb, pHdr->fFlags,
     262                          RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cbReq,
    244263                          RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
    245264#endif
     
    292311#ifdef RTR0MEM_STRICT
    293312        AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
    294                          ("pHdr=%p pv=%p cb=%zu\n"
     313                         ("pHdr=%p pv=%p cbReq=%u cb=%u fFlags=%#x\n"
    295314                          "fence:    %.*Rhxs\n"
    296315                          "expected: %.*Rhxs\n",
    297                           pHdr, pv, pHdr->cb,
    298                           RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
     316                          pHdr, pv, pHdr->cbReq, pHdr->cb, pHdr->fFlags,
     317                          RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cbReq,
    299318                          RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
    300319#endif
     
    393412#ifdef RTR0MEM_STRICT
    394413        AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
    395                          ("pHdr=%p pv=%p cb=%zu\n"
     414                         ("pHdr=%p pv=%p cbReq=%u cb=%u fFlags=%#x\n"
    396415                          "fence:    %.*Rhxs\n"
    397416                          "expected: %.*Rhxs\n",
    398                           pHdr, pv, pHdr->cb,
    399                           RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
     417                          pHdr, pv, pHdr->cbReq, pHdr->cb, pHdr->fFlags,
     418                          RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cbReq,
    400419                          RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
    401420#endif
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