VirtualBox

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

Last change on this file since 29945 was 29250, checked in by vboxsync, 15 years ago

iprt/asm*.h: split out asm-math.h, don't include asm-*.h from asm.h, don't include asm.h from sup.h. Fixed a couple file headers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.7 KB
Line 
1/* $Id: alloc-r0drv.cpp 29250 2010-05-09 17:53:58Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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
84/**
85 * Allocates temporary memory.
86 *
87 * Temporary memory blocks are used for not too large memory blocks which
88 * are believed not to stick around for too long. Using this API instead
89 * of RTMemAlloc() not only gives the heap manager room for optimization
90 * but makes the code easier to read.
91 *
92 * @returns Pointer to the allocated memory.
93 * @returns NULL on failure.
94 * @param cb Size in bytes of the memory block to allocated.
95 */
96RTDECL(void *) RTMemTmpAlloc(size_t cb) RT_NO_THROW
97{
98 return RTMemAlloc(cb);
99}
100RT_EXPORT_SYMBOL(RTMemTmpAlloc);
101
102
103/**
104 * Allocates zero'ed temporary memory.
105 *
106 * Same as RTMemTmpAlloc() but the memory will be zero'ed.
107 *
108 * @returns Pointer to the allocated memory.
109 * @returns NULL on failure.
110 * @param cb Size in bytes of the memory block to allocated.
111 */
112RTDECL(void *) RTMemTmpAllocZ(size_t cb) RT_NO_THROW
113{
114 return RTMemAllocZ(cb);
115}
116RT_EXPORT_SYMBOL(RTMemTmpAllocZ);
117
118
119/**
120 * Free temporary memory.
121 *
122 * @param pv Pointer to memory block.
123 */
124RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW
125{
126 return RTMemFree(pv);
127}
128RT_EXPORT_SYMBOL(RTMemTmpFree);
129
130
131/**
132 * Allocates memory.
133 *
134 * @returns Pointer to the allocated memory.
135 * @returns NULL on failure.
136 * @param cb Size in bytes of the memory block to allocated.
137 */
138RTDECL(void *) RTMemAlloc(size_t cb) RT_NO_THROW
139{
140 PRTMEMHDR pHdr;
141 RT_ASSERT_INTS_ON();
142
143 pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, 0);
144 if (pHdr)
145 {
146#ifdef RTR0MEM_STRICT
147 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
148 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
149#endif
150 return pHdr + 1;
151 }
152 return NULL;
153}
154RT_EXPORT_SYMBOL(RTMemAlloc);
155
156
157/**
158 * Allocates zero'ed memory.
159 *
160 * Instead of memset(pv, 0, sizeof()) use this when you want zero'ed
161 * memory. This keeps the code smaller and the heap can skip the memset
162 * in about 0.42% of calls :-).
163 *
164 * @returns Pointer to the allocated memory.
165 * @returns NULL on failure.
166 * @param cb Size in bytes of the memory block to allocated.
167 */
168RTDECL(void *) RTMemAllocZ(size_t cb) RT_NO_THROW
169{
170 PRTMEMHDR pHdr;
171 RT_ASSERT_INTS_ON();
172
173 pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_ZEROED);
174 if (pHdr)
175 {
176#ifdef RTR0MEM_STRICT
177 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
178 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
179 return memset(pHdr + 1, 0, cb);
180#else
181 return memset(pHdr + 1, 0, pHdr->cb);
182#endif
183 }
184 return NULL;
185}
186RT_EXPORT_SYMBOL(RTMemAllocZ);
187
188
189/**
190 * Wrapper around RTMemAlloc for automatically aligning variable sized
191 * allocations so that the various electric fence heaps works correctly.
192 *
193 * @returns See RTMemAlloc.
194 * @param cbUnaligned The unaligned size.
195 */
196RTDECL(void *) RTMemAllocVar(size_t cbUnaligned)
197{
198 size_t cbAligned;
199 if (cbUnaligned >= 16)
200 cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
201 else
202 cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
203 return RTMemAlloc(cbAligned);
204}
205RT_EXPORT_SYMBOL(RTMemAllocVar);
206
207
208/**
209 * Wrapper around RTMemAllocZ for automatically aligning variable sized
210 * allocations so that the various electric fence heaps works correctly.
211 *
212 * @returns See RTMemAllocZ.
213 * @param cbUnaligned The unaligned size.
214 */
215RTDECL(void *) RTMemAllocZVar(size_t cbUnaligned)
216{
217 size_t cbAligned;
218 if (cbUnaligned >= 16)
219 cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
220 else
221 cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
222 return RTMemAllocZ(cbAligned);
223}
224RT_EXPORT_SYMBOL(RTMemAllocZVar);
225
226
227/**
228 * Reallocates memory.
229 *
230 * @returns Pointer to the allocated memory.
231 * @returns NULL on failure.
232 * @param pvOld The memory block to reallocate.
233 * @param cbNew The new block size (in bytes).
234 */
235RTDECL(void *) RTMemRealloc(void *pvOld, size_t cbNew) RT_NO_THROW
236{
237 if (!cbNew)
238 RTMemFree(pvOld);
239 else if (!pvOld)
240 return RTMemAlloc(cbNew);
241 else
242 {
243 PRTMEMHDR pHdrOld = (PRTMEMHDR)pvOld - 1;
244 RT_ASSERT_PREEMPTIBLE();
245
246 if (pHdrOld->u32Magic == RTMEMHDR_MAGIC)
247 {
248 PRTMEMHDR pHdrNew;
249 if (pHdrOld->cb >= cbNew && pHdrOld->cb - cbNew <= 128)
250 return pvOld;
251 pHdrNew = rtR0MemAlloc(cbNew + RTR0MEM_FENCE_EXTRA, 0);
252 if (pHdrNew)
253 {
254 size_t cbCopy = RT_MIN(pHdrOld->cb, pHdrNew->cb);
255 memcpy(pHdrNew + 1, pvOld, cbCopy);
256#ifdef RTR0MEM_STRICT
257 pHdrNew->cbReq = (uint32_t)cbNew; Assert(pHdrNew->cbReq == cbNew);
258 memcpy((uint8_t *)(pHdrNew + 1) + cbNew, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
259 AssertReleaseMsg(!memcmp((uint8_t *)(pHdrOld + 1) + pHdrOld->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
260 ("pHdr=%p pvOld=%p cb=%zu cbNew=%zu\n"
261 "fence: %.*Rhxs\n"
262 "expected: %.*Rhxs\n",
263 pHdrOld, pvOld, pHdrOld->cb, cbNew,
264 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdrOld + 1) + pHdrOld->cb,
265 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
266#endif
267 rtR0MemFree(pHdrOld);
268 return pHdrNew + 1;
269 }
270 }
271 else
272 AssertMsgFailed(("pHdrOld->u32Magic=%RX32 pvOld=%p cbNew=%#zx\n", pHdrOld->u32Magic, pvOld, cbNew));
273 }
274
275 return NULL;
276}
277RT_EXPORT_SYMBOL(RTMemRealloc);
278
279
280/**
281 * Free memory related to an virtual machine
282 *
283 * @param pv Pointer to memory block.
284 */
285RTDECL(void) RTMemFree(void *pv) RT_NO_THROW
286{
287 PRTMEMHDR pHdr;
288 RT_ASSERT_INTS_ON();
289
290 if (!pv)
291 return;
292 pHdr = (PRTMEMHDR)pv - 1;
293 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
294 {
295 Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_EXEC));
296#ifdef RTR0MEM_STRICT
297 AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
298 ("pHdr=%p pv=%p cb=%zu\n"
299 "fence: %.*Rhxs\n"
300 "expected: %.*Rhxs\n",
301 pHdr, pv, pHdr->cb,
302 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
303 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
304#endif
305 rtR0MemFree(pHdr);
306 }
307 else
308 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
309}
310RT_EXPORT_SYMBOL(RTMemFree);
311
312
313/**
314 * Allocates memory which may contain code.
315 *
316 * @returns Pointer to the allocated memory.
317 * @returns NULL on failure.
318 * @param cb Size in bytes of the memory block to allocate.
319 */
320RTDECL(void *) RTMemExecAlloc(size_t cb) RT_NO_THROW
321{
322 PRTMEMHDR pHdr;
323#ifdef RT_OS_SOLARIS /** @todo figure out why */
324 RT_ASSERT_INTS_ON();
325#else
326 RT_ASSERT_PREEMPTIBLE();
327#endif
328
329 pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_EXEC);
330 if (pHdr)
331 {
332#ifdef RTR0MEM_STRICT
333 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
334 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
335#endif
336 return pHdr + 1;
337 }
338 return NULL;
339}
340RT_EXPORT_SYMBOL(RTMemExecAlloc);
341
342
343/**
344 * Free executable/read/write memory allocated by RTMemExecAlloc().
345 *
346 * @param pv Pointer to memory block.
347 */
348RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
349{
350 PRTMEMHDR pHdr;
351 RT_ASSERT_INTS_ON();
352
353 if (!pv)
354 return;
355 pHdr = (PRTMEMHDR)pv - 1;
356 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
357 {
358#ifdef RTR0MEM_STRICT
359 AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
360 ("pHdr=%p pv=%p cb=%zu\n"
361 "fence: %.*Rhxs\n"
362 "expected: %.*Rhxs\n",
363 pHdr, pv, pHdr->cb,
364 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
365 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
366#endif
367 rtR0MemFree(pHdr);
368 }
369 else
370 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
371}
372RT_EXPORT_SYMBOL(RTMemExecFree);
373
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