VirtualBox

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

Last change on this file since 96574 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette