VirtualBox

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

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

IPRT: Added RTMemAllocEx[Tag] and RTMemFreeEx, only implemented in ring-0 only.

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