VirtualBox

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

Last change on this file since 7347 was 7245, checked in by vboxsync, 17 years ago

Must update cbReq.

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