1 | /** @file
|
---|
2 | * IPRT - Memory Management and Manipulation.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
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_INCLUDED_mem_h
|
---|
27 | #define IPRT_INCLUDED_mem_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 |
|
---|
33 | #include <iprt/cdefs.h>
|
---|
34 | #include <iprt/types.h>
|
---|
35 |
|
---|
36 | #ifdef IPRT_WITH_GCC_SANITIZER
|
---|
37 | # include <sanitizer/lsan_interface.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #ifdef IN_RC
|
---|
41 | # error "There are no RTMem APIs available Guest Context!"
|
---|
42 | #endif
|
---|
43 |
|
---|
44 |
|
---|
45 | /** @defgroup grp_rt_mem RTMem - Memory Management and Manipulation
|
---|
46 | * @ingroup grp_rt
|
---|
47 | * @{
|
---|
48 | */
|
---|
49 |
|
---|
50 | RT_C_DECLS_BEGIN
|
---|
51 |
|
---|
52 | /** @def RTMEM_ALIGNMENT
|
---|
53 | * The alignment of the memory blocks returned by RTMemAlloc(), RTMemAllocZ(),
|
---|
54 | * RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ() for allocations greater
|
---|
55 | * than RTMEM_ALIGNMENT.
|
---|
56 | *
|
---|
57 | * @note This alignment is not forced if the electric fence is active!
|
---|
58 | */
|
---|
59 | #if defined(RT_OS_OS2)
|
---|
60 | # define RTMEM_ALIGNMENT 4
|
---|
61 | #else
|
---|
62 | # define RTMEM_ALIGNMENT 8
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | /** @def RTMEM_TAG
|
---|
66 | * The default allocation tag used by the RTMem allocation APIs.
|
---|
67 | *
|
---|
68 | * When not defined before the inclusion of iprt/mem.h or iprt/memobj.h, this
|
---|
69 | * will default to the pointer to the current file name. The memory API will
|
---|
70 | * make of use of this as pointer to a volatile but read-only string.
|
---|
71 | * The alternative tag includes the line number for a more-detailed analysis.
|
---|
72 | */
|
---|
73 | #ifndef RTMEM_TAG
|
---|
74 | # if 0
|
---|
75 | # define RTMEM_TAG (__FILE__ ":" RT_XSTR(__LINE__))
|
---|
76 | # else
|
---|
77 | # define RTMEM_TAG (__FILE__)
|
---|
78 | # endif
|
---|
79 | #endif
|
---|
80 |
|
---|
81 |
|
---|
82 | /** @name Allocate temporary memory.
|
---|
83 | * @{ */
|
---|
84 | /**
|
---|
85 | * Allocates temporary memory with default tag.
|
---|
86 | *
|
---|
87 | * Temporary memory blocks are used for not too large memory blocks which
|
---|
88 | * are believed not to stick around for too long. Using this API instead
|
---|
89 | * of RTMemAlloc() not only gives the heap manager room for optimization
|
---|
90 | * but makes the code easier to read.
|
---|
91 | *
|
---|
92 | * @returns Pointer to the allocated memory.
|
---|
93 | * @returns NULL on failure, assertion raised in strict builds.
|
---|
94 | * @param cb Size in bytes of the memory block to allocated.
|
---|
95 | */
|
---|
96 | #define RTMemTmpAlloc(cb) RTMemTmpAllocTag((cb), RTMEM_TAG)
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Allocates temporary memory with custom tag.
|
---|
100 | *
|
---|
101 | * Temporary memory blocks are used for not too large memory blocks which
|
---|
102 | * are believed not to stick around for too long. Using this API instead
|
---|
103 | * of RTMemAlloc() not only gives the heap manager room for optimization
|
---|
104 | * but makes the code easier to read.
|
---|
105 | *
|
---|
106 | * @returns Pointer to the allocated memory.
|
---|
107 | * @returns NULL on failure, assertion raised in strict builds.
|
---|
108 | * @param cb Size in bytes of the memory block to allocated.
|
---|
109 | * @param pszTag Allocation tag used for statistics and such.
|
---|
110 | */
|
---|
111 | RTDECL(void *) RTMemTmpAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Allocates zero'd temporary memory with default tag.
|
---|
115 | *
|
---|
116 | * Same as RTMemTmpAlloc() but the memory will be zero'd.
|
---|
117 | *
|
---|
118 | * @returns Pointer to the allocated memory.
|
---|
119 | * @returns NULL on failure, assertion raised in strict builds.
|
---|
120 | * @param cb Size in bytes of the memory block to allocated.
|
---|
121 | */
|
---|
122 | #define RTMemTmpAllocZ(cb) RTMemTmpAllocZTag((cb), RTMEM_TAG)
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Allocates zero'd temporary memory with custom tag.
|
---|
126 | *
|
---|
127 | * Same as RTMemTmpAlloc() but the memory will be zero'd.
|
---|
128 | *
|
---|
129 | * @returns Pointer to the allocated memory.
|
---|
130 | * @returns NULL on failure, assertion raised in strict builds.
|
---|
131 | * @param cb Size in bytes of the memory block to allocated.
|
---|
132 | * @param pszTag Allocation tag used for statistics and such.
|
---|
133 | */
|
---|
134 | RTDECL(void *) RTMemTmpAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Free temporary memory.
|
---|
138 | *
|
---|
139 | * @param pv Pointer to memory block.
|
---|
140 | */
|
---|
141 | RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW_PROTO;
|
---|
142 |
|
---|
143 | /** @} */
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Allocates memory with default tag.
|
---|
148 | *
|
---|
149 | * @returns Pointer to the allocated memory.
|
---|
150 | * @returns NULL on failure, assertion raised in strict builds.
|
---|
151 | * @param cb Size in bytes of the memory block to allocated.
|
---|
152 | */
|
---|
153 | #define RTMemAlloc(cb) RTMemAllocTag((cb), RTMEM_TAG)
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Allocates memory with custom tag.
|
---|
157 | *
|
---|
158 | * @returns Pointer to the allocated memory.
|
---|
159 | * @returns NULL on failure, assertion raised in strict builds.
|
---|
160 | * @param cb Size in bytes of the memory block to allocated.
|
---|
161 | * @param pszTag Allocation tag used for statistics and such.
|
---|
162 | */
|
---|
163 | RTDECL(void *) RTMemAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Allocates zero'd memory with default tag.
|
---|
167 | *
|
---|
168 | * Instead of memset(pv, 0, sizeof()) use this when you want zero'd
|
---|
169 | * memory. This keeps the code smaller and the heap can skip the memset
|
---|
170 | * in about 0.42% of calls :-).
|
---|
171 | *
|
---|
172 | * @returns Pointer to the allocated memory.
|
---|
173 | * @returns NULL on failure.
|
---|
174 | * @param cb Size in bytes of the memory block to allocated.
|
---|
175 | */
|
---|
176 | #define RTMemAllocZ(cb) RTMemAllocZTag((cb), RTMEM_TAG)
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Allocates zero'd memory with custom tag.
|
---|
180 | *
|
---|
181 | * Instead of memset(pv, 0, sizeof()) use this when you want zero'd
|
---|
182 | * memory. This keeps the code smaller and the heap can skip the memset
|
---|
183 | * in about 0.42% of calls :-).
|
---|
184 | *
|
---|
185 | * @returns Pointer to the allocated memory.
|
---|
186 | * @returns NULL on failure.
|
---|
187 | * @param cb Size in bytes of the memory block to allocated.
|
---|
188 | * @param pszTag Allocation tag used for statistics and such.
|
---|
189 | */
|
---|
190 | RTDECL(void *) RTMemAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Wrapper around RTMemAlloc for automatically aligning variable sized
|
---|
194 | * allocations so that the various electric fence heaps works correctly.
|
---|
195 | *
|
---|
196 | * @returns See RTMemAlloc.
|
---|
197 | * @param cbUnaligned The unaligned size.
|
---|
198 | */
|
---|
199 | #define RTMemAllocVar(cbUnaligned) RTMemAllocVarTag((cbUnaligned), RTMEM_TAG)
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Wrapper around RTMemAllocTag for automatically aligning variable sized
|
---|
203 | * allocations so that the various electric fence heaps works correctly.
|
---|
204 | *
|
---|
205 | * @returns See RTMemAlloc.
|
---|
206 | * @param cbUnaligned The unaligned size.
|
---|
207 | * @param pszTag Allocation tag used for statistics and such.
|
---|
208 | */
|
---|
209 | RTDECL(void *) RTMemAllocVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Wrapper around RTMemAllocZ for automatically aligning variable sized
|
---|
213 | * allocations so that the various electric fence heaps works correctly.
|
---|
214 | *
|
---|
215 | * @returns See RTMemAllocZ.
|
---|
216 | * @param cbUnaligned The unaligned size.
|
---|
217 | */
|
---|
218 | #define RTMemAllocZVar(cbUnaligned) RTMemAllocZVarTag((cbUnaligned), RTMEM_TAG)
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Wrapper around RTMemAllocZTag for automatically aligning variable sized
|
---|
222 | * allocations so that the various electric fence heaps works correctly.
|
---|
223 | *
|
---|
224 | * @returns See RTMemAllocZ.
|
---|
225 | * @param cbUnaligned The unaligned size.
|
---|
226 | * @param pszTag Allocation tag used for statistics and such.
|
---|
227 | */
|
---|
228 | RTDECL(void *) RTMemAllocZVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Duplicates a chunk of memory into a new heap block (default tag).
|
---|
232 | *
|
---|
233 | * @returns New heap block with the duplicate data.
|
---|
234 | * @returns NULL if we're out of memory.
|
---|
235 | * @param pvSrc The memory to duplicate.
|
---|
236 | * @param cb The amount of memory to duplicate.
|
---|
237 | */
|
---|
238 | #define RTMemDup(pvSrc, cb) RTMemDupTag((pvSrc), (cb), RTMEM_TAG)
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Duplicates a chunk of memory into a new heap block (custom tag).
|
---|
242 | *
|
---|
243 | * @returns New heap block with the duplicate data.
|
---|
244 | * @returns NULL if we're out of memory.
|
---|
245 | * @param pvSrc The memory to duplicate.
|
---|
246 | * @param cb The amount of memory to duplicate.
|
---|
247 | * @param pszTag Allocation tag used for statistics and such.
|
---|
248 | */
|
---|
249 | RTDECL(void *) RTMemDupTag(const void *pvSrc, size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Duplicates a chunk of memory into a new heap block with some additional
|
---|
253 | * zeroed memory (default tag).
|
---|
254 | *
|
---|
255 | * @returns New heap block with the duplicate data.
|
---|
256 | * @returns NULL if we're out of memory.
|
---|
257 | * @param pvSrc The memory to duplicate.
|
---|
258 | * @param cbSrc The amount of memory to duplicate.
|
---|
259 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
260 | */
|
---|
261 | #define RTMemDupEx(pvSrc, cbSrc, cbExtra) RTMemDupExTag((pvSrc), (cbSrc), (cbExtra), RTMEM_TAG)
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Duplicates a chunk of memory into a new heap block with some additional
|
---|
265 | * zeroed memory (default tag).
|
---|
266 | *
|
---|
267 | * @returns New heap block with the duplicate data.
|
---|
268 | * @returns NULL if we're out of memory.
|
---|
269 | * @param pvSrc The memory to duplicate.
|
---|
270 | * @param cbSrc The amount of memory to duplicate.
|
---|
271 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
272 | * @param pszTag Allocation tag used for statistics and such.
|
---|
273 | */
|
---|
274 | RTDECL(void *) RTMemDupExTag(const void *pvSrc, size_t cbSrc, size_t cbExtra, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Reallocates memory with default tag.
|
---|
278 | *
|
---|
279 | * @returns Pointer to the allocated memory.
|
---|
280 | * @returns NULL on failure.
|
---|
281 | * @param pvOld The memory block to reallocate.
|
---|
282 | * @param cbNew The new block size (in bytes).
|
---|
283 | */
|
---|
284 | #define RTMemRealloc(pvOld, cbNew) RTMemReallocTag((pvOld), (cbNew), RTMEM_TAG)
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Reallocates memory with custom tag.
|
---|
288 | *
|
---|
289 | * @returns Pointer to the allocated memory.
|
---|
290 | * @returns NULL on failure.
|
---|
291 | * @param pvOld The memory block to reallocate.
|
---|
292 | * @param cbNew The new block size (in bytes).
|
---|
293 | * @param pszTag Allocation tag used for statistics and such.
|
---|
294 | */
|
---|
295 | RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Frees memory.
|
---|
299 | *
|
---|
300 | * @param pv Pointer to memory block.
|
---|
301 | */
|
---|
302 | RTDECL(void) RTMemFree(void *pv) RT_NO_THROW_PROTO;
|
---|
303 |
|
---|
304 |
|
---|
305 |
|
---|
306 | /** @name RTR0MemAllocEx and RTR0MemAllocExTag flags.
|
---|
307 | * @{ */
|
---|
308 | /** The returned memory should be zeroed. */
|
---|
309 | #define RTMEMALLOCEX_FLAGS_ZEROED RT_BIT(0)
|
---|
310 | /** It must be load code into the returned memory block and execute it. */
|
---|
311 | #define RTMEMALLOCEX_FLAGS_EXEC RT_BIT(1)
|
---|
312 | /** Allocation from any context.
|
---|
313 | * Will return VERR_NOT_SUPPORTED if not supported. */
|
---|
314 | #define RTMEMALLOCEX_FLAGS_ANY_CTX_ALLOC RT_BIT(2)
|
---|
315 | /** Allocate the memory such that it can be freed from any context.
|
---|
316 | * Will return VERR_NOT_SUPPORTED if not supported. */
|
---|
317 | #define RTMEMALLOCEX_FLAGS_ANY_CTX_FREE RT_BIT(3)
|
---|
318 | /** Allocate and free from any context.
|
---|
319 | * Will return VERR_NOT_SUPPORTED if not supported. */
|
---|
320 | #define RTMEMALLOCEX_FLAGS_ANY_CTX (RTMEMALLOCEX_FLAGS_ANY_CTX_ALLOC | RTMEMALLOCEX_FLAGS_ANY_CTX_FREE)
|
---|
321 | /** Reachable by 16-bit address.
|
---|
322 | * Will return VERR_NOT_SUPPORTED if not supported. */
|
---|
323 | #define RTMEMALLOCEX_FLAGS_16BIT_REACH RT_BIT(4)
|
---|
324 | /** Reachable by 32-bit address.
|
---|
325 | * Will return VERR_NOT_SUPPORTED if not supported. */
|
---|
326 | #define RTMEMALLOCEX_FLAGS_32BIT_REACH RT_BIT(5)
|
---|
327 | /** Mask of valid flags. */
|
---|
328 | #define RTMEMALLOCEX_FLAGS_VALID_MASK UINT32_C(0x0000003f)
|
---|
329 | /** Mask of valid flags for ring-0. */
|
---|
330 | #define RTMEMALLOCEX_FLAGS_VALID_MASK_R0 UINT32_C(0x0000000f)
|
---|
331 | /** @} */
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Extended heap allocation API, default tag.
|
---|
335 | *
|
---|
336 | * @returns IPRT status code.
|
---|
337 | * @retval VERR_NO_MEMORY if we're out of memory.
|
---|
338 | * @retval VERR_NO_EXEC_MEMORY if we're out of executable memory.
|
---|
339 | * @retval VERR_NOT_SUPPORTED if any of the specified flags are unsupported.
|
---|
340 | *
|
---|
341 | * @param cb The amount of memory to allocate.
|
---|
342 | * @param cbAlignment The alignment requirements. Use 0 to indicate
|
---|
343 | * default alignment.
|
---|
344 | * @param fFlags A combination of the RTMEMALLOCEX_FLAGS_XXX
|
---|
345 | * defines.
|
---|
346 | * @param ppv Where to return the memory.
|
---|
347 | */
|
---|
348 | #define RTMemAllocEx(cb, cbAlignment, fFlags, ppv) RTMemAllocExTag((cb), (cbAlignment), (fFlags), RTMEM_TAG, (ppv))
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Extended heap allocation API, custom tag.
|
---|
352 | *
|
---|
353 | * Depending on the implementation, using this function may add extra overhead,
|
---|
354 | * so use the simpler APIs where ever possible.
|
---|
355 | *
|
---|
356 | * @returns IPRT status code.
|
---|
357 | * @retval VERR_NO_MEMORY if we're out of memory.
|
---|
358 | * @retval VERR_NO_EXEC_MEMORY if we're out of executable memory.
|
---|
359 | * @retval VERR_NOT_SUPPORTED if any of the specified flags are unsupported.
|
---|
360 | *
|
---|
361 | * @param cb The amount of memory to allocate.
|
---|
362 | * @param cbAlignment The alignment requirements. Use 0 to indicate
|
---|
363 | * default alignment.
|
---|
364 | * @param fFlags A combination of the RTMEMALLOCEX_FLAGS_XXX
|
---|
365 | * defines.
|
---|
366 | * @param pszTag The tag.
|
---|
367 | * @param ppv Where to return the memory.
|
---|
368 | */
|
---|
369 | RTDECL(int) RTMemAllocExTag(size_t cb, size_t cbAlignment, uint32_t fFlags, const char *pszTag, void **ppv) RT_NO_THROW_PROTO;
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * For freeing memory allocated by RTMemAllocEx or RTMemAllocExTag.
|
---|
373 | *
|
---|
374 | * @param pv What to free, NULL is fine.
|
---|
375 | * @param cb The amount of allocated memory.
|
---|
376 | */
|
---|
377 | RTDECL(void) RTMemFreeEx(void *pv, size_t cb) RT_NO_THROW_PROTO;
|
---|
378 |
|
---|
379 |
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Allocates memory which may contain code (default tag).
|
---|
383 | *
|
---|
384 | * @returns Pointer to the allocated memory.
|
---|
385 | * @returns NULL on failure.
|
---|
386 | * @param cb Size in bytes of the memory block to allocate.
|
---|
387 | */
|
---|
388 | #define RTMemExecAlloc(cb) RTMemExecAllocTag((cb), RTMEM_TAG)
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Allocates memory which may contain code (custom tag).
|
---|
392 | *
|
---|
393 | * @returns Pointer to the allocated memory.
|
---|
394 | * @returns NULL on failure.
|
---|
395 | * @param cb Size in bytes of the memory block to allocate.
|
---|
396 | * @param pszTag Allocation tag used for statistics and such.
|
---|
397 | */
|
---|
398 | RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Free executable/read/write memory allocated by RTMemExecAlloc().
|
---|
402 | *
|
---|
403 | * @param pv Pointer to memory block.
|
---|
404 | * @param cb The allocation size.
|
---|
405 | */
|
---|
406 | RTDECL(void) RTMemExecFree(void *pv, size_t cb) RT_NO_THROW_PROTO;
|
---|
407 |
|
---|
408 | #if defined(IN_RING0) && defined(RT_ARCH_AMD64) && defined(RT_OS_LINUX)
|
---|
409 | /**
|
---|
410 | * Donate read+write+execute memory to the exec heap.
|
---|
411 | *
|
---|
412 | * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
|
---|
413 | * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
|
---|
414 | * allocated memory in the module if it wishes for GCC generated code to work.
|
---|
415 | * GCC can only generate modules that work in the address range ~2GB to ~0
|
---|
416 | * currently.
|
---|
417 | *
|
---|
418 | * The API only accept one single donation.
|
---|
419 | *
|
---|
420 | * @returns IPRT status code.
|
---|
421 | * @param pvMemory Pointer to the memory block.
|
---|
422 | * @param cb The size of the memory block.
|
---|
423 | */
|
---|
424 | RTR0DECL(int) RTR0MemExecDonate(void *pvMemory, size_t cb) RT_NO_THROW_PROTO;
|
---|
425 | #endif /* R0+AMD64+LINUX */
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Allocate page aligned memory with default tag.
|
---|
429 | *
|
---|
430 | * @returns Pointer to the allocated memory.
|
---|
431 | * @returns NULL if we're out of memory.
|
---|
432 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
433 | */
|
---|
434 | #define RTMemPageAlloc(cb) RTMemPageAllocTag((cb), RTMEM_TAG)
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Allocate page aligned memory with custom tag.
|
---|
438 | *
|
---|
439 | * @returns Pointer to the allocated memory.
|
---|
440 | * @returns NULL if we're out of memory.
|
---|
441 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
442 | * @param pszTag Allocation tag used for statistics and such.
|
---|
443 | */
|
---|
444 | RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * Allocate zero'd page aligned memory with default tag.
|
---|
448 | *
|
---|
449 | * @returns Pointer to the allocated memory.
|
---|
450 | * @returns NULL if we're out of memory.
|
---|
451 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
452 | */
|
---|
453 | #define RTMemPageAllocZ(cb) RTMemPageAllocZTag((cb), RTMEM_TAG)
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Allocate zero'd page aligned memory with custom tag.
|
---|
457 | *
|
---|
458 | * @returns Pointer to the allocated memory.
|
---|
459 | * @returns NULL if we're out of memory.
|
---|
460 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
461 | * @param pszTag Allocation tag used for statistics and such.
|
---|
462 | */
|
---|
463 | RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Allocate page aligned memory with default tag, extended version.
|
---|
467 | *
|
---|
468 | * @returns Pointer to the allocated memory.
|
---|
469 | * @returns NULL if we're out of memory.
|
---|
470 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
471 | * @param fFlags RTMEMPAGEALLOC_F_XXX.
|
---|
472 | */
|
---|
473 | #define RTMemPageAllocEx(cb, fFlags) RTMemPageAllocExTag((cb), (fFlags), RTMEM_TAG)
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Allocate page aligned memory with custom tag, extended version.
|
---|
477 | *
|
---|
478 | * @returns Pointer to the allocated memory.
|
---|
479 | * @returns NULL if we're out of memory.
|
---|
480 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
481 | * @param fFlags RTMEMPAGEALLOC_F_XXX.
|
---|
482 | * @param pszTag Allocation tag used for statistics and such.
|
---|
483 | */
|
---|
484 | RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
485 |
|
---|
486 | /** @name RTMEMPAGEALLOC_F_XXX - flags for RTMemPageAllocEx() and RTMemPageAllocExTag()
|
---|
487 | * @{ */
|
---|
488 | /** Zero the allocation. */
|
---|
489 | #define RTMEMPAGEALLOC_F_ZERO RT_BIT_32(0)
|
---|
490 | /** Try lock the allocation (failure ignored). */
|
---|
491 | #define RTMEMPAGEALLOC_F_ADVISE_LOCKED RT_BIT_32(1)
|
---|
492 | /** Try prevent the memory from ending up in a dump/core. */
|
---|
493 | #define RTMEMPAGEALLOC_F_ADVISE_NO_DUMP RT_BIT_32(2)
|
---|
494 | /** Valid bit mask. */
|
---|
495 | #define RTMEMPAGEALLOC_F_VALID_MASK UINT32_C(0x00000007)
|
---|
496 | /** @} */
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
|
---|
500 | *
|
---|
501 | * @param pv Pointer to the block as it was returned by the allocation function.
|
---|
502 | * NULL will be ignored.
|
---|
503 | * @param cb The allocation size. Will be rounded up to page size.
|
---|
504 | * Ignored if @a pv is NULL.
|
---|
505 | */
|
---|
506 | RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_PROTO;
|
---|
507 |
|
---|
508 | /** Page level protection flags for RTMemProtect().
|
---|
509 | * @{
|
---|
510 | */
|
---|
511 | /** No access at all. */
|
---|
512 | #define RTMEM_PROT_NONE 0
|
---|
513 | /** Read access. */
|
---|
514 | #define RTMEM_PROT_READ 1
|
---|
515 | /** Write access. */
|
---|
516 | #define RTMEM_PROT_WRITE 2
|
---|
517 | /** Execute access. */
|
---|
518 | #define RTMEM_PROT_EXEC 4
|
---|
519 | /** @} */
|
---|
520 |
|
---|
521 | /**
|
---|
522 | * Change the page level protection of a memory region.
|
---|
523 | *
|
---|
524 | * @returns iprt status code.
|
---|
525 | * @param pv Start of the region. Will be rounded down to nearest page boundary.
|
---|
526 | * @param cb Size of the region. Will be rounded up to the nearest page boundary.
|
---|
527 | * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
|
---|
528 | */
|
---|
529 | RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW_PROTO;
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Goes thru some pains to make sure the specified memory block is thoroughly
|
---|
533 | * scrambled.
|
---|
534 | *
|
---|
535 | * @param pv The start of the memory block.
|
---|
536 | * @param cb The size of the memory block.
|
---|
537 | * @param cMinPasses The minimum number of passes to make.
|
---|
538 | */
|
---|
539 | RTDECL(void) RTMemWipeThoroughly(void *pv, size_t cb, size_t cMinPasses) RT_NO_THROW_PROTO;
|
---|
540 |
|
---|
541 |
|
---|
542 | /** @def RTMEM_WILL_LEAK
|
---|
543 | * Macro for hinting that a memory allocation @a a_pv will leak.
|
---|
544 | *
|
---|
545 | * @note This shall only be used in code that doesn't allocate the object.
|
---|
546 | * Code allocating memory knowing it will leak shall start the allocation
|
---|
547 | * tag string with 'will-leak:'.
|
---|
548 | */
|
---|
549 | /** @def RTMEM_MAY_LEAK
|
---|
550 | * Macro for hinting that a memory allocation @a a_pv may leak.
|
---|
551 | *
|
---|
552 | * @note This shall only be used in code that doesn't allocate the object.
|
---|
553 | * Code allocating memory knowing it may leak shall start the allocation
|
---|
554 | * tag string with 'may-leak:'.
|
---|
555 | */
|
---|
556 | #ifdef IPRT_WITH_GCC_SANITIZER
|
---|
557 | # define RTMEM_WILL_LEAK(a_pv) __lsan_ignore_object(a_pv)
|
---|
558 | # define RTMEM_MAY_LEAK(a_pv) __lsan_ignore_object(a_pv)
|
---|
559 | #else
|
---|
560 | # define RTMEM_WILL_LEAK(a_pv) do { } while (0)
|
---|
561 | # define RTMEM_MAY_LEAK(a_pv) do { } while (0)
|
---|
562 | #endif
|
---|
563 |
|
---|
564 |
|
---|
565 | /** @def RTMEM_IMPLEMENT_NEW_AND_DELETE
|
---|
566 | * Provides a new and delete implementation to a class using IPRT's RTMem
|
---|
567 | * allocator.
|
---|
568 | */
|
---|
569 | #if !defined(RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF) || defined(RTMEM_NO_WRAP_SOME_NEW_AND_DELETE_TO_EF)
|
---|
570 | # ifdef RT_EXCEPTIONS_ENABLED
|
---|
571 | # define RTMEM_IMPLEMENT_NEW_AND_DELETE() \
|
---|
572 | void *operator new(size_t cb) RT_THROW(std::bad_alloc) \
|
---|
573 | { \
|
---|
574 | void *pv = RTMemAlloc(cb); \
|
---|
575 | if (RT_LIKELY(pv)) \
|
---|
576 | return pv; \
|
---|
577 | throw std::bad_alloc(); \
|
---|
578 | } \
|
---|
579 | void *operator new(size_t cb, const std::nothrow_t ¬hrow_constant) RT_NO_THROW_DEF \
|
---|
580 | { \
|
---|
581 | NOREF(nothrow_constant); \
|
---|
582 | return RTMemAlloc(cb); \
|
---|
583 | } \
|
---|
584 | void *operator new(size_t cb, void *pvBuf) RT_NO_THROW_DEF \
|
---|
585 | { \
|
---|
586 | NOREF(cb); \
|
---|
587 | return pvBuf; \
|
---|
588 | } \
|
---|
589 | void *operator new[](size_t cb) RT_THROW(std::bad_alloc) \
|
---|
590 | { \
|
---|
591 | void *pv = RTMemAlloc(cb); \
|
---|
592 | if (RT_LIKELY(pv)) \
|
---|
593 | return pv; \
|
---|
594 | throw std::bad_alloc(); \
|
---|
595 | } \
|
---|
596 | void *operator new[](size_t cb, const std::nothrow_t ¬hrow_constant) RT_NO_THROW_DEF \
|
---|
597 | { \
|
---|
598 | NOREF(nothrow_constant); \
|
---|
599 | return RTMemAlloc(cb); \
|
---|
600 | } \
|
---|
601 | \
|
---|
602 | void operator delete(void *pv) RT_NO_THROW_DEF \
|
---|
603 | { \
|
---|
604 | RTMemFree(pv); \
|
---|
605 | } \
|
---|
606 | void operator delete(void *pv, const std::nothrow_t ¬hrow_constant) RT_NO_THROW_DEF \
|
---|
607 | { \
|
---|
608 | NOREF(nothrow_constant); \
|
---|
609 | RTMemFree(pv); \
|
---|
610 | } \
|
---|
611 | void operator delete[](void *pv) RT_NO_THROW_DEF \
|
---|
612 | { \
|
---|
613 | RTMemFree(pv); \
|
---|
614 | } \
|
---|
615 | void operator delete[](void *pv, const std::nothrow_t ¬hrow_constant) RT_NO_THROW_DEF \
|
---|
616 | { \
|
---|
617 | NOREF(nothrow_constant); \
|
---|
618 | RTMemFree(pv); \
|
---|
619 | } \
|
---|
620 | \
|
---|
621 | typedef int UsingIprtNewAndDeleteOperators
|
---|
622 | # else /* !RT_EXCEPTIONS_ENABLED */
|
---|
623 | # define RTMEM_IMPLEMENT_NEW_AND_DELETE() \
|
---|
624 | void *operator new(size_t cb) \
|
---|
625 | { \
|
---|
626 | return RTMemAlloc(cb); \
|
---|
627 | } \
|
---|
628 | void *operator new(size_t cb, const std::nothrow_t ¬hrow_constant) \
|
---|
629 | { \
|
---|
630 | NOREF(nothrow_constant); \
|
---|
631 | return RTMemAlloc(cb); \
|
---|
632 | } \
|
---|
633 | void *operator new(size_t cb, void *pvBuf) RT_NO_THROW_DEF \
|
---|
634 | { \
|
---|
635 | NOREF(cb); \
|
---|
636 | return pvBuf; \
|
---|
637 | } \
|
---|
638 | void *operator new[](size_t cb) \
|
---|
639 | { \
|
---|
640 | return RTMemAlloc(cb); \
|
---|
641 | } \
|
---|
642 | void *operator new[](size_t cb, const std::nothrow_t ¬hrow_constant) \
|
---|
643 | { \
|
---|
644 | NOREF(nothrow_constant); \
|
---|
645 | return RTMemAlloc(cb); \
|
---|
646 | } \
|
---|
647 | \
|
---|
648 | void operator delete(void *pv) \
|
---|
649 | { \
|
---|
650 | RTMemFree(pv); \
|
---|
651 | } \
|
---|
652 | void operator delete(void *pv, const std::nothrow_t ¬hrow_constant) \
|
---|
653 | { \
|
---|
654 | NOREF(nothrow_constant); \
|
---|
655 | RTMemFree(pv); \
|
---|
656 | } \
|
---|
657 | void operator delete[](void *pv) \
|
---|
658 | { \
|
---|
659 | RTMemFree(pv); \
|
---|
660 | } \
|
---|
661 | void operator delete[](void *pv, const std::nothrow_t ¬hrow_constant) \
|
---|
662 | { \
|
---|
663 | NOREF(nothrow_constant); \
|
---|
664 | RTMemFree(pv); \
|
---|
665 | } \
|
---|
666 | \
|
---|
667 | typedef int UsingIprtNewAndDeleteOperators
|
---|
668 | # endif /* !RT_EXCEPTIONS_ENABLED */
|
---|
669 | #else /* defined(RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF) && !defined(RTMEM_NO_WRAP_SOME_NEW_AND_DELETE_TO_EF) */
|
---|
670 | # define RTMEM_IMPLEMENT_NEW_AND_DELETE() RTMEMEF_NEW_AND_DELETE_OPERATORS()
|
---|
671 | #endif /* defined(RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF) && !defined(RTMEM_NO_WRAP_SOME_NEW_AND_DELETE_TO_EF) */
|
---|
672 |
|
---|
673 |
|
---|
674 | #ifdef IN_RING0
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * Allocates physical contiguous memory (below 4GB).
|
---|
678 | * The allocation is page aligned and the content is undefined.
|
---|
679 | *
|
---|
680 | * @returns Pointer to the memory block. This is page aligned.
|
---|
681 | * @param pPhys Where to store the physical address.
|
---|
682 | * @param cb The allocation size in bytes. This is always
|
---|
683 | * rounded up to PAGE_SIZE.
|
---|
684 | */
|
---|
685 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb) RT_NO_THROW_PROTO;
|
---|
686 |
|
---|
687 | /**
|
---|
688 | * Frees memory allocated ysing RTMemContAlloc().
|
---|
689 | *
|
---|
690 | * @param pv Pointer to return from RTMemContAlloc().
|
---|
691 | * @param cb The cb parameter passed to RTMemContAlloc().
|
---|
692 | */
|
---|
693 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb) RT_NO_THROW_PROTO;
|
---|
694 |
|
---|
695 | /**
|
---|
696 | * Copy memory from an user mode buffer into a kernel buffer.
|
---|
697 | *
|
---|
698 | * @retval VINF_SUCCESS on success.
|
---|
699 | * @retval VERR_ACCESS_DENIED on error.
|
---|
700 | *
|
---|
701 | * @param pvDst The kernel mode destination address.
|
---|
702 | * @param R3PtrSrc The user mode source address.
|
---|
703 | * @param cb The number of bytes to copy.
|
---|
704 | */
|
---|
705 | RTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst, RTR3PTR R3PtrSrc, size_t cb);
|
---|
706 |
|
---|
707 | /**
|
---|
708 | * Copy memory from a kernel buffer into a user mode one.
|
---|
709 | *
|
---|
710 | * @retval VINF_SUCCESS on success.
|
---|
711 | * @retval VERR_ACCESS_DENIED on error.
|
---|
712 | *
|
---|
713 | * @param R3PtrDst The user mode destination address.
|
---|
714 | * @param pvSrc The kernel mode source address.
|
---|
715 | * @param cb The number of bytes to copy.
|
---|
716 | */
|
---|
717 | RTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb);
|
---|
718 |
|
---|
719 | /**
|
---|
720 | * Tests if the specified address is in the user addressable range.
|
---|
721 | *
|
---|
722 | * This function does not check whether the memory at that address is accessible
|
---|
723 | * or anything of that sort, only if the address it self is in the user mode
|
---|
724 | * range.
|
---|
725 | *
|
---|
726 | * @returns true if it's in the user addressable range. false if not.
|
---|
727 | * @param R3Ptr The user mode pointer to test.
|
---|
728 | *
|
---|
729 | * @remarks Some systems may have overlapping kernel and user address ranges.
|
---|
730 | * One prominent example of this is the x86 version of Mac OS X. Use
|
---|
731 | * RTR0MemAreKrnlAndUsrDifferent() to check.
|
---|
732 | */
|
---|
733 | RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr);
|
---|
734 |
|
---|
735 | /**
|
---|
736 | * Tests if the specified address is in the kernel mode range.
|
---|
737 | *
|
---|
738 | * This function does not check whether the memory at that address is accessible
|
---|
739 | * or anything of that sort, only if the address it self is in the kernel mode
|
---|
740 | * range.
|
---|
741 | *
|
---|
742 | * @returns true if it's in the kernel range. false if not.
|
---|
743 | * @param pv The alleged kernel mode pointer.
|
---|
744 | *
|
---|
745 | * @remarks Some systems may have overlapping kernel and user address ranges.
|
---|
746 | * One prominent example of this is the x86 version of Mac OS X. Use
|
---|
747 | * RTR0MemAreKrnlAndUsrDifferent() to check.
|
---|
748 | */
|
---|
749 | RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv);
|
---|
750 |
|
---|
751 | /**
|
---|
752 | * Are user mode and kernel mode address ranges distinctly different.
|
---|
753 | *
|
---|
754 | * This determines whether RTR0MemKernelIsValidAddr and RTR0MemUserIsValidAddr
|
---|
755 | * can be used for deciding whether some arbitrary address is a user mode or a
|
---|
756 | * kernel mode one.
|
---|
757 | *
|
---|
758 | * @returns true if they are, false if not.
|
---|
759 | */
|
---|
760 | RTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void);
|
---|
761 |
|
---|
762 | /**
|
---|
763 | * Copy memory from an potentially unsafe kernel mode location and into a safe
|
---|
764 | * (kernel) buffer.
|
---|
765 | *
|
---|
766 | * @retval VINF_SUCCESS on success.
|
---|
767 | * @retval VERR_ACCESS_DENIED on error.
|
---|
768 | * @retval VERR_NOT_SUPPORTED if not (yet) supported.
|
---|
769 | *
|
---|
770 | * @param pvDst The destination address (safe).
|
---|
771 | * @param pvSrc The source address (potentially unsafe).
|
---|
772 | * @param cb The number of bytes to copy.
|
---|
773 | */
|
---|
774 | RTR0DECL(int) RTR0MemKernelCopyFrom(void *pvDst, void const *pvSrc, size_t cb);
|
---|
775 |
|
---|
776 | /**
|
---|
777 | * Copy from a safe (kernel) buffer and to a potentially unsafe kenrel mode
|
---|
778 | * location.
|
---|
779 | *
|
---|
780 | * @retval VINF_SUCCESS on success.
|
---|
781 | * @retval VERR_ACCESS_DENIED on error.
|
---|
782 | * @retval VERR_NOT_SUPPORTED if not (yet) supported.
|
---|
783 | *
|
---|
784 | * @param pvDst The destination address (potentially unsafe).
|
---|
785 | * @param pvSrc The source address (safe).
|
---|
786 | * @param cb The number of bytes to copy.
|
---|
787 | */
|
---|
788 | RTR0DECL(int) RTR0MemKernelCopyTo(void *pvDst, void const *pvSrc, size_t cb);
|
---|
789 |
|
---|
790 | #endif /* IN_RING0 */
|
---|
791 |
|
---|
792 |
|
---|
793 | /** @name Electrical Fence Version of some APIs.
|
---|
794 | * @{
|
---|
795 | */
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Same as RTMemTmpAllocTag() except that it's fenced.
|
---|
799 | *
|
---|
800 | * @returns Pointer to the allocated memory.
|
---|
801 | * @returns NULL on failure.
|
---|
802 | * @param cb Size in bytes of the memory block to allocate.
|
---|
803 | * @param pszTag Allocation tag used for statistics and such.
|
---|
804 | * @param SRC_POS The source position where call is being made from.
|
---|
805 | * Use RT_SRC_POS when possible. Optional.
|
---|
806 | */
|
---|
807 | RTDECL(void *) RTMemEfTmpAlloc(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
808 |
|
---|
809 | /**
|
---|
810 | * Same as RTMemTmpAllocZTag() except that it's fenced.
|
---|
811 | *
|
---|
812 | * @returns Pointer to the allocated memory.
|
---|
813 | * @returns NULL on failure.
|
---|
814 | * @param cb Size in bytes of the memory block to allocate.
|
---|
815 | * @param pszTag Allocation tag used for statistics and such.
|
---|
816 | * @param SRC_POS The source position where call is being made from. Use
|
---|
817 | * RT_SRC_POS when possible. Optional.
|
---|
818 | */
|
---|
819 | RTDECL(void *) RTMemEfTmpAllocZ(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
820 |
|
---|
821 | /**
|
---|
822 | * Same as RTMemTmpFree() except that it's for fenced memory.
|
---|
823 | *
|
---|
824 | * @param pv Pointer to memory block.
|
---|
825 | * @param SRC_POS The source position where call is being made from. Use
|
---|
826 | * RT_SRC_POS when possible. Optional.
|
---|
827 | */
|
---|
828 | RTDECL(void) RTMemEfTmpFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
829 |
|
---|
830 | /**
|
---|
831 | * Same as RTMemAllocTag() except that it's fenced.
|
---|
832 | *
|
---|
833 | * @returns Pointer to the allocated memory. Free with RTMemEfFree().
|
---|
834 | * @returns NULL on failure.
|
---|
835 | * @param cb Size in bytes of the memory block to allocate.
|
---|
836 | * @param pszTag Allocation tag used for statistics and such.
|
---|
837 | * @param SRC_POS The source position where call is being made from. Use
|
---|
838 | * RT_SRC_POS when possible. Optional.
|
---|
839 | */
|
---|
840 | RTDECL(void *) RTMemEfAlloc(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
841 |
|
---|
842 | /**
|
---|
843 | * Same as RTMemAllocZTag() except that it's fenced.
|
---|
844 | *
|
---|
845 | * @returns Pointer to the allocated memory.
|
---|
846 | * @returns NULL on failure.
|
---|
847 | * @param cb Size in bytes of the memory block to allocate.
|
---|
848 | * @param pszTag Allocation tag used for statistics and such.
|
---|
849 | * @param SRC_POS The source position where call is being made from. Use
|
---|
850 | * RT_SRC_POS when possible. Optional.
|
---|
851 | */
|
---|
852 | RTDECL(void *) RTMemEfAllocZ(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
853 |
|
---|
854 | /**
|
---|
855 | * Same as RTMemAllocVarTag() except that it's fenced.
|
---|
856 | *
|
---|
857 | * @returns Pointer to the allocated memory. Free with RTMemEfFree().
|
---|
858 | * @returns NULL on failure.
|
---|
859 | * @param cbUnaligned Size in bytes of the memory block to allocate.
|
---|
860 | * @param pszTag Allocation tag used for statistics and such.
|
---|
861 | * @param SRC_POS The source position where call is being made from. Use
|
---|
862 | * RT_SRC_POS when possible. Optional.
|
---|
863 | */
|
---|
864 | RTDECL(void *) RTMemEfAllocVar(size_t cbUnaligned, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
865 |
|
---|
866 | /**
|
---|
867 | * Same as RTMemAllocZVarTag() except that it's fenced.
|
---|
868 | *
|
---|
869 | * @returns Pointer to the allocated memory.
|
---|
870 | * @returns NULL on failure.
|
---|
871 | * @param cbUnaligned Size in bytes of the memory block to allocate.
|
---|
872 | * @param pszTag Allocation tag used for statistics and such.
|
---|
873 | * @param SRC_POS The source position where call is being made from. Use
|
---|
874 | * RT_SRC_POS when possible. Optional.
|
---|
875 | */
|
---|
876 | RTDECL(void *) RTMemEfAllocZVar(size_t cbUnaligned, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
877 |
|
---|
878 | /**
|
---|
879 | * Same as RTMemReallocTag() except that it's fenced.
|
---|
880 | *
|
---|
881 | * @returns Pointer to the allocated memory.
|
---|
882 | * @returns NULL on failure.
|
---|
883 | * @param pvOld The memory block to reallocate.
|
---|
884 | * @param cbNew The new block size (in bytes).
|
---|
885 | * @param pszTag Allocation tag used for statistics and such.
|
---|
886 | * @param SRC_POS The source position where call is being made from. Use
|
---|
887 | * RT_SRC_POS when possible. Optional.
|
---|
888 | */
|
---|
889 | RTDECL(void *) RTMemEfRealloc(void *pvOld, size_t cbNew, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
890 |
|
---|
891 | /**
|
---|
892 | * Free memory allocated by any of the RTMemEf* allocators.
|
---|
893 | *
|
---|
894 | * @param pv Pointer to memory block.
|
---|
895 | * @param SRC_POS The source position where call is being made from. Use
|
---|
896 | * RT_SRC_POS when possible. Optional.
|
---|
897 | */
|
---|
898 | RTDECL(void) RTMemEfFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
899 |
|
---|
900 | /**
|
---|
901 | * Same as RTMemDupTag() except that it's fenced.
|
---|
902 | *
|
---|
903 | * @returns New heap block with the duplicate data.
|
---|
904 | * @returns NULL if we're out of memory.
|
---|
905 | * @param pvSrc The memory to duplicate.
|
---|
906 | * @param cb The amount of memory to duplicate.
|
---|
907 | * @param pszTag Allocation tag used for statistics and such.
|
---|
908 | * @param SRC_POS The source position where call is being made from. Use
|
---|
909 | * RT_SRC_POS when possible. Optional.
|
---|
910 | */
|
---|
911 | RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
912 |
|
---|
913 | /**
|
---|
914 | * Same as RTMemEfDupExTag except that it's fenced.
|
---|
915 | *
|
---|
916 | * @returns New heap block with the duplicate data.
|
---|
917 | * @returns NULL if we're out of memory.
|
---|
918 | * @param pvSrc The memory to duplicate.
|
---|
919 | * @param cbSrc The amount of memory to duplicate.
|
---|
920 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
921 | * @param pszTag Allocation tag used for statistics and such.
|
---|
922 | * @param SRC_POS The source position where call is being made from. Use
|
---|
923 | * RT_SRC_POS when possible. Optional.
|
---|
924 | */
|
---|
925 | RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_PROTO;
|
---|
926 |
|
---|
927 | /** @def RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF
|
---|
928 | * Define RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF to enable electric fence new and
|
---|
929 | * delete operators for classes which uses the RTMEMEF_NEW_AND_DELETE_OPERATORS
|
---|
930 | * macro.
|
---|
931 | */
|
---|
932 | /** @def RTMEMEF_NEW_AND_DELETE_OPERATORS
|
---|
933 | * Defines the electric fence new and delete operators for a class when
|
---|
934 | * RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF is define.
|
---|
935 | */
|
---|
936 | /** @def RTR0MEMEF_NEW_AND_DELETE_OPERATORS_IOKIT
|
---|
937 | * Defines the electric fence new and delete operators for an IOKit class when
|
---|
938 | * RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF is define.
|
---|
939 | *
|
---|
940 | * This differs from RTMEMEF_NEW_AND_DELETE_OPERATORS in that the memory we
|
---|
941 | * allocate is initialized to zero. It is also assuming we don't have nothrow
|
---|
942 | * variants and exceptions, so fewer variations.
|
---|
943 | */
|
---|
944 | #if defined(RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF) && !defined(RTMEM_NO_WRAP_SOME_NEW_AND_DELETE_TO_EF)
|
---|
945 | # if defined(RT_EXCEPTIONS_ENABLED)
|
---|
946 | # define RTMEMEF_NEW_AND_DELETE_OPERATORS() \
|
---|
947 | void *operator new(size_t cb) RT_THROW(std::bad_alloc) \
|
---|
948 | { \
|
---|
949 | void *pv = RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
|
---|
950 | if (RT_LIKELY(pv)) \
|
---|
951 | return pv; \
|
---|
952 | throw std::bad_alloc(); \
|
---|
953 | } \
|
---|
954 | void *operator new(size_t cb, const std::nothrow_t ¬hrow_constant) RT_NO_THROW_DEF \
|
---|
955 | { \
|
---|
956 | NOREF(nothrow_constant); \
|
---|
957 | return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
|
---|
958 | } \
|
---|
959 | void *operator new(size_t cb, void *pvBuf) RT_NO_THROW_DEF \
|
---|
960 | { \
|
---|
961 | NOREF(cb); \
|
---|
962 | return pvBuf; \
|
---|
963 | } \
|
---|
964 | void *operator new[](size_t cb) RT_THROW(std::bad_alloc) \
|
---|
965 | { \
|
---|
966 | void *pv = RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
|
---|
967 | if (RT_LIKELY(pv)) \
|
---|
968 | return pv; \
|
---|
969 | throw std::bad_alloc(); \
|
---|
970 | } \
|
---|
971 | void *operator new[](size_t cb, const std::nothrow_t ¬hrow_constant) RT_NO_THROW_DEF \
|
---|
972 | { \
|
---|
973 | NOREF(nothrow_constant); \
|
---|
974 | return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
|
---|
975 | } \
|
---|
976 | \
|
---|
977 | void operator delete(void *pv) RT_NO_THROW_DEF \
|
---|
978 | { \
|
---|
979 | RTMemEfFree(pv, RT_SRC_POS); \
|
---|
980 | } \
|
---|
981 | void operator delete(void *pv, const std::nothrow_t ¬hrow_constant) RT_NO_THROW_DEF \
|
---|
982 | { \
|
---|
983 | NOREF(nothrow_constant); \
|
---|
984 | RTMemEfFree(pv, RT_SRC_POS); \
|
---|
985 | } \
|
---|
986 | void operator delete[](void *pv) RT_NO_THROW_DEF \
|
---|
987 | { \
|
---|
988 | RTMemEfFree(pv, RT_SRC_POS); \
|
---|
989 | } \
|
---|
990 | void operator delete[](void *pv, const std::nothrow_t ¬hrow_constant) RT_NO_THROW_DEF \
|
---|
991 | { \
|
---|
992 | NOREF(nothrow_constant); \
|
---|
993 | RTMemEfFree(pv, RT_SRC_POS); \
|
---|
994 | } \
|
---|
995 | \
|
---|
996 | typedef int UsingElectricNewAndDeleteOperators
|
---|
997 | # else
|
---|
998 | # define RTMEMEF_NEW_AND_DELETE_OPERATORS() \
|
---|
999 | void *operator new(size_t cb) \
|
---|
1000 | { \
|
---|
1001 | return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
|
---|
1002 | } \
|
---|
1003 | void *operator new(size_t cb, const std::nothrow_t ¬hrow_constant) \
|
---|
1004 | { \
|
---|
1005 | NOREF(nothrow_constant); \
|
---|
1006 | return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
|
---|
1007 | } \
|
---|
1008 | void *operator new(size_t cb, void *pvBuf) RT_NO_THROW_DEF \
|
---|
1009 | { \
|
---|
1010 | NOREF(cb); \
|
---|
1011 | return pvBuf; \
|
---|
1012 | } \
|
---|
1013 | void *operator new[](size_t cb) \
|
---|
1014 | { \
|
---|
1015 | return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
|
---|
1016 | } \
|
---|
1017 | void *operator new[](size_t cb, const std::nothrow_t ¬hrow_constant) \
|
---|
1018 | { \
|
---|
1019 | NOREF(nothrow_constant); \
|
---|
1020 | return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
|
---|
1021 | } \
|
---|
1022 | \
|
---|
1023 | void operator delete(void *pv) \
|
---|
1024 | { \
|
---|
1025 | RTMemEfFree(pv, RT_SRC_POS); \
|
---|
1026 | } \
|
---|
1027 | void operator delete(void *pv, const std::nothrow_t ¬hrow_constant) \
|
---|
1028 | { \
|
---|
1029 | NOREF(nothrow_constant); \
|
---|
1030 | RTMemEfFree(pv, RT_SRC_POS); \
|
---|
1031 | } \
|
---|
1032 | void operator delete[](void *pv) \
|
---|
1033 | { \
|
---|
1034 | RTMemEfFree(pv, RT_SRC_POS); \
|
---|
1035 | } \
|
---|
1036 | void operator delete[](void *pv, const std::nothrow_t ¬hrow_constant) \
|
---|
1037 | { \
|
---|
1038 | NOREF(nothrow_constant); \
|
---|
1039 | RTMemEfFree(pv, RT_SRC_POS); \
|
---|
1040 | } \
|
---|
1041 | \
|
---|
1042 | typedef int UsingElectricNewAndDeleteOperators
|
---|
1043 | # endif
|
---|
1044 | # define RTR0MEMEF_NEW_AND_DELETE_OPERATORS_IOKIT() \
|
---|
1045 | void *operator new(size_t cb) \
|
---|
1046 | { \
|
---|
1047 | return RTMemEfAllocZ(cb, RTMEM_TAG, RT_SRC_POS); \
|
---|
1048 | } \
|
---|
1049 | void *operator new[](size_t cb) \
|
---|
1050 | { \
|
---|
1051 | return RTMemEfAllocZ(cb, RTMEM_TAG, RT_SRC_POS); \
|
---|
1052 | } \
|
---|
1053 | \
|
---|
1054 | void operator delete(void *pv) \
|
---|
1055 | { \
|
---|
1056 | RTMemEfFree(pv, RT_SRC_POS); \
|
---|
1057 | } \
|
---|
1058 | void operator delete[](void *pv) \
|
---|
1059 | { \
|
---|
1060 | RTMemEfFree(pv, RT_SRC_POS); \
|
---|
1061 | } \
|
---|
1062 | \
|
---|
1063 | typedef int UsingElectricNewAndDeleteOperators
|
---|
1064 | #else
|
---|
1065 | # define RTMEMEF_NEW_AND_DELETE_OPERATORS() \
|
---|
1066 | typedef int UsingDefaultNewAndDeleteOperators
|
---|
1067 | # define RTR0MEMEF_NEW_AND_DELETE_OPERATORS_IOKIT() \
|
---|
1068 | typedef int UsingDefaultNewAndDeleteOperators
|
---|
1069 | #endif
|
---|
1070 | #ifdef DOXYGEN_RUNNING
|
---|
1071 | # define RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF
|
---|
1072 | #endif
|
---|
1073 |
|
---|
1074 | /** @def RTMEM_WRAP_TO_EF_APIS
|
---|
1075 | * Define RTMEM_WRAP_TO_EF_APIS to wrap RTMem APIs to RTMemEf APIs.
|
---|
1076 | */
|
---|
1077 | #if defined(RTMEM_WRAP_TO_EF_APIS) && !defined(RTMEM_NO_WRAP_TO_EF_APIS) \
|
---|
1078 | && ( defined(IN_RING3) || ( defined(IN_RING0) && !defined(IN_RING0_AGNOSTIC) && (defined(RT_OS_DARWIN) || 0) ) )
|
---|
1079 | # define RTMemTmpAllocTag(cb, pszTag) RTMemEfTmpAlloc((cb), (pszTag), RT_SRC_POS)
|
---|
1080 | # define RTMemTmpAllocZTag(cb, pszTag) RTMemEfTmpAllocZ((cb), (pszTag), RT_SRC_POS)
|
---|
1081 | # define RTMemTmpFree(pv) RTMemEfTmpFree((pv), RT_SRC_POS)
|
---|
1082 | # define RTMemAllocTag(cb, pszTag) RTMemEfAlloc((cb), (pszTag), RT_SRC_POS)
|
---|
1083 | # define RTMemAllocZTag(cb, pszTag) RTMemEfAllocZ((cb), (pszTag), RT_SRC_POS)
|
---|
1084 | # define RTMemAllocVarTag(cbUnaligned, pszTag) RTMemEfAllocVar((cbUnaligned), (pszTag), RT_SRC_POS)
|
---|
1085 | # define RTMemAllocZVarTag(cbUnaligned, pszTag) RTMemEfAllocZVar((cbUnaligned), (pszTag), RT_SRC_POS)
|
---|
1086 | # define RTMemReallocTag(pvOld, cbNew, pszTag) RTMemEfRealloc((pvOld), (cbNew), (pszTag), RT_SRC_POS)
|
---|
1087 | # define RTMemFree(pv) RTMemEfFree((pv), RT_SRC_POS)
|
---|
1088 | # define RTMemDupTag(pvSrc, cb, pszTag) RTMemEfDup((pvSrc), (cb), (pszTag), RT_SRC_POS)
|
---|
1089 | # define RTMemDupExTag(pvSrc, cbSrc, cbExtra, pszTag) RTMemEfDupEx((pvSrc), (cbSrc), (cbExtra), (pszTag), RT_SRC_POS)
|
---|
1090 | #endif
|
---|
1091 | #ifdef DOXYGEN_RUNNING
|
---|
1092 | # define RTMEM_WRAP_TO_EF_APIS
|
---|
1093 | #endif
|
---|
1094 |
|
---|
1095 | /**
|
---|
1096 | * Fenced drop-in replacement for RTMemTmpAllocTag.
|
---|
1097 | * @copydoc RTMemTmpAllocTag
|
---|
1098 | */
|
---|
1099 | RTDECL(void *) RTMemEfTmpAllocNP(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Fenced drop-in replacement for RTMemTmpAllocZTag.
|
---|
1103 | * @copydoc RTMemTmpAllocZTag
|
---|
1104 | */
|
---|
1105 | RTDECL(void *) RTMemEfTmpAllocZNP(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
1106 |
|
---|
1107 | /**
|
---|
1108 | * Fenced drop-in replacement for RTMemTmpFreeTag.
|
---|
1109 | * @copydoc RTMemTmpFree
|
---|
1110 | */
|
---|
1111 | RTDECL(void) RTMemEfTmpFreeNP(void *pv) RT_NO_THROW_PROTO;
|
---|
1112 |
|
---|
1113 | /**
|
---|
1114 | * Fenced drop-in replacement for RTMemAllocTag.
|
---|
1115 | * @copydoc RTMemAllocTag
|
---|
1116 | */
|
---|
1117 | RTDECL(void *) RTMemEfAllocNP(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
1118 |
|
---|
1119 | /**
|
---|
1120 | * Fenced drop-in replacement for RTMemAllocZTag.
|
---|
1121 | * @copydoc RTMemAllocZTag
|
---|
1122 | */
|
---|
1123 | RTDECL(void *) RTMemEfAllocZNP(size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
1124 |
|
---|
1125 | /**
|
---|
1126 | * Fenced drop-in replacement for RTMemAllocVarTag
|
---|
1127 | * @copydoc RTMemAllocVarTag
|
---|
1128 | */
|
---|
1129 | RTDECL(void *) RTMemEfAllocVarNP(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
1130 |
|
---|
1131 | /**
|
---|
1132 | * Fenced drop-in replacement for RTMemAllocZVarTag.
|
---|
1133 | * @copydoc RTMemAllocZVarTag
|
---|
1134 | */
|
---|
1135 | RTDECL(void *) RTMemEfAllocZVarNP(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
1136 |
|
---|
1137 | /**
|
---|
1138 | * Fenced drop-in replacement for RTMemReallocTag.
|
---|
1139 | * @copydoc RTMemReallocTag
|
---|
1140 | */
|
---|
1141 | RTDECL(void *) RTMemEfReallocNP(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
1142 |
|
---|
1143 | /**
|
---|
1144 | * Fenced drop-in replacement for RTMemFree.
|
---|
1145 | * @copydoc RTMemFree
|
---|
1146 | */
|
---|
1147 | RTDECL(void) RTMemEfFreeNP(void *pv) RT_NO_THROW_PROTO;
|
---|
1148 |
|
---|
1149 | /**
|
---|
1150 | * Fenced drop-in replacement for RTMemDupExTag.
|
---|
1151 | * @copydoc RTMemDupTag
|
---|
1152 | */
|
---|
1153 | RTDECL(void *) RTMemEfDupNP(const void *pvSrc, size_t cb, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
1154 |
|
---|
1155 | /**
|
---|
1156 | * Fenced drop-in replacement for RTMemDupExTag.
|
---|
1157 | * @copydoc RTMemDupExTag
|
---|
1158 | */
|
---|
1159 | RTDECL(void *) RTMemEfDupExNP(const void *pvSrc, size_t cbSrc, size_t cbExtra, const char *pszTag) RT_NO_THROW_PROTO;
|
---|
1160 |
|
---|
1161 | /** @} */
|
---|
1162 |
|
---|
1163 | RT_C_DECLS_END
|
---|
1164 |
|
---|
1165 | /** @} */
|
---|
1166 |
|
---|
1167 |
|
---|
1168 | #endif /* !IPRT_INCLUDED_mem_h */
|
---|
1169 |
|
---|