VirtualBox

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

Last change on this file since 25046 was 22174, checked in by vboxsync, 15 years ago

alloc-r0drv.cpp: relaxed the assertion in RTMemExecAlloc a bit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.5 KB
Line 
1/* $Id: alloc-r0drv.cpp 22174 2009-08-11 15:59:53Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/mem.h>
36#include "internal/iprt.h"
37
38#include <iprt/asm.h>
39#include <iprt/assert.h>
40#include <iprt/param.h>
41#include <iprt/string.h>
42#include <iprt/thread.h>
43#include "r0drv/alloc-r0drv.h"
44
45
46/*******************************************************************************
47* Defined Constants And Macros *
48*******************************************************************************/
49#ifdef RT_STRICT
50# define RTR0MEM_STRICT
51#endif
52
53#ifdef RTR0MEM_STRICT
54# define RTR0MEM_FENCE_EXTRA 16
55#else
56# define RTR0MEM_FENCE_EXTRA 0
57#endif
58
59
60/*******************************************************************************
61* Global Variables *
62*******************************************************************************/
63#ifdef RTR0MEM_STRICT
64/** Fence data. */
65static uint8_t const g_abFence[RTR0MEM_FENCE_EXTRA] =
66{
67 0x77, 0x88, 0x66, 0x99, 0x55, 0xaa, 0x44, 0xbb,
68 0x33, 0xcc, 0x22, 0xdd, 0x11, 0xee, 0x00, 0xff
69};
70#endif
71
72
73
74/**
75 * Allocates temporary memory.
76 *
77 * Temporary memory blocks are used for not too large memory blocks which
78 * are believed not to stick around for too long. Using this API instead
79 * of RTMemAlloc() not only gives the heap manager room for optimization
80 * but makes the code easier to read.
81 *
82 * @returns Pointer to the allocated memory.
83 * @returns NULL on failure.
84 * @param cb Size in bytes of the memory block to allocated.
85 */
86RTDECL(void *) RTMemTmpAlloc(size_t cb) RT_NO_THROW
87{
88 return RTMemAlloc(cb);
89}
90RT_EXPORT_SYMBOL(RTMemTmpAlloc);
91
92
93/**
94 * Allocates zero'ed temporary memory.
95 *
96 * Same as RTMemTmpAlloc() but the memory will be zero'ed.
97 *
98 * @returns Pointer to the allocated memory.
99 * @returns NULL on failure.
100 * @param cb Size in bytes of the memory block to allocated.
101 */
102RTDECL(void *) RTMemTmpAllocZ(size_t cb) RT_NO_THROW
103{
104 return RTMemAllocZ(cb);
105}
106RT_EXPORT_SYMBOL(RTMemTmpAllocZ);
107
108
109/**
110 * Free temporary memory.
111 *
112 * @param pv Pointer to memory block.
113 */
114RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW
115{
116 return RTMemFree(pv);
117}
118RT_EXPORT_SYMBOL(RTMemTmpFree);
119
120
121/**
122 * Allocates memory.
123 *
124 * @returns Pointer to the allocated memory.
125 * @returns NULL on failure.
126 * @param cb Size in bytes of the memory block to allocated.
127 */
128RTDECL(void *) RTMemAlloc(size_t cb) RT_NO_THROW
129{
130 PRTMEMHDR pHdr;
131 RT_ASSERT_INTS_ON();
132
133 pHdr = rtMemAlloc(cb + RTR0MEM_FENCE_EXTRA, 0);
134 if (pHdr)
135 {
136#ifdef RTR0MEM_STRICT
137 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
138 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
139#endif
140 return pHdr + 1;
141 }
142 return NULL;
143}
144RT_EXPORT_SYMBOL(RTMemAlloc);
145
146
147/**
148 * Allocates zero'ed memory.
149 *
150 * Instead of memset(pv, 0, sizeof()) use this when you want zero'ed
151 * memory. This keeps the code smaller and the heap can skip the memset
152 * in about 0.42% of calls :-).
153 *
154 * @returns Pointer to the allocated memory.
155 * @returns NULL on failure.
156 * @param cb Size in bytes of the memory block to allocated.
157 */
158RTDECL(void *) RTMemAllocZ(size_t cb) RT_NO_THROW
159{
160 PRTMEMHDR pHdr;
161 RT_ASSERT_INTS_ON();
162
163 pHdr = rtMemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_ZEROED);
164 if (pHdr)
165 {
166#ifdef RTR0MEM_STRICT
167 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
168 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
169 return memset(pHdr + 1, 0, cb);
170#else
171 return memset(pHdr + 1, 0, pHdr->cb);
172#endif
173 }
174 return NULL;
175}
176RT_EXPORT_SYMBOL(RTMemAllocZ);
177
178
179/**
180 * Reallocates memory.
181 *
182 * @returns Pointer to the allocated memory.
183 * @returns NULL on failure.
184 * @param pvOld The memory block to reallocate.
185 * @param cbNew The new block size (in bytes).
186 */
187RTDECL(void *) RTMemRealloc(void *pvOld, size_t cbNew) RT_NO_THROW
188{
189 if (!cbNew)
190 RTMemFree(pvOld);
191 else if (!pvOld)
192 return RTMemAlloc(cbNew);
193 else
194 {
195 PRTMEMHDR pHdrOld = (PRTMEMHDR)pvOld - 1;
196 RT_ASSERT_PREEMPTIBLE();
197
198 if (pHdrOld->u32Magic == RTMEMHDR_MAGIC)
199 {
200 PRTMEMHDR pHdrNew;
201 if (pHdrOld->cb >= cbNew && pHdrOld->cb - cbNew <= 128)
202 return pvOld;
203 pHdrNew = rtMemAlloc(cbNew + RTR0MEM_FENCE_EXTRA, 0);
204 if (pHdrNew)
205 {
206 size_t cbCopy = RT_MIN(pHdrOld->cb, pHdrNew->cb);
207 memcpy(pHdrNew + 1, pvOld, cbCopy);
208#ifdef RTR0MEM_STRICT
209 pHdrNew->cbReq = (uint32_t)cbNew; Assert(pHdrNew->cbReq == cbNew);
210 memcpy((uint8_t *)(pHdrNew + 1) + cbNew, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
211 AssertReleaseMsg(!memcmp((uint8_t *)(pHdrOld + 1) + pHdrOld->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
212 ("pHdr=%p pvOld=%p cb=%zu cbNew=%zu\n"
213 "fence: %.*Rhxs\n"
214 "expected: %.*Rhxs\n",
215 pHdrOld, pvOld, pHdrOld->cb, cbNew,
216 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdrOld + 1) + pHdrOld->cb,
217 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
218#endif
219 rtMemFree(pHdrOld);
220 return pHdrNew + 1;
221 }
222 }
223 else
224 AssertMsgFailed(("pHdrOld->u32Magic=%RX32 pvOld=%p cbNew=%#zx\n", pHdrOld->u32Magic, pvOld, cbNew));
225 }
226
227 return NULL;
228}
229RT_EXPORT_SYMBOL(RTMemRealloc);
230
231
232/**
233 * Free memory related to an virtual machine
234 *
235 * @param pv Pointer to memory block.
236 */
237RTDECL(void) RTMemFree(void *pv) RT_NO_THROW
238{
239 PRTMEMHDR pHdr;
240 RT_ASSERT_INTS_ON();
241
242 if (!pv)
243 return;
244 pHdr = (PRTMEMHDR)pv - 1;
245 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
246 {
247 Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_EXEC));
248#ifdef RTR0MEM_STRICT
249 AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
250 ("pHdr=%p pv=%p cb=%zu\n"
251 "fence: %.*Rhxs\n"
252 "expected: %.*Rhxs\n",
253 pHdr, pv, pHdr->cb,
254 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
255 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
256#endif
257 rtMemFree(pHdr);
258 }
259 else
260 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
261}
262RT_EXPORT_SYMBOL(RTMemFree);
263
264
265/**
266 * Allocates memory which may contain code.
267 *
268 * @returns Pointer to the allocated memory.
269 * @returns NULL on failure.
270 * @param cb Size in bytes of the memory block to allocate.
271 */
272RTDECL(void *) RTMemExecAlloc(size_t cb) RT_NO_THROW
273{
274 PRTMEMHDR pHdr;
275#ifdef RT_OS_SOLARIS /** @todo figure out why */
276 RT_ASSERT_INTS_ON();
277#else
278 RT_ASSERT_PREEMPTIBLE();
279#endif
280
281 pHdr = rtMemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_EXEC);
282 if (pHdr)
283 {
284#ifdef RTR0MEM_STRICT
285 pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
286 memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
287#endif
288 return pHdr + 1;
289 }
290 return NULL;
291}
292RT_EXPORT_SYMBOL(RTMemExecAlloc);
293
294
295/**
296 * Free executable/read/write memory allocated by RTMemExecAlloc().
297 *
298 * @param pv Pointer to memory block.
299 */
300RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
301{
302 PRTMEMHDR pHdr;
303 RT_ASSERT_INTS_ON();
304
305 if (!pv)
306 return;
307 pHdr = (PRTMEMHDR)pv - 1;
308 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
309 {
310#ifdef RTR0MEM_STRICT
311 AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
312 ("pHdr=%p pv=%p cb=%zu\n"
313 "fence: %.*Rhxs\n"
314 "expected: %.*Rhxs\n",
315 pHdr, pv, pHdr->cb,
316 RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cb,
317 RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
318#endif
319 rtMemFree(pHdr);
320 }
321 else
322 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
323}
324RT_EXPORT_SYMBOL(RTMemExecFree);
325
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