VirtualBox

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

Last change on this file since 28318 was 28318, checked in by vboxsync, 15 years ago

RTMemPageFree + all users: Added size parameter to RTMemPageFree so we can avoid tracking structures when using mmap/munmap.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.8 KB
Line 
1/** @file
2 * IPRT - Memory Management and Manipulation.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_mem_h
31#define ___iprt_mem_h
32
33
34#include <iprt/cdefs.h>
35#include <iprt/types.h>
36#ifdef __cplusplus /** @todo remove when spitting. */
37# include <iprt/cpp/autores.h>
38#endif
39
40
41#ifdef IN_RC
42# error "There are no RTMem APIs available Guest Context!"
43#endif
44
45
46/** @defgroup grp_rt_mem RTMem - Memory Management and Manipulation
47 * @ingroup grp_rt
48 * @{
49 */
50
51RT_C_DECLS_BEGIN
52
53/** @def RTMEM_ALIGNMENT
54 * The alignment of the memory blocks returned by RTMemAlloc(), RTMemAllocZ(),
55 * RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ() for allocations greater
56 * than RTMEM_ALIGNMENT.
57 */
58#define RTMEM_ALIGNMENT 8
59
60/**
61 * Allocates temporary memory.
62 *
63 * Temporary memory blocks are used for not too large memory blocks which
64 * are believed not to stick around for too long. Using this API instead
65 * of RTMemAlloc() not only gives the heap manager room for optimization
66 * but makes the code easier to read.
67 *
68 * @returns Pointer to the allocated memory.
69 * @returns NULL on failure.
70 * @param cb Size in bytes of the memory block to allocated.
71 */
72RTDECL(void *) RTMemTmpAlloc(size_t cb) RT_NO_THROW;
73
74/**
75 * Allocates zero'ed temporary memory.
76 *
77 * Same as RTMemTmpAlloc() but the memory will be zero'ed.
78 *
79 * @returns Pointer to the allocated memory.
80 * @returns NULL on failure.
81 * @param cb Size in bytes of the memory block to allocated.
82 */
83RTDECL(void *) RTMemTmpAllocZ(size_t cb) RT_NO_THROW;
84
85/**
86 * Free temporary memory.
87 *
88 * @param pv Pointer to memory block.
89 */
90RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW;
91
92
93/**
94 * Allocates memory.
95 *
96 * @returns Pointer to the allocated memory.
97 * @returns NULL on failure.
98 * @param cb Size in bytes of the memory block to allocated.
99 */
100RTDECL(void *) RTMemAlloc(size_t cb) RT_NO_THROW;
101
102/**
103 * Allocates zero'ed memory.
104 *
105 * Instead of memset(pv, 0, sizeof()) use this when you want zero'ed
106 * memory. This keeps the code smaller and the heap can skip the memset
107 * in about 0.42% of calls :-).
108 *
109 * @returns Pointer to the allocated memory.
110 * @returns NULL on failure.
111 * @param cb Size in bytes of the memory block to allocated.
112 */
113RTDECL(void *) RTMemAllocZ(size_t cb) RT_NO_THROW;
114
115/**
116 * Wrapper around RTMemAlloc for automatically aligning variable sized
117 * allocations so that the various electric fence heaps works correctly.
118 *
119 * @returns See RTMemAlloc.
120 * @param cbUnaligned The unaligned size.
121 */
122RTDECL(void *) RTMemAllocVar(size_t cbUnaligned);
123
124/**
125 * Wrapper around RTMemAllocZ for automatically aligning variable sized
126 * allocations so that the various electric fence heaps works correctly.
127 *
128 * @returns See RTMemAllocZ.
129 * @param cbUnaligned The unaligned size.
130 */
131RTDECL(void *) RTMemAllocZVar(size_t cbUnaligned);
132
133/**
134 * Duplicates a chunk of memory into a new heap block.
135 *
136 * @returns New heap block with the duplicate data.
137 * @returns NULL if we're out of memory.
138 * @param pvSrc The memory to duplicate.
139 * @param cb The amount of memory to duplicate.
140 */
141RTDECL(void *) RTMemDup(const void *pvSrc, size_t cb) RT_NO_THROW;
142
143/**
144 * Duplicates a chunk of memory into a new heap block with some
145 * additional zeroed memory.
146 *
147 * @returns New heap block with the duplicate data.
148 * @returns NULL if we're out of memory.
149 * @param pvSrc The memory to duplicate.
150 * @param cbSrc The amount of memory to duplicate.
151 * @param cbExtra The amount of extra memory to allocate and zero.
152 */
153RTDECL(void *) RTMemDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW;
154
155/**
156 * Reallocates memory.
157 *
158 * @returns Pointer to the allocated memory.
159 * @returns NULL on failure.
160 * @param pvOld The memory block to reallocate.
161 * @param cbNew The new block size (in bytes).
162 */
163RTDECL(void *) RTMemRealloc(void *pvOld, size_t cbNew) RT_NO_THROW;
164
165/**
166 * Frees memory.
167 *
168 * @param pv Pointer to memory block.
169 */
170RTDECL(void) RTMemFree(void *pv) RT_NO_THROW;
171
172/**
173 * Allocates memory which may contain code.
174 *
175 * @returns Pointer to the allocated memory.
176 * @returns NULL on failure.
177 * @param cb Size in bytes of the memory block to allocate.
178 */
179RTDECL(void *) RTMemExecAlloc(size_t cb) RT_NO_THROW;
180
181/**
182 * Free executable/read/write memory allocated by RTMemExecAlloc().
183 *
184 * @param pv Pointer to memory block.
185 */
186RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW;
187
188#if defined(IN_RING0) && defined(RT_ARCH_AMD64) && defined(RT_OS_LINUX)
189/**
190 * Donate read+write+execute memory to the exec heap.
191 *
192 * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
193 * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
194 * allocated memory in the module if it wishes for GCC generated code to work.
195 * GCC can only generate modules that work in the address range ~2GB to ~0
196 * currently.
197 *
198 * The API only accept one single donation.
199 *
200 * @returns IPRT status code.
201 * @param pvMemory Pointer to the memory block.
202 * @param cb The size of the memory block.
203 */
204RTR0DECL(int) RTR0MemExecDonate(void *pvMemory, size_t cb) RT_NO_THROW;
205#endif /* R0+AMD64+LINUX */
206
207/**
208 * Allocate page aligned memory.
209 *
210 * @returns Pointer to the allocated memory.
211 * @returns NULL if we're out of memory.
212 * @param cb Size of the memory block. Will be rounded up to page size.
213 */
214RTDECL(void *) RTMemPageAlloc(size_t cb) RT_NO_THROW;
215
216/**
217 * Allocate zero'ed page aligned memory.
218 *
219 * @returns Pointer to the allocated memory.
220 * @returns NULL if we're out of memory.
221 * @param cb Size of the memory block. Will be rounded up to page size.
222 */
223RTDECL(void *) RTMemPageAllocZ(size_t cb) RT_NO_THROW;
224
225/**
226 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
227 *
228 * @param pv Pointer to the block as it was returned by the allocation function.
229 * NULL will be ignored.
230 * @param cb The allocation size. Will be rounded up to page size.
231 * Ignored if @a pv is NULL.
232 */
233RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW;
234
235/** Page level protection flags for RTMemProtect().
236 * @{
237 */
238/** No access at all. */
239#define RTMEM_PROT_NONE 0
240/** Read access. */
241#define RTMEM_PROT_READ 1
242/** Write access. */
243#define RTMEM_PROT_WRITE 2
244/** Execute access. */
245#define RTMEM_PROT_EXEC 4
246/** @} */
247
248/**
249 * Change the page level protection of a memory region.
250 *
251 * @returns iprt status code.
252 * @param pv Start of the region. Will be rounded down to nearest page boundary.
253 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
254 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
255 */
256RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW;
257
258
259#ifdef IN_RING0
260
261/**
262 * Allocates physical contiguous memory (below 4GB).
263 * The allocation is page aligned and the content is undefined.
264 *
265 * @returns Pointer to the memory block. This is page aligned.
266 * @param pPhys Where to store the physical address.
267 * @param cb The allocation size in bytes. This is always
268 * rounded up to PAGE_SIZE.
269 */
270RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb) RT_NO_THROW;
271
272/**
273 * Frees memory allocated ysing RTMemContAlloc().
274 *
275 * @param pv Pointer to return from RTMemContAlloc().
276 * @param cb The cb parameter passed to RTMemContAlloc().
277 */
278RTR0DECL(void) RTMemContFree(void *pv, size_t cb) RT_NO_THROW;
279
280/**
281 * Copy memory from an user mode buffer into a kernel buffer.
282 *
283 * @retval VINF_SUCCESS on success.
284 * @retval VERR_ACCESS_DENIED on error.
285 *
286 * @param pvDst The kernel mode destination address.
287 * @param R3PtrSrc The user mode source address.
288 * @param cb The number of bytes to copy.
289 */
290RTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst, RTR3PTR R3PtrSrc, size_t cb);
291
292/**
293 * Copy memory from a kernel buffer into a user mode one.
294 *
295 * @retval VINF_SUCCESS on success.
296 * @retval VERR_ACCESS_DENIED on error.
297 *
298 * @param R3PtrDst The user mode destination address.
299 * @param pvSrc The kernel mode source address.
300 * @param cb The number of bytes to copy.
301 */
302RTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb);
303
304/**
305 * Tests if the specified address is in the user addressable range.
306 *
307 * This function does not check whether the memory at that address is accessible
308 * or anything of that sort, only if the address it self is in the user mode
309 * range.
310 *
311 * @returns true if it's in the user addressable range. false if not.
312 * @param R3Ptr The user mode pointer to test.
313 *
314 * @remarks Some systems may have overlapping kernel and user address ranges.
315 * One prominent example of this is the x86 version of Mac OS X. Use
316 * RTR0MemAreKrnlAndUsrDifferent() to check.
317 */
318RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr);
319
320/**
321 * Tests if the specified address is in the kernel mode range.
322 *
323 * This function does not check whether the memory at that address is accessible
324 * or anything of that sort, only if the address it self is in the kernel mode
325 * range.
326 *
327 * @returns true if it's in the kernel range. false if not.
328 * @param pv The alleged kernel mode pointer.
329 *
330 * @remarks Some systems may have overlapping kernel and user address ranges.
331 * One prominent example of this is the x86 version of Mac OS X. Use
332 * RTR0MemAreKrnlAndUsrDifferent() to check.
333 */
334RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv);
335
336/**
337 * Are user mode and kernel mode address ranges distinctly different.
338 *
339 * This determins whether RTR0MemKernelIsValidAddr and RTR0MemUserIsValidAddr
340 * can be used for deciding whether some arbitrary address is a user mode or a
341 * kernel mode one.
342 *
343 * @returns true if they are, false if not.
344 */
345RTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void);
346
347#endif /* IN_RING0 */
348
349
350/** @name Electrical Fence Version of some APIs.
351 * @{
352 */
353
354/**
355 * Same as RTMemTmpAlloc() except that it's fenced.
356 *
357 * @returns Pointer to the allocated memory.
358 * @returns NULL on failure.
359 * @param cb Size in bytes of the memory block to allocate.
360 */
361RTDECL(void *) RTMemEfTmpAlloc(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW;
362
363/**
364 * Same as RTMemTmpAllocZ() except that it's fenced.
365 *
366 * @returns Pointer to the allocated memory.
367 * @returns NULL on failure.
368 * @param cb Size in bytes of the memory block to allocate.
369 */
370RTDECL(void *) RTMemEfTmpAllocZ(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW;
371
372/**
373 * Same as RTMemTmpFree() except that it's for fenced memory.
374 *
375 * @param pv Pointer to memory block.
376 */
377RTDECL(void) RTMemEfTmpFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW;
378
379/**
380 * Same as RTMemAlloc() except that it's fenced.
381 *
382 * @returns Pointer to the allocated memory. Free with RTMemEfFree().
383 * @returns NULL on failure.
384 * @param cb Size in bytes of the memory block to allocate.
385 */
386RTDECL(void *) RTMemEfAlloc(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW;
387
388/**
389 * Same as RTMemAllocZ() except that it's fenced.
390 *
391 * @returns Pointer to the allocated memory.
392 * @returns NULL on failure.
393 * @param cb Size in bytes of the memory block to allocate.
394 */
395RTDECL(void *) RTMemEfAllocZ(size_t cb, RT_SRC_POS_DECL) RT_NO_THROW;
396
397/**
398 * Same as RTMemAllocVar() except that it's fenced.
399 *
400 * @returns Pointer to the allocated memory. Free with RTMemEfFree().
401 * @returns NULL on failure.
402 * @param cbUnaligned Size in bytes of the memory block to allocate.
403 */
404RTDECL(void *) RTMemEfAllocVar(size_t cbUnaligned, RT_SRC_POS_DECL) RT_NO_THROW;
405
406/**
407 * Same as RTMemAllocZVar() except that it's fenced.
408 *
409 * @returns Pointer to the allocated memory.
410 * @returns NULL on failure.
411 * @param cbUnaligned Size in bytes of the memory block to allocate.
412 */
413RTDECL(void *) RTMemEfAllocZVar(size_t cbUnaligned, RT_SRC_POS_DECL) RT_NO_THROW;
414
415/**
416 * Same as RTMemRealloc() except that it's fenced.
417 *
418 * @returns Pointer to the allocated memory.
419 * @returns NULL on failure.
420 * @param pvOld The memory block to reallocate.
421 * @param cbNew The new block size (in bytes).
422 */
423RTDECL(void *) RTMemEfRealloc(void *pvOld, size_t cbNew, RT_SRC_POS_DECL) RT_NO_THROW;
424
425/**
426 * Free memory allocated by any of the RTMemEf* allocators.
427 *
428 * @param pv Pointer to memory block.
429 */
430RTDECL(void) RTMemEfFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW;
431
432/**
433 * Same as RTMemDup() except that it's fenced.
434 *
435 * @returns New heap block with the duplicate data.
436 * @returns NULL if we're out of memory.
437 * @param pvSrc The memory to duplicate.
438 * @param cb The amount of memory to duplicate.
439 */
440RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb, RT_SRC_POS_DECL) RT_NO_THROW;
441
442/**
443 * Same as RTMemEfDupEx except that it's fenced.
444 *
445 * @returns New heap block with the duplicate data.
446 * @returns NULL if we're out of memory.
447 * @param pvSrc The memory to duplicate.
448 * @param cbSrc The amount of memory to duplicate.
449 * @param cbExtra The amount of extra memory to allocate and zero.
450 */
451RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra, RT_SRC_POS_DECL) RT_NO_THROW;
452
453/** @def RTMEM_WRAP_TO_EF_APIS
454 * Define RTMEM_WRAP_TO_EF_APIS to wrap RTMem APIs to RTMemEf APIs.
455 */
456#if defined(RTMEM_WRAP_TO_EF_APIS) && defined(IN_RING3) && !defined(RTMEM_NO_WRAP_TO_EF_APIS)
457# define RTMemTmpAlloc(cb) RTMemEfTmpAlloc((cb), RT_SRC_POS)
458# define RTMemTmpAllocZ(cb) RTMemEfTmpAllocZ((cb), RT_SRC_POS)
459# define RTMemTmpFree(pv) RTMemEfTmpFree((pv), RT_SRC_POS)
460# define RTMemAlloc(cb) RTMemEfAlloc((cb), RT_SRC_POS)
461# define RTMemAllocZ(cb) RTMemEfAllocZ((cb), RT_SRC_POS)
462# define RTMemAllocVar(cbUnaligned) RTMemEfAllocVar((cbUnaligned), RT_SRC_POS)
463# define RTMemAllocZVar(cbUnaligned) RTMemEfAllocZVar((cbUnaligned), RT_SRC_POS)
464# define RTMemRealloc(pvOld, cbNew) RTMemEfRealloc((pvOld), (cbNew), RT_SRC_POS)
465# define RTMemFree(pv) RTMemEfFree((pv), RT_SRC_POS)
466# define RTMemDup(pvSrc, cb) RTMemEfDup((pvSrc), (cb), RT_SRC_POS)
467# define RTMemDupEx(pvSrc, cbSrc, cbExtra) RTMemEfDupEx((pvSrc), (cbSrc), (cbExtra), RT_SRC_POS)
468#endif
469#ifdef DOXYGEN_RUNNING
470# define RTMEM_WRAP_TO_EF_APIS
471#endif
472
473/**
474 * Fenced drop-in replacement for RTMemTmpAlloc.
475 * @copydoc RTMemTmpAlloc
476 */
477RTDECL(void *) RTMemEfTmpAllocNP(size_t cb) RT_NO_THROW;
478
479/**
480 * Fenced drop-in replacement for RTMemTmpAllocZ.
481 * @copydoc RTMemTmpAllocZ
482 */
483RTDECL(void *) RTMemEfTmpAllocZNP(size_t cb) RT_NO_THROW;
484
485/**
486 * Fenced drop-in replacement for RTMemTmpFree.
487 * @copydoc RTMemTmpFree
488 */
489RTDECL(void) RTMemEfTmpFreeNP(void *pv) RT_NO_THROW;
490
491/**
492 * Fenced drop-in replacement for RTMemAlloc.
493 * @copydoc RTMemAlloc
494 */
495RTDECL(void *) RTMemEfAllocNP(size_t cb) RT_NO_THROW;
496
497/**
498 * Fenced drop-in replacement for RTMemAllocZ.
499 * @copydoc RTMemAllocZ
500 */
501RTDECL(void *) RTMemEfAllocZNP(size_t cb) RT_NO_THROW;
502
503/**
504 * Fenced drop-in replacement for RTMemAllocVar
505 * @copydoc RTMemAllocVar
506 */
507RTDECL(void *) RTMemEfAllocVarNP(size_t cbUnaligned) RT_NO_THROW;
508
509/**
510 * Fenced drop-in replacement for RTMemAllocZVar.
511 * @copydoc RTMemAllocZVar
512 */
513RTDECL(void *) RTMemEfAllocZVarNP(size_t cbUnaligned) RT_NO_THROW;
514
515/**
516 * Fenced drop-in replacement for RTMemRealloc.
517 * @copydoc RTMemRealloc
518 */
519RTDECL(void *) RTMemEfReallocNP(void *pvOld, size_t cbNew) RT_NO_THROW;
520
521/**
522 * Fenced drop-in replacement for RTMemFree.
523 * @copydoc RTMemFree
524 */
525RTDECL(void) RTMemEfFreeNP(void *pv) RT_NO_THROW;
526
527/**
528 * Fenced drop-in replacement for RTMemDupEx.
529 * @copydoc RTMemDupEx
530 */
531RTDECL(void *) RTMemEfDupNP(const void *pvSrc, size_t cb) RT_NO_THROW;
532
533/**
534 * Fenced drop-in replacement for RTMemDupEx.
535 * @copydoc RTMemDupEx
536 */
537RTDECL(void *) RTMemEfDupExNP(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW;
538
539/** @} */
540
541RT_C_DECLS_END
542
543
544#ifdef __cplusplus /** @todo Split this out into iprt/cpp/mem.h! */
545# include <iprt/assert.h>
546
547/**
548 * Template function wrapping RTMemFree to get the correct Destruct
549 * signature for RTAutoRes.
550 *
551 * We can't use a more complex template here, because the g++ on RHEL 3
552 * chokes on it with an internal compiler error.
553 *
554 * @tparam T The data type that's being managed.
555 * @param aMem Pointer to the memory that should be free.
556 */
557template <class T>
558inline void RTMemAutoDestructor(T *aMem) RT_NO_THROW
559{
560 RTMemFree(aMem);
561}
562
563
564/**
565 * RTMemAutoPtr allocator which uses RTMemTmpAlloc().
566 *
567 * @returns Allocated memory on success, NULL on failure.
568 * @param pvOld What to reallocate, shall always be NULL.
569 * @param cbNew The amount of memory to allocate (in bytes).
570 */
571inline void *RTMemTmpAutoAllocator(void *pvOld, size_t cbNew) RT_NO_THROW
572{
573 AssertReturn(!pvOld, NULL);
574 return RTMemTmpAlloc(cbNew);
575}
576
577
578/**
579 * Template function wrapping RTMemTmpFree to get the correct Destruct
580 * signature for RTAutoRes.
581 *
582 * We can't use a more complex template here, because the g++ on RHEL 3
583 * chokes on it with an internal compiler error.
584 *
585 * @tparam T The data type that's being managed.
586 * @param aMem Pointer to the memory that should be free.
587 */
588template <class T>
589inline void RTMemTmpAutoDestructor(T *aMem) RT_NO_THROW
590{
591 RTMemTmpFree(aMem);
592}
593
594
595/**
596 * Template function wrapping RTMemEfFree to get the correct Destruct
597 * signature for RTAutoRes.
598 *
599 * We can't use a more complex template here, because the g++ on RHEL 3
600 * chokes on it with an internal compiler error.
601 *
602 * @tparam T The data type that's being managed.
603 * @param aMem Pointer to the memory that should be free.
604 */
605template <class T>
606inline void RTMemEfAutoFree(T *aMem) RT_NO_THROW
607{
608 RTMemEfFreeNP(aMem);
609}
610
611
612/**
613 * Template function wrapping NULL to get the correct NilRes signature
614 * for RTAutoRes.
615 *
616 * @tparam T The data type that's being managed.
617 * @returns NULL with the right type.
618 */
619template <class T>
620inline T * RTMemAutoNil(void) RT_NO_THROW
621{
622 return (T *)(NULL);
623}
624
625
626/**
627 * An auto pointer-type template class for managing memory allocating
628 * via C APIs like RTMem (the default).
629 *
630 * The main purpose of this class is to automatically free memory that
631 * isn't explicitly used (release()'ed) when the object goes out of scope.
632 *
633 * As an additional service it can also make the allocations and
634 * reallocations for you if you like, but it can also take of memory
635 * you hand it.
636 *
637 * @tparam T The data type to manage allocations for.
638 * @tparam Destruct The function to be used to free the resource.
639 * This will default to RTMemFree.
640 * @tparam Allocator The function to be used to allocate or reallocate
641 * the managed memory.
642 * This is standard realloc() like stuff, so it's possible
643 * to support simple allocation without actually having
644 * to support reallocating memory if that's a problem.
645 * This will default to RTMemRealloc.
646 */
647template <class T, void Destruct(T *) = RTMemAutoDestructor<T>, void *Allocator(void *, size_t) = RTMemRealloc >
648class RTMemAutoPtr
649 : public RTAutoRes<T *, Destruct, RTMemAutoNil<T> >
650{
651public:
652 /**
653 * Constructor.
654 *
655 * @param aPtr Memory pointer to manage. Defaults to NULL.
656 */
657 RTMemAutoPtr(T *aPtr = NULL)
658 : RTAutoRes<T *, Destruct, RTMemAutoNil<T> >(aPtr)
659 {
660 }
661
662 /**
663 * Constructor that allocates memory.
664 *
665 * @param a_cElements The number of elements (of the data type) to allocate.
666 * @param a_fZeroed Whether the memory should be memset with zeros after
667 * the allocation. Defaults to false.
668 */
669 RTMemAutoPtr(size_t a_cElements, bool a_fZeroed = false)
670 : RTAutoRes<T *, Destruct, RTMemAutoNil<T> >((T *)Allocator(NULL, a_cElements * sizeof(T)))
671 {
672 if (a_fZeroed && RT_LIKELY(this->get() != NULL))
673 memset(this->get(), '\0', a_cElements * sizeof(T));
674 }
675
676 /**
677 * Free current memory and start managing aPtr.
678 *
679 * @param aPtr Memory pointer to manage.
680 */
681 RTMemAutoPtr &operator=(T *aPtr)
682 {
683 this->RTAutoRes<T *, Destruct, RTMemAutoNil<T> >::operator=(aPtr);
684 return *this;
685 }
686
687 /**
688 * Dereference with * operator.
689 */
690 T &operator*()
691 {
692 return *this->get();
693 }
694
695 /**
696 * Dereference with -> operator.
697 */
698 T *operator->()
699 {
700 return this->get();
701 }
702
703 /**
704 * Accessed with the subscript operator ([]).
705 *
706 * @returns Reference to the element.
707 * @param a_i The element to access.
708 */
709 T &operator[](size_t a_i)
710 {
711 return this->get()[a_i];
712 }
713
714 /**
715 * Allocates memory and start manage it.
716 *
717 * Any previously managed memory will be freed before making
718 * the new allocation.
719 *
720 * @returns Success indicator.
721 * @retval true if the new allocation succeeds.
722 * @retval false on failure, no memory is associated with the object.
723 *
724 * @param a_cElements The number of elements (of the data type) to allocate.
725 * This defaults to 1.
726 * @param a_fZeroed Whether the memory should be memset with zeros after
727 * the allocation. Defaults to false.
728 */
729 bool alloc(size_t a_cElements = 1, bool a_fZeroed = false)
730 {
731 this->reset(NULL);
732 T *pNewMem = (T *)Allocator(NULL, a_cElements * sizeof(T));
733 if (a_fZeroed && RT_LIKELY(pNewMem != NULL))
734 memset(pNewMem, '\0', a_cElements * sizeof(T));
735 this->reset(pNewMem);
736 return pNewMem != NULL;
737 }
738
739 /**
740 * Reallocate or allocates the memory resource.
741 *
742 * Free the old value if allocation fails.
743 *
744 * The content of any additional memory that was allocated is
745 * undefined when using the default allocator.
746 *
747 * @returns Success indicator.
748 * @retval true if the new allocation succeeds.
749 * @retval false on failure, no memory is associated with the object.
750 *
751 * @param a_cElements The new number of elements (of the data type) to
752 * allocate. The size of the allocation is the number of
753 * elements times the size of the data type - this is
754 * currently what's passed down to the Allocator.
755 * This defaults to 1.
756 */
757 bool realloc(size_t a_cElements = 1)
758 {
759 T *aNewValue = (T *)Allocator(this->get(), a_cElements * sizeof(T));
760 if (RT_LIKELY(aNewValue != NULL))
761 this->release();
762 /* We want this both if aNewValue is non-NULL and if it is NULL. */
763 this->reset(aNewValue);
764 return aNewValue != NULL;
765 }
766};
767
768
769#endif /* __cplusplus */
770
771
772/** @} */
773
774
775#endif
776
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