VirtualBox

Changeset 32707 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Sep 23, 2010 10:15:08 AM (14 years ago)
Author:
vboxsync
Message:

IPRT: Added RTMemAllocEx[Tag] and RTMemFreeEx, only implemented in ring-0 only.

Location:
trunk/src/VBox/Runtime/r0drv
Files:
9 edited

Legend:

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

    r32674 r32707  
    3636#endif
    3737#include <iprt/assert.h>
     38#ifdef RT_MORE_STRICT
     39# include <iprt/mp.h>
     40#endif
    3841#include <iprt/param.h>
    3942#include <iprt/string.h>
    4043#include <iprt/thread.h>
    4144#include "r0drv/alloc-r0drv.h"
    42 
    43 #undef RTMemTmpAlloc
    44 #undef RTMemTmpAllocZ
    45 #undef RTMemTmpFree
    46 #undef RTMemAlloc
    47 #undef RTMemAllocZ
    48 #undef RTMemAllocVar
    49 #undef RTMemAllocZVar
    50 #undef RTMemRealloc
    51 #undef RTMemFree
    52 #undef RTMemDup
    53 #undef RTMemDupEx
    5445
    5546
     
    6556#else
    6657# define RTR0MEM_FENCE_EXTRA    0
    67 #endif
    68 
    69 
    70 /*******************************************************************************
    71 *   Global Variables                                                           *
    72 *******************************************************************************/
    73 #ifdef RTR0MEM_STRICT
    74 /** Fence data. */
    75 static uint8_t const g_abFence[RTR0MEM_FENCE_EXTRA] =
    76 {
    77     0x77, 0x88, 0x66, 0x99,  0x55, 0xaa, 0x44, 0xbb,
    78     0x33, 0xcc, 0x22, 0xdd,  0x11, 0xee, 0x00, 0xff
    79 };
    8058#endif
    8159
     
    10179#undef RTMemDupEx
    10280#undef RTMemDupExTag
    103 
     81#undef rtR0MemAllocEx
     82#undef rtR0MemAllocExTag
     83#undef rtR0MemFreeEx
     84
     85
     86/*******************************************************************************
     87*   Global Variables                                                           *
     88*******************************************************************************/
     89#ifdef RTR0MEM_STRICT
     90/** Fence data. */
     91static uint8_t const g_abFence[RTR0MEM_FENCE_EXTRA] =
     92{
     93    0x77, 0x88, 0x66, 0x99,  0x55, 0xaa, 0x44, 0xbb,
     94    0x33, 0xcc, 0x22, 0xdd,  0x11, 0xee, 0x00, 0xff
     95};
     96#endif
     97
     98
     99/**
     100 * Wrapper around rtR0MemAllocEx.
     101 *
     102 * @returns Pointer to the allocated memory block header.
     103 * @param   cb                  The number of bytes to allocate (sans header).
     104 * @param   fFlags              The allocation flags.
     105 */
     106DECLINLINE(PRTMEMHDR) rtR0MemAlloc(size_t cb, uint32_t fFlags)
     107{
     108    PRTMEMHDR pHdr;
     109    int rc = rtR0MemAllocEx(cb, fFlags, &pHdr);
     110    if (RT_FAILURE(rc))
     111        return NULL;
     112    return pHdr;
     113}
    104114
    105115
     
    247257    if (pHdr->u32Magic == RTMEMHDR_MAGIC)
    248258    {
     259        Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_ALLOC_EX));
    249260        Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_EXEC));
    250261#ifdef RTR0MEM_STRICT
     
    302313    if (pHdr->u32Magic == RTMEMHDR_MAGIC)
    303314    {
     315        Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_ALLOC_EX));
    304316#ifdef RTR0MEM_STRICT
    305317        AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
     
    319331
    320332
    321 /**
    322  * Internal IPRT API for allocating special memory.
    323  *
    324  * @returns Pointer to the allocated memory, NULL on failure.
    325  * @param   cb                  The amount of memory to allocate.
    326  * @param   fFlags              Subset of the RTMEMHDR_FLAG_XXX flags.
    327  * @param   pszTag              The tag.
    328  */
    329 void *rtR0MemAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW
    330 {
    331     /** @todo continue later. */
    332     return NULL;
    333 }
    334 
    335 
    336 /**
    337  * For freeing memory allocated by rtR0MemAllocExTag.
    338  *
    339  * @param   pv                  What to free, NULL is fine.
    340  */
    341 void  rtR0MemFreeEx(void *pv) RT_NO_THROW
     333
     334
     335RTDECL(int) RTMemAllocExTag(size_t cb, size_t cbAlignment, uint32_t fFlags, const char *pszTag, void **ppv) RT_NO_THROW
     336{
     337    uint32_t    fHdrFlags = RTMEMHDR_FLAG_ALLOC_EX;
     338    PRTMEMHDR   pHdr;
     339    int         rc;
     340
     341    RT_ASSERT_PREEMPT_CPUID_VAR();
     342    if (!(fFlags & RTMEMHDR_FLAG_ANY_CTX_ALLOC))
     343        RT_ASSERT_INTS_ON();
     344
     345    /*
     346     * Fake up some alignment support.
     347     */
     348    AssertMsgReturn(cbAlignment <= sizeof(void *), ("%zu (%#x)\n", cbAlignment, cbAlignment), VERR_UNSUPPORTED_ALIGNMENT);
     349    if (cb < cbAlignment)
     350        cb = cbAlignment;
     351
     352    /*
     353     * Validate and convert flags.
     354     */
     355    AssertMsgReturn(!(fFlags & ~RTMEMALLOCEX_FLAGS_VALID_MASK), ("%#x\n", fFlags), NULL);
     356    if (fFlags & RTMEMALLOCEX_FLAGS_ZEROED)
     357        fHdrFlags |= RTMEMHDR_FLAG_ZEROED;
     358    if (fFlags & RTMEMALLOCEX_FLAGS_EXEC)
     359        fHdrFlags |= RTMEMHDR_FLAG_EXEC;
     360    if (fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX_ALLOC)
     361        fHdrFlags |= RTMEMHDR_FLAG_ANY_CTX_ALLOC;
     362    if (fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX_FREE)
     363        fHdrFlags |= RTMEMHDR_FLAG_ANY_CTX_FREE;
     364
     365    /*
     366     * Do the allocation.
     367     */
     368    rc = rtR0MemAllocEx(cb + RTR0MEM_FENCE_EXTRA, fHdrFlags, &pHdr);
     369    if (RT_SUCCESS(rc))
     370    {
     371        void *pv;
     372
     373        Assert(pHdr->cbReq  == cb + RTR0MEM_FENCE_EXTRA);
     374        Assert((pHdr->fFlags & fFlags) == fFlags);
     375
     376        /*
     377         * Calc user pointer, initialize the memory if requested, and if
     378         * memory strictness is enable set up the fence.
     379         */
     380        pv = pHdr + 1;
     381        *ppv = pv;
     382        if (fFlags & RTMEMHDR_FLAG_ZEROED)
     383            memset(pv, 0, pHdr->cb);
     384
     385#ifdef RTR0MEM_STRICT
     386        pHdr->cbReq = (uint32_t)cb;
     387        memcpy((uint8_t *)pv + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
     388#endif
     389    }
     390    else if (rc == VERR_NO_MEMORY && (fFlags & RTMEMALLOCEX_FLAGS_EXEC))
     391        rc = VERR_NO_EXEC_MEMORY;
     392
     393    RT_ASSERT_PREEMPT_CPUID();
     394    return rc;
     395}
     396RT_EXPORT_SYMBOL(RTMemAllocExTag);
     397
     398
     399RTDECL(void) RTMemFreeEx(void *pv, size_t cb) RT_NO_THROW
    342400{
    343401    PRTMEMHDR pHdr;
     
    345403    if (!pv)
    346404        return;
     405
     406    AssertPtr(pv);
    347407    pHdr = (PRTMEMHDR)pv - 1;
    348408    if (pHdr->u32Magic == RTMEMHDR_MAGIC)
    349409    {
     410        RT_ASSERT_PREEMPT_CPUID_VAR();
     411
    350412        Assert(pHdr->fFlags & RTMEMHDR_FLAG_ALLOC_EX);
    351         if (!(pHdr->fFlags & RTMEMHDR_FLAG_FREE_ANY_CTX))
     413        if (!(pHdr->fFlags & RTMEMHDR_FLAG_ANY_CTX_FREE))
    352414            RT_ASSERT_INTS_ON();
     415        AssertMsg(pHdr->cbReq == cb, ("cbReq=%zu cb=%zu\n", pHdr->cb, cb));
    353416
    354417#ifdef RTR0MEM_STRICT
     
    362425#endif
    363426        rtR0MemFree(pHdr);
     427        RT_ASSERT_PREEMPT_CPUID();
    364428    }
    365429    else
    366430        AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
    367431}
    368 
     432RT_EXPORT_SYMBOL(RTMemFreeEx);
     433
  • trunk/src/VBox/Runtime/r0drv/alloc-r0drv.h

    r32674 r32707  
    4444    /** Block flags (RTMEMHDR_FLAG_*). */
    4545    uint32_t    fFlags;
    46     /** The actual size of the block. */
     46    /** The actual size of the block, header not included. */
    4747    uint32_t    cb;
    48     /** The request allocation size. */
     48    /** The requested allocation size. */
    4949    uint32_t    cbReq;
    5050} RTMEMHDR, *PRTMEMHDR;
     
    5353/** @name RTMEMHDR::fFlags.
    5454 * @{ */
     55/** Clear the allocated memory. */
    5556#define RTMEMHDR_FLAG_ZEROED        RT_BIT(0)
     57/** Executable flag. */
    5658#define RTMEMHDR_FLAG_EXEC          RT_BIT(1)
    57 #define RTMEMHDR_FLAG_ALLOC_ANY_CTX RT_BIT(2)
    58 #define RTMEMHDR_FLAG_FREE_ANY_CTX  RT_BIT(3)
    59 #define RTMEMHDR_FLAG_ANY_CTX       (RTMEMHDR_FLAG_ALLOC_ANY_CTX | RTMEMHDR_FLAG_FREE_ANY_CTX)
     59/** Use allocation method suitable for any context. */
     60#define RTMEMHDR_FLAG_ANY_CTX_ALLOC RT_BIT(2)
     61/** Use allocation method which allow for freeing in any context. */
     62#define RTMEMHDR_FLAG_ANY_CTX_FREE  RT_BIT(3)
     63/** Both alloc and free in any context (or we're just darn lazy). */
     64#define RTMEMHDR_FLAG_ANY_CTX       (RTMEMHDR_FLAG_ANY_CTX_ALLOC | RTMEMHDR_FLAG_ANY_CTX_FREE)
     65/** Indicate that it was allocated by rtR0MemAllocExTag. */
    6066#define RTMEMHDR_FLAG_ALLOC_EX      RT_BIT(4)
    6167#ifdef RT_OS_LINUX
     68/** Linux: Allocated from the special heap for executable memory. */
    6269# define RTMEMHDR_FLAG_EXEC_HEAP    RT_BIT(30)
     70/** Linux: Allocated by kmalloc() instead of vmalloc(). */
    6371# define RTMEMHDR_FLAG_KMALLOC      RT_BIT(31)
    6472#endif
     
    6674
    6775
    68 PRTMEMHDR   rtR0MemAlloc(size_t cb, uint32_t fFlags);
     76/**
     77 * Heap allocation back end for ring-0.
     78 *
     79 * @returns IPRT status code.  VERR_NO_MEMORY suffices for RTMEMHDR_FLAG_EXEC,
     80 *          the caller will change it to VERR_NO_EXEC_MEMORY when appropriate.
     81 *
     82 * @param   cb          The amount of memory requested by the user.  This does
     83 *                      not include the header.
     84 * @param   fFlags      The allocation flags and more.  These should be
     85 *                      assigned to RTMEMHDR::fFlags together with any flags
     86 *                      the backend might be using.
     87 * @param   ppHdr       Where to return the memory header on success.
     88 */
     89int         rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr);
     90
     91/**
     92 * Free memory allocated by rtR0MemAllocEx.
     93 * @param   pHdr        The memory block to free.  (Never NULL.)
     94 */
    6995void        rtR0MemFree(PRTMEMHDR pHdr);
    70 #define rtR0MemAllocEx(cb, fFlags) rtR0MemAllocExTag((cb), (fFlags), RTMEM_TAG)
    71 void       *rtR0MemAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW;
    72 void        rtR0MemFreeEx(void *pv) RT_NO_THROW;
    7396
    7497RT_C_DECLS_END
  • trunk/src/VBox/Runtime/r0drv/darwin/alloc-r0drv-darwin.cpp

    r32674 r32707  
    4141 * OS specific allocation function.
    4242 */
    43 PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
     43int rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
    4444{
    45     AssertReturn(!(fFlags & RTMEMHDR_FLAG_ANY_CTX), NULL);
     45    if (RT_UNLIKELY(fFlags & RTMEMHDR_FLAG_ANY_CTX))
     46        return VERR_NOT_SUPPORTED;
    4647
    47     PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
    48     if (pHdr)
     48    PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr))
     49    if (RT_UNLIKELY(!pHdr))
    4950    {
    50         pHdr->u32Magic  = RTMEMHDR_MAGIC;
    51         pHdr->fFlags    = fFlags;
    52         pHdr->cb        = cb;
    53         pHdr->cbReq     = cb;
     51        printf("rtR0MemAllocEx(%#zx, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
     52        return VERR_NO_MEMORY;
    5453    }
    55     else
    56         printf("rmMemAlloc(%#zx, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
    57     return pHdr;
     54
     55    pHdr->u32Magic  = RTMEMHDR_MAGIC;
     56    pHdr->fFlags    = fFlags;
     57    pHdr->cb        = cb;
     58    pHdr->cbReq     = cb;
     59    *ppHdr = pHdr;;
     60    return VINF_SUCCESS;
    5861}
    5962
  • trunk/src/VBox/Runtime/r0drv/freebsd/alloc-r0drv-freebsd.c

    r32674 r32707  
    5050
    5151
    52 PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
     52int rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
    5353{
    5454    size_t      cbAllocated = cb;
    5555    PRTMEMHDR   pHdr        = NULL;
    5656
     57#ifdef RT_ARCH_AMD64
    5758    /*
    5859     * Things are a bit more complicated on AMD64 for executable memory
    5960     * because we need to be in the ~2GB..~0 range for code.
    6061     */
    61 #ifdef RT_ARCH_AMD64
    6262    if (fFlags & RTMEMHDR_FLAG_EXEC)
    6363    {
    64         AssertReturn(!(fFlags & RTMEMHDR_FLAG_ANY_CTX), NULL);
     64        if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
     65            return VERR_NOT_SUPPORTED;
    6566
    6667# ifdef USE_KMEM_ALLOC_PROT
     
    7475        pVmObject = vm_object_allocate(OBJT_DEFAULT, cbAllocated >> PAGE_SHIFT);
    7576        if (!pVmObject)
    76             return NULL;
     77            return VERR_NO_EXEC_MEMORY;
    7778
    7879        /* Addr contains a start address vm_map_find will start searching for suitable space at. */
     
    106107    }
    107108
    108     if (pHdr)
    109     {
    110         pHdr->u32Magic   = RTMEMHDR_MAGIC;
    111         pHdr->fFlags     = fFlags;
    112         pHdr->cb         = cbAllocated;
    113         pHdr->cbReq      = cb;
    114         return pHdr;
    115     }
    116     return NULL;
     109    if (RT_UNLIKELY(!pHdr))
     110        return VERR_NO_MEMORY;
     111
     112    pHdr->u32Magic   = RTMEMHDR_MAGIC;
     113    pHdr->fFlags     = fFlags;
     114    pHdr->cb         = cbAllocated;
     115    pHdr->cbReq      = cb;
     116
     117    *ppHdr = pHdr;
     118    return VINF_SUCCESS;
    117119}
    118120
  • trunk/src/VBox/Runtime/r0drv/linux/alloc-r0drv-linux.c

    r32674 r32707  
    111111 * OS specific allocation function.
    112112 */
    113 PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
     113int rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
    114114{
    115115    PRTMEMHDR pHdr;
     
    120120    if (fFlags & RTMEMHDR_FLAG_EXEC)
    121121    {
    122         AssertReturn(!(fFlags & RTMEMHDR_FLAG_ANY_CTX), NULL);
     122        if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
     123            return VERR_NOT_SUPPORTED;
    123124
    124125#if defined(RT_ARCH_AMD64)
     
    149150        {
    150151            fFlags |= RTMEMHDR_FLAG_KMALLOC;
    151             pHdr = kmalloc(cb + sizeof(*pHdr), GFP_KERNEL);
     152            pHdr = kmalloc(cb + sizeof(*pHdr),
     153                           (fFlags & RTMEMHDR_FLAG_ANY_CTX_ALLOC) ? GFP_ATOMIC : GFP_KERNEL);
    152154        }
    153155        else
    154156            pHdr = vmalloc(cb + sizeof(*pHdr));
    155157    }
     158    if (RT_UNLIKELY(!pHdr))
     159        return VERR_NO_MEMORY;
    156160
    157161    /*
    158162     * Initialize.
    159163     */
    160     if (pHdr)
    161     {
    162         pHdr->u32Magic  = RTMEMHDR_MAGIC;
    163         pHdr->fFlags    = fFlags;
    164         pHdr->cb        = cb;
    165         pHdr->cbReq     = cb;
    166     }
    167     return pHdr;
     164    pHdr->u32Magic  = RTMEMHDR_MAGIC;
     165    pHdr->fFlags    = fFlags;
     166    pHdr->cb        = cb;
     167    pHdr->cbReq     = cb;
     168
     169    *ppHdr = pHdr;
     170    return VINF_SUCCESS;
    168171}
    169172
  • trunk/src/VBox/Runtime/r0drv/linux/timer-r0drv-linux.c

    r32670 r32707  
    434434     */
    435435    ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
    436 #if 0 /*fixme*/
    437     rtR0MemFreeNoCtxCheck(pTimer);
    438 #else
    439     RTMemFree(pTimer);
    440 #endif
     436    RTMemFreeEx(pTimer, RT_OFFSETOF(RTTIMER, aSubTimers[pTimer->cCpus]));
    441437    if (hSpinlock != NIL_RTSPINLOCK)
    442438        RTSpinlockDestroy(hSpinlock);
     
    13931389    RTCPUID     iCpu;
    13941390    unsigned    cCpus;
     1391    int         rc;
    13951392
    13961393    *ppTimer = NULL;
     
    14191416#endif
    14201417
    1421     pTimer = (PRTTIMER)RTMemAllocZ(RT_OFFSETOF(RTTIMER, aSubTimers[cCpus]));
    1422     if (!pTimer)
    1423         return VERR_NO_MEMORY;
     1418    rc = RTMemAllocEx(RT_OFFSETOF(RTTIMER, aSubTimers[cCpus]), 0,
     1419                      RTMEMALLOCEX_FLAGS_ZEROED | RTMEMALLOCEX_FLAGS_ANY_CTX_FREE, (void **)&pTimer);
     1420    if (RT_FAILURE(rc))
     1421        return rc;
    14241422
    14251423    /*
  • trunk/src/VBox/Runtime/r0drv/nt/alloc-r0drv-nt.cpp

    r32674 r32707  
    3939 * OS specific allocation function.
    4040 */
    41 PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
     41int rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
    4242{
    43     AssertReturn(!(fFlags & RTMEMHDR_FLAG_ANY_CTX), NULL);
     43    if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
     44        return VERR_NOT_SUPPORTED;
    4445
    4546    PRTMEMHDR pHdr = (PRTMEMHDR)ExAllocatePoolWithTag(NonPagedPool, cb + sizeof(*pHdr), IPRT_NT_POOL_TAG);
    46     if (pHdr)
    47     {
    48         pHdr->u32Magic  = RTMEMHDR_MAGIC;
    49         pHdr->fFlags    = fFlags;
    50         pHdr->cb        = (uint32_t)cb; Assert(pHdr->cb == cb);
    51         pHdr->cbReq     = (uint32_t)cb;
    52     }
    53     return pHdr;
     47    if (RT_UNLIKELY(!pHdr))
     48        return VERR_NO_MEMORY;
     49
     50    pHdr->u32Magic  = RTMEMHDR_MAGIC;
     51    pHdr->fFlags    = fFlags;
     52    pHdr->cb        = (uint32_t)cb; Assert(pHdr->cb == cb);
     53    pHdr->cbReq     = (uint32_t)cb;
     54    *ppHdr = pHdr;
     55    return VINF_SUCCESS;
    5456}
    5557
  • trunk/src/VBox/Runtime/r0drv/os2/alloc-r0drv-os2.cpp

    r32674 r32707  
    4141
    4242
    43 PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
     43int rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
    4444{
    45     AssertReturn(!(fFlags & RTMEMHDR_FLAG_ANY_CTX), NULL);
     45    if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
     46        return VERR_NOT_SUPPORTED;
    4647
    4748    void *pv = NULL;
    4849    APIRET rc = KernVMAlloc(cb + sizeof(RTMEMHDR), VMDHA_FIXED, &pv, (void **)-1, NULL);
    49     if (!rc)
    50     {
    51         PRTMEMHDR pHdr = (PRTMEMHDR)pv;
    52         pHdr->u32Magic   = RTMEMHDR_MAGIC;
    53         pHdr->fFlags     = fFlags;
    54         pHdr->cb         = cb;
    55         pHdr->cbReq      = cb;
    56         return pHdr;
    57     }
    58     return NULL;
     50    if (RT_UNLIKELY(rc != NO_ERROR))
     51        return RTErrConvertFromOS2(rc);
     52
     53    PRTMEMHDR   pHdr = (PRTMEMHDR)pv;
     54    pHdr->u32Magic   = RTMEMHDR_MAGIC;
     55    pHdr->fFlags     = fFlags;
     56    pHdr->cb         = cb;
     57    pHdr->cbReq      = cb;
     58    *ppHdr = pHdr;
     59    return VINF_SUCCESS;
    5960}
    6061
  • trunk/src/VBox/Runtime/r0drv/solaris/vbi/alloc-r0drv-solaris.c

    r32674 r32707  
    4444 * OS specific allocation function.
    4545 */
    46 PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
     46int rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
    4747{
    4848    size_t      cbAllocated = cb;
     
    5959#endif
    6060    {
    61         unsigned fKmFlags = fFlags & RTMEMHDR_FLAG_ALLOC_ANY_CTX ? KM_NOSLEEP : KM_SLEEP;
     61        unsigned fKmFlags = fFlags & RTMEMHDR_FLAG_ANY_CTX_ALLOC ? KM_NOSLEEP : KM_SLEEP;
    6262        if (fFlags & RTMEMHDR_FLAG_ZEROED)
    6363            pHdr = (PRTMEMHDR)kmem_zalloc(cb + sizeof(*pHdr), fKmFlags);
     
    6565            pHdr = (PRTMEMHDR)kmem_alloc(cb + sizeof(*pHdr), fKmFlags);
    6666    }
    67     if (pHdr)
     67    if (RT_UNLIKELY(!pHdr))
    6868    {
    69         pHdr->u32Magic  = RTMEMHDR_MAGIC;
    70         pHdr->fFlags    = fFlags;
    71         pHdr->cb        = cbAllocated;
    72         pHdr->cbReq     = cb;
     69        LogRel(("rtMemAllocEx(%u, %#x) failed\n", (unsigned)cb + sizeof(*pHdr), fFlags));
     70        return VERR_NO_MEMORY;
    7371    }
    74     else
    75         LogRel(("rtMemAlloc(%u, %#x) failed\n", (unsigned)cb + sizeof(*pHdr), fFlags));
    76     return pHdr;
     72
     73    pHdr->u32Magic  = RTMEMHDR_MAGIC;
     74    pHdr->fFlags    = fFlags;
     75    pHdr->cb        = cbAllocated;
     76    pHdr->cbReq     = cb;
     77
     78    *ppHdr = pHdr;
     79    return VINF_SUCCESS;
    7780}
    7881
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