VirtualBox

Changeset 28271 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Apr 13, 2010 7:29:42 PM (15 years ago)
Author:
vboxsync
Message:

IPRT: Some efence adjustments, adding RTMemAllocVar and RTMemAllocZVar for dealing with struct + string style allocations.

Location:
trunk/src/VBox/Runtime
Files:
8 edited

Legend:

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

    r28053 r28271  
    186186RuntimeR3_SDKS.win      = WINPSDK W2K3DDK
    187187RuntimeR3_DEFS          = IN_RT_R3 IN_SUP_R3 LDR_WITH_NATIVE LDR_WITH_ELF32 LDR_WITH_PE RT_WITH_VBOX RT_NO_GIP
     188#RuntimeR3_DEFS         += RTMEM_WRAP_TO_EF_APIS
    188189ifdef IPRT_WITH_KSTUFF
    189190 RuntimeR3_DEFS        += LDR_WITH_KLDR
     
    17401741# Static library for new & delete for the electric fence.
    17411742#
    1742 RuntimeEFCPP_TEMPLATE   = $(RuntimeR3_TEMPLATE)
     1743RuntimeEFCPP_TEMPLATE   = $(VBoxRT_TEMPLATE)
    17431744RuntimeEFCPP_SDKS       = $(RuntimeR3_SDKS)
    17441745RuntimeEFCPP_SDKS.$(KBUILD_TARGET) = $(RuntimeR3_SDKS.$(KBUILD_TARGET))
  • trunk/src/VBox/Runtime/common/alloc/alloc.cpp

    r26344 r28271  
    3838#include <iprt/assert.h>
    3939#include <iprt/string.h>
     40
     41#ifdef RTMEM_WRAP_TO_EF_APIS
     42# undef RTMemAllocVar
     43# undef RTMemAllocZVar
     44# undef RTMemDup
     45# undef RTMemDupEx
     46#endif
     47
     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 */
     57RTDECL(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 */
     75RTDECL(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}
    4084
    4185
  • trunk/src/VBox/Runtime/common/log/log.cpp

    r27807 r28271  
    276276     */
    277277    cb = RT_OFFSETOF(RTLOGGER, afGroups[cGroups + 1]) + RTPATH_MAX;
    278     pLogger = (PRTLOGGER)RTMemAllocZ(cb);
     278    pLogger = (PRTLOGGER)RTMemAllocZVar(cb);
    279279    if (pLogger)
    280280    {
  • trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp

    r28267 r28271  
    10721072    size_t const       cbFile   = pSrcPos->pszFile ? strlen(pSrcPos->pszFile) + 1 : 0;
    10731073    size_t const     cbFunction = pSrcPos->pszFile ? strlen(pSrcPos->pszFunction) + 1 : 0;
    1074     RTLOCKVALCLASSINT *pThis    = (RTLOCKVALCLASSINT *)RTMemAlloc(sizeof(*pThis) + cbFile + cbFunction + cbName);
     1074    RTLOCKVALCLASSINT *pThis    = (RTLOCKVALCLASSINT *)RTMemAllocVar(sizeof(*pThis) + cbFile + cbFunction + cbName);
    10751075    if (!pThis)
    10761076        return VERR_NO_MEMORY;
  • trunk/src/VBox/Runtime/r3/alloc-ef-cpp.cpp

    r8245 r28271  
    55
    66/*
    7  * Copyright (C) 2006-2007 Sun Microsystems, Inc.
     7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3434#include "alloc-ef.h"
    3535
    36 #ifdef RTALLOC_EFENCE_CPP /* rest of the file */
     36
     37#if defined(RTALLOC_EFENCE_CPP) || defined(RTMEM_WRAP_TO_EF_APIS) /* rest of the file */
    3738
    3839#include <new>
     
    122123}
    123124
    124 #endif /* RTALLOC_EFENCE_CPP */
     125#endif /* RTALLOC_EFENCE_CPP || RTMEM_WRAP_TO_EF_APIS */
  • trunk/src/VBox/Runtime/r3/alloc-ef.cpp

    r27575 r28271  
    3434*******************************************************************************/
    3535#include "alloc-ef.h"
     36#include <iprt/mem.h>
    3637#include <iprt/log.h>
    3738#include <iprt/asm.h>
     
    272273 * Internal allocator.
    273274 */
    274 void *rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
     275RTDECL(void *) rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
    275276{
    276277    /*
     
    372373 * Internal free.
    373374 */
    374 void rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
     375RTDECL(void) rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
    375376{
    376377    /*
     
    498499}
    499500
     501
    500502/**
    501503 * Internal realloc.
    502504 */
    503 void *rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
     505RTDECL(void *) rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
    504506{
    505507    /*
     
    609611
    610612/**
     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 */
     619RTDECL(void *)  RTMemEfAllocVar(size_t cbUnaligned) RT_NO_THROW
     620{
     621    size_t cbAligned;
     622    if (cbUnaligned >= 16)
     623        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
     624    else
     625        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 */
     637RTDECL(void *)  RTMemEfAllocZVar(size_t cbUnaligned) RT_NO_THROW
     638{
     639    size_t cbAligned;
     640    if (cbUnaligned >= 16)
     641        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
     642    else
     643        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
     644    return rtMemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cbAligned, ((void **)&cbUnaligned)[-1], 0, NULL, NULL);
     645}
     646
     647
     648/**
    611649 * Same as RTMemRealloc() except that it's fenced.
    612650 *
  • trunk/src/VBox/Runtime/r3/alloc-ef.h

    r27297 r28271  
    4545 * RTMemAllocZ(), RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ().
    4646 */
    47 #if 0// defined(DEBUG_bird)
     47#if defined(DEBUG_bird)
    4848# define RTALLOC_USE_EFENCE
    4949#endif
     
    5757 * The allocation alignment, power of two of course.
    5858 *
    59  * Normally, 1 would be the perfect choice here, except that there is code, like
    60  * the atomic operations in iprt/asm.h, that makes assumptions about naturally
    61  * aligned data members.  If the code you're working against doesn't have strict
    62  * alignment requirements, changing this to 1 is highly desirable.
     59 * Use this for working around misaligned sizes, usually stemming from
     60 * allocating a string or something after the main structure.  When you
     61 * encounter this, please fix the allocation to RTMemAllocVar or RTMemAllocZVar.
    6362 */
    64 #if 1
     63#if 0
    6564# define RTALLOC_EFENCE_ALIGNMENT       (ARCH_BITS / 8)
    6665#else
     
    186185******************************************************************************/
    187186RT_C_DECLS_BEGIN
    188 void *  rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
    189 void *  rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
    190 void    rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
     187RTDECL(void *)  rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
     188RTDECL(void *)  rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
     189RTDECL(void)    rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
    191190RT_C_DECLS_END
    192191
  • trunk/src/VBox/Runtime/r3/alloc.cpp

    r24958 r28271  
    2828 * additional information or have any questions.
    2929 */
     30
     31
     32/*******************************************************************************
     33*   Defined Constants And Macros                                               *
     34*******************************************************************************/
     35#ifdef RTMEM_WRAP_TO_EF_APIS
     36# undef RTMEM_WRAP_TO_EF_APIS
     37# define RTALLOC_USE_EFENCE 1
     38#endif
    3039
    3140
     
    103112    void *pv = malloc(cb);
    104113    AssertMsg(pv, ("malloc(%#zx) failed!!!\n", cb));
    105 #ifdef RT_OS_OS2 /* temporary workaround until libc062. */
    106     AssertMsg(   cb < 32
    107               || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
    108 #else
    109114    AssertMsg(   cb < RTMEM_ALIGNMENT
    110               || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
    111 #endif
     115              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
     116              || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
     117              , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
    112118#endif /* !RTALLOC_USE_EFENCE */
    113119    return pv;
     
    137143    void *pv = calloc(1, cb);
    138144    AssertMsg(pv, ("calloc(1,%#zx) failed!!!\n", cb));
    139 #ifdef RT_OS_OS2 /* temporary workaround until libc062. */
    140     AssertMsg(   cb < 32
    141               || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
    142 #else
    143145    AssertMsg(   cb < RTMEM_ALIGNMENT
    144               || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
    145 #endif
     146              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
     147              || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
     148              , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
    146149#endif /* !RTALLOC_USE_EFENCE */
    147150    return pv;
     
    166169    void *pv = realloc(pvOld, cbNew);
    167170    AssertMsg(pv && cbNew, ("realloc(%p, %#zx) failed!!!\n", pvOld, cbNew));
    168 #ifdef RT_OS_OS2 /* temporary workaround until libc062. */
    169     AssertMsg(   cbNew < 32
    170               || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
    171 #else
    172171    AssertMsg(   cbNew < RTMEM_ALIGNMENT
    173               || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
    174 #endif
     172              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
     173              || ( (cbNew & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
     174              , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
    175175#endif  /* !RTALLOC_USE_EFENCE */
    176176    return pv;
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