VirtualBox

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

Last change on this file was 103142, checked in by vboxsync, 4 months ago

Runtime: Some warning fixes about externally visible functions which should be static, bugref:3409 [committed a bit too much]

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

© 2023 Oracle
ContactPrivacy policyTerms of Use