VirtualBox

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

Last change on this file since 2409 was 217, checked in by vboxsync, 18 years ago

hacking darwin memory objects.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1/* $Id: alloc-r0drv.cpp 217 2007-01-21 21:41:29Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - Memory Allocation, Ring-0 Driver.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung 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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include <iprt/string.h>
27#include <iprt/alloc.h>
28#include <iprt/assert.h>
29#include <iprt/param.h>
30#include "r0drv/alloc-r0drv.h"
31
32
33/**
34 * Allocates temporary memory.
35 *
36 * Temporary memory blocks are used for not too large memory blocks which
37 * are believed not to stick around for too long. Using this API instead
38 * of RTMemAlloc() not only gives the heap manager room for optimization
39 * but makes the code easier to read.
40 *
41 * @returns Pointer to the allocated memory.
42 * @returns NULL on failure.
43 * @param cb Size in bytes of the memory block to allocated.
44 */
45RTDECL(void *) RTMemTmpAlloc(size_t cb)
46{
47 return RTMemAlloc(cb);
48}
49
50
51/**
52 * Allocates zero'ed temporary memory.
53 *
54 * Same as RTMemTmpAlloc() but the memory will be zero'ed.
55 *
56 * @returns Pointer to the allocated memory.
57 * @returns NULL on failure.
58 * @param cb Size in bytes of the memory block to allocated.
59 */
60RTDECL(void *) RTMemTmpAllocZ(size_t cb)
61{
62 return RTMemAllocZ(cb);
63}
64
65
66/**
67 * Free temporary memory.
68 *
69 * @param pv Pointer to memory block.
70 */
71RTDECL(void) RTMemTmpFree(void *pv)
72{
73 return RTMemFree(pv);
74}
75
76
77/**
78 * Allocates memory.
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 *) RTMemAlloc(size_t cb)
85{
86 PRTMEMHDR pHdr = rtMemAlloc(cb, 0);
87 if (pHdr)
88 return pHdr + 1;
89 return NULL;
90}
91
92
93/**
94 * Allocates zero'ed memory.
95 *
96 * Instead of memset(pv, 0, sizeof()) use this when you want zero'ed
97 * memory. This keeps the code smaller and the heap can skip the memset
98 * in about 0.42% of calls :-).
99 *
100 * @returns Pointer to the allocated memory.
101 * @returns NULL on failure.
102 * @param cb Size in bytes of the memory block to allocated.
103 */
104RTDECL(void *) RTMemAllocZ(size_t cb)
105{
106 PRTMEMHDR pHdr = rtMemAlloc(cb, RTMEMHDR_FLAG_ZEROED);
107 if (pHdr)
108 return memset(pHdr + 1, 0, pHdr->cb);
109 return NULL;
110}
111
112
113/**
114 * Reallocates memory.
115 *
116 * @returns Pointer to the allocated memory.
117 * @returns NULL on failure.
118 * @param pvOld The memory block to reallocate.
119 * @param cbNew The new block size (in bytes).
120 */
121RTDECL(void *) RTMemRealloc(void *pvOld, size_t cbNew)
122{
123 if (!cbNew)
124 RTMemFree(pvOld);
125 else if (!pvOld)
126 return RTMemAlloc(cbNew);
127 else
128 {
129 PRTMEMHDR pHdrOld = (PRTMEMHDR)pvOld - 1;
130 if (pHdrOld->u32Magic == RTMEMHDR_MAGIC)
131 {
132 PRTMEMHDR pHdrNew;
133 if (pHdrOld->cb >= cbNew && pHdrOld->cb - cbNew <= 128)
134 return pvOld;
135 pHdrNew = rtMemAlloc(cbNew, 0);
136 if (pHdrNew)
137 {
138 size_t cbCopy = RT_MIN(pHdrOld->cb, pHdrNew->cb);
139 memcpy(pHdrNew + 1, pvOld, cbCopy);
140 rtMemFree(pHdrOld);
141 return pHdrNew + 1;
142 }
143 }
144 else
145 AssertMsgFailed(("pHdrOld->u32Magic=%RX32 pvOld=%p cbNew=%#zx\n", pHdrOld->u32Magic, pvOld, cbNew));
146 }
147
148 return NULL;
149}
150
151
152/**
153 * Free memory related to an virtual machine
154 *
155 * @param pv Pointer to memory block.
156 */
157RTDECL(void) RTMemFree(void *pv)
158{
159 PRTMEMHDR pHdr;
160 if (!pv)
161 return;
162 pHdr = (PRTMEMHDR)pv - 1;
163 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
164 {
165 Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_EXEC));
166 rtMemFree(pHdr);
167 }
168 else
169 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
170}
171
172
173/**
174 * Allocates memory which may contain code.
175 *
176 * @returns Pointer to the allocated memory.
177 * @returns NULL on failure.
178 * @param cb Size in bytes of the memory block to allocate.
179 */
180RTDECL(void *) RTMemExecAlloc(size_t cb)
181{
182 PRTMEMHDR pHdr = rtMemAlloc(cb, RTMEMHDR_FLAG_EXEC);
183 if (pHdr)
184 return pHdr + 1;
185 return NULL;
186}
187
188
189/**
190 * Free executable/read/write memory allocated by RTMemExecAlloc().
191 *
192 * @param pv Pointer to memory block.
193 */
194RTDECL(void) RTMemExecFree(void *pv)
195{
196 PRTMEMHDR pHdr;
197 if (!pv)
198 return;
199 pHdr = (PRTMEMHDR)pv - 1;
200 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
201 rtMemFree(pHdr);
202 else
203 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
204}
205
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