VirtualBox

Changeset 28298 in vbox


Ignore:
Timestamp:
Apr 14, 2010 12:20:39 PM (15 years ago)
Author:
vboxsync
Message:

iprt,Config.kmk: Make sure the RTMemAllocVar* alignment gets poisoned by the electric fence. Some interface refactoring.

Location:
trunk
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/Config.kmk

    r28272 r28298  
    13901390# The kprofile build automagically links with kProfile.
    13911391ifeq ($(KBUILD_TYPE),kprofile)
    1392  LIB_RUNTIME    += $(LIB_KPROFILE)
    1393 endif
    1394 
    1395 ## Enable C++ electric fence heap. (Warning: will easily run out of virtual memory / kernel stuff on Linux/Windows.)
    1396 #LIB_RUNTIME := $(LIB_RUNTIME_EF) $(LIB_RUNTIME)
     1392 LIB_RUNTIME += $(LIB_KPROFILE)
     1393endif
     1394
     1395# Enable C++ electric fence heap. (Warning: will easily run out of virtual memory / kernel stuff on Linux/Windows.)
     1396ifdef VBOX_WITH_CPP_EF
     1397 LIB_RUNTIME <= $(LIB_RUNTIME_EF)
     1398endif
    13971399
    13981400# Overridable libraries (unix).
  • trunk/include/iprt/mem.h

    r28271 r28298  
    357357 * @param   cb      Size in bytes of the memory block to allocate.
    358358 */
    359 RTDECL(void *)  RTMemEfTmpAlloc(size_t cb) RT_NO_THROW;
     359RTDECL(void *)  RTMemEfTmpAlloc(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW;
    360360
    361361/**
     
    366366 * @param   cb      Size in bytes of the memory block to allocate.
    367367 */
    368 RTDECL(void *)  RTMemEfTmpAllocZ(size_t cb) RT_NO_THROW;
     368RTDECL(void *)  RTMemEfTmpAllocZ(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW;
    369369
    370370/**
     
    373373 * @param   pv      Pointer to memory block.
    374374 */
    375 RTDECL(void)    RTMemEfTmpFree(void *pv) RT_NO_THROW;
     375RTDECL(void)    RTMemEfTmpFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW;
    376376
    377377/**
     
    382382 * @param   cb      Size in bytes of the memory block to allocate.
    383383 */
    384 RTDECL(void *)  RTMemEfAlloc(size_t cb) RT_NO_THROW;
     384RTDECL(void *)  RTMemEfAlloc(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW;
    385385
    386386/**
     
    391391 * @param   cb      Size in bytes of the memory block to allocate.
    392392 */
    393 RTDECL(void *)  RTMemEfAllocZ(size_t cb) RT_NO_THROW;
     393RTDECL(void *)  RTMemEfAllocZ(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW;
    394394
    395395/**
     
    400400 * @param   cbUnaligned Size in bytes of the memory block to allocate.
    401401 */
    402 RTDECL(void *)  RTMemEfAllocVar(size_t cbUnaligned) RT_NO_THROW;
     402RTDECL(void *)  RTMemEfAllocVar(size_t cbUnaligned, RT_SRC_POS_DECL) RT_NO_THROW;
    403403
    404404/**
     
    409409 * @param   cbUnaligned Size in bytes of the memory block to allocate.
    410410 */
    411 RTDECL(void *)  RTMemEfAllocZVar(size_t cbUnaligned) RT_NO_THROW;
     411RTDECL(void *)  RTMemEfAllocZVar(size_t cbUnaligned, RT_SRC_POS_DECL) RT_NO_THROW;
    412412
    413413/**
     
    419419 * @param   cbNew   The new block size (in bytes).
    420420 */
    421 RTDECL(void *)  RTMemEfRealloc(void *pvOld, size_t cbNew) RT_NO_THROW;
     421RTDECL(void *)  RTMemEfRealloc(void *pvOld, size_t cbNew, RT_SRC_POS_DECL) RT_NO_THROW;
    422422
    423423/**
     
    426426 * @param   pv      Pointer to memory block.
    427427 */
    428 RTDECL(void)    RTMemEfFree(void *pv) RT_NO_THROW;
     428RTDECL(void)    RTMemEfFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW;
    429429
    430430/**
     
    436436 * @param   cb      The amount of memory to duplicate.
    437437 */
    438 RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb) RT_NO_THROW;
     438RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb, RT_SRC_POS_DECL) RT_NO_THROW;
    439439
    440440/**
     
    447447 * @param   cbExtra The amount of extra memory to allocate and zero.
    448448 */
    449 RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW;
     449RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra, RT_SRC_POS_DECL) RT_NO_THROW;
    450450
    451451/** @def RTMEM_WRAP_TO_EF_APIS
    452452 * Define RTMEM_WRAP_TO_EF_APIS to wrap RTMem APIs to RTMemEf APIs.
    453453 */
    454 #ifdef RTMEM_WRAP_TO_EF_APIS
    455 # define RTMemTmpAlloc  RTMemEfTmpAlloc
    456 # define RTMemTmpAllocZ RTMemEfTmpAllocZ
    457 # define RTMemTmpFree   RTMemEfTmpFree
    458 # define RTMemAlloc     RTMemEfAlloc
    459 # define RTMemAllocZ    RTMemEfAllocZ
    460 # define RTMemAllocVar  RTMemEfAllocVar
    461 # define RTMemAllocZVar RTMemEfAllocZVar
    462 # define RTMemRealloc   RTMemEfRealloc
    463 # define RTMemFree      RTMemEfFree
    464 # define RTMemDup       RTMemEfDup
    465 # define RTMemDupEx     RTMemEfDupEx
     454#if defined(RTMEM_WRAP_TO_EF_APIS) && defined(IN_RING3)
     455# define RTMemTmpAlloc(cb)                  RTMemEfTmpAlloc((cb), RT_SRC_POS)
     456# define RTMemTmpAllocZ(cb)                 RTMemEfTmpAllocZ((cb), RT_SRC_POS)
     457# define RTMemTmpFree(pv)                   RTMemEfTmpFree((pv), RT_SRC_POS)
     458# define RTMemAlloc(cb)                     RTMemEfAlloc((cb), RT_SRC_POS)
     459# define RTMemAllocZ(cb)                    RTMemEfAllocZ((cb), RT_SRC_POS)
     460# define RTMemAllocVar(cbUnaligned)         RTMemEfAllocVar((cbUnaligned), RT_SRC_POS)
     461# define RTMemAllocZVar(cbUnaligned)        RTMemEfAllocZVar((cbUnaligned), RT_SRC_POS)
     462# define RTMemRealloc(pvOld, cbNew)         RTMemEfRealloc((pvOld), (cbNew), RT_SRC_POS)
     463# define RTMemFree(pv)                      RTMemEfFree((pv), RT_SRC_POS)
     464# define RTMemDup(pvSrc, cb)                RTMemEfDup((pvSrc), (cb), RT_SRC_POS)
     465# define RTMemDupEx(pvSrc, cbSrc, cbExtra)  RTMemEfDupEx((pvSrc), (cbSrc), (cbExtra), RT_SRC_POS)
    466466#endif
    467467#ifdef DOXYGEN_RUNNING
    468468# define RTMEM_WRAP_TO_EF_APIS
    469469#endif
     470
     471/**
     472 * Fenced drop-in replacement for RTMemTmpAlloc.
     473 * @copydoc RTMemTmpAlloc
     474 */
     475RTDECL(void *)  RTMemEfTmpAllocNP(size_t cb) RT_NO_THROW;
     476
     477/**
     478 * Fenced drop-in replacement for RTMemTmpAllocZ.
     479 * @copydoc RTMemTmpAllocZ
     480 */
     481RTDECL(void *)  RTMemEfTmpAllocZNP(size_t cb) RT_NO_THROW;
     482
     483/**
     484 * Fenced drop-in replacement for RTMemTmpFree.
     485 * @copydoc RTMemTmpFree
     486 */
     487RTDECL(void)    RTMemEfTmpFreeNP(void *pv) RT_NO_THROW;
     488
     489/**
     490 * Fenced drop-in replacement for RTMemAlloc.
     491 * @copydoc RTMemAlloc
     492 */
     493RTDECL(void *)  RTMemEfAllocNP(size_t cb) RT_NO_THROW;
     494
     495/**
     496 * Fenced drop-in replacement for RTMemAllocZ.
     497 * @copydoc RTMemAllocZ
     498 */
     499RTDECL(void *)  RTMemEfAllocZNP(size_t cb) RT_NO_THROW;
     500
     501/**
     502 * Fenced drop-in replacement for RTMemAllocVar
     503 * @copydoc RTMemAllocVar
     504 */
     505RTDECL(void *)  RTMemEfAllocVarNP(size_t cbUnaligned) RT_NO_THROW;
     506
     507/**
     508 * Fenced drop-in replacement for RTMemAllocZVar.
     509 * @copydoc RTMemAllocZVar
     510 */
     511RTDECL(void *)  RTMemEfAllocZVarNP(size_t cbUnaligned) RT_NO_THROW;
     512
     513/**
     514 * Fenced drop-in replacement for RTMemRealloc.
     515 * @copydoc RTMemRealloc
     516 */
     517RTDECL(void *)  RTMemEfReallocNP(void *pvOld, size_t cbNew) RT_NO_THROW;
     518
     519/**
     520 * Fenced drop-in replacement for RTMemFree.
     521 * @copydoc RTMemFree
     522 */
     523RTDECL(void)    RTMemEfFreeNP(void *pv) RT_NO_THROW;
     524
     525/**
     526 * Fenced drop-in replacement for RTMemDupEx.
     527 * @copydoc RTMemDupEx
     528 */
     529RTDECL(void *) RTMemEfDupNP(const void *pvSrc, size_t cb) RT_NO_THROW;
     530
     531/**
     532 * Fenced drop-in replacement for RTMemDupEx.
     533 * @copydoc RTMemDupEx
     534 */
     535RTDECL(void *) RTMemEfDupExNP(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW;
    470536
    471537/** @} */
     
    538604inline void RTMemEfAutoFree(T *aMem) RT_NO_THROW
    539605{
    540     RTMemEfFree(aMem);
     606    RTMemEfFreeNP(aMem);
    541607}
    542608
  • trunk/src/VBox/Runtime/common/alloc/alloc.cpp

    r28271 r28298  
    3939#include <iprt/string.h>
    4040
    41 #ifdef RTMEM_WRAP_TO_EF_APIS
    42 # undef RTMemAllocVar
    43 # undef RTMemAllocZVar
    44 # undef RTMemDup
    45 # undef RTMemDupEx
    46 #endif
     41#undef RTMemDup
     42#undef RTMemDupEx
    4743
    48 
    49 
    50 /**
    51  * Wrapper around RTMemAlloc for automatically aligning variable sized
    52  * allocations so that the various electric fence heaps works correctly.
    53  *
    54  * @returns See RTMemAlloc.
    55  * @param   cbUnaligned         The unaligned size.
    56  */
    57 RTDECL(void *) RTMemAllocVar(size_t cbUnaligned)
    58 {
    59     size_t cbAligned;
    60     if (cbUnaligned >= 16)
    61         cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
    62     else
    63         cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
    64     return RTMemAlloc(cbAligned);
    65 }
    66 
    67 
    68 /**
    69  * Wrapper around RTMemAllocZ for automatically aligning variable sized
    70  * allocations so that the various electric fence heaps works correctly.
    71  *
    72  * @returns See RTMemAllocZ.
    73  * @param   cbUnaligned         The unaligned size.
    74  */
    75 RTDECL(void *) RTMemAllocZVar(size_t cbUnaligned)
    76 {
    77     size_t cbAligned;
    78     if (cbUnaligned >= 16)
    79         cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
    80     else
    81         cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
    82     return RTMemAllocZ(cbAligned);
    83 }
    8444
    8545
  • trunk/src/VBox/Runtime/r0drv/alloc-r0drv.cpp

    r22174 r28298  
    4343#include "r0drv/alloc-r0drv.h"
    4444
     45#undef RTMemTmpAlloc
     46#undef RTMemTmpAllocZ
     47#undef RTMemTmpFree
     48#undef RTMemAlloc
     49#undef RTMemAllocZ
     50#undef RTMemAllocVar
     51#undef RTMemAllocZVar
     52#undef RTMemRealloc
     53#undef RTMemFree
     54#undef RTMemDup
     55#undef RTMemDupEx
     56
    4557
    4658/*******************************************************************************
     
    131143    RT_ASSERT_INTS_ON();
    132144
    133     pHdr = rtMemAlloc(cb + RTR0MEM_FENCE_EXTRA, 0);
     145    pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, 0);
    134146    if (pHdr)
    135147    {
     
    161173    RT_ASSERT_INTS_ON();
    162174
    163     pHdr = rtMemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_ZEROED);
     175    pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_ZEROED);
    164176    if (pHdr)
    165177    {
     
    178190
    179191/**
     192 * Wrapper around RTMemAlloc for automatically aligning variable sized
     193 * allocations so that the various electric fence heaps works correctly.
     194 *
     195 * @returns See RTMemAlloc.
     196 * @param   cbUnaligned         The unaligned size.
     197 */
     198RTDECL(void *) RTMemAllocVar(size_t cbUnaligned)
     199{
     200    size_t cbAligned;
     201    if (cbUnaligned >= 16)
     202        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
     203    else
     204        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
     205    return RTMemAlloc(cbAligned);
     206}
     207RT_EXPORT_SYMBOL(RTMemAllocVar);
     208
     209
     210/**
     211 * Wrapper around RTMemAllocZ for automatically aligning variable sized
     212 * allocations so that the various electric fence heaps works correctly.
     213 *
     214 * @returns See RTMemAllocZ.
     215 * @param   cbUnaligned         The unaligned size.
     216 */
     217RTDECL(void *) RTMemAllocZVar(size_t cbUnaligned)
     218{
     219    size_t cbAligned;
     220    if (cbUnaligned >= 16)
     221        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
     222    else
     223        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
     224    return RTMemAllocZ(cbAligned);
     225}
     226RT_EXPORT_SYMBOL(RTMemAllocZVar);
     227
     228
     229/**
    180230 * Reallocates memory.
    181231 *
     
    201251            if (pHdrOld->cb >= cbNew && pHdrOld->cb - cbNew <= 128)
    202252                return pvOld;
    203             pHdrNew = rtMemAlloc(cbNew + RTR0MEM_FENCE_EXTRA, 0);
     253            pHdrNew = rtR0MemAlloc(cbNew + RTR0MEM_FENCE_EXTRA, 0);
    204254            if (pHdrNew)
    205255            {
     
    217267                                  RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
    218268#endif
    219                 rtMemFree(pHdrOld);
     269                rtR0MemFree(pHdrOld);
    220270                return pHdrNew + 1;
    221271            }
     
    255305                          RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
    256306#endif
    257         rtMemFree(pHdr);
     307        rtR0MemFree(pHdr);
    258308    }
    259309    else
     
    279329#endif
    280330
    281     pHdr = rtMemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_EXEC);
     331    pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_EXEC);
    282332    if (pHdr)
    283333    {
     
    317367                          RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
    318368#endif
    319         rtMemFree(pHdr);
     369        rtR0MemFree(pHdr);
    320370    }
    321371    else
  • trunk/src/VBox/Runtime/r0drv/alloc-r0drv.h

    r20374 r28298  
    6565
    6666
    67 PRTMEMHDR   rtMemAlloc(size_t cb, uint32_t fFlags);
    68 void        rtMemFree(PRTMEMHDR pHdr);
     67PRTMEMHDR   rtR0MemAlloc(size_t cb, uint32_t fFlags);
     68void        rtR0MemFree(PRTMEMHDR pHdr);
    6969
    7070RT_C_DECLS_END
  • trunk/src/VBox/Runtime/r0drv/darwin/alloc-r0drv-darwin.cpp

    r22052 r28298  
    4545 * OS specific allocation function.
    4646 */
    47 PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
     47PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
    4848{
    4949    PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
     
    6464 * OS specific free function.
    6565 */
    66 void rtMemFree(PRTMEMHDR pHdr)
     66void rtR0MemFree(PRTMEMHDR pHdr)
    6767{
    6868    pHdr->u32Magic += 1;
  • trunk/src/VBox/Runtime/r0drv/freebsd/alloc-r0drv-freebsd.c

    r19761 r28298  
    5050
    5151
    52 PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
     52PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
    5353{
    5454    size_t cbAllocated = cb;
     
    141141
    142142
    143 void rtMemFree(PRTMEMHDR pHdr)
     143void rtR0MemFree(PRTMEMHDR pHdr)
    144144{
    145145    pHdr->u32Magic += 1;
  • trunk/src/VBox/Runtime/r0drv/linux/alloc-r0drv-linux.c

    r25642 r28298  
    115115 * OS specific allocation function.
    116116 */
    117 PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
     117PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
    118118{
    119119    /*
     
    173173 * OS specific free function.
    174174 */
    175 void rtMemFree(PRTMEMHDR pHdr)
     175void rtR0MemFree(PRTMEMHDR pHdr)
    176176{
    177177    pHdr->u32Magic += 1;
  • trunk/src/VBox/Runtime/r0drv/nt/alloc-r0drv-nt.cpp

    r14069 r28298  
    4343 * OS specific allocation function.
    4444 */
    45 PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
     45PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
    4646{
    4747    PRTMEMHDR pHdr = (PRTMEMHDR)ExAllocatePoolWithTag(NonPagedPool, cb + sizeof(*pHdr), IPRT_NT_POOL_TAG);
     
    6060 * OS specific free function.
    6161 */
    62 void rtMemFree(PRTMEMHDR pHdr)
     62void rtR0MemFree(PRTMEMHDR pHdr)
    6363{
    6464    pHdr->u32Magic += 1;
  • trunk/src/VBox/Runtime/r0drv/os2/alloc-r0drv-os2.cpp

    r8245 r28298  
    4141
    4242
    43 PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
     43PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
    4444{
    4545    void *pv = NULL;
     
    5858
    5959
    60 void rtMemFree(PRTMEMHDR pHdr)
     60void rtR0MemFree(PRTMEMHDR pHdr)
    6161{
    6262    pHdr->u32Magic += 1;
  • trunk/src/VBox/Runtime/r0drv/solaris/vbi/alloc-r0drv-solaris.c

    r27050 r28298  
    4848 * OS specific allocation function.
    4949 */
    50 PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
     50PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
    5151{
    5252    size_t cbAllocated = cb;
     
    8080 * OS specific free function.
    8181 */
    82 void rtMemFree(PRTMEMHDR pHdr)
     82void rtR0MemFree(PRTMEMHDR pHdr)
    8383{
    8484    pHdr->u32Magic += 1;
  • trunk/src/VBox/Runtime/r3/alloc-ef-cpp.cpp

    r28271 r28298  
    3434#include "alloc-ef.h"
    3535
    36 
    3736#if defined(RTALLOC_EFENCE_CPP) || defined(RTMEM_WRAP_TO_EF_APIS) /* rest of the file */
    3837
     38#include <iprt/asm.h>
    3939#include <new>
    4040
     
    6262void *RT_EF_CDECL operator new(RT_EF_SIZE_T cb) throw(std::bad_alloc)
    6363{
    64     void *pv = rtMemAlloc("new", RTMEMTYPE_NEW, cb, ((void **)&cb)[-1], 0, NULL, NULL);
     64    void *pv = rtR3MemAlloc("new", RTMEMTYPE_NEW, cb, cb, ASMReturnAddress(), NULL, 0, NULL);
    6565    if (!pv)
    6666        throw std::bad_alloc();
     
    7171void *RT_EF_CDECL operator new(RT_EF_SIZE_T cb, const std::nothrow_t &) throw()
    7272{
    73     void *pv = rtMemAlloc("new nothrow", RTMEMTYPE_NEW, cb, ((void **)&cb)[-1], 0, NULL, NULL);
     73    void *pv = rtR3MemAlloc("new nothrow", RTMEMTYPE_NEW, cb, cb, ASMReturnAddress(), NULL, 0, NULL);
    7474    return pv;
    7575}
     
    7878void RT_EF_CDECL operator delete(void *pv) throw()
    7979{
    80     rtMemFree("delete", RTMEMTYPE_DELETE, pv, ((void **)&pv)[-1], 0, NULL, NULL);
     80    rtR3MemFree("delete", RTMEMTYPE_DELETE, pv, ASMReturnAddress(), NULL, 0, NULL);
    8181}
    8282
     
    8484void RT_EF_CDECL operator delete(void * pv, const std::nothrow_t &) throw()
    8585{
    86     rtMemFree("delete nothrow", RTMEMTYPE_DELETE, pv, ((void **)&pv)[-1], 0, NULL, NULL);
     86    rtR3MemFree("delete nothrow", RTMEMTYPE_DELETE, pv, ASMReturnAddress(), NULL, 0, NULL);
    8787}
    8888
     
    9898void *RT_EF_CDECL operator new[](RT_EF_SIZE_T cb) throw(std::bad_alloc)
    9999{
    100     void *pv = rtMemAlloc("new[]", RTMEMTYPE_NEW_ARRAY, cb, ((void **)&cb)[-1], 0, NULL, NULL);
     100    void *pv = rtR3MemAlloc("new[]", RTMEMTYPE_NEW_ARRAY, cb, cb, ASMReturnAddress(), NULL, 0, NULL);
    101101    if (!pv)
    102102        throw std::bad_alloc();
     
    107107void * RT_EF_CDECL operator new[](RT_EF_SIZE_T cb, const std::nothrow_t &) throw()
    108108{
    109     void *pv = rtMemAlloc("new[] nothrow", RTMEMTYPE_NEW_ARRAY, cb, ((void **)&cb)[-1], 0, NULL, NULL);
     109    void *pv = rtR3MemAlloc("new[] nothrow", RTMEMTYPE_NEW_ARRAY, cb, cb, ASMReturnAddress(), NULL, 0, NULL);
    110110    return pv;
    111111}
     
    114114void RT_EF_CDECL operator delete[](void * pv) throw()
    115115{
    116     rtMemFree("delete[]", RTMEMTYPE_DELETE_ARRAY, pv, ((void **)&pv)[-1], 0, NULL, NULL);
     116    rtR3MemFree("delete[]", RTMEMTYPE_DELETE_ARRAY, pv, ASMReturnAddress(), NULL, 0, NULL);
    117117}
    118118
     
    120120void RT_EF_CDECL operator delete[](void *pv, const std::nothrow_t &) throw()
    121121{
    122     rtMemFree("delete[] nothrow", RTMEMTYPE_DELETE_ARRAY, pv, ((void **)&pv)[-1], 0, NULL, NULL);
     122    rtR3MemFree("delete[] nothrow", RTMEMTYPE_DELETE_ARRAY, pv, ASMReturnAddress(), NULL, 0, NULL);
    123123}
    124124
  • trunk/src/VBox/Runtime/r3/alloc-ef.cpp

    r28273 r28298  
    9292 * Log an event.
    9393 */
    94 static inline void rtmemLog(const char *pszOp, const char *pszFormat, ...)
     94DECLINLINE(void) rtmemLog(const char *pszOp, const char *pszFormat, ...)
    9595{
    9696#if 0
     
    109109 * Aquires the lock.
    110110 */
    111 static inline void rtmemBlockLock(void)
     111DECLINLINE(void) rtmemBlockLock(void)
    112112{
    113113    unsigned c = 0;
     
    120120 * Releases the lock.
    121121 */
    122 static inline void rtmemBlockUnlock(void)
     122DECLINLINE(void) rtmemBlockUnlock(void)
    123123{
    124124    Assert(g_BlocksLock == 1);
     
    130130 * Creates a block.
    131131 */
    132 static inline PRTMEMBLOCK rtmemBlockCreate(RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
     132DECLINLINE(PRTMEMBLOCK) rtmemBlockCreate(RTMEMTYPE enmType, size_t cbUnaligned, size_t cbAligned,
     133                                         void *pvCaller, RT_SRC_POS_DECL)
    133134{
    134135    PRTMEMBLOCK pBlock = (PRTMEMBLOCK)malloc(sizeof(*pBlock));
    135136    if (pBlock)
    136137    {
    137         pBlock->enmType = enmType;
    138         pBlock->cb = cb;
    139         pBlock->pvCaller = pvCaller;
    140         pBlock->iLine = iLine;
    141         pBlock->pszFile = pszFile;
     138        pBlock->enmType     = enmType;
     139        pBlock->cbUnaligned = cbUnaligned;
     140        pBlock->cbAligned   = cbAligned;
     141        pBlock->pvCaller    = pvCaller;
     142        pBlock->iLine       = iLine;
     143        pBlock->pszFile     = pszFile;
    142144        pBlock->pszFunction = pszFunction;
    143145    }
     
    149151 * Frees a block.
    150152 */
    151 static inline void rtmemBlockFree(PRTMEMBLOCK pBlock)
     153DECLINLINE(void) rtmemBlockFree(PRTMEMBLOCK pBlock)
    152154{
    153155    free(pBlock);
     
    158160 * Insert a block from the tree.
    159161 */
    160 static inline void rtmemBlockInsert(PRTMEMBLOCK pBlock, void *pv)
     162DECLINLINE(void) rtmemBlockInsert(PRTMEMBLOCK pBlock, void *pv)
    161163{
    162164    pBlock->Core.Key = pv;
     
    171173 * Remove a block from the tree and returns it to the caller.
    172174 */
    173 static inline PRTMEMBLOCK rtmemBlockRemove(void *pv)
     175DECLINLINE(PRTMEMBLOCK) rtmemBlockRemove(void *pv)
    174176{
    175177    rtmemBlockLock();
     
    182184 * Gets a block.
    183185 */
    184 static inline PRTMEMBLOCK rtmemBlockGet(void *pv)
     186DECLINLINE(PRTMEMBLOCK) rtmemBlockGet(void *pv)
    185187{
    186188    rtmemBlockLock();
     
    196198{
    197199    PRTMEMBLOCK pBlock = (PRTMEMBLOCK)pNode;
    198     fprintf(stderr, "%p %08lx %p\n",
     200    fprintf(stderr, "%p %08lx(+%02lx) %p\n",
    199201            pBlock->Core.Key,
    200             (long)pBlock->cb,
     202            (unsigned long)pBlock->cbUnaligned,
     203            (unsigned long)(pBlock->cbAligned - pBlock->cbUnaligned),
    201204            pBlock->pvCaller);
    202205    return 0;
     
    210213void RTMemDump(void)
    211214{
    212     fprintf(stderr, "address  size     caller\n");
     215    fprintf(stderr, "address  size(alg)     caller\n");
    213216    RTAvlPVDoWithAll(&g_BlocksTree, true, RTMemDumpOne, NULL);
    214217}
     
    219222 * Insert a delayed block.
    220223 */
    221 static inline void rtmemBlockDelayInsert(PRTMEMBLOCK pBlock)
    222 {
    223     size_t cbBlock = RT_ALIGN_Z(pBlock->cb, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
     224DECLINLINE(void) rtmemBlockDelayInsert(PRTMEMBLOCK pBlock)
     225{
     226    size_t cbBlock = RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
    224227    pBlock->Core.pRight = NULL;
    225228    pBlock->Core.pLeft = NULL;
     
    243246 * Removes a delayed block.
    244247 */
    245 static inline PRTMEMBLOCK rtmemBlockDelayRemove(void)
     248DECLINLINE(PRTMEMBLOCK) rtmemBlockDelayRemove(void)
    246249{
    247250    PRTMEMBLOCK pBlock = NULL;
     
    257260            else
    258261                g_pBlocksDelayHead = NULL;
    259             g_cbBlocksDelay -= RT_ALIGN_Z(pBlock->cb, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
     262            g_cbBlocksDelay -= RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
    260263        }
    261264    }
     
    273276 * Internal allocator.
    274277 */
    275 RTDECL(void *) rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
     278RTDECL(void *) rtR3MemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cbUnaligned, size_t cbAligned,
     279                            void *pvCaller, RT_SRC_POS_DECL)
    276280{
    277281    /*
     
    284288        return NULL;
    285289    }
    286     if (!cb)
     290    if (!cbUnaligned)
    287291    {
    288292#if 0
     
    290294        return NULL;
    291295#else
    292         cb = 1;
    293 #endif
    294     }
     296        cbAligned = cbUnaligned = 1;
     297#endif
     298    }
     299
    295300#ifndef RTALLOC_EFENCE_IN_FRONT
    296301    /* Alignment decreases fence accuracy, but this is at least partially
    297302     * counteracted by filling and checking the alignment padding. When the
    298303     * fence is in front then then no extra alignment is needed. */
    299     size_t cbAligned = RT_ALIGN_Z(cb, RTALLOC_EFENCE_ALIGNMENT);
     304    cbAligned = RT_ALIGN_Z(cbAligned, RTALLOC_EFENCE_ALIGNMENT);
    300305#endif
    301306
     
    304309     * Allocate the trace block.
    305310     */
    306     PRTMEMBLOCK pBlock = rtmemBlockCreate(enmType, cb, pvCaller, iLine, pszFile, pszFunction);
     311    PRTMEMBLOCK pBlock = rtmemBlockCreate(enmType, cbUnaligned, cbAligned, pvCaller, RT_SRC_POS_ARGS);
    307312    if (!pBlock)
    308313    {
     
    315320     * Allocate a block with page alignment space + the size of the E-fence.
    316321     */
    317     size_t  cbBlock = RT_ALIGN_Z(cb, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
     322    size_t  cbBlock = RT_ALIGN_Z(cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
    318323    void   *pvBlock = RTMemPageAlloc(cbBlock);
    319324    if (pvBlock)
     
    325330#ifdef RTALLOC_EFENCE_IN_FRONT
    326331        void *pvEFence = pvBlock;
    327         void *pv = (char *)pvEFence + RTALLOC_EFENCE_SIZE;
     332        void *pv       = (char *)pvEFence + RTALLOC_EFENCE_SIZE;
    328333# ifdef RTALLOC_EFENCE_NOMAN_FILLER
    329         memset((char *)pv + cb, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - RTALLOC_EFENCE_SIZE - cb);
     334        memset((char *)pv + cb, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - RTALLOC_EFENCE_SIZE - cbUnaligned);
    330335# endif
    331336#else
    332337        void *pvEFence = (char *)pvBlock + (cbBlock - RTALLOC_EFENCE_SIZE);
    333         void *pv = (char *)pvEFence - cbAligned;
     338        void *pv       = (char *)pvEFence - cbAligned;
    334339# ifdef RTALLOC_EFENCE_NOMAN_FILLER
    335340        memset(pvBlock, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - RTALLOC_EFENCE_SIZE - cbAligned);
    336         memset((char *)pv + cb, RTALLOC_EFENCE_NOMAN_FILLER, cbAligned - cb);
     341        memset((char *)pv + cbUnaligned, RTALLOC_EFENCE_NOMAN_FILLER, cbAligned - cbUnaligned);
    337342# endif
    338343#endif
     
    348353#endif
    349354            if (enmType == RTMEMTYPE_RTMEMALLOCZ)
    350                 memset(pv, 0, cb);
     355                memset(pv, 0, cbUnaligned);
    351356#ifdef RTALLOC_EFENCE_FILLER
    352357            else
    353                 memset(pv, RTALLOC_EFENCE_FILLER, cb);
    354 #endif
    355 
    356             rtmemLog(pszOp, "returns %p (pvBlock=%p cbBlock=%#x pvEFence=%p cb=%#x)\n", pv, pvBlock, cbBlock, pvEFence, cb);
     358                memset(pv, RTALLOC_EFENCE_FILLER, cbUnaligned);
     359#endif
     360
     361            rtmemLog(pszOp, "returns %p (pvBlock=%p cbBlock=%#x pvEFence=%p cbUnaligned=%#x)\n", pv, pvBlock, cbBlock, pvEFence, cbUnaligned);
    357362            return pv;
    358363        }
     
    361366    }
    362367    else
    363         rtmemComplain(pszOp, "Failed to allocated %d bytes.\n", cb);
     368        rtmemComplain(pszOp, "Failed to allocated %lu (%lu) bytes.\n", (unsigned long)cbBlock, (unsigned long)cbUnaligned);
    364369
    365370#ifdef RTALLOC_EFENCE_TRACE
     
    373378 * Internal free.
    374379 */
    375 RTDECL(void) rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
     380RTDECL(void) rtR3MemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, RT_SRC_POS_DECL)
    376381{
    377382    /*
     
    396401    {
    397402        if (gfRTMemFreeLog)
    398             RTLogPrintf("RTMem %s: pv=%p pvCaller=%p cb=%#x\n", pszOp, pv, pvCaller, pBlock->cb);
     403            RTLogPrintf("RTMem %s: pv=%p pvCaller=%p cbUnaligned=%#x\n", pszOp, pv, pvCaller, pBlock->cbUnaligned);
    399404
    400405# ifdef RTALLOC_EFENCE_NOMAN_FILLER
     
    408413#  else
    409414        /* Alignment must match allocation alignment in rtMemAlloc(). */
    410         size_t cbAligned = RT_ALIGN_Z(pBlock->cb, RTALLOC_EFENCE_ALIGNMENT);
    411         void  *pvWrong   = ASMMemIsAll8((char *)pv + pBlock->cb,
    412                                         cbAligned - pBlock->cb,
     415        void  *pvWrong   = ASMMemIsAll8((char *)pv + pBlock->cbUnaligned,
     416                                        pBlock->cbAligned - pBlock->cbUnaligned,
    413417                                        RTALLOC_EFENCE_NOMAN_FILLER);
    414418        if (pvWrong)
    415419            RTAssertDoPanic();
    416420        pvWrong = ASMMemIsAll8((void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK),
    417                                RT_ALIGN_Z(cbAligned, PAGE_SIZE) - cbAligned,
     421                               RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) - pBlock->cbAligned,
    418422                               RTALLOC_EFENCE_NOMAN_FILLER);
    419423#  endif
     
    426430         * Fill the user part of the block.
    427431         */
    428         memset(pv, RTALLOC_EFENCE_FREE_FILL, pBlock->cb);
     432        memset(pv, RTALLOC_EFENCE_FREE_FILL, pBlock->cbUnaligned);
    429433# endif
    430434
     
    434438         * That means we'll expand the E-fence to cover the entire block.
    435439         */
    436         int rc = RTMemProtect(pv, pBlock->cb, RTMEM_PROT_NONE);
     440        int rc = RTMemProtect(pv, pBlock->cbAligned, RTMEM_PROT_NONE);
    437441        if (RT_SUCCESS(rc))
    438442        {
     
    445449                pv = pBlock->Core.Key;
    446450#  ifdef RTALLOC_EFENCE_IN_FRONT
    447                 void *pvBlock = (char *)pv - RTALLOC_EFENCE_SIZE;
     451                void  *pvBlock = (char *)pv - RTALLOC_EFENCE_SIZE;
    448452#  else
    449                 void *pvBlock = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
     453                void  *pvBlock = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
    450454#  endif
    451                 size_t cbBlock = RT_ALIGN_Z(pBlock->cb, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
     455                size_t cbBlock = RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
    452456                rc = RTMemProtect(pvBlock, cbBlock, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
    453457                if (RT_SUCCESS(rc))
     
    503507 * Internal realloc.
    504508 */
    505 RTDECL(void *) rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
     509RTDECL(void *) rtR3MemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, RT_SRC_POS_DECL)
    506510{
    507511    /*
     
    509513     */
    510514    if (!pvOld)
    511         return rtMemAlloc(pszOp, enmType, cbNew, pvCaller, iLine, pszFile, pszFunction);
     515        return rtR3MemAlloc(pszOp, enmType, cbNew, cbNew, pvCaller, RT_SRC_POS_ARGS);
    512516    if (!cbNew)
    513517    {
    514         rtMemFree(pszOp, RTMEMTYPE_RTMEMREALLOC, pvOld, pvCaller, iLine, pszFile, pszFunction);
     518        rtR3MemFree(pszOp, RTMEMTYPE_RTMEMREALLOC, pvOld, pvCaller, RT_SRC_POS_ARGS);
    515519        return NULL;
    516520    }
     
    524528    if (pBlock)
    525529    {
    526         void *pvRet = rtMemAlloc(pszOp, enmType, cbNew, pvCaller, iLine, pszFile, pszFunction);
     530        void *pvRet = rtR3MemAlloc(pszOp, enmType, cbNew, cbNew, pvCaller, RT_SRC_POS_ARGS);
    527531        if (pvRet)
    528532        {
    529             memcpy(pvRet, pvOld, RT_MIN(cbNew, pBlock->cb));
    530             rtMemFree(pszOp, RTMEMTYPE_RTMEMREALLOC, pvOld, pvCaller, iLine, pszFile, pszFunction);
     533            memcpy(pvRet, pvOld, RT_MIN(cbNew, pBlock->cbUnaligned));
     534            rtR3MemFree(pszOp, RTMEMTYPE_RTMEMREALLOC, pvOld, pvCaller, RT_SRC_POS_ARGS);
    531535        }
    532536        return pvRet;
     
    547551
    548552
    549 /**
    550  * Same as RTMemTmpAlloc() except that it's fenced.
    551  *
    552  * @returns Pointer to the allocated memory.
    553  * @returns NULL on failure.
    554  * @param   cb      Size in bytes of the memory block to allocate.
    555  */
    556 RTDECL(void *)  RTMemEfTmpAlloc(size_t cb) RT_NO_THROW
    557 {
    558     return RTMemEfAlloc(cb);
    559 }
    560 
    561 
    562 /**
    563  * Same as RTMemTmpAllocZ() except that it's fenced.
    564  *
    565  * @returns Pointer to the allocated memory.
    566  * @returns NULL on failure.
    567  * @param   cb      Size in bytes of the memory block to allocate.
    568  */
    569 RTDECL(void *)  RTMemEfTmpAllocZ(size_t cb) RT_NO_THROW
    570 {
    571     return RTMemEfAllocZ(cb);
    572 }
    573 
    574 
    575 /**
    576  * Same as RTMemTmpFree() except that it's for fenced memory.
    577  *
    578  * @param   pv      Pointer to memory block.
    579  */
    580 RTDECL(void)    RTMemEfTmpFree(void *pv) RT_NO_THROW
    581 {
    582     RTMemEfFree(pv);
    583 }
    584 
    585 
    586 /**
    587  * Same as RTMemAlloc() except that it's fenced.
    588  *
    589  * @returns Pointer to the allocated memory. Free with RTMemEfFree().
    590  * @returns NULL on failure.
    591  * @param   cb      Size in bytes of the memory block to allocate.
    592  */
    593 RTDECL(void *)  RTMemEfAlloc(size_t cb) RT_NO_THROW
    594 {
    595     return rtMemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, ((void **)&cb)[-1], 0, NULL, NULL);
    596 }
    597 
    598 
    599 /**
    600  * Same as RTMemAllocZ() except that it's fenced.
    601  *
    602  * @returns Pointer to the allocated memory.
    603  * @returns NULL on failure.
    604  * @param   cb      Size in bytes of the memory block to allocate.
    605  */
    606 RTDECL(void *)  RTMemEfAllocZ(size_t cb) RT_NO_THROW
    607 {
    608     return rtMemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, ((void **)&cb)[-1], 0, NULL, NULL);
    609 }
    610 
    611 
    612 /**
    613  * Same as RTMemAllocVar() except that it's fenced.
    614  *
    615  * @returns Pointer to the allocated memory. Free with RTMemEfFree().
    616  * @returns NULL on failure.
    617  * @param   cbUnaligned Size in bytes of the memory block to allocate.
    618  */
    619 RTDECL(void *)  RTMemEfAllocVar(size_t cbUnaligned) RT_NO_THROW
     553RTDECL(void *)  RTMemEfTmpAlloc(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW
     554{
     555    return rtR3MemAlloc("TmpAlloc", RTMEMTYPE_RTMEMALLOC, cb, cb, ASMReturnAddress(), RT_SRC_POS_ARGS);
     556}
     557
     558
     559RTDECL(void *)  RTMemEfTmpAllocZ(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW
     560{
     561    return rtR3MemAlloc("TmpAlloc", RTMEMTYPE_RTMEMALLOCZ, cb, cb, ASMReturnAddress(), RT_SRC_POS_ARGS);
     562}
     563
     564
     565RTDECL(void)    RTMemEfTmpFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW
     566{
     567    if (pv)
     568        rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ASMReturnAddress(), RT_SRC_POS_ARGS);
     569}
     570
     571
     572RTDECL(void *)  RTMemEfAlloc(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW
     573{
     574    return rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, cb, ASMReturnAddress(), RT_SRC_POS_ARGS);
     575}
     576
     577
     578RTDECL(void *)  RTMemEfAllocZ(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW
     579{
     580    return rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, ASMReturnAddress(), RT_SRC_POS_ARGS);
     581}
     582
     583
     584RTDECL(void *)  RTMemEfAllocVar(size_t cbUnaligned, RT_SRC_POS_DECL) RT_NO_THROW
    620585{
    621586    size_t cbAligned;
     
    624589    else
    625590        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
    626     return rtMemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cbAligned, ((void **)&cbUnaligned)[-1], 0, NULL, NULL);
    627 }
    628 
    629 
    630 /**
    631  * Same as RTMemAllocZVar() except that it's fenced.
    632  *
    633  * @returns Pointer to the allocated memory.
    634  * @returns NULL on failure.
    635  * @param   cbUnaligned Size in bytes of the memory block to allocate.
    636  */
    637 RTDECL(void *)  RTMemEfAllocZVar(size_t cbUnaligned) RT_NO_THROW
     591    return rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cbUnaligned, cbAligned, ASMReturnAddress(), RT_SRC_POS_ARGS);
     592}
     593
     594
     595RTDECL(void *)  RTMemEfAllocZVar(size_t cbUnaligned, RT_SRC_POS_DECL) RT_NO_THROW
    638596{
    639597    size_t cbAligned;
     
    642600    else
    643601        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
    644     return rtMemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cbAligned, ((void **)&cbUnaligned)[-1], 0, NULL, NULL);
    645 }
    646 
    647 
    648 /**
    649  * Same as RTMemRealloc() except that it's fenced.
    650  *
    651  * @returns Pointer to the allocated memory.
    652  * @returns NULL on failure.
    653  * @param   pvOld   The memory block to reallocate.
    654  * @param   cbNew   The new block size (in bytes).
    655  */
    656 RTDECL(void *)  RTMemEfRealloc(void *pvOld, size_t cbNew) RT_NO_THROW
    657 {
    658     return rtMemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, ((void **)&pvOld)[-1], 0, NULL, NULL);
    659 }
    660 
    661 
    662 /**
    663  * Free memory allocated by any of the RTMemEf* allocators.
    664  *
    665  * @param   pv      Pointer to memory block.
    666  */
    667 RTDECL(void)    RTMemEfFree(void *pv) RT_NO_THROW
     602    return rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cbUnaligned, cbAligned, ASMReturnAddress(), RT_SRC_POS_ARGS);
     603}
     604
     605
     606RTDECL(void *)  RTMemEfRealloc(void *pvOld, size_t cbNew, RT_SRC_POS_DECL) RT_NO_THROW
     607{
     608    return rtR3MemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, ASMReturnAddress(), RT_SRC_POS_ARGS);
     609}
     610
     611
     612RTDECL(void)    RTMemEfFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW
    668613{
    669614    if (pv)
    670         rtMemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ((void **)&pv)[-1], 0, NULL, NULL);
    671 }
    672 
    673 
    674 /**
    675  * Same as RTMemDup() except that it's fenced.
    676  *
    677  * @returns New heap block with the duplicate data.
    678  * @returns NULL if we're out of memory.
    679  * @param   pvSrc   The memory to duplicate.
    680  * @param   cb      The amount of memory to duplicate.
    681  */
    682 RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb) RT_NO_THROW
    683 {
    684     void *pvDst = RTMemEfAlloc(cb);
     615        rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ASMReturnAddress(), RT_SRC_POS_ARGS);
     616}
     617
     618
     619RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb, RT_SRC_POS_DECL) RT_NO_THROW
     620{
     621    void *pvDst = RTMemEfAlloc(cb, RT_SRC_POS_ARGS);
    685622    if (pvDst)
    686623        memcpy(pvDst, pvSrc, cb);
     
    689626
    690627
    691 /**
    692  * Same as RTMemDupEx except that it's fenced.
    693  *
    694  * @returns New heap block with the duplicate data.
    695  * @returns NULL if we're out of memory.
    696  * @param   pvSrc   The memory to duplicate.
    697  * @param   cbSrc   The amount of memory to duplicate.
    698  * @param   cbExtra The amount of extra memory to allocate and zero.
    699  */
    700 RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW
    701 {
    702     void *pvDst = RTMemEfAlloc(cbSrc + cbExtra);
     628RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra, RT_SRC_POS_DECL) RT_NO_THROW
     629{
     630    void *pvDst = RTMemEfAlloc(cbSrc + cbExtra, RT_SRC_POS_ARGS);
    703631    if (pvDst)
    704632    {
     
    709637}
    710638
     639
     640
     641
     642/*
     643 *
     644 * The NP (no position) versions.
     645 *
     646 */
     647
     648
     649
     650RTDECL(void *)  RTMemEfTmpAllocNP(size_t cb) RT_NO_THROW
     651{
     652    return rtR3MemAlloc("TmpAlloc", RTMEMTYPE_RTMEMALLOC, cb, cb, ASMReturnAddress(), NULL, 0, NULL);
     653}
     654
     655
     656RTDECL(void *)  RTMemEfTmpAllocZNP(size_t cb) RT_NO_THROW
     657{
     658    return rtR3MemAlloc("TmpAllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, ASMReturnAddress(), NULL, 0, NULL);
     659}
     660
     661
     662RTDECL(void)    RTMemEfTmpFreeNP(void *pv) RT_NO_THROW
     663{
     664    if (pv)
     665        rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ASMReturnAddress(), NULL, 0, NULL);
     666}
     667
     668
     669RTDECL(void *)  RTMemEfAllocNP(size_t cb) RT_NO_THROW
     670{
     671    return rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, cb, ASMReturnAddress(), NULL, 0, NULL);
     672}
     673
     674
     675RTDECL(void *)  RTMemEfAllocZNP(size_t cb) RT_NO_THROW
     676{
     677    return rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, ASMReturnAddress(), NULL, 0, NULL);
     678}
     679
     680
     681RTDECL(void *)  RTMemEfAllocVarNP(size_t cbUnaligned) RT_NO_THROW
     682{
     683    size_t cbAligned;
     684    if (cbUnaligned >= 16)
     685        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
     686    else
     687        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
     688    return rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cbUnaligned, cbAligned, ASMReturnAddress(), NULL, 0, NULL);
     689}
     690
     691
     692RTDECL(void *)  RTMemEfAllocZVarNP(size_t cbUnaligned) RT_NO_THROW
     693{
     694    size_t cbAligned;
     695    if (cbUnaligned >= 16)
     696        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
     697    else
     698        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
     699    return rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cbUnaligned, cbAligned, ASMReturnAddress(), NULL, 0, NULL);
     700}
     701
     702
     703RTDECL(void *)  RTMemEfReallocNP(void *pvOld, size_t cbNew) RT_NO_THROW
     704{
     705    return rtR3MemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, ASMReturnAddress(), NULL, 0, NULL);
     706}
     707
     708
     709RTDECL(void)    RTMemEfFreeNP(void *pv) RT_NO_THROW
     710{
     711    if (pv)
     712        rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ASMReturnAddress(), NULL, 0, NULL);
     713}
     714
     715
     716RTDECL(void *) RTMemEfDupNP(const void *pvSrc, size_t cb) RT_NO_THROW
     717{
     718    void *pvDst = RTMemEfAlloc(cb, NULL, 0, NULL);
     719    if (pvDst)
     720        memcpy(pvDst, pvSrc, cb);
     721    return pvDst;
     722}
     723
     724
     725RTDECL(void *) RTMemEfDupExNP(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW
     726{
     727    void *pvDst = RTMemEfAlloc(cbSrc + cbExtra, NULL, 0, NULL);
     728    if (pvDst)
     729    {
     730        memcpy(pvDst, pvSrc, cbSrc);
     731        memset((uint8_t *)pvDst + cbSrc, 0, cbExtra);
     732    }
     733    return pvDst;
     734}
     735
     736
  • trunk/src/VBox/Runtime/r3/alloc-ef.h

    r28271 r28298  
    4545 * RTMemAllocZ(), RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ().
    4646 */
    47 #if defined(DEBUG_bird)
     47#if 0
    4848# define RTALLOC_USE_EFENCE
    4949#endif
     
    166166    /** Allocation type. */
    167167    RTMEMTYPE       enmType;
    168     /** The size of the block. */
    169     size_t          cb;
     168    /** The unaligned size of the block. */
     169    size_t          cbUnaligned;
     170    /** The aligned size of the block. */
     171    size_t          cbAligned;
    170172    /** The return address of the allocator function. */
    171173    void           *pvCaller;
     
    185187******************************************************************************/
    186188RT_C_DECLS_BEGIN
    187 RTDECL(void *)  rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
    188 RTDECL(void *)  rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
    189 RTDECL(void)    rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
     189RTDECL(void *)  rtR3MemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cbUnaligned, size_t cbAligned,
     190                             void *pvCaller, RT_SRC_POS_DECL);
     191RTDECL(void *)  rtR3MemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, RT_SRC_POS_DECL);
     192RTDECL(void)    rtR3MemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, RT_SRC_POS_DECL);
    190193RT_C_DECLS_END
    191194
  • trunk/src/VBox/Runtime/r3/alloc.cpp

    r28271 r28298  
    4444#include "alloc-ef.h"
    4545#include <iprt/alloc.h>
     46#include <iprt/asm.h>
    4647#include <iprt/assert.h>
    4748#include <iprt/param.h>
     
    4950
    5051#include <stdlib.h>
     52
     53#undef RTMemTmpAlloc
     54#undef RTMemTmpAllocZ
     55#undef RTMemTmpFree
     56#undef RTMemAlloc
     57#undef RTMemAllocZ
     58#undef RTMemAllocVar
     59#undef RTMemAllocZVar
     60#undef RTMemRealloc
     61#undef RTMemFree
     62#undef RTMemDup
     63#undef RTMemDupEx
    5164
    5265
     
    105118{
    106119#ifdef RTALLOC_USE_EFENCE
    107     void *pv = rtMemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, ((void **)&cb)[-1], 0, NULL, NULL);
     120    void *pv = rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, cb, ASMReturnAddress(), NULL, 0, NULL);
    108121
    109122#else /* !RTALLOC_USE_EFENCE */
     
    135148{
    136149#ifdef RTALLOC_USE_EFENCE
    137     void *pv = rtMemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, ((void **)&cb)[-1], 0, NULL, NULL);
     150    void *pv = rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, ASMReturnAddress(), NULL, 0, NULL);
    138151
    139152#else /* !RTALLOC_USE_EFENCE */
     
    153166
    154167/**
     168 * Wrapper around RTMemAlloc for automatically aligning variable sized
     169 * allocations so that the various electric fence heaps works correctly.
     170 *
     171 * @returns See RTMemAlloc.
     172 * @param   cbUnaligned         The unaligned size.
     173 */
     174RTDECL(void *) RTMemAllocVar(size_t cbUnaligned)
     175{
     176    size_t cbAligned;
     177    if (cbUnaligned >= 16)
     178        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
     179    else
     180        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
     181#ifdef RTALLOC_USE_EFENCE
     182    void *pv = RTMemAlloc(cbAligned);
     183#else
     184    void *pv = rtR3MemAlloc("AllocVar", RTMEMTYPE_RTMEMALLOC, cbUnaligned, cbAligned, ASMReturnAddress(), NULL, 0, NULL);
     185#endif
     186    return pv;
     187}
     188
     189
     190/**
     191 * Wrapper around RTMemAllocZ for automatically aligning variable sized
     192 * allocations so that the various electric fence heaps works correctly.
     193 *
     194 * @returns See RTMemAllocZ.
     195 * @param   cbUnaligned         The unaligned size.
     196 */
     197RTDECL(void *) RTMemAllocZVar(size_t cbUnaligned)
     198{
     199    size_t cbAligned;
     200    if (cbUnaligned >= 16)
     201        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
     202    else
     203        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
     204#ifdef RTALLOC_USE_EFENCE
     205    void *pv = RTMemAllocZ(cbAligned);
     206#else
     207    void *pv = rtR3MemAlloc("AllocZVar", RTMEMTYPE_RTMEMALLOCZ, cbUnaligned, cbAligned, ASMReturnAddress(), NULL, 0, NULL);
     208#endif
     209    return pv;
     210}
     211
     212
     213/**
    155214 * Reallocates memory.
    156215 *
     
    163222{
    164223#ifdef RTALLOC_USE_EFENCE
    165     void *pv = rtMemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, ((void **)&pvOld)[-1], 0, NULL, NULL);
     224    void *pv = rtR3MemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, ASMReturnAddress(), NULL, 0, NULL);
    166225
    167226#else /* !RTALLOC_USE_EFENCE */
     
    187246    if (pv)
    188247#ifdef RTALLOC_USE_EFENCE
    189         rtMemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ((void **)&pv)[-1], 0, NULL, NULL);
     248        rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ASMReturnAddress(), NULL, 0, NULL);
    190249#else
    191250        free(pv);
  • trunk/src/VBox/Runtime/testcase/tstMemAutoPtr.cpp

    r11822 r28298  
    113113        g_cErrors++;
    114114    }
    115     RTMemEfFree(aMem);
     115    RTMemEfFreeNP(aMem);
    116116    g_cFrees++;
    117117}
     
    165165     */
    166166    {
    167         RTMemAutoPtr<char, RTMemEfAutoFree<char>, RTMemEfRealloc> Electric(10);
     167        RTMemAutoPtr<char, RTMemEfAutoFree<char>, RTMemEfReallocNP> Electric(10);
    168168        CHECK_EXPR(Electric.get() != NULL);
    169169        Electric[0] = '0';
     
    182182    g_cFrees = 0;
    183183    {
    184         RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt(128);
     184        RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfReallocNP> FreeIt(128);
    185185        FreeIt[127] = '0';
    186186    }
     
    189189    g_cFrees = 0;
    190190    {
    191         RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt2(128);
     191        RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfReallocNP> FreeIt2(128);
    192192        FreeIt2[127] = '1';
    193193        FreeIt2.reset();
     
    200200    g_cFrees = 0;
    201201    {
    202         RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> DontFreeIt(256);
     202        RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfReallocNP> DontFreeIt(256);
    203203        DontFreeIt[255] = '0';
    204         RTMemEfFree(DontFreeIt.release());
     204        RTMemEfFreeNP(DontFreeIt.release());
    205205    }
    206206    CHECK_EXPR(g_cFrees == 0);
     
    208208    g_cFrees = 0;
    209209    {
    210         RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt3(128);
     210        RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfReallocNP> FreeIt3(128);
    211211        FreeIt3[127] = '0';
    212212        CHECK_EXPR(FreeIt3.realloc(128));
     
    223223    g_cFrees = 0;
    224224    {
    225         RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt4;
     225        RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfReallocNP> FreeIt4;
    226226        CHECK_EXPR(FreeIt4.alloc(123));
    227227        CHECK_EXPR(FreeIt4.realloc(543));
  • trunk/src/VBox/Runtime/testcase/tstRTMemEf.cpp

    r27283 r28298  
    5151     * the word after the allocated block and the word before. One of them
    5252     * will crash no matter whether the fence is at the bottom or on top. */
    53     int32_t *p = (int32_t *)RTMemEfAlloc(sizeof(int32_t));
     53    int32_t *p = (int32_t *)RTMemEfAllocNP(sizeof(int32_t));
    5454    RTPrintf("tstRTMemAllocEfAccess: allocated int32_t at %#p\n", p);
    5555    RTPrintf("tstRTMemAllocEfAccess: triggering buffer overrun...\n");
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