VirtualBox

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

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

alloc-ef.cpp: RTALLOC_EFENCE_ALIGNMENT.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1/* $Id: alloc-ef.h 27297 2010-03-11 17:48:40Z 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.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 * 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// defined(DEBUG_bird)
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 * Normally, 1 would be the perfect choice here, except that there is code, like
60 * the atomic operations in iprt/asm.h, that makes assumptions about naturally
61 * aligned data members. If the code you're working against doesn't have strict
62 * alignment requirements, changing this to 1 is highly desirable.
63 */
64#if 1
65# define RTALLOC_EFENCE_ALIGNMENT (ARCH_BITS / 8)
66#else
67# define RTALLOC_EFENCE_ALIGNMENT 1
68#endif
69
70/** @def RTALLOC_EFENCE_IN_FRONT
71 * Define this to put the fence up in front of the block.
72 * The default (when this isn't defined) is to up it up after the block.
73 */
74//# define RTALLOC_EFENCE_IN_FRONT
75
76/** @def RTALLOC_EFENCE_TRACE
77 * Define this to support actual free and reallocation of blocks.
78 */
79#define RTALLOC_EFENCE_TRACE
80
81/** @def RTALLOC_EFENCE_FREE_DELAYED
82 * This define will enable free() delay and protection of the freed data
83 * while it's being delayed. The value of RTALLOC_EFENCE_FREE_DELAYED defines
84 * the threshold of the delayed blocks.
85 * Delayed blocks does not consume any physical memory, only virtual address space.
86 * Requires RTALLOC_EFENCE_TRACE.
87 */
88#define RTALLOC_EFENCE_FREE_DELAYED (20 * _1M)
89
90/** @def RTALLOC_EFENCE_FREE_FILL
91 * This define will enable memset(,RTALLOC_EFENCE_FREE_FILL,)'ing the user memory
92 * in the block before freeing/decommitting it. This is useful in GDB since GDB
93 * appeares to be able to read the content of the page even after it's been
94 * decommitted.
95 * Requires RTALLOC_EFENCE_TRACE.
96 */
97#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
98# define RTALLOC_EFENCE_FREE_FILL 'f'
99#endif
100
101/** @def RTALLOC_EFENCE_FILLER
102 * This define will enable memset(,RTALLOC_EFENCE_FILLER,)'ing the allocated
103 * memory when the API doesn't require it to be zero'ed.
104 */
105#define RTALLOC_EFENCE_FILLER 0xef
106
107/** @def RTALLOC_EFENCE_NOMAN_FILLER
108 * This define will enable memset(,RTALLOC_EFENCE_NOMAN_FILLER,)'ing the
109 * unprotected but not allocated area of memory, the so called no man's land.
110 */
111#define RTALLOC_EFENCE_NOMAN_FILLER 0xaa
112
113/** @def RTALLOC_EFENCE_FENCE_FILLER
114 * This define will enable memset(,RTALLOC_EFENCE_FENCE_FILLER,)'ing the
115 * fence itself, as debuggers can usually read them.
116 */
117#define RTALLOC_EFENCE_FENCE_FILLER 0xcc
118
119#if defined(DOXYGEN_RUNNING)
120/** @def RTALLOC_EFENCE_CPP
121 * This define will enable the new and delete wrappers.
122 */
123# define RTALLOC_EFENCE_CPP
124#endif
125
126
127
128/*******************************************************************************
129* Header Files *
130*******************************************************************************/
131#ifdef RT_OS_WINDOWS
132# include <Windows.h>
133#else
134# include <sys/mman.h>
135#endif
136#include <iprt/avl.h>
137#include <iprt/thread.h>
138
139
140/*******************************************************************************
141* Structures and Typedefs *
142*******************************************************************************/
143/**
144 * Allocation types.
145 */
146typedef enum RTMEMTYPE
147{
148 RTMEMTYPE_RTMEMALLOC,
149 RTMEMTYPE_RTMEMALLOCZ,
150 RTMEMTYPE_RTMEMREALLOC,
151 RTMEMTYPE_RTMEMFREE,
152
153 RTMEMTYPE_NEW,
154 RTMEMTYPE_NEW_ARRAY,
155 RTMEMTYPE_DELETE,
156 RTMEMTYPE_DELETE_ARRAY
157} RTMEMTYPE;
158
159#ifdef RTALLOC_EFENCE_TRACE
160/**
161 * Node tracking a memory allocation.
162 */
163typedef struct RTMEMBLOCK
164{
165 /** Avl node code, key is the user block pointer. */
166 AVLPVNODECORE Core;
167 /** Allocation type. */
168 RTMEMTYPE enmType;
169 /** The size of the block. */
170 size_t cb;
171 /** The return address of the allocator function. */
172 void *pvCaller;
173 /** Line number of the alloc call. */
174 unsigned iLine;
175 /** File from within the allocation was made. */
176 const char *pszFile;
177 /** Function from within the allocation was made. */
178 const char *pszFunction;
179} RTMEMBLOCK, *PRTMEMBLOCK;
180
181#endif
182
183
184/*******************************************************************************
185* Internal Functions *
186******************************************************************************/
187RT_C_DECLS_BEGIN
188void * rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
189void * rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
190void rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
191RT_C_DECLS_END
192
193#endif
194
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