1 | /** @file
|
---|
2 | * innotek Portable Runtime - Memory Management and Manipulation.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_mem_h
|
---|
27 | #define ___iprt_mem_h
|
---|
28 |
|
---|
29 |
|
---|
30 | #include <iprt/cdefs.h>
|
---|
31 | #include <iprt/types.h>
|
---|
32 |
|
---|
33 | #ifdef IN_GC
|
---|
34 | # error "There are no RTMem APIs available Guest Context!"
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | __BEGIN_DECLS
|
---|
38 |
|
---|
39 | /** @defgroup grp_rt_mem RTMem - Memory Management and Manipulation
|
---|
40 | * @ingroup grp_rt
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 | /** @def RTMEM_ALIGNMENT
|
---|
45 | * The alignment of the memory blocks returned by RTMemAlloc(), RTMemAllocZ(),
|
---|
46 | * RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ() for allocations greater
|
---|
47 | * than RTMEM_ALIGNMENT.
|
---|
48 | */
|
---|
49 | #define RTMEM_ALIGNMENT 8
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Allocates temporary memory.
|
---|
53 | *
|
---|
54 | * Temporary memory blocks are used for not too large memory blocks which
|
---|
55 | * are believed not to stick around for too long. Using this API instead
|
---|
56 | * of RTMemAlloc() not only gives the heap manager room for optimization
|
---|
57 | * but makes the code easier to read.
|
---|
58 | *
|
---|
59 | * @returns Pointer to the allocated memory.
|
---|
60 | * @returns NULL on failure.
|
---|
61 | * @param cb Size in bytes of the memory block to allocated.
|
---|
62 | */
|
---|
63 | RTDECL(void *) RTMemTmpAlloc(size_t cb);
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Allocates zero'ed temporary memory.
|
---|
67 | *
|
---|
68 | * Same as RTMemTmpAlloc() but the memory will be zero'ed.
|
---|
69 | *
|
---|
70 | * @returns Pointer to the allocated memory.
|
---|
71 | * @returns NULL on failure.
|
---|
72 | * @param cb Size in bytes of the memory block to allocated.
|
---|
73 | */
|
---|
74 | RTDECL(void *) RTMemTmpAllocZ(size_t cb);
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Free temporary memory.
|
---|
78 | *
|
---|
79 | * @param pv Pointer to memory block.
|
---|
80 | */
|
---|
81 | RTDECL(void) RTMemTmpFree(void *pv);
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Allocates memory.
|
---|
86 | *
|
---|
87 | * @returns Pointer to the allocated memory.
|
---|
88 | * @returns NULL on failure.
|
---|
89 | * @param cb Size in bytes of the memory block to allocated.
|
---|
90 | */
|
---|
91 | RTDECL(void *) RTMemAlloc(size_t cb);
|
---|
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 | */
|
---|
104 | RTDECL(void *) RTMemAllocZ(size_t cb);
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Duplicates a chunk of memory into a new heap block.
|
---|
108 | *
|
---|
109 | * @returns New heap block with the duplicate data.
|
---|
110 | * @returns NULL if we're out of memory.
|
---|
111 | * @param pvSrc The memory to duplicate.
|
---|
112 | * @param cb The amount of memory to duplicate.
|
---|
113 | */
|
---|
114 | RTDECL(void *) RTMemDup(const void *pvSrc, size_t cb);
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Duplicates a chunk of memory into a new heap block with some
|
---|
118 | * additional zeroed memory.
|
---|
119 | *
|
---|
120 | * @returns New heap block with the duplicate data.
|
---|
121 | * @returns NULL if we're out of memory.
|
---|
122 | * @param pvSrc The memory to duplicate.
|
---|
123 | * @param cbSrc The amount of memory to duplicate.
|
---|
124 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
125 | */
|
---|
126 | RTDECL(void *) RTMemDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra);
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Reallocates memory.
|
---|
130 | *
|
---|
131 | * @returns Pointer to the allocated memory.
|
---|
132 | * @returns NULL on failure.
|
---|
133 | * @param pvOld The memory block to reallocate.
|
---|
134 | * @param cbNew The new block size (in bytes).
|
---|
135 | */
|
---|
136 | RTDECL(void *) RTMemRealloc(void *pvOld, size_t cbNew);
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Free memory related to an virtual machine
|
---|
140 | *
|
---|
141 | * @param pv Pointer to memory block.
|
---|
142 | */
|
---|
143 | RTDECL(void) RTMemFree(void *pv);
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Allocates memory which may contain code.
|
---|
147 | *
|
---|
148 | * @returns Pointer to the allocated memory.
|
---|
149 | * @returns NULL on failure.
|
---|
150 | * @param cb Size in bytes of the memory block to allocate.
|
---|
151 | */
|
---|
152 | RTDECL(void *) RTMemExecAlloc(size_t cb);
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Free executable/read/write memory allocated by RTMemExecAlloc().
|
---|
156 | *
|
---|
157 | * @param pv Pointer to memory block.
|
---|
158 | */
|
---|
159 | RTDECL(void) RTMemExecFree(void *pv);
|
---|
160 |
|
---|
161 | #if defined(IN_RING0) && defined(RT_ARCH_AMD64) && defined(RT_OS_LINUX)
|
---|
162 | /**
|
---|
163 | * Donate read+write+execute memory to the exec heap.
|
---|
164 | *
|
---|
165 | * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
|
---|
166 | * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
|
---|
167 | * allocated memory in the module if it wishes for GCC generated code to work.
|
---|
168 | * GCC can only generate modules that work in the address range ~2GB to ~0
|
---|
169 | * currently.
|
---|
170 | *
|
---|
171 | * The API only accept one single donation.
|
---|
172 | *
|
---|
173 | * @returns IPRT status code.
|
---|
174 | * @param pvMemory Pointer to the memory block.
|
---|
175 | * @param cb The size of the memory block.
|
---|
176 | */
|
---|
177 | RTR0DECL(int) RTR0MemExecDonate(void *pvMemory, size_t cb);
|
---|
178 | #endif /* R0+AMD64+LINUX */
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Allocate page aligned memory.
|
---|
182 | *
|
---|
183 | * @returns Pointer to the allocated memory.
|
---|
184 | * @returns NULL if we're out of memory.
|
---|
185 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
186 | */
|
---|
187 | RTDECL(void *) RTMemPageAlloc(size_t cb);
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Allocate zero'ed page aligned memory.
|
---|
191 | *
|
---|
192 | * @returns Pointer to the allocated memory.
|
---|
193 | * @returns NULL if we're out of memory.
|
---|
194 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
195 | */
|
---|
196 | RTDECL(void *) RTMemPageAllocZ(size_t cb);
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
|
---|
200 | *
|
---|
201 | * @param pv Pointer to the block as it was returned by the allocation function.
|
---|
202 | * NULL will be ignored.
|
---|
203 | */
|
---|
204 | RTDECL(void) RTMemPageFree(void *pv);
|
---|
205 |
|
---|
206 | /** Page level protection flags for RTMemProtect().
|
---|
207 | * @{
|
---|
208 | */
|
---|
209 | /** Read access. */
|
---|
210 | #define RTMEM_PROT_NONE 0
|
---|
211 | /** Read access. */
|
---|
212 | #define RTMEM_PROT_READ 1
|
---|
213 | /** Write access. */
|
---|
214 | #define RTMEM_PROT_WRITE 2
|
---|
215 | /** Execute access. */
|
---|
216 | #define RTMEM_PROT_EXEC 4
|
---|
217 | /** @} */
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Change the page level protection of a memory region.
|
---|
221 | *
|
---|
222 | * @returns iprt status code.
|
---|
223 | * @param pv Start of the region. Will be rounded down to nearest page boundary.
|
---|
224 | * @param cb Size of the region. Will be rounded up to the nearest page boundary.
|
---|
225 | * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
|
---|
226 | */
|
---|
227 | RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect);
|
---|
228 |
|
---|
229 |
|
---|
230 | #ifdef IN_RING0
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Allocates physical contiguous memory (below 4GB).
|
---|
234 | * The allocation is page aligned and the content is undefined.
|
---|
235 | *
|
---|
236 | * @returns Pointer to the memory block. This is page aligned.
|
---|
237 | * @param pPhys Where to store the physical address.
|
---|
238 | * @param cb The allocation size in bytes. This is always
|
---|
239 | * rounded up to PAGE_SIZE.
|
---|
240 | */
|
---|
241 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb);
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Frees memory allocated ysing RTMemContAlloc().
|
---|
245 | *
|
---|
246 | * @param pv Pointer to return from RTMemContAlloc().
|
---|
247 | * @param cb The cb parameter passed to RTMemContAlloc().
|
---|
248 | */
|
---|
249 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb);
|
---|
250 |
|
---|
251 | #endif
|
---|
252 |
|
---|
253 |
|
---|
254 | /** @name Electrical Fence Version of some APIs.
|
---|
255 | * @{
|
---|
256 | */
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Same as RTMemTmpAlloc() except that it's fenced.
|
---|
260 | *
|
---|
261 | * @returns Pointer to the allocated memory.
|
---|
262 | * @returns NULL on failure.
|
---|
263 | * @param cb Size in bytes of the memory block to allocate.
|
---|
264 | */
|
---|
265 | RTDECL(void *) RTMemEfTmpAlloc(size_t cb);
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Same as RTMemTmpAllocZ() except that it's fenced.
|
---|
269 | *
|
---|
270 | * @returns Pointer to the allocated memory.
|
---|
271 | * @returns NULL on failure.
|
---|
272 | * @param cb Size in bytes of the memory block to allocate.
|
---|
273 | */
|
---|
274 | RTDECL(void *) RTMemEfTmpAllocZ(size_t cb);
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Same as RTMemTmpFree() except that it's for fenced memory.
|
---|
278 | *
|
---|
279 | * @param pv Pointer to memory block.
|
---|
280 | */
|
---|
281 | RTDECL(void) RTMemEfTmpFree(void *pv);
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Same as RTMemAlloc() except that it's fenced.
|
---|
285 | *
|
---|
286 | * @returns Pointer to the allocated memory. Free with RTMemEfFree().
|
---|
287 | * @returns NULL on failure.
|
---|
288 | * @param cb Size in bytes of the memory block to allocate.
|
---|
289 | */
|
---|
290 | RTDECL(void *) RTMemEfAlloc(size_t cb);
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Same as RTMemAllocZ() except that it's fenced.
|
---|
294 | *
|
---|
295 | * @returns Pointer to the allocated memory.
|
---|
296 | * @returns NULL on failure.
|
---|
297 | * @param cb Size in bytes of the memory block to allocate.
|
---|
298 | */
|
---|
299 | RTDECL(void *) RTMemEfAllocZ(size_t cb);
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Same as RTMemRealloc() except that it's fenced.
|
---|
303 | *
|
---|
304 | * @returns Pointer to the allocated memory.
|
---|
305 | * @returns NULL on failure.
|
---|
306 | * @param pvOld The memory block to reallocate.
|
---|
307 | * @param cbNew The new block size (in bytes).
|
---|
308 | */
|
---|
309 | RTDECL(void *) RTMemEfRealloc(void *pvOld, size_t cbNew);
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Free memory allocated by any of the RTMemEf* allocators.
|
---|
313 | *
|
---|
314 | * @param pv Pointer to memory block.
|
---|
315 | */
|
---|
316 | RTDECL(void) RTMemEfFree(void *pv);
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Same as RTMemDup() except that it's fenced.
|
---|
320 | *
|
---|
321 | * @returns New heap block with the duplicate data.
|
---|
322 | * @returns NULL if we're out of memory.
|
---|
323 | * @param pvSrc The memory to duplicate.
|
---|
324 | * @param cb The amount of memory to duplicate.
|
---|
325 | */
|
---|
326 | RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb);
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Same as RTMemEfDupEx except that it's fenced.
|
---|
330 | *
|
---|
331 | * @returns New heap block with the duplicate data.
|
---|
332 | * @returns NULL if we're out of memory.
|
---|
333 | * @param pvSrc The memory to duplicate.
|
---|
334 | * @param cbSrc The amount of memory to duplicate.
|
---|
335 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
336 | */
|
---|
337 | RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra);
|
---|
338 |
|
---|
339 | /** @def RTMEM_WRAP_TO_EF_APIS
|
---|
340 | * Define RTMEM_WRAP_TO_EF_APIS to wrap RTMem APIs to RTMemEf APIs.
|
---|
341 | */
|
---|
342 | #ifdef RTMEM_WRAP_TO_EF_APIS
|
---|
343 | # define RTMemTmpAlloc RTMemEfTmpAlloc
|
---|
344 | # define RTMemTmpAllocZ RTMemEfTmpAllocZ
|
---|
345 | # define RTMemTmpFree RTMemEfTmpFree
|
---|
346 | # define RTMemAlloc RTMemEfAlloc
|
---|
347 | # define RTMemAllocZ RTMemEfAllocZ
|
---|
348 | # define RTMemRealloc RTMemEfRealloc
|
---|
349 | # define RTMemFree RTMemEfFree
|
---|
350 | # define RTMemDup RTMemEfDup
|
---|
351 | # define RTMemDupEx RTMemEfDupEx
|
---|
352 | #endif
|
---|
353 | #ifdef __DOXYGEN__
|
---|
354 | # define RTMEM_WRAP_TO_EF_APIS
|
---|
355 | #endif
|
---|
356 |
|
---|
357 | /** @} */
|
---|
358 |
|
---|
359 | /** @} */
|
---|
360 |
|
---|
361 | __END_DECLS
|
---|
362 |
|
---|
363 | #endif
|
---|
364 |
|
---|