VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/alloc-r0drv.cpp@ 31157

Last change on this file since 31157 was 31157, checked in by vboxsync, 14 years ago

iprt,++: Tag allocation in all builds with a string, defaulting to FILE.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.0 KB
Line 
1/* $Id: alloc-r0drv.cpp 31157 2010-07-28 03:15:35Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver.
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
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/mem.h>
32#include "internal/iprt.h"
33
34#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
35# include <iprt/asm-amd64-x86.h>
36#endif
37#include <iprt/assert.h>
38#include <iprt/param.h>
39#include <iprt/string.h>
40#include <iprt/thread.h>
41#include "r0drv/alloc-r0drv.h"
42
43#undef RTMemTmpAlloc
44#undef RTMemTmpAllocZ
45#undef RTMemTmpFree
46#undef RTMemAlloc
47#undef RTMemAllocZ
48#undef RTMemAllocVar
49#undef RTMemAllocZVar
50#undef RTMemRealloc
51#undef RTMemFree
52#undef RTMemDup
53#undef RTMemDupEx
54
55
56/*******************************************************************************
57* Defined Constants And Macros *
58*******************************************************************************/
59#ifdef RT_STRICT
60# define RTR0MEM_STRICT
61#endif
62
63#ifdef RTR0MEM_STRICT
64# define RTR0MEM_FENCE_EXTRA 16
65#else
66# define RTR0MEM_FENCE_EXTRA 0
67#endif
68
69
70/*******************************************************************************
71* Global Variables *
72*******************************************************************************/
73#ifdef RTR0MEM_STRICT
74/** Fence data. */
75static uint8_t const g_abFence[RTR0MEM_FENCE_EXTRA] =
76{
77 0x77, 0x88, 0x66, 0x99, 0x55, 0xaa, 0x44, 0xbb,
78 0x33, 0xcc, 0x22, 0xdd, 0x11, 0xee, 0x00, 0xff
79};
80#endif
81
82
83#undef RTMemTmpAlloc
84#undef RTMemTmpAllocTag
85#undef RTMemTmpAllocZ
86#undef RTMemTmpAllocZTag
87#undef RTMemTmpFree
88#undef RTMemAlloc
89#undef RTMemAllocTag
90#undef RTMemAllocZ
91#undef RTMemAllocZTag
92#undef RTMemAllocVar
93#undef RTMemAllocVarTag
94#undef RTMemAllocZVar
95#undef RTMemAllocZVarTag
96#undef RTMemRealloc
97#undef RTMemReallocTag
98#undef RTMemFree
99#undef RTMemDup
100#undef RTMemDupTag
101#undef RTMemDupEx
102#undef RTMemDupExTag
103
104
105
106RTDECL(void *) RTMemTmpAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
107{
108 return RTMemAllocTag(cb, pszTag);
109}
110RT_EXPORT_SYMBOL(RTMemTmpAllocTag);
111
112
113RTDECL(void *) RTMemTmpAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
114{
115 return RTMemAllocZTag(cb, pszTag);
116}
117RT_EXPORT_SYMBOL(RTMemTmpAllocZTag);
118
119
120RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW
121{
122 return RTMemFree(pv);
123}
124RT_EXPORT_SYMBOL(RTMemTmpFree);
125
126
127
128
129
130RTDECL(void *) RTMemAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
131{
132 PRTMEMHDR pHdr;
133 RT_ASSERT_INTS_ON();
134
135 pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, 0);
136 if (pHdr)
137 {
138#ifdef RTR0MEM_STRICT
139 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
140 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
141#endif
142 return pHdr + 1;
143 }
144 return NULL;
145}
146RT_EXPORT_SYMBOL(RTMemAllocTag);
147
148
149RTDECL(void *) RTMemAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
150{
151 PRTMEMHDR pHdr;
152 RT_ASSERT_INTS_ON();
153
154 pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_ZEROED);
155 if (pHdr)
156 {
157#ifdef RTR0MEM_STRICT
158 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
159 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
160 return memset(pHdr + 1, 0, cb);
161#else
162 return memset(pHdr + 1, 0, pHdr->cb);
163#endif
164 }
165 return NULL;
166}
167RT_EXPORT_SYMBOL(RTMemAllocZTag);
168
169
170RTDECL(void *) RTMemAllocVarTag(size_t cbUnaligned, const char *pszTag)
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 return RTMemAllocTag(cbAligned, pszTag);
178}
179RT_EXPORT_SYMBOL(RTMemAllocVarTag);
180
181
182RTDECL(void *) RTMemAllocZVarTag(size_t cbUnaligned, const char *pszTag)
183{
184 size_t cbAligned;
185 if (cbUnaligned >= 16)
186 cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
187 else
188 cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
189 return RTMemAllocZTag(cbAligned, pszTag);
190}
191RT_EXPORT_SYMBOL(RTMemAllocZVarTag);
192
193
194RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW
195{
196 if (!cbNew)
197 RTMemFree(pvOld);
198 else if (!pvOld)
199 return RTMemAllocTag(cbNew, pszTag);
200 else
201 {
202 PRTMEMHDR pHdrOld = (PRTMEMHDR)pvOld - 1;
203 RT_ASSERT_PREEMPTIBLE();
204
205 if (pHdrOld->u32Magic == RTMEMHDR_MAGIC)
206 {
207 PRTMEMHDR pHdrNew;
208 if (pHdrOld->cb >= cbNew && pHdrOld->cb - cbNew <= 128)
209 return pvOld;
210 pHdrNew = rtR0MemAlloc(cbNew + RTR0MEM_FENCE_EXTRA, 0);
211 if (pHdrNew)
212 {
213 size_t cbCopy = RT_MIN(pHdrOld->cb, pHdrNew->cb);
214 memcpy(pHdrNew + 1, pvOld, cbCopy);
215#ifdef RTR0MEM_STRICT
216 pHdrNew->cbReq = (uint32_t)cbNew; Assert(pHdrNew->cbReq == cbNew);
217 memcpy((uint8_t *)(pHdrNew + 1) + cbNew, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
218 AssertReleaseMsg(!memcmp((uint8_t *)(pHdrOld + 1) + pHdrOld->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
219 ("pHdr=%p pvOld=%p cb=%zu cbNew=%zu\n"
220 "fence: %.*Rhxs\n"
221 "expected: %.*Rhxs\n",
222 pHdrOld, pvOld, pHdrOld->cb, cbNew,
223 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdrOld + 1) + pHdrOld->cb,
224 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
225#endif
226 rtR0MemFree(pHdrOld);
227 return pHdrNew + 1;
228 }
229 }
230 else
231 AssertMsgFailed(("pHdrOld->u32Magic=%RX32 pvOld=%p cbNew=%#zx\n", pHdrOld->u32Magic, pvOld, cbNew));
232 }
233
234 return NULL;
235}
236RT_EXPORT_SYMBOL(RTMemReallocTag);
237
238
239RTDECL(void) RTMemFree(void *pv) RT_NO_THROW
240{
241 PRTMEMHDR pHdr;
242 RT_ASSERT_INTS_ON();
243
244 if (!pv)
245 return;
246 pHdr = (PRTMEMHDR)pv - 1;
247 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
248 {
249 Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_EXEC));
250#ifdef RTR0MEM_STRICT
251 AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
252 ("pHdr=%p pv=%p cb=%zu\n"
253 "fence: %.*Rhxs\n"
254 "expected: %.*Rhxs\n",
255 pHdr, pv, pHdr->cb,
256 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
257 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
258#endif
259 rtR0MemFree(pHdr);
260 }
261 else
262 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
263}
264RT_EXPORT_SYMBOL(RTMemFree);
265
266
267
268
269
270
271RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
272{
273 PRTMEMHDR pHdr;
274#ifdef RT_OS_SOLARIS /** @todo figure out why */
275 RT_ASSERT_INTS_ON();
276#else
277 RT_ASSERT_PREEMPTIBLE();
278#endif
279
280 pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_EXEC);
281 if (pHdr)
282 {
283#ifdef RTR0MEM_STRICT
284 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
285 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
286#endif
287 return pHdr + 1;
288 }
289 return NULL;
290}
291RT_EXPORT_SYMBOL(RTMemExecAllocTag);
292
293
294RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
295{
296 PRTMEMHDR pHdr;
297 RT_ASSERT_INTS_ON();
298
299 if (!pv)
300 return;
301 pHdr = (PRTMEMHDR)pv - 1;
302 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
303 {
304#ifdef RTR0MEM_STRICT
305 AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
306 ("pHdr=%p pv=%p cb=%zu\n"
307 "fence: %.*Rhxs\n"
308 "expected: %.*Rhxs\n",
309 pHdr, pv, pHdr->cb,
310 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
311 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
312#endif
313 rtR0MemFree(pHdr);
314 }
315 else
316 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
317}
318RT_EXPORT_SYMBOL(RTMemExecFree);
319
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