alloc-ef.h revision 256f29fc441df62ed8f175b5248f217cdd08e41b
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/* $Id$ */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/** @file
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * IPRT - Memory Allocation, electric fence.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/*
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Copyright (C) 2006-2010 Sun Microsystems, Inc.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync *
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * available from http://www.virtualbox.org. This file is free software;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * you can redistribute it and/or modify it under the terms of the GNU
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * General Public License (GPL) as published by the Free Software
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync *
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * The contents of this file may alternatively be used under the terms
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * of the Common Development and Distribution License Version 1.0
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * VirtualBox OSE distribution, in which case the provisions of the
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * CDDL are applicable instead of those of the GPL.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync *
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * You may elect to license modified versions of this file under the
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * terms and conditions of either the GPL or the CDDL or both.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync *
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * additional information or have any questions.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
c8bcebedf264bc1287bcce50bdf66d08e28a88dcvboxsync
5debbbcc37114f1dbecaecfc66c81ea2fbda6140vboxsync#ifndef ___alloc_ef_h
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#define ___alloc_ef_h
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/*******************************************************************************
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync* Defined Constants And Macros *
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync*******************************************************************************/
1f6b1f68eb26f8af1f9a4b5ffd00df66ad7af1devboxsync#if defined(DOXYGEN_RUNNING)
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync# define RTALLOC_USE_EFENCE
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync# define RTALLOC_EFENCE_IN_FRONT
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync# define RTALLOC_EFENCE_FREE_FILL 'f'
1f6b1f68eb26f8af1f9a4b5ffd00df66ad7af1devboxsync#endif
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync/** @def RTALLOC_USE_EFENCE
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync * If defined the electric fence put up for ALL allocations by RTMemAlloc(),
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync * RTMemAllocZ(), RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ().
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync */
1f6b1f68eb26f8af1f9a4b5ffd00df66ad7af1devboxsync#if 1
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync# define RTALLOC_USE_EFENCE
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync#endif
d44cb80e1d772854c10ef9ae03daba8dcf61d389vboxsync
d44cb80e1d772854c10ef9ae03daba8dcf61d389vboxsync/** @def RTALLOC_EFENCE_SIZE
d44cb80e1d772854c10ef9ae03daba8dcf61d389vboxsync * The size of the fence. This must be page aligned.
d44cb80e1d772854c10ef9ae03daba8dcf61d389vboxsync */
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync#define RTALLOC_EFENCE_SIZE PAGE_SIZE
3c0e6e6c131cfdaeaeaea598603f6ef6ab10e781vboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/** @def RTALLOC_EFENCE_IN_FRONT
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Define this to put the fence up in front of the block.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * The default (when this isn't defined) is to up it up after the block.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync//# define RTALLOC_EFENCE_IN_FRONT
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/** @def RTALLOC_EFENCE_TRACE
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Define this to support actual free and reallocation of blocks.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#define RTALLOC_EFENCE_TRACE
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/** @def RTALLOC_EFENCE_FREE_DELAYED
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * This define will enable free() delay and protection of the freed data
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * while it's being delayed. The value of RTALLOC_EFENCE_FREE_DELAYED defines
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * the threshold of the delayed blocks.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Delayed blocks does not consume any physical memory, only virtual address space.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Requires RTALLOC_EFENCE_TRACE.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#define RTALLOC_EFENCE_FREE_DELAYED (20 * _1M)
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/** @def RTALLOC_EFENCE_FREE_FILL
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * This define will enable memset(,RTALLOC_EFENCE_FREE_FILL,)'ing the user memory
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * in the block before freeing/decommitting it. This is useful in GDB since GDB
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * appeares to be able to read the content of the page even after it's been
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * decommitted.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Requires RTALLOC_EFENCE_TRACE.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync# define RTALLOC_EFENCE_FREE_FILL 'f'
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#endif
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/** @def RTALLOC_EFENCE_FILLER
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * This define will enable memset(,RTALLOC_EFENCE_FILLER,)'ing the allocated
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * memory when the API doesn't require it to be zero'ed.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#define RTALLOC_EFENCE_FILLER 0xef
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/** @def RTALLOC_EFENCE_NOMAN_FILLER
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * This define will enable memset(,RTALLOC_EFENCE_NOMAN_FILLER,)'ing the
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * unprotected but not allocated area of memory, the so called no man's land.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#define RTALLOC_EFENCE_NOMAN_FILLER 0xaa
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/** @def RTALLOC_EFENCE_FENCE_FILLER
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * This define will enable memset(,RTALLOC_EFENCE_FENCE_FILLER,)'ing the
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * fence itself, as debuggers can usually read them.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#define RTALLOC_EFENCE_FENCE_FILLER 0xcc
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#if defined(DOXYGEN_RUNNING)
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/** @def RTALLOC_EFENCE_CPP
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * This define will enable the new and delete wrappers.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync# define RTALLOC_EFENCE_CPP
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#endif
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
e7e60b177e688900656c04404c5123f1dfa1d02cvboxsync/*******************************************************************************
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync* Header Files *
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync*******************************************************************************/
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#ifdef RT_OS_WINDOWS
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync# include <Windows.h>
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#else
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync# include <sys/mman.h>
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#endif
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#include <iprt/avl.h>
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#include <iprt/thread.h>
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
ada022f71e6728d4f2b15e643d35aa94992656c7vboxsync
bf4de0c2965291e927412f708c6c27eeeb5825d4vboxsync/*******************************************************************************
bf4de0c2965291e927412f708c6c27eeeb5825d4vboxsync* Structures and Typedefs *
bf4de0c2965291e927412f708c6c27eeeb5825d4vboxsync*******************************************************************************/
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/**
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Allocation types.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsynctypedef enum RTMEMTYPE
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync{
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync RTMEMTYPE_RTMEMALLOC,
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync RTMEMTYPE_RTMEMALLOCZ,
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync RTMEMTYPE_RTMEMREALLOC,
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync RTMEMTYPE_RTMEMFREE,
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync RTMEMTYPE_NEW,
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync RTMEMTYPE_NEW_ARRAY,
bf4de0c2965291e927412f708c6c27eeeb5825d4vboxsync RTMEMTYPE_DELETE,
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync RTMEMTYPE_DELETE_ARRAY
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync} RTMEMTYPE;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#ifdef RTALLOC_EFENCE_TRACE
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/**
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * Node tracking a memory allocation.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsynctypedef struct RTMEMBLOCK
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync{
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync /** Avl node code, key is the user block pointer. */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync AVLPVNODECORE Core;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync /** Allocation type. */
a2d53f020c57ede4a469a06d8ef8f735c7a46596vboxsync RTMEMTYPE enmType;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync /** The size of the block. */
a2d53f020c57ede4a469a06d8ef8f735c7a46596vboxsync size_t cb;
a2d53f020c57ede4a469a06d8ef8f735c7a46596vboxsync /** The return address of the allocator function. */
a2d53f020c57ede4a469a06d8ef8f735c7a46596vboxsync void *pvCaller;
a2d53f020c57ede4a469a06d8ef8f735c7a46596vboxsync /** Line number of the alloc call. */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync unsigned iLine;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync /** File from within the allocation was made. */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync const char *pszFile;
e7e60b177e688900656c04404c5123f1dfa1d02cvboxsync /** Function from within the allocation was made. */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync const char *pszFunction;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync} RTMEMBLOCK, *PRTMEMBLOCK;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#endif
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync/*******************************************************************************
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync* Internal Functions *
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync******************************************************************************/
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsyncRT_C_DECLS_BEGIN
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsyncvoid * rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
bf4de0c2965291e927412f708c6c27eeeb5825d4vboxsyncvoid * rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
bf4de0c2965291e927412f708c6c27eeeb5825d4vboxsyncvoid rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsyncRT_C_DECLS_END
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#endif
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync