VirtualBox

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

Last change on this file since 87007 was 83546, checked in by vboxsync, 5 years ago

IPRT: Added RTMemFreeZ, RTMemTmpFreeZ, and RTMemEf* variants. bugref:9698

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

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