VirtualBox

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

Last change on this file since 21349 was 21337, checked in by vboxsync, 15 years ago

IPRT,HostDrv,AddDrv: Export public IPRT symbols for the linux kernel (pain).

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