VirtualBox

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

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

MSC v7.1 build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.8 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
410/**
411 * Allocate read+write+execute memory to the exec heap.
412 *
413 * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
414 * use RTMemExecAlloc on AMD64 Linux/GNU will have to initialize some allocated
415 * memory in the module range if it wishes for GCC generated code to work. GCC
416 * can only generate modules that work in the address range ~2GB to ~0 currently.
417 * As RTR0MemExecDonate() does not work if CONFIG_DEBUG_SET_MODULE_RONX is
418 * enabled, use a different approach (only very recent Linux kernels).
419 *
420 * The API only accept one single initialization.
421 *
422 * @returns IPRT status code.
423 * @param cb The size of the memory block.
424 */
425RTR0DECL(int) RTR0MemExecInit(size_t cb) RT_NO_THROW;
426#endif /* R0+AMD64+LINUX */
427
428/**
429 * Allocate page aligned memory with default tag.
430 *
431 * @returns Pointer to the allocated memory.
432 * @returns NULL if we're out of memory.
433 * @param cb Size of the memory block. Will be rounded up to page size.
434 */
435#define RTMemPageAlloc(cb) RTMemPageAllocTag((cb), RTMEM_TAG)
436
437/**
438 * Allocate page aligned memory with custom tag.
439 *
440 * @returns Pointer to the allocated memory.
441 * @returns NULL if we're out of memory.
442 * @param cb Size of the memory block. Will be rounded up to page size.
443 * @param pszTag Allocation tag used for statistics and such.
444 */
445RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW;
446
447/**
448 * Allocate zero'd page aligned memory with default tag.
449 *
450 * @returns Pointer to the allocated memory.
451 * @returns NULL if we're out of memory.
452 * @param cb Size of the memory block. Will be rounded up to page size.
453 */
454#define RTMemPageAllocZ(cb) RTMemPageAllocZTag((cb), RTMEM_TAG)
455
456/**
457 * Allocate zero'd page aligned memory with custom tag.
458 *
459 * @returns Pointer to the allocated memory.
460 * @returns NULL if we're out of memory.
461 * @param cb Size of the memory block. Will be rounded up to page size.
462 * @param pszTag Allocation tag used for statistics and such.
463 */
464RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW;
465
466/**
467 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
468 *
469 * @param pv Pointer to the block as it was returned by the allocation function.
470 * NULL will be ignored.
471 * @param cb The allocation size. Will be rounded up to page size.
472 * Ignored if @a pv is NULL.
473 */
474RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW;
475
476/** Page level protection flags for RTMemProtect().
477 * @{
478 */
479/** No access at all. */
480#define RTMEM_PROT_NONE 0
481/** Read access. */
482#define RTMEM_PROT_READ 1
483/** Write access. */
484#define RTMEM_PROT_WRITE 2
485/** Execute access. */
486#define RTMEM_PROT_EXEC 4
487/** @} */
488
489/**
490 * Change the page level protection of a memory region.
491 *
492 * @returns iprt status code.
493 * @param pv Start of the region. Will be rounded down to nearest page boundary.
494 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
495 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
496 */
497RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW;
498
499/**
500 * Goes thru some pains to make sure the specified memory block is thoroughly
501 * scrambled.
502 *
503 * @param pv The start of the memory block.
504 * @param cb The size of the memory block.
505 * @param cMinPasses The minimum number of passes to make.
506 */
507RTDECL(void) RTMemWipeThoroughly(void *pv, size_t cb, size_t cMinPasses) RT_NO_THROW;
508
509#ifdef IN_RING0
510
511/**
512 * Allocates physical contiguous memory (below 4GB).
513 * The allocation is page aligned and the content is undefined.
514 *
515 * @returns Pointer to the memory block. This is page aligned.
516 * @param pPhys Where to store the physical address.
517 * @param cb The allocation size in bytes. This is always
518 * rounded up to PAGE_SIZE.
519 */
520RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb) RT_NO_THROW;
521
522/**
523 * Frees memory allocated ysing RTMemContAlloc().
524 *
525 * @param pv Pointer to return from RTMemContAlloc().
526 * @param cb The cb parameter passed to RTMemContAlloc().
527 */
528RTR0DECL(void) RTMemContFree(void *pv, size_t cb) RT_NO_THROW;
529
530/**
531 * Copy memory from an user mode buffer into a kernel buffer.
532 *
533 * @retval VINF_SUCCESS on success.
534 * @retval VERR_ACCESS_DENIED on error.
535 *
536 * @param pvDst The kernel mode destination address.
537 * @param R3PtrSrc The user mode source address.
538 * @param cb The number of bytes to copy.
539 */
540RTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst, RTR3PTR R3PtrSrc, size_t cb);
541
542/**
543 * Copy memory from a kernel buffer into a user mode one.
544 *
545 * @retval VINF_SUCCESS on success.
546 * @retval VERR_ACCESS_DENIED on error.
547 *
548 * @param R3PtrDst The user mode destination address.
549 * @param pvSrc The kernel mode source address.
550 * @param cb The number of bytes to copy.
551 */
552RTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb);
553
554/**
555 * Tests if the specified address is in the user addressable range.
556 *
557 * This function does not check whether the memory at that address is accessible
558 * or anything of that sort, only if the address it self is in the user mode
559 * range.
560 *
561 * @returns true if it's in the user addressable range. false if not.
562 * @param R3Ptr The user mode pointer to test.
563 *
564 * @remarks Some systems may have overlapping kernel and user address ranges.
565 * One prominent example of this is the x86 version of Mac OS X. Use
566 * RTR0MemAreKrnlAndUsrDifferent() to check.
567 */
568RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr);
569
570/**
571 * Tests if the specified address is in the kernel mode range.
572 *
573 * This function does not check whether the memory at that address is accessible
574 * or anything of that sort, only if the address it self is in the kernel mode
575 * range.
576 *
577 * @returns true if it's in the kernel range. false if not.
578 * @param pv The alleged kernel mode pointer.
579 *
580 * @remarks Some systems may have overlapping kernel and user address ranges.
581 * One prominent example of this is the x86 version of Mac OS X. Use
582 * RTR0MemAreKrnlAndUsrDifferent() to check.
583 */
584RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv);
585
586/**
587 * Are user mode and kernel mode address ranges distinctly different.
588 *
589 * This determines whether RTR0MemKernelIsValidAddr and RTR0MemUserIsValidAddr
590 * can be used for deciding whether some arbitrary address is a user mode or a
591 * kernel mode one.
592 *
593 * @returns true if they are, false if not.
594 */
595RTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void);
596
597#endif /* IN_RING0 */
598
599
600/** @name Electrical Fence Version of some APIs.
601 * @{
602 */
603
604/**
605 * Same as RTMemTmpAllocTag() except that it's fenced.
606 *
607 * @returns Pointer to the allocated memory.
608 * @returns NULL on failure.
609 * @param cb Size in bytes of the memory block to allocate.
610 * @param pszTag Allocation tag used for statistics and such.
611 */
612RTDECL(void *) RTMemEfTmpAlloc(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
613
614/**
615 * Same as RTMemTmpAllocZTag() except that it's fenced.
616 *
617 * @returns Pointer to the allocated memory.
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 *) RTMemEfTmpAllocZ(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
623
624/**
625 * Same as RTMemTmpFree() except that it's for fenced memory.
626 *
627 * @param pv Pointer to memory block.
628 */
629RTDECL(void) RTMemEfTmpFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW;
630
631/**
632 * Same as RTMemAllocTag() except that it's fenced.
633 *
634 * @returns Pointer to the allocated memory. Free with RTMemEfFree().
635 * @returns NULL on failure.
636 * @param cb Size in bytes of the memory block to allocate.
637 * @param pszTag Allocation tag used for statistics and such.
638 */
639RTDECL(void *) RTMemEfAlloc(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
640
641/**
642 * Same as RTMemAllocZTag() except that it's fenced.
643 *
644 * @returns Pointer to the allocated memory.
645 * @returns NULL on failure.
646 * @param cb Size in bytes of the memory block to allocate.
647 * @param pszTag Allocation tag used for statistics and such.
648 */
649RTDECL(void *) RTMemEfAllocZ(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
650
651/**
652 * Same as RTMemAllocVarTag() except that it's fenced.
653 *
654 * @returns Pointer to the allocated memory. Free with RTMemEfFree().
655 * @returns NULL on failure.
656 * @param cbUnaligned Size in bytes of the memory block to allocate.
657 * @param pszTag Allocation tag used for statistics and such.
658 */
659RTDECL(void *) RTMemEfAllocVar(size_t cbUnaligned, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
660
661/**
662 * Same as RTMemAllocZVarTag() except that it's fenced.
663 *
664 * @returns Pointer to the allocated memory.
665 * @returns NULL on failure.
666 * @param cbUnaligned Size in bytes of the memory block to allocate.
667 * @param pszTag Allocation tag used for statistics and such.
668 */
669RTDECL(void *) RTMemEfAllocZVar(size_t cbUnaligned, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
670
671/**
672 * Same as RTMemReallocTag() except that it's fenced.
673 *
674 * @returns Pointer to the allocated memory.
675 * @returns NULL on failure.
676 * @param pvOld The memory block to reallocate.
677 * @param cbNew The new block size (in bytes).
678 * @param pszTag Allocation tag used for statistics and such.
679 */
680RTDECL(void *) RTMemEfRealloc(void *pvOld, size_t cbNew, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
681
682/**
683 * Free memory allocated by any of the RTMemEf* allocators.
684 *
685 * @param pv Pointer to memory block.
686 */
687RTDECL(void) RTMemEfFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW;
688
689/**
690 * Same as RTMemDupTag() except that it's fenced.
691 *
692 * @returns New heap block with the duplicate data.
693 * @returns NULL if we're out of memory.
694 * @param pvSrc The memory to duplicate.
695 * @param cb The amount of memory to duplicate.
696 * @param pszTag Allocation tag used for statistics and such.
697 */
698RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
699
700/**
701 * Same as RTMemEfDupExTag except that it's fenced.
702 *
703 * @returns New heap block with the duplicate data.
704 * @returns NULL if we're out of memory.
705 * @param pvSrc The memory to duplicate.
706 * @param cbSrc The amount of memory to duplicate.
707 * @param cbExtra The amount of extra memory to allocate and zero.
708 * @param pszTag Allocation tag used for statistics and such.
709 */
710RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW;
711
712/** @def RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF
713 * Define RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF to enable electric fence new and
714 * delete operators for classes which uses the RTMEMEF_NEW_AND_DELETE_OPERATORS
715 * macro.
716 */
717/** @def RTMEMEF_NEW_AND_DELETE_OPERATORS
718 * Defines the electric fence new and delete operators for a class when
719 * RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF is define.
720 */
721#if defined(RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF) && !defined(RTMEM_NO_WRAP_SOME_NEW_AND_DELETE_TO_EF)
722# if defined(RT_EXCEPTIONS_ENABLED)
723# define RTMEMEF_NEW_AND_DELETE_OPERATORS() \
724 void *operator new(size_t cb) RT_THROW(std::bad_alloc) \
725 { \
726 void *pv = RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
727 if (RT_UNLIKELY(!pv)) \
728 throw std::bad_alloc(); \
729 return pv; \
730 } \
731 void *operator new(size_t cb, const std::nothrow_t &nothrow_constant) RT_NO_THROW \
732 { \
733 NOREF(nothrow_constant); \
734 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
735 } \
736 void *operator new[](size_t cb) RT_THROW(std::bad_alloc) \
737 { \
738 void *pv = RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
739 if (RT_UNLIKELY(!pv)) \
740 throw std::bad_alloc(); \
741 return pv; \
742 } \
743 void *operator new[](size_t cb, const std::nothrow_t &nothrow_constant) RT_NO_THROW \
744 { \
745 NOREF(nothrow_constant); \
746 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
747 } \
748 \
749 void operator delete(void *pv) RT_NO_THROW \
750 { \
751 RTMemEfFree(pv, RT_SRC_POS); \
752 } \
753 void operator delete(void *pv, const std::nothrow_t &nothrow_constant) RT_NO_THROW \
754 { \
755 NOREF(nothrow_constant); \
756 RTMemEfFree(pv, RT_SRC_POS); \
757 } \
758 void operator delete[](void *pv) RT_NO_THROW \
759 { \
760 RTMemEfFree(pv, RT_SRC_POS); \
761 } \
762 void operator delete[](void *pv, const std::nothrow_t &nothrow_constant) RT_NO_THROW \
763 { \
764 NOREF(nothrow_constant); \
765 RTMemEfFree(pv, RT_SRC_POS); \
766 } \
767 \
768 typedef int UsingElectricNewAndDeleteOperators
769# else
770# define RTMEMEF_NEW_AND_DELETE_OPERATORS() \
771 void *operator new(size_t cb) \
772 { \
773 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
774 } \
775 void *operator new(size_t cb, const std::nothrow_t &nothrow_constant) \
776 { \
777 NOREF(nothrow_constant); \
778 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
779 } \
780 void *operator new[](size_t cb) \
781 { \
782 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
783 } \
784 void *operator new[](size_t cb, const std::nothrow_t &nothrow_constant) \
785 { \
786 NOREF(nothrow_constant); \
787 return RTMemEfAlloc(cb, RTMEM_TAG, RT_SRC_POS); \
788 } \
789 \
790 void operator delete(void *pv) \
791 { \
792 RTMemEfFree(pv, RT_SRC_POS); \
793 } \
794 void operator delete(void *pv, const std::nothrow_t &nothrow_constant) \
795 { \
796 NOREF(nothrow_constant); \
797 RTMemEfFree(pv, RT_SRC_POS); \
798 } \
799 void operator delete[](void *pv) \
800 { \
801 RTMemEfFree(pv, RT_SRC_POS); \
802 } \
803 void operator delete[](void *pv, const std::nothrow_t &nothrow_constant) \
804 { \
805 NOREF(nothrow_constant); \
806 RTMemEfFree(pv, RT_SRC_POS); \
807 } \
808 \
809 typedef int UsingElectricNewAndDeleteOperators
810# endif
811#else
812# define RTMEMEF_NEW_AND_DELETE_OPERATORS() \
813 typedef int UsingDefaultNewAndDeleteOperators
814#endif
815#ifdef DOXYGEN_RUNNING
816# define RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF
817#endif
818
819/** @def RTMEM_WRAP_TO_EF_APIS
820 * Define RTMEM_WRAP_TO_EF_APIS to wrap RTMem APIs to RTMemEf APIs.
821 */
822#if defined(RTMEM_WRAP_TO_EF_APIS) && defined(IN_RING3) && !defined(RTMEM_NO_WRAP_TO_EF_APIS)
823# define RTMemTmpAllocTag(cb, pszTag) RTMemEfTmpAlloc((cb), (pszTag), RT_SRC_POS)
824# define RTMemTmpAllocZTag(cb, pszTag) RTMemEfTmpAllocZ((cb), (pszTag), RT_SRC_POS)
825# define RTMemTmpFree(pv) RTMemEfTmpFree((pv), RT_SRC_POS)
826# define RTMemAllocTag(cb, pszTag) RTMemEfAlloc((cb), (pszTag), RT_SRC_POS)
827# define RTMemAllocZTag(cb, pszTag) RTMemEfAllocZ((cb), (pszTag), RT_SRC_POS)
828# define RTMemAllocVarTag(cbUnaligned, pszTag) RTMemEfAllocVar((cbUnaligned), (pszTag), RT_SRC_POS)
829# define RTMemAllocZVarTag(cbUnaligned, pszTag) RTMemEfAllocZVar((cbUnaligned), (pszTag), RT_SRC_POS)
830# define RTMemReallocTag(pvOld, cbNew, pszTag) RTMemEfRealloc((pvOld), (cbNew), (pszTag), RT_SRC_POS)
831# define RTMemFree(pv) RTMemEfFree((pv), RT_SRC_POS)
832# define RTMemDupTag(pvSrc, cb, pszTag) RTMemEfDup((pvSrc), (cb), (pszTag), RT_SRC_POS)
833# define RTMemDupExTag(pvSrc, cbSrc, cbExtra, pszTag) RTMemEfDupEx((pvSrc), (cbSrc), (cbExtra), (pszTag), RT_SRC_POS)
834#endif
835#ifdef DOXYGEN_RUNNING
836# define RTMEM_WRAP_TO_EF_APIS
837#endif
838
839/**
840 * Fenced drop-in replacement for RTMemTmpAllocTag.
841 * @copydoc RTMemTmpAllocTag
842 */
843RTDECL(void *) RTMemEfTmpAllocNP(size_t cb, const char *pszTag) RT_NO_THROW;
844
845/**
846 * Fenced drop-in replacement for RTMemTmpAllocZTag.
847 * @copydoc RTMemTmpAllocZTag
848 */
849RTDECL(void *) RTMemEfTmpAllocZNP(size_t cb, const char *pszTag) RT_NO_THROW;
850
851/**
852 * Fenced drop-in replacement for RTMemTmpFreeTag.
853 * @copydoc RTMemTmpFreeTag
854 */
855RTDECL(void) RTMemEfTmpFreeNP(void *pv) RT_NO_THROW;
856
857/**
858 * Fenced drop-in replacement for RTMemAllocTag.
859 * @copydoc RTMemAllocTag
860 */
861RTDECL(void *) RTMemEfAllocNP(size_t cb, const char *pszTag) RT_NO_THROW;
862
863/**
864 * Fenced drop-in replacement for RTMemAllocZTag.
865 * @copydoc RTMemAllocZTag
866 */
867RTDECL(void *) RTMemEfAllocZNP(size_t cb, const char *pszTag) RT_NO_THROW;
868
869/**
870 * Fenced drop-in replacement for RTMemAllocVarTag
871 * @copydoc RTMemAllocVarTag
872 */
873RTDECL(void *) RTMemEfAllocVarNP(size_t cbUnaligned, const char *pszTag) RT_NO_THROW;
874
875/**
876 * Fenced drop-in replacement for RTMemAllocZVarTag.
877 * @copydoc RTMemAllocZVarTag
878 */
879RTDECL(void *) RTMemEfAllocZVarNP(size_t cbUnaligned, const char *pszTag) RT_NO_THROW;
880
881/**
882 * Fenced drop-in replacement for RTMemReallocTag.
883 * @copydoc RTMemReallocTag
884 */
885RTDECL(void *) RTMemEfReallocNP(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW;
886
887/**
888 * Fenced drop-in replacement for RTMemFree.
889 * @copydoc RTMemFree
890 */
891RTDECL(void) RTMemEfFreeNP(void *pv) RT_NO_THROW;
892
893/**
894 * Fenced drop-in replacement for RTMemDupExTag.
895 * @copydoc RTMemDupExTag
896 */
897RTDECL(void *) RTMemEfDupNP(const void *pvSrc, size_t cb, const char *pszTag) RT_NO_THROW;
898
899/**
900 * Fenced drop-in replacement for RTMemDupExTag.
901 * @copydoc RTMemDupExTag
902 */
903RTDECL(void *) RTMemEfDupExNP(const void *pvSrc, size_t cbSrc, size_t cbExtra, const char *pszTag) RT_NO_THROW;
904
905/** @} */
906
907RT_C_DECLS_END
908
909/** @} */
910
911
912#endif
913
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