VirtualBox

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

Last change on this file since 36535 was 36535, checked in by vboxsync, 14 years ago

iprt: try to fix osx builds

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