VirtualBox

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

Last change on this file since 28399 was 28298, checked in by vboxsync, 15 years ago

iprt,Config.kmk: Make sure the RTMemAllocVar* alignment gets poisoned by the electric fence. Some interface refactoring.

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