VirtualBox

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

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

IPRT: A quick replacement of the RTMemPage* and RTMemExec* APIs on posix. (Turned out to be a bit more work than expected because of the electric fence heap and init dependencies.)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.5 KB
Line 
1/* $Id: alloc-r0drv.cpp 33269 2010-10-20 15:42:28Z 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#ifdef RT_MORE_STRICT
39# include <iprt/mp.h>
40#endif
41#include <iprt/param.h>
42#include <iprt/string.h>
43#include <iprt/thread.h>
44#include "r0drv/alloc-r0drv.h"
45
46
47/*******************************************************************************
48* Defined Constants And Macros *
49*******************************************************************************/
50#ifdef RT_STRICT
51# define RTR0MEM_STRICT
52#endif
53
54#ifdef RTR0MEM_STRICT
55# define RTR0MEM_FENCE_EXTRA 16
56#else
57# define RTR0MEM_FENCE_EXTRA 0
58#endif
59
60
61#undef RTMemTmpAlloc
62#undef RTMemTmpAllocTag
63#undef RTMemTmpAllocZ
64#undef RTMemTmpAllocZTag
65#undef RTMemTmpFree
66#undef RTMemAlloc
67#undef RTMemAllocTag
68#undef RTMemAllocZ
69#undef RTMemAllocZTag
70#undef RTMemAllocVar
71#undef RTMemAllocVarTag
72#undef RTMemAllocZVar
73#undef RTMemAllocZVarTag
74#undef RTMemRealloc
75#undef RTMemReallocTag
76#undef RTMemFree
77#undef RTMemDup
78#undef RTMemDupTag
79#undef RTMemDupEx
80#undef RTMemDupExTag
81#undef rtR0MemAllocEx
82#undef rtR0MemAllocExTag
83#undef rtR0MemFreeEx
84
85
86/*******************************************************************************
87* Global Variables *
88*******************************************************************************/
89#ifdef RTR0MEM_STRICT
90/** Fence data. */
91static uint8_t const g_abFence[RTR0MEM_FENCE_EXTRA] =
92{
93 0x77, 0x88, 0x66, 0x99, 0x55, 0xaa, 0x44, 0xbb,
94 0x33, 0xcc, 0x22, 0xdd, 0x11, 0xee, 0x00, 0xff
95};
96#endif
97
98
99/**
100 * Wrapper around rtR0MemAllocEx.
101 *
102 * @returns Pointer to the allocated memory block header.
103 * @param cb The number of bytes to allocate (sans header).
104 * @param fFlags The allocation flags.
105 */
106DECLINLINE(PRTMEMHDR) rtR0MemAlloc(size_t cb, uint32_t fFlags)
107{
108 PRTMEMHDR pHdr;
109 int rc = rtR0MemAllocEx(cb, fFlags, &pHdr);
110 if (RT_FAILURE(rc))
111 return NULL;
112 return pHdr;
113}
114
115
116RTDECL(void *) RTMemTmpAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
117{
118 return RTMemAllocTag(cb, pszTag);
119}
120RT_EXPORT_SYMBOL(RTMemTmpAllocTag);
121
122
123RTDECL(void *) RTMemTmpAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
124{
125 return RTMemAllocZTag(cb, pszTag);
126}
127RT_EXPORT_SYMBOL(RTMemTmpAllocZTag);
128
129
130RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW
131{
132 return RTMemFree(pv);
133}
134RT_EXPORT_SYMBOL(RTMemTmpFree);
135
136
137
138
139
140RTDECL(void *) RTMemAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
141{
142 PRTMEMHDR pHdr;
143 RT_ASSERT_INTS_ON();
144
145 pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, 0);
146 if (pHdr)
147 {
148#ifdef RTR0MEM_STRICT
149 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
150 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
151#endif
152 return pHdr + 1;
153 }
154 return NULL;
155}
156RT_EXPORT_SYMBOL(RTMemAllocTag);
157
158
159RTDECL(void *) RTMemAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
160{
161 PRTMEMHDR pHdr;
162 RT_ASSERT_INTS_ON();
163
164 pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_ZEROED);
165 if (pHdr)
166 {
167#ifdef RTR0MEM_STRICT
168 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
169 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
170 return memset(pHdr + 1, 0, cb);
171#else
172 return memset(pHdr + 1, 0, pHdr->cb);
173#endif
174 }
175 return NULL;
176}
177RT_EXPORT_SYMBOL(RTMemAllocZTag);
178
179
180RTDECL(void *) RTMemAllocVarTag(size_t cbUnaligned, const char *pszTag)
181{
182 size_t cbAligned;
183 if (cbUnaligned >= 16)
184 cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
185 else
186 cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
187 return RTMemAllocTag(cbAligned, pszTag);
188}
189RT_EXPORT_SYMBOL(RTMemAllocVarTag);
190
191
192RTDECL(void *) RTMemAllocZVarTag(size_t cbUnaligned, const char *pszTag)
193{
194 size_t cbAligned;
195 if (cbUnaligned >= 16)
196 cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
197 else
198 cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
199 return RTMemAllocZTag(cbAligned, pszTag);
200}
201RT_EXPORT_SYMBOL(RTMemAllocZVarTag);
202
203
204RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW
205{
206 if (!cbNew)
207 RTMemFree(pvOld);
208 else if (!pvOld)
209 return RTMemAllocTag(cbNew, pszTag);
210 else
211 {
212 PRTMEMHDR pHdrOld = (PRTMEMHDR)pvOld - 1;
213 RT_ASSERT_PREEMPTIBLE();
214
215 if (pHdrOld->u32Magic == RTMEMHDR_MAGIC)
216 {
217 PRTMEMHDR pHdrNew;
218 if (pHdrOld->cb >= cbNew && pHdrOld->cb - cbNew <= 128)
219 return pvOld;
220 pHdrNew = rtR0MemAlloc(cbNew + RTR0MEM_FENCE_EXTRA, 0);
221 if (pHdrNew)
222 {
223 size_t cbCopy = RT_MIN(pHdrOld->cb, pHdrNew->cb);
224 memcpy(pHdrNew + 1, pvOld, cbCopy);
225#ifdef RTR0MEM_STRICT
226 pHdrNew->cbReq = (uint32_t)cbNew; Assert(pHdrNew->cbReq == cbNew);
227 memcpy((uint8_t *)(pHdrNew + 1) + cbNew, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
228 AssertReleaseMsg(!memcmp((uint8_t *)(pHdrOld + 1) + pHdrOld->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
229 ("pHdr=%p pvOld=%p cb=%zu cbNew=%zu\n"
230 "fence: %.*Rhxs\n"
231 "expected: %.*Rhxs\n",
232 pHdrOld, pvOld, pHdrOld->cb, cbNew,
233 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdrOld + 1) + pHdrOld->cb,
234 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
235#endif
236 rtR0MemFree(pHdrOld);
237 return pHdrNew + 1;
238 }
239 }
240 else
241 AssertMsgFailed(("pHdrOld->u32Magic=%RX32 pvOld=%p cbNew=%#zx\n", pHdrOld->u32Magic, pvOld, cbNew));
242 }
243
244 return NULL;
245}
246RT_EXPORT_SYMBOL(RTMemReallocTag);
247
248
249RTDECL(void) RTMemFree(void *pv) RT_NO_THROW
250{
251 PRTMEMHDR pHdr;
252 RT_ASSERT_INTS_ON();
253
254 if (!pv)
255 return;
256 pHdr = (PRTMEMHDR)pv - 1;
257 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
258 {
259 Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_ALLOC_EX));
260 Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_EXEC));
261#ifdef RTR0MEM_STRICT
262 AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
263 ("pHdr=%p pv=%p cb=%zu\n"
264 "fence: %.*Rhxs\n"
265 "expected: %.*Rhxs\n",
266 pHdr, pv, pHdr->cb,
267 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
268 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
269#endif
270 rtR0MemFree(pHdr);
271 }
272 else
273 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
274}
275RT_EXPORT_SYMBOL(RTMemFree);
276
277
278
279
280
281
282RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
283{
284 PRTMEMHDR pHdr;
285#ifdef RT_OS_SOLARIS /** @todo figure out why */
286 RT_ASSERT_INTS_ON();
287#else
288 RT_ASSERT_PREEMPTIBLE();
289#endif
290
291 pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_EXEC);
292 if (pHdr)
293 {
294#ifdef RTR0MEM_STRICT
295 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
296 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
297#endif
298 return pHdr + 1;
299 }
300 return NULL;
301}
302RT_EXPORT_SYMBOL(RTMemExecAllocTag);
303
304
305RTDECL(void) RTMemExecFree(void *pv, size_t cb) RT_NO_THROW
306{
307 PRTMEMHDR pHdr;
308 RT_ASSERT_INTS_ON();
309
310 if (!pv)
311 return;
312 pHdr = (PRTMEMHDR)pv - 1;
313 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
314 {
315 Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_ALLOC_EX));
316#ifdef RTR0MEM_STRICT
317 AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
318 ("pHdr=%p pv=%p cb=%zu\n"
319 "fence: %.*Rhxs\n"
320 "expected: %.*Rhxs\n",
321 pHdr, pv, pHdr->cb,
322 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
323 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
324#endif
325 rtR0MemFree(pHdr);
326 }
327 else
328 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
329}
330RT_EXPORT_SYMBOL(RTMemExecFree);
331
332
333
334
335RTDECL(int) RTMemAllocExTag(size_t cb, size_t cbAlignment, uint32_t fFlags, const char *pszTag, void **ppv) RT_NO_THROW
336{
337 uint32_t fHdrFlags = RTMEMHDR_FLAG_ALLOC_EX;
338 PRTMEMHDR pHdr;
339 int rc;
340
341 RT_ASSERT_PREEMPT_CPUID_VAR();
342 if (!(fFlags & RTMEMHDR_FLAG_ANY_CTX_ALLOC))
343 RT_ASSERT_INTS_ON();
344
345 /*
346 * Fake up some alignment support.
347 */
348 AssertMsgReturn(cbAlignment <= sizeof(void *), ("%zu (%#x)\n", cbAlignment, cbAlignment), VERR_UNSUPPORTED_ALIGNMENT);
349 if (cb < cbAlignment)
350 cb = cbAlignment;
351
352 /*
353 * Validate and convert flags.
354 */
355 AssertMsgReturn(!(fFlags & ~RTMEMALLOCEX_FLAGS_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
356 if (fFlags & RTMEMALLOCEX_FLAGS_ZEROED)
357 fHdrFlags |= RTMEMHDR_FLAG_ZEROED;
358 if (fFlags & RTMEMALLOCEX_FLAGS_EXEC)
359 fHdrFlags |= RTMEMHDR_FLAG_EXEC;
360 if (fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX_ALLOC)
361 fHdrFlags |= RTMEMHDR_FLAG_ANY_CTX_ALLOC;
362 if (fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX_FREE)
363 fHdrFlags |= RTMEMHDR_FLAG_ANY_CTX_FREE;
364
365 /*
366 * Do the allocation.
367 */
368 rc = rtR0MemAllocEx(cb + RTR0MEM_FENCE_EXTRA, fHdrFlags, &pHdr);
369 if (RT_SUCCESS(rc))
370 {
371 void *pv;
372
373 Assert(pHdr->cbReq == cb + RTR0MEM_FENCE_EXTRA);
374 Assert((pHdr->fFlags & fFlags) == fFlags);
375
376 /*
377 * Calc user pointer, initialize the memory if requested, and if
378 * memory strictness is enable set up the fence.
379 */
380 pv = pHdr + 1;
381 *ppv = pv;
382 if (fFlags & RTMEMHDR_FLAG_ZEROED)
383 memset(pv, 0, pHdr->cb);
384
385#ifdef RTR0MEM_STRICT
386 pHdr->cbReq = (uint32_t)cb;
387 memcpy((uint8_t *)pv + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
388#endif
389 }
390 else if (rc == VERR_NO_MEMORY && (fFlags & RTMEMALLOCEX_FLAGS_EXEC))
391 rc = VERR_NO_EXEC_MEMORY;
392
393 RT_ASSERT_PREEMPT_CPUID();
394 return rc;
395}
396RT_EXPORT_SYMBOL(RTMemAllocExTag);
397
398
399RTDECL(void) RTMemFreeEx(void *pv, size_t cb) RT_NO_THROW
400{
401 PRTMEMHDR pHdr;
402
403 if (!pv)
404 return;
405
406 AssertPtr(pv);
407 pHdr = (PRTMEMHDR)pv - 1;
408 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
409 {
410 RT_ASSERT_PREEMPT_CPUID_VAR();
411
412 Assert(pHdr->fFlags & RTMEMHDR_FLAG_ALLOC_EX);
413 if (!(pHdr->fFlags & RTMEMHDR_FLAG_ANY_CTX_FREE))
414 RT_ASSERT_INTS_ON();
415 AssertMsg(pHdr->cbReq == cb, ("cbReq=%zu cb=%zu\n", pHdr->cb, cb));
416
417#ifdef RTR0MEM_STRICT
418 AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
419 ("pHdr=%p pv=%p cb=%zu\n"
420 "fence: %.*Rhxs\n"
421 "expected: %.*Rhxs\n",
422 pHdr, pv, pHdr->cb,
423 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
424 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
425#endif
426 rtR0MemFree(pHdr);
427 RT_ASSERT_PREEMPT_CPUID();
428 }
429 else
430 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
431}
432RT_EXPORT_SYMBOL(RTMemFreeEx);
433
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