alloc-ef.cpp revision 256f29fc441df62ed8f175b5248f217cdd08e41b
/* $Id$ */
/** @file
* IPRT - Memory Allocation, electric fence.
*/
/*
* Copyright (C) 2006-2010 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "alloc-ef.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifdef RTALLOC_EFENCE_TRACE
/** Spinlock protecting the allthe blocks globals. */
static volatile uint32_t g_BlocksLock;
/** Tree tracking the allocations. */
static AVLPVTREE g_BlocksTree;
#ifdef RTALLOC_EFENCE_FREE_DELAYED
/** Tail of the delayed blocks. */
static volatile PRTMEMBLOCK g_pBlocksDelayHead;
/** Tail of the delayed blocks. */
static volatile PRTMEMBLOCK g_pBlocksDelayTail;
/** Number of bytes in the delay list (includes fences). */
static volatile size_t g_cbBlocksDelay;
#endif
#endif
/** Array of pointers free watches for. */
/** Enable logging of all freed memory. */
bool gfRTMemFreeLog = false;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Complains about something.
*/
{
}
/**
* Log an event.
*/
{
#if 0
#endif
}
#ifdef RTALLOC_EFENCE_TRACE
/**
* Aquires the lock.
*/
static inline void rtmemBlockLock(void)
{
unsigned c = 0;
}
/**
* Releases the lock.
*/
static inline void rtmemBlockUnlock(void)
{
ASMAtomicXchgU32(&g_BlocksLock, 0);
}
/**
* Creates a block.
*/
static inline PRTMEMBLOCK rtmemBlockCreate(RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
{
if (pBlock)
{
}
return pBlock;
}
/**
* Frees a block.
*/
{
}
/**
* Insert a block from the tree.
*/
{
}
/**
* Remove a block from the tree and returns it to the caller.
*/
{
return pBlock;
}
/**
* Gets a block.
*/
{
return pBlock;
}
/**
* Dumps one allocation.
*/
{
return 0;
}
/**
* Dumps the allocated blocks.
* This is something which you should call from gdb.
*/
extern "C" void RTMemDump(void);
void RTMemDump(void)
{
}
#ifdef RTALLOC_EFENCE_FREE_DELAYED
/**
* Insert a delayed block.
*/
{
if (g_pBlocksDelayHead)
{
}
else
{
}
}
/**
* Removes a delayed block.
*/
static inline PRTMEMBLOCK rtmemBlockDelayRemove(void)
{
{
if (pBlock)
{
else
}
}
return pBlock;
}
#endif /* DELAY */
#endif /* RTALLOC_EFENCE_TRACE */
/**
* Internal allocator.
*/
void *rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
{
/*
* Sanity.
*/
&& RTALLOC_EFENCE_SIZE <= 0)
{
return NULL;
}
if (!cb)
{
#if 0
return NULL;
#else
cb = 1;
#endif
}
/** @todo this alignment decreases fence accuracy, but there are lots
* of places in VirtualBox which assumes that the allocation is aligned
* properly even for totally unusual allocation sizes, otherwise assertions
* are triggered, which ensure a particular alignment of elements. */
#ifdef RTALLOC_EFENCE_TRACE
/*
* Allocate the trace block.
*/
if (!pBlock)
{
return NULL;
}
#endif
/*
* Allocate a block with page alignment space + the size of the E-fence.
*/
if (pvBlock)
{
/*
* Calc the start of the fence and the user block
* and then change the page protection of the fence.
*/
#ifdef RTALLOC_EFENCE_IN_FRONT
#ifdef RTALLOC_EFENCE_NOMAN_FILLER
#endif
#else
#ifdef RTALLOC_EFENCE_NOMAN_FILLER
#endif
#endif
#ifdef RTALLOC_EFENCE_FENCE_FILLER
#endif
if (!rc)
{
#ifdef RTALLOC_EFENCE_TRACE
#endif
if (enmType == RTMEMTYPE_RTMEMALLOCZ)
#ifdef RTALLOC_EFENCE_FILLER
else
#endif
rtmemLog(pszOp, "returns %p (pvBlock=%p cbBlock=%#x pvEFence=%p cb=%#x)\n", pv, pvBlock, cbBlock, pvEFence, cb);
return pv;
}
rtmemComplain(pszOp, "RTMemProtect failed, pvEFence=%p size %d, rc=%d\n", pvEFence, RTALLOC_EFENCE_SIZE, rc);
}
else
#ifdef RTALLOC_EFENCE_TRACE
#endif
return NULL;
}
/**
* Internal free.
*/
void rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
{
/*
* Simple case.
*/
if (!pv)
return;
/*
* Check watch points.
*/
for (unsigned i = 0; i < RT_ELEMENTS(gapvRTMemFreeWatch); i++)
if (gapvRTMemFreeWatch[i] == pv)
#ifdef RTALLOC_EFENCE_TRACE
/*
* Find the block.
*/
if (pBlock)
{
if (gfRTMemFreeLog)
#ifdef RTALLOC_EFENCE_NOMAN_FILLER
/*
* Check whether the no man's land is untouched.
*/
# ifdef RTALLOC_EFENCE_IN_FRONT
# else
# endif
void *p = ASMMemIsAll8(pvNoMan,
if (p)
#endif
#ifdef RTALLOC_EFENCE_FREE_FILL
/*
* Fill the user part of the block.
*/
#endif
#if defined(RTALLOC_EFENCE_FREE_DELAYED) && RTALLOC_EFENCE_FREE_DELAYED > 0
/*
* We're doing delayed freeing.
* That means we'll expand the E-fence to cover the entire block.
*/
if (RT_SUCCESS(rc))
{
/*
* Insert it into the free list and process pending frees.
*/
{
#ifdef RTALLOC_EFENCE_IN_FRONT
#else
#endif
if (RT_SUCCESS(rc))
else
rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvBlock, cbBlock, rc);
}
}
else
#else /* !RTALLOC_EFENCE_FREE_DELAYED */
/*
* Turn of the E-fence and free it.
*/
#ifdef RTALLOC_EFENCE_IN_FRONT
#else
#endif
if (RT_SUCCESS(rc))
else
rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvEFence, RTALLOC_EFENCE_SIZE, rc);
#endif /* !RTALLOC_EFENCE_FREE_DELAYED */
}
else
#else /* !RTALLOC_EFENCE_TRACE */
/*
* We have no size tracking, so we're not doing any freeing because
* we cannot if the E-fence is after the block.
* Let's just expand the E-fence to the first page of the user bit
* since we know that it's around.
*/
if (RT_FAILURE(rc))
rtmemComplain(pszOp, "RTMemProtect(%p, PAGE_SIZE, RTMEM_PROT_NONE) -> %d\n", (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK), rc);
#endif /* !RTALLOC_EFENCE_TRACE */
}
/**
* Internal realloc.
*/
void *rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
{
/*
* Allocate new and copy.
*/
if (!pvOld)
if (!cbNew)
{
return NULL;
}
#ifdef RTALLOC_EFENCE_TRACE
/*
* Get the block, allocate the new, copy the data, free the old one.
*/
if (pBlock)
{
if (pvRet)
{
}
return pvRet;
}
else
return NULL;
#else /* !RTALLOC_EFENCE_TRACE */
return NULL;
#endif /* !RTALLOC_EFENCE_TRACE */
}
/**
* Same as RTMemTmpAlloc() except that it's fenced.
*
* @returns Pointer to the allocated memory.
* @returns NULL on failure.
* @param cb Size in bytes of the memory block to allocate.
*/
{
return RTMemEfAlloc(cb);
}
/**
* Same as RTMemTmpAllocZ() except that it's fenced.
*
* @returns Pointer to the allocated memory.
* @returns NULL on failure.
* @param cb Size in bytes of the memory block to allocate.
*/
{
return RTMemEfAllocZ(cb);
}
/**
* Same as RTMemTmpFree() except that it's for fenced memory.
*
* @param pv Pointer to memory block.
*/
{
}
/**
* Same as RTMemAlloc() except that it's fenced.
*
* @returns Pointer to the allocated memory. Free with RTMemEfFree().
* @returns NULL on failure.
* @param cb Size in bytes of the memory block to allocate.
*/
{
}
/**
* Same as RTMemAllocZ() except that it's fenced.
*
* @returns Pointer to the allocated memory.
* @returns NULL on failure.
* @param cb Size in bytes of the memory block to allocate.
*/
{
}
/**
* Same as RTMemRealloc() except that it's fenced.
*
* @returns Pointer to the allocated memory.
* @returns NULL on failure.
* @param pvOld The memory block to reallocate.
* @param cbNew The new block size (in bytes).
*/
{
return rtMemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, ((void **)&pvOld)[-1], 0, NULL, NULL);
}
/**
* Free memory allocated by any of the RTMemEf* allocators.
*
* @param pv Pointer to memory block.
*/
{
if (pv)
}
/**
* Same as RTMemDup() except that it's fenced.
*
* @returns New heap block with the duplicate data.
* @returns NULL if we're out of memory.
* @param pvSrc The memory to duplicate.
* @param cb The amount of memory to duplicate.
*/
{
if (pvDst)
return pvDst;
}
/**
* Same as RTMemDupEx except that it's fenced.
*
* @returns New heap block with the duplicate data.
* @returns NULL if we're out of memory.
* @param pvSrc The memory to duplicate.
* @param cbSrc The amount of memory to duplicate.
* @param cbExtra The amount of extra memory to allocate and zero.
*/
{
if (pvDst)
{
}
return pvDst;
}