1 | /* $Id: alloc-ef.h 31157 2010-07-28 03:15:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, electric fence.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. 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 |
|
---|
27 | #ifndef ___alloc_ef_h
|
---|
28 | #define ___alloc_ef_h
|
---|
29 |
|
---|
30 | /*******************************************************************************
|
---|
31 | * Defined Constants And Macros *
|
---|
32 | *******************************************************************************/
|
---|
33 | #if defined(DOXYGEN_RUNNING)
|
---|
34 | # define RTALLOC_USE_EFENCE
|
---|
35 | # define RTALLOC_EFENCE_IN_FRONT
|
---|
36 | # define RTALLOC_EFENCE_FREE_FILL 'f'
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | /** @def RTALLOC_USE_EFENCE
|
---|
40 | * If defined the electric fence put up for ALL allocations by RTMemAlloc(),
|
---|
41 | * RTMemAllocZ(), RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ().
|
---|
42 | */
|
---|
43 | #if 0
|
---|
44 | # define RTALLOC_USE_EFENCE
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | /** @def RTALLOC_EFENCE_SIZE
|
---|
48 | * The size of the fence. This must be page aligned.
|
---|
49 | */
|
---|
50 | #define RTALLOC_EFENCE_SIZE PAGE_SIZE
|
---|
51 |
|
---|
52 | /** @def RTALLOC_EFENCE_ALIGNMENT
|
---|
53 | * The allocation alignment, power of two of course.
|
---|
54 | *
|
---|
55 | * Use this for working around misaligned sizes, usually stemming from
|
---|
56 | * allocating a string or something after the main structure. When you
|
---|
57 | * encounter this, please fix the allocation to RTMemAllocVar or RTMemAllocZVar.
|
---|
58 | */
|
---|
59 | #if 0
|
---|
60 | # define RTALLOC_EFENCE_ALIGNMENT (ARCH_BITS / 8)
|
---|
61 | #else
|
---|
62 | # define RTALLOC_EFENCE_ALIGNMENT 1
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | /** @def RTALLOC_EFENCE_IN_FRONT
|
---|
66 | * Define this to put the fence up in front of the block.
|
---|
67 | * The default (when this isn't defined) is to up it up after the block.
|
---|
68 | */
|
---|
69 | //# define RTALLOC_EFENCE_IN_FRONT
|
---|
70 |
|
---|
71 | /** @def RTALLOC_EFENCE_TRACE
|
---|
72 | * Define this to support actual free and reallocation of blocks.
|
---|
73 | */
|
---|
74 | #define RTALLOC_EFENCE_TRACE
|
---|
75 |
|
---|
76 | /** @def RTALLOC_EFENCE_FREE_DELAYED
|
---|
77 | * This define will enable free() delay and protection of the freed data
|
---|
78 | * while it's being delayed. The value of RTALLOC_EFENCE_FREE_DELAYED defines
|
---|
79 | * the threshold of the delayed blocks.
|
---|
80 | * Delayed blocks does not consume any physical memory, only virtual address space.
|
---|
81 | * Requires RTALLOC_EFENCE_TRACE.
|
---|
82 | */
|
---|
83 | #define RTALLOC_EFENCE_FREE_DELAYED (20 * _1M)
|
---|
84 |
|
---|
85 | /** @def RTALLOC_EFENCE_FREE_FILL
|
---|
86 | * This define will enable memset(,RTALLOC_EFENCE_FREE_FILL,)'ing the user memory
|
---|
87 | * in the block before freeing/decommitting it. This is useful in GDB since GDB
|
---|
88 | * appeares to be able to read the content of the page even after it's been
|
---|
89 | * decommitted.
|
---|
90 | * Requires RTALLOC_EFENCE_TRACE.
|
---|
91 | */
|
---|
92 | #if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
|
---|
93 | # define RTALLOC_EFENCE_FREE_FILL 'f'
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | /** @def RTALLOC_EFENCE_FILLER
|
---|
97 | * This define will enable memset(,RTALLOC_EFENCE_FILLER,)'ing the allocated
|
---|
98 | * memory when the API doesn't require it to be zero'ed.
|
---|
99 | */
|
---|
100 | #define RTALLOC_EFENCE_FILLER 0xef
|
---|
101 |
|
---|
102 | /** @def RTALLOC_EFENCE_NOMAN_FILLER
|
---|
103 | * This define will enable memset(,RTALLOC_EFENCE_NOMAN_FILLER,)'ing the
|
---|
104 | * unprotected but not allocated area of memory, the so called no man's land.
|
---|
105 | */
|
---|
106 | #define RTALLOC_EFENCE_NOMAN_FILLER 0xaa
|
---|
107 |
|
---|
108 | /** @def RTALLOC_EFENCE_FENCE_FILLER
|
---|
109 | * This define will enable memset(,RTALLOC_EFENCE_FENCE_FILLER,)'ing the
|
---|
110 | * fence itself, as debuggers can usually read them.
|
---|
111 | */
|
---|
112 | #define RTALLOC_EFENCE_FENCE_FILLER 0xcc
|
---|
113 |
|
---|
114 | #if defined(DOXYGEN_RUNNING)
|
---|
115 | /** @def RTALLOC_EFENCE_CPP
|
---|
116 | * This define will enable the new and delete wrappers.
|
---|
117 | */
|
---|
118 | # define RTALLOC_EFENCE_CPP
|
---|
119 | #endif
|
---|
120 |
|
---|
121 |
|
---|
122 |
|
---|
123 | /*******************************************************************************
|
---|
124 | * Header Files *
|
---|
125 | *******************************************************************************/
|
---|
126 | #ifdef RT_OS_WINDOWS
|
---|
127 | # include <Windows.h>
|
---|
128 | #else
|
---|
129 | # include <sys/mman.h>
|
---|
130 | #endif
|
---|
131 | #include <iprt/avl.h>
|
---|
132 | #include <iprt/thread.h>
|
---|
133 |
|
---|
134 |
|
---|
135 | /*******************************************************************************
|
---|
136 | * Structures and Typedefs *
|
---|
137 | *******************************************************************************/
|
---|
138 | /**
|
---|
139 | * Allocation types.
|
---|
140 | */
|
---|
141 | typedef enum RTMEMTYPE
|
---|
142 | {
|
---|
143 | RTMEMTYPE_RTMEMALLOC,
|
---|
144 | RTMEMTYPE_RTMEMALLOCZ,
|
---|
145 | RTMEMTYPE_RTMEMREALLOC,
|
---|
146 | RTMEMTYPE_RTMEMFREE,
|
---|
147 |
|
---|
148 | RTMEMTYPE_NEW,
|
---|
149 | RTMEMTYPE_NEW_ARRAY,
|
---|
150 | RTMEMTYPE_DELETE,
|
---|
151 | RTMEMTYPE_DELETE_ARRAY
|
---|
152 | } RTMEMTYPE;
|
---|
153 |
|
---|
154 | #ifdef RTALLOC_EFENCE_TRACE
|
---|
155 | /**
|
---|
156 | * Node tracking a memory allocation.
|
---|
157 | */
|
---|
158 | typedef struct RTMEMBLOCK
|
---|
159 | {
|
---|
160 | /** Avl node code, key is the user block pointer. */
|
---|
161 | AVLPVNODECORE Core;
|
---|
162 | /** Allocation type. */
|
---|
163 | RTMEMTYPE enmType;
|
---|
164 | /** The unaligned size of the block. */
|
---|
165 | size_t cbUnaligned;
|
---|
166 | /** The aligned size of the block. */
|
---|
167 | size_t cbAligned;
|
---|
168 | /** The allocation tag (read-only string). */
|
---|
169 | const char *pszTag;
|
---|
170 | /** The return address of the allocator function. */
|
---|
171 | void *pvCaller;
|
---|
172 | /** Line number of the alloc call. */
|
---|
173 | unsigned iLine;
|
---|
174 | /** File from within the allocation was made. */
|
---|
175 | const char *pszFile;
|
---|
176 | /** Function from within the allocation was made. */
|
---|
177 | const char *pszFunction;
|
---|
178 | } RTMEMBLOCK, *PRTMEMBLOCK;
|
---|
179 |
|
---|
180 | #endif
|
---|
181 |
|
---|
182 |
|
---|
183 | /*******************************************************************************
|
---|
184 | * Internal Functions *
|
---|
185 | ******************************************************************************/
|
---|
186 | RT_C_DECLS_BEGIN
|
---|
187 | RTDECL(void *) rtR3MemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cbUnaligned, size_t cbAligned,
|
---|
188 | const char *pszTag, void *pvCaller, RT_SRC_POS_DECL);
|
---|
189 | RTDECL(void *) rtR3MemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew,
|
---|
190 | const char *pszTag, void *pvCaller, RT_SRC_POS_DECL);
|
---|
191 | RTDECL(void) rtR3MemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, RT_SRC_POS_DECL);
|
---|
192 | RT_C_DECLS_END
|
---|
193 |
|
---|
194 | #endif
|
---|
195 |
|
---|