1 | /* $Id: alloc-r0drv.cpp 5999 2007-12-07 15:05:06Z 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 | * Allocates temporary memory.
|
---|
40 | *
|
---|
41 | * Temporary memory blocks are used for not too large memory blocks which
|
---|
42 | * are believed not to stick around for too long. Using this API instead
|
---|
43 | * of RTMemAlloc() not only gives the heap manager room for optimization
|
---|
44 | * but makes the code easier to read.
|
---|
45 | *
|
---|
46 | * @returns Pointer to the allocated memory.
|
---|
47 | * @returns NULL on failure.
|
---|
48 | * @param cb Size in bytes of the memory block to allocated.
|
---|
49 | */
|
---|
50 | RTDECL(void *) RTMemTmpAlloc(size_t cb)
|
---|
51 | {
|
---|
52 | return RTMemAlloc(cb);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Allocates zero'ed temporary memory.
|
---|
58 | *
|
---|
59 | * Same as RTMemTmpAlloc() but the memory will be zero'ed.
|
---|
60 | *
|
---|
61 | * @returns Pointer to the allocated memory.
|
---|
62 | * @returns NULL on failure.
|
---|
63 | * @param cb Size in bytes of the memory block to allocated.
|
---|
64 | */
|
---|
65 | RTDECL(void *) RTMemTmpAllocZ(size_t cb)
|
---|
66 | {
|
---|
67 | return RTMemAllocZ(cb);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Free temporary memory.
|
---|
73 | *
|
---|
74 | * @param pv Pointer to memory block.
|
---|
75 | */
|
---|
76 | RTDECL(void) RTMemTmpFree(void *pv)
|
---|
77 | {
|
---|
78 | return RTMemFree(pv);
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Allocates memory.
|
---|
84 | *
|
---|
85 | * @returns Pointer to the allocated memory.
|
---|
86 | * @returns NULL on failure.
|
---|
87 | * @param cb Size in bytes of the memory block to allocated.
|
---|
88 | */
|
---|
89 | RTDECL(void *) RTMemAlloc(size_t cb)
|
---|
90 | {
|
---|
91 | PRTMEMHDR pHdr = rtMemAlloc(cb, 0);
|
---|
92 | if (pHdr)
|
---|
93 | return pHdr + 1;
|
---|
94 | return NULL;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Allocates zero'ed memory.
|
---|
100 | *
|
---|
101 | * Instead of memset(pv, 0, sizeof()) use this when you want zero'ed
|
---|
102 | * memory. This keeps the code smaller and the heap can skip the memset
|
---|
103 | * in about 0.42% of calls :-).
|
---|
104 | *
|
---|
105 | * @returns Pointer to the allocated memory.
|
---|
106 | * @returns NULL on failure.
|
---|
107 | * @param cb Size in bytes of the memory block to allocated.
|
---|
108 | */
|
---|
109 | RTDECL(void *) RTMemAllocZ(size_t cb)
|
---|
110 | {
|
---|
111 | PRTMEMHDR pHdr = rtMemAlloc(cb, RTMEMHDR_FLAG_ZEROED);
|
---|
112 | if (pHdr)
|
---|
113 | return memset(pHdr + 1, 0, pHdr->cb);
|
---|
114 | return NULL;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Reallocates memory.
|
---|
120 | *
|
---|
121 | * @returns Pointer to the allocated memory.
|
---|
122 | * @returns NULL on failure.
|
---|
123 | * @param pvOld The memory block to reallocate.
|
---|
124 | * @param cbNew The new block size (in bytes).
|
---|
125 | */
|
---|
126 | RTDECL(void *) RTMemRealloc(void *pvOld, size_t cbNew)
|
---|
127 | {
|
---|
128 | if (!cbNew)
|
---|
129 | RTMemFree(pvOld);
|
---|
130 | else if (!pvOld)
|
---|
131 | return RTMemAlloc(cbNew);
|
---|
132 | else
|
---|
133 | {
|
---|
134 | PRTMEMHDR pHdrOld = (PRTMEMHDR)pvOld - 1;
|
---|
135 | if (pHdrOld->u32Magic == RTMEMHDR_MAGIC)
|
---|
136 | {
|
---|
137 | PRTMEMHDR pHdrNew;
|
---|
138 | if (pHdrOld->cb >= cbNew && pHdrOld->cb - cbNew <= 128)
|
---|
139 | return pvOld;
|
---|
140 | pHdrNew = rtMemAlloc(cbNew, 0);
|
---|
141 | if (pHdrNew)
|
---|
142 | {
|
---|
143 | size_t cbCopy = RT_MIN(pHdrOld->cb, pHdrNew->cb);
|
---|
144 | memcpy(pHdrNew + 1, pvOld, cbCopy);
|
---|
145 | rtMemFree(pHdrOld);
|
---|
146 | return pHdrNew + 1;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | else
|
---|
150 | AssertMsgFailed(("pHdrOld->u32Magic=%RX32 pvOld=%p cbNew=%#zx\n", pHdrOld->u32Magic, pvOld, cbNew));
|
---|
151 | }
|
---|
152 |
|
---|
153 | return NULL;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Free memory related to an virtual machine
|
---|
159 | *
|
---|
160 | * @param pv Pointer to memory block.
|
---|
161 | */
|
---|
162 | RTDECL(void) RTMemFree(void *pv)
|
---|
163 | {
|
---|
164 | PRTMEMHDR pHdr;
|
---|
165 | if (!pv)
|
---|
166 | return;
|
---|
167 | pHdr = (PRTMEMHDR)pv - 1;
|
---|
168 | if (pHdr->u32Magic == RTMEMHDR_MAGIC)
|
---|
169 | {
|
---|
170 | Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_EXEC));
|
---|
171 | rtMemFree(pHdr);
|
---|
172 | }
|
---|
173 | else
|
---|
174 | AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Allocates memory which may contain code.
|
---|
180 | *
|
---|
181 | * @returns Pointer to the allocated memory.
|
---|
182 | * @returns NULL on failure.
|
---|
183 | * @param cb Size in bytes of the memory block to allocate.
|
---|
184 | */
|
---|
185 | RTDECL(void *) RTMemExecAlloc(size_t cb)
|
---|
186 | {
|
---|
187 | PRTMEMHDR pHdr = rtMemAlloc(cb, RTMEMHDR_FLAG_EXEC);
|
---|
188 | if (pHdr)
|
---|
189 | return pHdr + 1;
|
---|
190 | return NULL;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Free executable/read/write memory allocated by RTMemExecAlloc().
|
---|
196 | *
|
---|
197 | * @param pv Pointer to memory block.
|
---|
198 | */
|
---|
199 | RTDECL(void) RTMemExecFree(void *pv)
|
---|
200 | {
|
---|
201 | PRTMEMHDR pHdr;
|
---|
202 | if (!pv)
|
---|
203 | return;
|
---|
204 | pHdr = (PRTMEMHDR)pv - 1;
|
---|
205 | if (pHdr->u32Magic == RTMEMHDR_MAGIC)
|
---|
206 | rtMemFree(pHdr);
|
---|
207 | else
|
---|
208 | AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
|
---|
209 | }
|
---|
210 |
|
---|