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