VirtualBox

source: vbox/trunk/include/iprt/mem.h@ 8155

Last change on this file since 8155 was 8155, checked in by vboxsync, 16 years ago

The Big Sun Rebranding Header Change

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

© 2023 Oracle
ContactPrivacy policyTerms of Use