VirtualBox

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

Last change on this file since 41116 was 40894, checked in by vboxsync, 13 years ago

IPRT/SUPDrv: Don't create a fixed sized heap if we don't have to, use get_vm_area and friends for each request instead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.1 KB
Line 
1/** @file
2 * IPRT - Memory Management and Manipulation.
3 */
4
5/*
6 * Copyright (C) 2006-2011 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_mem_h
27#define ___iprt_mem_h
28
29
30#include <iprt/cdefs.h>
31#include <iprt/types.h>
32
33
34#ifdef IN_RC
35# error "There are no RTMem APIs available Guest Context!"
36#endif
37
38
39/** @defgroup grp_rt_mem RTMem - Memory Management and Manipulation
40 * @ingroup grp_rt
41 * @{
42 */
43
44RT_C_DECLS_BEGIN
45
46/** @def RTMEM_ALIGNMENT
47 * The alignment of the memory blocks returned by RTMemAlloc(), RTMemAllocZ(),
48 * RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ() for allocations greater
49 * than RTMEM_ALIGNMENT.
50 *
51 * @note This alignment is not forced if the electric fence is active!
52 */
53#if defined(RT_OS_OS2)
54# define RTMEM_ALIGNMENT 4
55#else
56# define RTMEM_ALIGNMENT 8
57#endif
58
59/** @def RTMEM_TAG
60 * The default allocation tag used by the RTMem allocation APIs.
61 *
62 * When not defined before the inclusion of iprt/mem.h or iprt/memobj.h, this
63 * will default to the pointer to the current file name. The memory API will
64 * make of use of this as pointer to a volatile but read-only string.
65 * The alternative tag includes the line number for a more-detailed analysis.
66 */
67#ifndef RTMEM_TAG
68# if 0
69# define RTMEM_TAG (__FILE__ ":" RT_XSTR(__LINE__))
70# else
71# define RTMEM_TAG (__FILE__)
72# endif
73#endif
74
75
76/** @name Allocate temporary memory.
77 * @{ */
78/**
79 * Allocates temporary memory with default tag.
80 *
81 * Temporary memory blocks are used for not too large memory blocks which
82 * are believed not to stick around for too long. Using this API instead
83 * of RTMemAlloc() not only gives the heap manager room for optimization
84 * but makes the code easier to read.
85 *
86 * @returns Pointer to the allocated memory.
87 * @returns NULL on failure, assertion raised in strict builds.
88 * @param cb Size in bytes of the memory block to allocated.
89 */
90#define RTMemTmpAlloc(cb) RTMemTmpAllocTag((cb), RTMEM_TAG)
91
92/**
93 * Allocates temporary memory with custom tag.
94 *
95 * Temporary memory blocks are used for not too large memory blocks which
96 * are believed not to stick around for too long. Using this API instead
97 * of RTMemAlloc() not only gives the heap manager room for optimization
98 * but makes the code easier to read.
99 *
100 * @returns Pointer to the allocated memory.
101 * @returns NULL on failure, assertion raised in strict builds.
102 * @param cb Size in bytes of the memory block to allocated.
103 * @param pszTag Allocation tag used for statistics and such.
104 */
105RTDECL(void *) RTMemTmpAllocTag(size_t cb, const char *pszTag) RT_NO_THROW;
106
107/**
108 * Allocates zero'd temporary memory with default tag.
109 *
110 * Same as RTMemTmpAlloc() but the memory will be zero'd.
111 *
112 * @returns Pointer to the allocated memory.
113 * @returns NULL on failure, assertion raised in strict builds.
114 * @param cb Size in bytes of the memory block to allocated.
115 */
116#define RTMemTmpAllocZ(cb) RTMemTmpAllocZTag((cb), RTMEM_TAG)
117
118/**
119 * Allocates zero'd temporary memory with custom tag.
120 *
121 * Same as RTMemTmpAlloc() but the memory will be zero'd.
122 *
123 * @returns Pointer to the allocated memory.
124 * @returns NULL on failure, assertion raised in strict builds.
125 * @param cb Size in bytes of the memory block to allocated.
126 * @param pszTag Allocation tag used for statistics and such.
127 */
128RTDECL(void *) RTMemTmpAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW;
129
130/**
131 * Free temporary memory.
132 *
133 * @param pv Pointer to memory block.
134 */
135RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW;
136
137/** @} */
138
139
140/**
141 * Allocates memory with default tag.
142 *
143 * @returns Pointer to the allocated memory.
144 * @returns NULL on failure, assertion raised in strict builds.
145 * @param cb Size in bytes of the memory block to allocated.
146 * @param pszTag Allocation tag used for statistics and such.
147 */
148#define RTMemAlloc(cb) RTMemAllocTag((cb), RTMEM_TAG)
149
150/**
151 * Allocates memory with custom tag.
152 *
153 * @returns Pointer to the allocated memory.
154 * @returns NULL on failure, assertion raised in strict builds.
155 * @param cb Size in bytes of the memory block to allocated.
156 * @param pszTag Allocation tag used for statistics and such.
157 */
158RTDECL(void *) RTMemAllocTag(size_t cb, const char *pszTag) RT_NO_THROW;
159
160/**
161 * Allocates zero'd memory with default tag.
162 *
163 * Instead of memset(pv, 0, sizeof()) use this when you want zero'd
164 * memory. This keeps the code smaller and the heap can skip the memset
165 * in about 0.42% of calls :-).
166 *
167 * @returns Pointer to the allocated memory.
168 * @returns NULL on failure.
169 * @param cb Size in bytes of the memory block to allocated.
170 */
171#define RTMemAllocZ(cb) RTMemAllocZTag((cb), RTMEM_TAG)
172
173/**
174 * Allocates zero'd memory with custom tag.
175 *
176 * Instead of memset(pv, 0, sizeof()) use this when you want zero'd
177 * memory. This keeps the code smaller and the heap can skip the memset
178 * in about 0.42% of calls :-).
179 *
180 * @returns Pointer to the allocated memory.
181 * @returns NULL on failure.
182 * @param cb Size in bytes of the memory block to allocated.
183 * @param pszTag Allocation tag used for statistics and such.
184 */
185RTDECL(void *) RTMemAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW;
186
187/**
188 * Wrapper around RTMemAlloc for automatically aligning variable sized
189 * allocations so that the various electric fence heaps works correctly.
190 *
191 * @returns See RTMemAlloc.
192 * @param cbUnaligned The unaligned size.
193 */
194#define RTMemAllocVar(cbUnaligned) RTMemAllocVarTag((cbUnaligned), RTMEM_TAG)
195
196/**
197 * Wrapper around RTMemAllocTag for automatically aligning variable sized
198 * allocations so that the various electric fence heaps works correctly.
199 *
200 * @returns See RTMemAlloc.
201 * @param cbUnaligned The unaligned size.
202 * @param pszTag Allocation tag used for statistics and such.
203 */
204RTDECL(void *) RTMemAllocVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW;
205
206/**
207 * Wrapper around RTMemAllocZ for automatically aligning variable sized
208 * allocations so that the various electric fence heaps works correctly.
209 *
210 * @returns See RTMemAllocZ.
211 * @param cbUnaligned The unaligned size.
212 */
213#define RTMemAllocZVar(cbUnaligned) RTMemAllocZVarTag((cbUnaligned), RTMEM_TAG)
214
215/**
216 * Wrapper around RTMemAllocZTag for automatically aligning variable sized
217 * allocations so that the various electric fence heaps works correctly.
218 *
219 * @returns See RTMemAllocZ.
220 * @param cbUnaligned The unaligned size.
221 * @param pszTag Allocation tag used for statistics and such.
222 */
223RTDECL(void *) RTMemAllocZVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW;
224
225/**
226 * Duplicates a chunk of memory into a new heap block (default tag).
227 *
228 * @returns New heap block with the duplicate data.
229 * @returns NULL if we're out of memory.
230 * @param pvSrc The memory to duplicate.
231 * @param cb The amount of memory to duplicate.
232 */
233#define RTMemDup(pvSrc, cb) RTMemDupTag((pvSrc), (cb), RTMEM_TAG)
234
235/**
236 * Duplicates a chunk of memory into a new heap block (custom tag).
237 *
238 * @returns New heap block with the duplicate data.
239 * @returns NULL if we're out of memory.
240 * @param pvSrc The memory to duplicate.
241 * @param cb The amount of memory to duplicate.
242 * @param pszTag Allocation tag used for statistics and such.
243 */
244RTDECL(void *) RTMemDupTag(const void *pvSrc, size_t cb, const char *pszTag) RT_NO_THROW;
245
246/**
247 * Duplicates a chunk of memory into a new heap block with some additional
248 * zeroed memory (default tag).
249 *
250 * @returns New heap block with the duplicate data.
251 * @returns NULL if we're out of memory.
252 * @param pvSrc The memory to duplicate.
253 * @param cbSrc The amount of memory to duplicate.
254 * @param cbExtra The amount of extra memory to allocate and zero.
255 */
256#define RTMemDupEx(pvSrc, cbSrc, cbExtra) RTMemDupExTag((pvSrc), (cbSrc), (cbExtra), RTMEM_TAG)
257
258/**
259 * Duplicates a chunk of memory into a new heap block with some additional
260 * zeroed memory (default tag).
261 *
262 * @returns New heap block with the duplicate data.
263 * @returns NULL if we're out of memory.
264 * @param pvSrc The memory to duplicate.
265 * @param cbSrc The amount of memory to duplicate.
266 * @param cbExtra The amount of extra memory to allocate and zero.
267 * @param pszTag Allocation tag used for statistics and such.
268 */
269RTDECL(void *) RTMemDupExTag(const void *pvSrc, size_t cbSrc, size_t cbExtra, const char *pszTag) RT_NO_THROW;
270
271/**
272 * Reallocates memory with default tag.
273 *
274 * @returns Pointer to the allocated memory.
275 * @returns NULL on failure.
276 * @param pvOld The memory block to reallocate.
277 * @param cbNew The new block size (in bytes).
278 */
279#define RTMemRealloc(pvOld, cbNew) RTMemReallocTag((pvOld), (cbNew), RTMEM_TAG)
280
281/**
282 * Reallocates memory with custom tag.
283 *
284 * @returns Pointer to the allocated memory.
285 * @returns NULL on failure.
286 * @param pvOld The memory block to reallocate.
287 * @param cbNew The new block size (in bytes).
288 * @param pszTag Allocation tag used for statistics and such.
289 */
290RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW;
291
292/**
293 * Frees memory.
294 *
295 * @param pv Pointer to memory block.
296 */
297RTDECL(void) RTMemFree(void *pv) RT_NO_THROW;
298
299
300
301/** @def RTR0MemAllocEx and RTR0MemAllocExTag flags.
302 * @{ */
303/** The returned memory should be zeroed. */
304#define RTMEMALLOCEX_FLAGS_ZEROED RT_BIT(0)
305/** It must be load code into the returned memory block and execute it. */
306#define RTMEMALLOCEX_FLAGS_EXEC RT_BIT(1)
307/** Allocation from any context.
308 * Will return VERR_NOT_SUPPORTED if not supported. */
309#define RTMEMALLOCEX_FLAGS_ANY_CTX_ALLOC RT_BIT(2)
310/** Allocate the memory such that it can be freed from any context.
311 * Will return VERR_NOT_SUPPORTED if not supported. */
312#define RTMEMALLOCEX_FLAGS_ANY_CTX_FREE RT_BIT(3)
313/** Allocate and free from any context.
314 * Will return VERR_NOT_SUPPORTED if not supported. */
315#define RTMEMALLOCEX_FLAGS_ANY_CTX (RTMEMALLOCEX_FLAGS_ANY_CTX_ALLOC | RTMEMALLOCEX_FLAGS_ANY_CTX_FREE)
316/** Mask of valid flags. */
317#define RTMEMALLOCEX_FLAGS_VALID_MASK UINT32_C(0x0000000f)
318/** @} */
319
320/**
321 * Extended heap allocation API, default tag.
322 *
323 * @returns IPRT status code.
324 * @retval VERR_NO_MEMORY if we're out of memory.
325 * @retval VERR_NO_EXEC_MEMORY if we're out of executable memory.
326 * @retval VERR_NOT_SUPPORTED if any of the specified flags are unsupported.
327 *
328 * @param cb The amount of memory to allocate.
329 * @param cbAlignment The alignment requirements. Use 0 to indicate
330 * default alignment.
331 * @param fFlags A combination of the RTMEMALLOCEX_FLAGS_XXX
332 * defines.
333 * @param ppv Where to return the memory.
334 */
335#define RTMemAllocEx(cb, cbAlignment, fFlags, ppv) RTMemAllocExTag((cb), (cbAlignment), (fFlags), RTMEM_TAG, (ppv))
336
337/**
338 * Extended heap allocation API, custom tag.
339 *
340 * @returns IPRT status code.
341 * @retval VERR_NO_MEMORY if we're out of memory.
342 * @retval VERR_NO_EXEC_MEMORY if we're out of executable memory.
343 * @retval VERR_NOT_SUPPORTED if any of the specified flags are unsupported.
344 *
345 * @param cb The amount of memory to allocate.
346 * @param cbAlignment The alignment requirements. Use 0 to indicate
347 * default alignment.
348 * @param fFlags A combination of the RTMEMALLOCEX_FLAGS_XXX
349 * defines.
350 * @param pszTag The tag.
351 * @param ppv Where to return the memory.
352 */
353RTDECL(int) RTMemAllocExTag(size_t cb, size_t cbAlignment, uint32_t fFlags, const char *pszTag, void **ppv) RT_NO_THROW;
354
355/**
356 * For freeing memory allocated by RTMemAllocEx or RTMemAllocExTag.
357 *
358 * @param pv What to free, NULL is fine.
359 * @param cb The amount of allocated memory.
360 */
361RTDECL(void) RTMemFreeEx(void *pv, size_t cb) RT_NO_THROW;
362
363
364
365/**
366 * Allocates memory which may contain code (default tag).
367 *
368 * @returns Pointer to the allocated memory.
369 * @returns NULL on failure.
370 * @param cb Size in bytes of the memory block to allocate.
371 */
372#define RTMemExecAlloc(cb) RTMemExecAllocTag((cb), RTMEM_TAG)
373
374/**
375 * Allocates memory which may contain code (custom tag).
376 *
377 * @returns Pointer to the allocated memory.
378 * @returns NULL on failure.
379 * @param cb Size in bytes of the memory block to allocate.
380 * @param pszTag Allocation tag used for statistics and such.
381 */
382RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW;
383
384/**
385 * Free executable/read/write memory allocated by RTMemExecAlloc().
386 *
387 * @param pv Pointer to memory block.
388 * @param cb The allocation size.
389 */
390RTDECL(void) RTMemExecFree(void *pv, size_t cb) RT_NO_THROW;
391
392#if defined(IN_RING0) && defined(RT_ARCH_AMD64) && defined(RT_OS_LINUX)
393/**
394 * Donate read+write+execute memory to the exec heap.
395 *
396 * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
397 * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
398 * allocated memory in the module if it wishes for GCC generated code to work.
399 * GCC can only generate modules that work in the address range ~2GB to ~0
400 * currently.
401 *
402 * The API only accept one single donation.
403 *
404 * @returns IPRT status code.
405 * @param pvMemory Pointer to the memory block.
406 * @param cb The size of the memory block.
407 */
408RTR0DECL(int) RTR0MemExecDonate(void *pvMemory, size_t cb) RT_NO_THROW;
409#endif /* R0+AMD64+LINUX */
410
411/**
412 * Allocate page aligned memory with default tag.
413 *
414 * @returns Pointer to the allocated memory.
415 * @returns NULL if we're out of memory.
416 * @param cb Size of the memory block. Will be rounded up to page size.
417 */
418#define RTMemPageAlloc(cb) RTMemPageAllocTag((cb), RTMEM_TAG)
419
420/**
421 * Allocate page aligned memory with custom tag.
422 *
423 * @returns Pointer to the allocated memory.
424 * @returns NULL if we're out of memory.
425 * @param cb Size of the memory block. Will be rounded up to page size.
426 * @param pszTag Allocation tag used for statistics and such.
427 */
428RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW;
429
430/**
431 * Allocate zero'd page aligned memory with default tag.
432 *
433 * @returns Pointer to the allocated memory.
434 * @returns NULL if we're out of memory.
435 * @param cb Size of the memory block. Will be rounded up to page size.
436 */
437#define RTMemPageAllocZ(cb) RTMemPageAllocZTag((cb), RTMEM_TAG)
438
439/**
440 * Allocate zero'd page aligned memory with custom tag.
441 *
442 * @returns Pointer to the allocated memory.
443 * @returns NULL if we're out of memory.
444 * @param cb Size of the memory block. Will be rounded up to page size.
445 * @param pszTag Allocation tag used for statistics and such.
446 */
447RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW;
448
449/**
450 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
451 *
452 * @param pv Pointer to the block as it was returned by the allocation function.
453 * NULL will be ignored.
454 * @param cb The allocation size. Will be rounded up to page size.
455 * Ignored if @a pv is NULL.
456 */
457RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW;
458
459/** Page level protection flags for RTMemProtect().
460 * @{
461 */
462/** No access at all. */
463#define RTMEM_PROT_NONE 0
464/** Read access. */
465#define RTMEM_PROT_READ 1
466/** Write access. */
467#define RTMEM_PROT_WRITE 2
468/** Execute access. */
469#define RTMEM_PROT_EXEC 4
470/** @} */
471
472/**
473 * Change the page level protection of a memory region.
474 *
475 * @returns iprt status code.
476 * @param pv Start of the region. Will be rounded down to nearest page boundary.
477 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
478 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
479 */
480RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW;
481
482/**
483 * Goes thru some pains to make sure the specified memory block is thoroughly
484 * scrambled.
485 *
486 * @param pv The start of the memory block.
487 * @param cb The size of the memory block.
488 * @param cMinPasses The minimum number of passes to make.
489 */
490RTDECL(void) RTMemWipeThoroughly(void *pv, size_t cb, size_t cMinPasses) RT_NO_THROW;
491
492#ifdef IN_RING0
493
494/**
495 * Allocates physical contiguous memory (below 4GB).
496 * The allocation is page aligned and the content is undefined.
497 *
498 * @returns Pointer to the memory block. This is page aligned.
499 * @param pPhys Where to store the physical address.
500 * @param cb The allocation size in bytes. This is always
501 * rounded up to PAGE_SIZE.
502 */
503RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb) RT_NO_THROW;
504
505/**
506 * Frees memory allocated ysing RTMemContAlloc().
507 *
508 * @param pv Pointer to return from RTMemContAlloc().
509 * @param cb The cb parameter passed to RTMemContAlloc().
510 */
511RTR0DECL(void) RTMemContFree(void *pv, size_t cb) RT_NO_THROW;
512
513/**
514 * Copy memory from an user mode buffer into a kernel buffer.
515 *
516 * @retval VINF_SUCCESS on success.
517 * @retval VERR_ACCESS_DENIED on error.
518 *
519 * @param pvDst The kernel mode destination address.
520 * @param R3PtrSrc The user mode source address.
521 * @param cb The number of bytes to copy.
522 */
523RTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst, RTR3PTR R3PtrSrc, size_t cb);
524
525/**
526 * Copy memory from a kernel buffer into a user mode one.
527 *
528 * @retval VINF_SUCCESS on success.
529 * @retval VERR_ACCESS_DENIED on error.
530 *
531 * @param R3PtrDst The user mode destination address.
532 * @param pvSrc The kernel mode source address.
533 * @param cb The number of bytes to copy.
534 */
535RTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb);
536
537/**
538 * Tests if the specified address is in the user addressable range.
539 *
540 * This function does not check whether the memory at that address is accessible
541 * or anything of that sort, only if the address it self is in the user mode
542 * range.
543 *
544 * @returns true if it's in the user addressable range. false if not.
545 * @param R3Ptr The user mode pointer to test.
546 *
547 * @remarks Some systems may have overlapping kernel and user address ranges.
548 * One prominent example of this is the x86 version of Mac OS X. Use
549 * RTR0MemAreKrnlAndUsrDifferent() to check.
550 */
551RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr);
552
553/**
554 * Tests if the specified address is in the kernel mode range.
555 *
556 * This function does not check whether the memory at that address is accessible
557 * or anything of that sort, only if the address it self is in the kernel mode
558 * range.
559 *
560 * @returns true if it's in the kernel range. false if not.
561 * @param pv The alleged kernel mode pointer.
562 *
563 * @remarks Some systems may have overlapping kernel and user address ranges.
564 * One prominent example of this is the x86 version of Mac OS X. Use
565 * RTR0MemAreKrnlAndUsrDifferent() to check.
566 */
567RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv);
568
569/**
570 * Are user mode and kernel mode address ranges distinctly different.
571 *
572 * This determines whether RTR0MemKernelIsValidAddr and RTR0MemUserIsValidAddr
573 * can be used for deciding whether some arbitrary address is a user mode or a
574 * kernel mode one.
575 *
576 * @returns true if they are, false if not.
577 */
578RTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void);
579
580#endif /* IN_RING0 */
581
582
583/** @name Electrical Fence Version of some APIs.
584 * @{
585 */
586
587/**
588 * Same as RTMemTmpAllocTag() except that it's fenced.
589 *
590 * @returns Pointer to the allocated memory.
591 * @returns NULL on failure.
592 * @param cb Size in bytes of the memory block to allocate.
593 * @param pszTag Allocation tag used for statistics and such.
594 */
595RTDECL(void *) RTMemEfTmpAlloc(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
596
597/**
598 * Same as RTMemTmpAllocZTag() except that it's fenced.
599 *
600 * @returns Pointer to the allocated memory.
601 * @returns NULL on failure.
602 * @param cb Size in bytes of the memory block to allocate.
603 * @param pszTag Allocation tag used for statistics and such.
604 */
605RTDECL(void *) RTMemEfTmpAllocZ(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
606
607/**
608 * Same as RTMemTmpFree() except that it's for fenced memory.
609 *
610 * @param pv Pointer to memory block.
611 */
612RTDECL(void) RTMemEfTmpFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW;
613
614/**
615 * Same as RTMemAllocTag() except that it's fenced.
616 *
617 * @returns Pointer to the allocated memory. Free with RTMemEfFree().
618 * @returns NULL on failure.
619 * @param cb Size in bytes of the memory block to allocate.
620 * @param pszTag Allocation tag used for statistics and such.
621 */
622RTDECL(void *) RTMemEfAlloc(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
623
624/**
625 * Same as RTMemAllocZTag() except that it's fenced.
626 *
627 * @returns Pointer to the allocated memory.
628 * @returns NULL on failure.
629 * @param cb Size in bytes of the memory block to allocate.
630 * @param pszTag Allocation tag used for statistics and such.
631 */
632RTDECL(void *) RTMemEfAllocZ(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
633
634/**
635 * Same as RTMemAllocVarTag() except that it's fenced.
636 *
637 * @returns Pointer to the allocated memory. Free with RTMemEfFree().
638 * @returns NULL on failure.
639 * @param cbUnaligned Size in bytes of the memory block to allocate.
640 * @param pszTag Allocation tag used for statistics and such.
641 */
642RTDECL(void *) RTMemEfAllocVar(size_t cbUnaligned, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
643
644/**
645 * Same as RTMemAllocZVarTag() except that it's fenced.
646 *
647 * @returns Pointer to the allocated memory.
648 * @returns NULL on failure.
649 * @param cbUnaligned Size in bytes of the memory block to allocate.
650 * @param pszTag Allocation tag used for statistics and such.
651 */
652RTDECL(void *) RTMemEfAllocZVar(size_t cbUnaligned, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
653
654/**
655 * Same as RTMemReallocTag() except that it's fenced.
656 *
657 * @returns Pointer to the allocated memory.
658 * @returns NULL on failure.
659 * @param pvOld The memory block to reallocate.
660 * @param cbNew The new block size (in bytes).
661 * @param pszTag Allocation tag used for statistics and such.
662 */
663RTDECL(void *) RTMemEfRealloc(void *pvOld, size_t cbNew, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
664
665/**
666 * Free memory allocated by any of the RTMemEf* allocators.
667 *
668 * @param pv Pointer to memory block.
669 */
670RTDECL(void) RTMemEfFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW;
671
672/**
673 * Same as RTMemDupTag() except that it's fenced.
674 *
675 * @returns New heap block with the duplicate data.
676 * @returns NULL if we're out of memory.
677 * @param pvSrc The memory to duplicate.
678 * @param cb The amount of memory to duplicate.
679 * @param pszTag Allocation tag used for statistics and such.
680 */
681RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
682
683/**
684 * Same as RTMemEfDupExTag except that it's fenced.
685 *
686 * @returns New heap block with the duplicate data.
687 * @returns NULL if we're out of memory.
688 * @param pvSrc The memory to duplicate.
689 * @param cbSrc The amount of memory to duplicate.
690 * @param cbExtra The amount of extra memory to allocate and zero.
691 * @param pszTag Allocation tag used for statistics and such.
692 */
693RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
694
695/** @def RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF
696 * Define RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF to enable electric fence new and
697 * delete operators for classes which uses the RTMEMEF_NEW_AND_DELETE_OPERATORS
698 * macro.
699 */
700/** @def RTMEMEF_NEW_AND_DELETE_OPERATORS
701 * Defines the electric fence new and delete operators for a class when
702 * RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF is define.
703 */
704#if defined(RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF) && !defined(RTMEM_NO_WRAP_SOME_NEW_AND_DELETE_TO_EF)
705# if defined(RT_EXCEPTIONS_ENABLED)
706# define RTMEMEF_NEW_AND_DELETE_OPERATORS() \
707 void *operator new(size_t cb) RT_THROW(std::bad_alloc) \
708 { \
709 void *pv = RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
710 if (RT_UNLIKELY(!pv)) \
711 throw std::bad_alloc(); \
712 return pv; \
713 } \
714 void *operator new(size_t cb, const std::nothrow_t &nothrow_constant) RT_NO_THROW \
715 { \
716 NOREF(nothrow_constant); \
717 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
718 } \
719 void *operator new[](size_t cb) RT_THROW(std::bad_alloc) \
720 { \
721 void *pv = RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
722 if (RT_UNLIKELY(!pv)) \
723 throw std::bad_alloc(); \
724 return pv; \
725 } \
726 void *operator new[](size_t cb, const std::nothrow_t &nothrow_constant) RT_NO_THROW \
727 { \
728 NOREF(nothrow_constant); \
729 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
730 } \
731 \
732 void operator delete(void *pv) RT_NO_THROW \
733 { \
734 RTMemEfFree(pv, RT_SRC_POS); \
735 } \
736 void operator delete(void *pv, const std::nothrow_t &nothrow_constant) RT_NO_THROW \
737 { \
738 NOREF(nothrow_constant); \
739 RTMemEfFree(pv, RT_SRC_POS); \
740 } \
741 void operator delete[](void *pv) RT_NO_THROW \
742 { \
743 RTMemEfFree(pv, RT_SRC_POS); \
744 } \
745 void operator delete[](void *pv, const std::nothrow_t &nothrow_constant) RT_NO_THROW \
746 { \
747 NOREF(nothrow_constant); \
748 RTMemEfFree(pv, RT_SRC_POS); \
749 } \
750 \
751 typedef int UsingElectricNewAndDeleteOperators
752# else
753# define RTMEMEF_NEW_AND_DELETE_OPERATORS() \
754 void *operator new(size_t cb) \
755 { \
756 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
757 } \
758 void *operator new(size_t cb, const std::nothrow_t &nothrow_constant) \
759 { \
760 NOREF(nothrow_constant); \
761 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
762 } \
763 void *operator new[](size_t cb) \
764 { \
765 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
766 } \
767 void *operator new[](size_t cb, const std::nothrow_t &nothrow_constant) \
768 { \
769 NOREF(nothrow_constant); \
770 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
771 } \
772 \
773 void operator delete(void *pv) \
774 { \
775 RTMemEfFree(pv, RT_SRC_POS); \
776 } \
777 void operator delete(void *pv, const std::nothrow_t &nothrow_constant) \
778 { \
779 NOREF(nothrow_constant); \
780 RTMemEfFree(pv, RT_SRC_POS); \
781 } \
782 void operator delete[](void *pv) \
783 { \
784 RTMemEfFree(pv, RT_SRC_POS); \
785 } \
786 void operator delete[](void *pv, const std::nothrow_t &nothrow_constant) \
787 { \
788 NOREF(nothrow_constant); \
789 RTMemEfFree(pv, RT_SRC_POS); \
790 } \
791 \
792 typedef int UsingElectricNewAndDeleteOperators
793# endif
794#else
795# define RTMEMEF_NEW_AND_DELETE_OPERATORS() \
796 typedef int UsingDefaultNewAndDeleteOperators
797#endif
798#ifdef DOXYGEN_RUNNING
799# define RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF
800#endif
801
802/** @def RTMEM_WRAP_TO_EF_APIS
803 * Define RTMEM_WRAP_TO_EF_APIS to wrap RTMem APIs to RTMemEf APIs.
804 */
805#if defined(RTMEM_WRAP_TO_EF_APIS) && defined(IN_RING3) && !defined(RTMEM_NO_WRAP_TO_EF_APIS)
806# define RTMemTmpAllocTag(cb, pszTag) RTMemEfTmpAlloc((cb), (pszTag), RT_SRC_POS)
807# define RTMemTmpAllocZTag(cb, pszTag) RTMemEfTmpAllocZ((cb), (pszTag), RT_SRC_POS)
808# define RTMemTmpFree(pv) RTMemEfTmpFree((pv), RT_SRC_POS)
809# define RTMemAllocTag(cb, pszTag) RTMemEfAlloc((cb), (pszTag), RT_SRC_POS)
810# define RTMemAllocZTag(cb, pszTag) RTMemEfAllocZ((cb), (pszTag), RT_SRC_POS)
811# define RTMemAllocVarTag(cbUnaligned, pszTag) RTMemEfAllocVar((cbUnaligned), (pszTag), RT_SRC_POS)
812# define RTMemAllocZVarTag(cbUnaligned, pszTag) RTMemEfAllocZVar((cbUnaligned), (pszTag), RT_SRC_POS)
813# define RTMemReallocTag(pvOld, cbNew, pszTag) RTMemEfRealloc((pvOld), (cbNew), (pszTag), RT_SRC_POS)
814# define RTMemFree(pv) RTMemEfFree((pv), RT_SRC_POS)
815# define RTMemDupTag(pvSrc, cb, pszTag) RTMemEfDup((pvSrc), (cb), (pszTag), RT_SRC_POS)
816# define RTMemDupExTag(pvSrc, cbSrc, cbExtra, pszTag) RTMemEfDupEx((pvSrc), (cbSrc), (cbExtra), (pszTag), RT_SRC_POS)
817#endif
818#ifdef DOXYGEN_RUNNING
819# define RTMEM_WRAP_TO_EF_APIS
820#endif
821
822/**
823 * Fenced drop-in replacement for RTMemTmpAllocTag.
824 * @copydoc RTMemTmpAllocTag
825 */
826RTDECL(void *) RTMemEfTmpAllocNP(size_t cb, const char *pszTag) RT_NO_THROW;
827
828/**
829 * Fenced drop-in replacement for RTMemTmpAllocZTag.
830 * @copydoc RTMemTmpAllocZTag
831 */
832RTDECL(void *) RTMemEfTmpAllocZNP(size_t cb, const char *pszTag) RT_NO_THROW;
833
834/**
835 * Fenced drop-in replacement for RTMemTmpFreeTag.
836 * @copydoc RTMemTmpFreeTag
837 */
838RTDECL(void) RTMemEfTmpFreeNP(void *pv) RT_NO_THROW;
839
840/**
841 * Fenced drop-in replacement for RTMemAllocTag.
842 * @copydoc RTMemAllocTag
843 */
844RTDECL(void *) RTMemEfAllocNP(size_t cb, const char *pszTag) RT_NO_THROW;
845
846/**
847 * Fenced drop-in replacement for RTMemAllocZTag.
848 * @copydoc RTMemAllocZTag
849 */
850RTDECL(void *) RTMemEfAllocZNP(size_t cb, const char *pszTag) RT_NO_THROW;
851
852/**
853 * Fenced drop-in replacement for RTMemAllocVarTag
854 * @copydoc RTMemAllocVarTag
855 */
856RTDECL(void *) RTMemEfAllocVarNP(size_t cbUnaligned, const char *pszTag) RT_NO_THROW;
857
858/**
859 * Fenced drop-in replacement for RTMemAllocZVarTag.
860 * @copydoc RTMemAllocZVarTag
861 */
862RTDECL(void *) RTMemEfAllocZVarNP(size_t cbUnaligned, const char *pszTag) RT_NO_THROW;
863
864/**
865 * Fenced drop-in replacement for RTMemReallocTag.
866 * @copydoc RTMemReallocTag
867 */
868RTDECL(void *) RTMemEfReallocNP(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW;
869
870/**
871 * Fenced drop-in replacement for RTMemFree.
872 * @copydoc RTMemFree
873 */
874RTDECL(void) RTMemEfFreeNP(void *pv) RT_NO_THROW;
875
876/**
877 * Fenced drop-in replacement for RTMemDupExTag.
878 * @copydoc RTMemDupExTag
879 */
880RTDECL(void *) RTMemEfDupNP(const void *pvSrc, size_t cb, const char *pszTag) RT_NO_THROW;
881
882/**
883 * Fenced drop-in replacement for RTMemDupExTag.
884 * @copydoc RTMemDupExTag
885 */
886RTDECL(void *) RTMemEfDupExNP(const void *pvSrc, size_t cbSrc, size_t cbExtra, const char *pszTag) RT_NO_THROW;
887
888/** @} */
889
890RT_C_DECLS_END
891
892/** @} */
893
894
895#endif
896
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