VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/alloc-ef.h@ 28298

Last change on this file since 28298 was 28298, checked in by vboxsync, 15 years ago

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1/* $Id: alloc-ef.h 28298 2010-04-14 12:20:39Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, electric fence.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31#ifndef ___alloc_ef_h
32#define ___alloc_ef_h
33
34/*******************************************************************************
35* Defined Constants And Macros *
36*******************************************************************************/
37#if defined(DOXYGEN_RUNNING)
38# define RTALLOC_USE_EFENCE
39# define RTALLOC_EFENCE_IN_FRONT
40# define RTALLOC_EFENCE_FREE_FILL 'f'
41#endif
42
43/** @def RTALLOC_USE_EFENCE
44 * If defined the electric fence put up for ALL allocations by RTMemAlloc(),
45 * RTMemAllocZ(), RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ().
46 */
47#if 0
48# define RTALLOC_USE_EFENCE
49#endif
50
51/** @def RTALLOC_EFENCE_SIZE
52 * The size of the fence. This must be page aligned.
53 */
54#define RTALLOC_EFENCE_SIZE PAGE_SIZE
55
56/** @def RTALLOC_EFENCE_ALIGNMENT
57 * The allocation alignment, power of two of course.
58 *
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.
62 */
63#if 0
64# define RTALLOC_EFENCE_ALIGNMENT (ARCH_BITS / 8)
65#else
66# define RTALLOC_EFENCE_ALIGNMENT 1
67#endif
68
69/** @def RTALLOC_EFENCE_IN_FRONT
70 * Define this to put the fence up in front of the block.
71 * The default (when this isn't defined) is to up it up after the block.
72 */
73//# define RTALLOC_EFENCE_IN_FRONT
74
75/** @def RTALLOC_EFENCE_TRACE
76 * Define this to support actual free and reallocation of blocks.
77 */
78#define RTALLOC_EFENCE_TRACE
79
80/** @def RTALLOC_EFENCE_FREE_DELAYED
81 * This define will enable free() delay and protection of the freed data
82 * while it's being delayed. The value of RTALLOC_EFENCE_FREE_DELAYED defines
83 * the threshold of the delayed blocks.
84 * Delayed blocks does not consume any physical memory, only virtual address space.
85 * Requires RTALLOC_EFENCE_TRACE.
86 */
87#define RTALLOC_EFENCE_FREE_DELAYED (20 * _1M)
88
89/** @def RTALLOC_EFENCE_FREE_FILL
90 * This define will enable memset(,RTALLOC_EFENCE_FREE_FILL,)'ing the user memory
91 * in the block before freeing/decommitting it. This is useful in GDB since GDB
92 * appeares to be able to read the content of the page even after it's been
93 * decommitted.
94 * Requires RTALLOC_EFENCE_TRACE.
95 */
96#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
97# define RTALLOC_EFENCE_FREE_FILL 'f'
98#endif
99
100/** @def RTALLOC_EFENCE_FILLER
101 * This define will enable memset(,RTALLOC_EFENCE_FILLER,)'ing the allocated
102 * memory when the API doesn't require it to be zero'ed.
103 */
104#define RTALLOC_EFENCE_FILLER 0xef
105
106/** @def RTALLOC_EFENCE_NOMAN_FILLER
107 * This define will enable memset(,RTALLOC_EFENCE_NOMAN_FILLER,)'ing the
108 * unprotected but not allocated area of memory, the so called no man's land.
109 */
110#define RTALLOC_EFENCE_NOMAN_FILLER 0xaa
111
112/** @def RTALLOC_EFENCE_FENCE_FILLER
113 * This define will enable memset(,RTALLOC_EFENCE_FENCE_FILLER,)'ing the
114 * fence itself, as debuggers can usually read them.
115 */
116#define RTALLOC_EFENCE_FENCE_FILLER 0xcc
117
118#if defined(DOXYGEN_RUNNING)
119/** @def RTALLOC_EFENCE_CPP
120 * This define will enable the new and delete wrappers.
121 */
122# define RTALLOC_EFENCE_CPP
123#endif
124
125
126
127/*******************************************************************************
128* Header Files *
129*******************************************************************************/
130#ifdef RT_OS_WINDOWS
131# include <Windows.h>
132#else
133# include <sys/mman.h>
134#endif
135#include <iprt/avl.h>
136#include <iprt/thread.h>
137
138
139/*******************************************************************************
140* Structures and Typedefs *
141*******************************************************************************/
142/**
143 * Allocation types.
144 */
145typedef enum RTMEMTYPE
146{
147 RTMEMTYPE_RTMEMALLOC,
148 RTMEMTYPE_RTMEMALLOCZ,
149 RTMEMTYPE_RTMEMREALLOC,
150 RTMEMTYPE_RTMEMFREE,
151
152 RTMEMTYPE_NEW,
153 RTMEMTYPE_NEW_ARRAY,
154 RTMEMTYPE_DELETE,
155 RTMEMTYPE_DELETE_ARRAY
156} RTMEMTYPE;
157
158#ifdef RTALLOC_EFENCE_TRACE
159/**
160 * Node tracking a memory allocation.
161 */
162typedef struct RTMEMBLOCK
163{
164 /** Avl node code, key is the user block pointer. */
165 AVLPVNODECORE Core;
166 /** Allocation type. */
167 RTMEMTYPE enmType;
168 /** The unaligned size of the block. */
169 size_t cbUnaligned;
170 /** The aligned size of the block. */
171 size_t cbAligned;
172 /** The return address of the allocator function. */
173 void *pvCaller;
174 /** Line number of the alloc call. */
175 unsigned iLine;
176 /** File from within the allocation was made. */
177 const char *pszFile;
178 /** Function from within the allocation was made. */
179 const char *pszFunction;
180} RTMEMBLOCK, *PRTMEMBLOCK;
181
182#endif
183
184
185/*******************************************************************************
186* Internal Functions *
187******************************************************************************/
188RT_C_DECLS_BEGIN
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);
193RT_C_DECLS_END
194
195#endif
196
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette