VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/alloc.cpp@ 69971

最後變更 在這個檔案從69971是 69971,由 vboxsync 提交於 7 年 前

iprt/memtracker: Add the caller address to the tracking information.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 7.7 KB
 
1/* $Id: alloc.cpp 69971 2017-12-07 11:16:53Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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
28/*********************************************************************************************************************************
29* Defined Constants And Macros *
30*********************************************************************************************************************************/
31#define RTMEMALLOC_USE_TRACKER
32#if defined(RTMEM_WRAP_TO_EF_APIS) && !defined(RTMEM_NO_WRAP_TO_EF_APIS)
33# undef RTMEM_WRAP_TO_EF_APIS
34# define RTALLOC_USE_EFENCE 1
35#endif
36
37/*#define RTMEMALLOC_USE_TRACKER*/
38/* Don't enable the tracker when building the minimal IPRT. */
39#ifdef RT_MINI
40# undef RTMEMALLOC_USE_TRACKER
41#endif
42
43#if defined(RTMEMALLOC_USE_TRACKER) && defined(RTALLOC_USE_EFENCE)
44# error "Cannot define both RTMEMALLOC_USE_TRACKER and RTALLOC_USE_EFENCE!"
45#endif
46
47
48/*********************************************************************************************************************************
49* Header Files *
50*********************************************************************************************************************************/
51#include "alloc-ef.h"
52#include <iprt/mem.h>
53
54#include <iprt/asm.h>
55#include <iprt/assert.h>
56#ifdef RTMEMALLOC_USE_TRACKER
57# include <iprt/memtracker.h>
58#endif
59#include <iprt/param.h>
60#include <iprt/string.h>
61#include "internal/mem.h"
62
63#include <stdlib.h>
64
65#undef RTMemTmpAlloc
66#undef RTMemTmpAllocTag
67#undef RTMemTmpAllocZ
68#undef RTMemTmpAllocZTag
69#undef RTMemTmpFree
70#undef RTMemAlloc
71#undef RTMemAllocTag
72#undef RTMemAllocZ
73#undef RTMemAllocZTag
74#undef RTMemAllocVar
75#undef RTMemAllocVarTag
76#undef RTMemAllocZVar
77#undef RTMemAllocZVarTag
78#undef RTMemRealloc
79#undef RTMemReallocTag
80#undef RTMemFree
81#undef RTMemDup
82#undef RTMemDupTag
83#undef RTMemDupEx
84#undef RTMemDupExTag
85
86#undef RTALLOC_USE_EFENCE
87
88
89RTDECL(void *) RTMemTmpAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
90{
91 return RTMemAllocTag(cb, pszTag);
92}
93
94
95RTDECL(void *) RTMemTmpAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
96{
97 return RTMemAllocZTag(cb, pszTag);
98}
99
100
101RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW_DEF
102{
103 RTMemFree(pv);
104}
105
106
107RTDECL(void *) RTMemAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
108{
109#ifdef RTALLOC_USE_EFENCE
110 void *pv = rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
111
112#else /* !RTALLOC_USE_EFENCE */
113
114 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
115# ifdef RTMEMALLOC_USE_TRACKER
116 void *pv = RTMemTrackerHdrAlloc(malloc(cb + sizeof(RTMEMTRACKERHDR)), cb, pszTag, ASMReturnAddress(), RTMEMTRACKERMETHOD_ALLOC);
117# else
118 void *pv = malloc(cb); NOREF(pszTag);
119# endif
120 AssertMsg(pv, ("malloc(%#zx) failed!!!\n", cb));
121 AssertMsg( cb < RTMEM_ALIGNMENT
122 || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
123 || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
124 , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
125#endif /* !RTALLOC_USE_EFENCE */
126 return pv;
127}
128
129
130RTDECL(void *) RTMemAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
131{
132#ifdef RTALLOC_USE_EFENCE
133 void *pv = rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
134
135#else /* !RTALLOC_USE_EFENCE */
136
137 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
138
139# ifdef RTMEMALLOC_USE_TRACKER
140 void *pv = RTMemTrackerHdrAlloc(calloc(1, cb + sizeof(RTMEMTRACKERHDR)), cb, pszTag, ASMReturnAddress(), RTMEMTRACKERMETHOD_ALLOCZ);
141#else
142 void *pv = calloc(1, cb); NOREF(pszTag);
143#endif
144 AssertMsg(pv, ("calloc(1,%#zx) failed!!!\n", cb));
145 AssertMsg( cb < RTMEM_ALIGNMENT
146 || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
147 || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
148 , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
149#endif /* !RTALLOC_USE_EFENCE */
150 return pv;
151}
152
153
154RTDECL(void *) RTMemAllocVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_DEF
155{
156 size_t cbAligned;
157 if (cbUnaligned >= 16)
158 cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
159 else
160 cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
161#ifdef RTALLOC_USE_EFENCE
162 void *pv = rtR3MemAlloc("AllocVar", RTMEMTYPE_RTMEMALLOC, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), NULL, 0, NULL);
163#else
164 void *pv = RTMemAllocTag(cbAligned, pszTag);
165#endif
166 return pv;
167}
168
169
170RTDECL(void *) RTMemAllocZVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_DEF
171{
172 size_t cbAligned;
173 if (cbUnaligned >= 16)
174 cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
175 else
176 cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
177#ifdef RTALLOC_USE_EFENCE
178 void *pv = rtR3MemAlloc("AllocZVar", RTMEMTYPE_RTMEMALLOCZ, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), NULL, 0, NULL);
179#else
180 void *pv = RTMemAllocZTag(cbAligned, pszTag);
181#endif
182 return pv;
183}
184
185
186RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW_DEF
187{
188#ifdef RTALLOC_USE_EFENCE
189 void *pv = rtR3MemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, pszTag, ASMReturnAddress(), NULL, 0, NULL);
190
191#else /* !RTALLOC_USE_EFENCE */
192
193# ifdef RTMEMALLOC_USE_TRACKER
194 void *pvRealOld = RTMemTrackerHdrReallocPrep(pvOld, 0, pszTag, ASMReturnAddress());
195 size_t cbRealNew = cbNew || !pvRealOld ? cbNew + sizeof(RTMEMTRACKERHDR) : 0;
196 void *pvNew = realloc(pvRealOld, cbRealNew);
197 void *pv = RTMemTrackerHdrReallocDone(pvNew, cbNew, pvOld, pszTag, ASMReturnAddress());
198# else
199 void *pv = realloc(pvOld, cbNew); NOREF(pszTag);
200# endif
201 AssertMsg(pv || !cbNew, ("realloc(%p, %#zx) failed!!!\n", pvOld, cbNew));
202 AssertMsg( cbNew < RTMEM_ALIGNMENT
203 || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
204 || ( (cbNew & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
205 , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
206#endif /* !RTALLOC_USE_EFENCE */
207 return pv;
208}
209
210
211RTDECL(void) RTMemFree(void *pv) RT_NO_THROW_DEF
212{
213 if (pv)
214#ifdef RTALLOC_USE_EFENCE
215 rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ASMReturnAddress(), NULL, 0, NULL);
216#else
217# ifdef RTMEMALLOC_USE_TRACKER
218 pv = RTMemTrackerHdrFree(pv, 0, NULL, ASMReturnAddress(), RTMEMTRACKERMETHOD_FREE);
219# endif
220 free(pv);
221#endif
222}
223
224
225
226DECLHIDDEN(void *) rtMemBaseAlloc(size_t cb)
227{
228 Assert(cb > 0 && cb < _1M);
229 return malloc(cb);
230}
231
232
233DECLHIDDEN(void) rtMemBaseFree(void *pv)
234{
235 free(pv);
236}
237
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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