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
|
---|
37 | # include <iprt/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 |
|
---|
51 | RT_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 | */
|
---|
72 | RTDECL(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 | */
|
---|
83 | RTDECL(void *) RTMemTmpAllocZ(size_t cb) RT_NO_THROW;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Free temporary memory.
|
---|
87 | *
|
---|
88 | * @param pv Pointer to memory block.
|
---|
89 | */
|
---|
90 | RTDECL(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 | */
|
---|
100 | RTDECL(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 | */
|
---|
113 | RTDECL(void *) RTMemAllocZ(size_t cb) RT_NO_THROW;
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Duplicates a chunk of memory into a new heap block.
|
---|
117 | *
|
---|
118 | * @returns New heap block with the duplicate data.
|
---|
119 | * @returns NULL if we're out of memory.
|
---|
120 | * @param pvSrc The memory to duplicate.
|
---|
121 | * @param cb The amount of memory to duplicate.
|
---|
122 | */
|
---|
123 | RTDECL(void *) RTMemDup(const void *pvSrc, size_t cb) RT_NO_THROW;
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Duplicates a chunk of memory into a new heap block with some
|
---|
127 | * additional zeroed memory.
|
---|
128 | *
|
---|
129 | * @returns New heap block with the duplicate data.
|
---|
130 | * @returns NULL if we're out of memory.
|
---|
131 | * @param pvSrc The memory to duplicate.
|
---|
132 | * @param cbSrc The amount of memory to duplicate.
|
---|
133 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
134 | */
|
---|
135 | RTDECL(void *) RTMemDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW;
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Reallocates memory.
|
---|
139 | *
|
---|
140 | * @returns Pointer to the allocated memory.
|
---|
141 | * @returns NULL on failure.
|
---|
142 | * @param pvOld The memory block to reallocate.
|
---|
143 | * @param cbNew The new block size (in bytes).
|
---|
144 | */
|
---|
145 | RTDECL(void *) RTMemRealloc(void *pvOld, size_t cbNew) RT_NO_THROW;
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Frees memory.
|
---|
149 | *
|
---|
150 | * @param pv Pointer to memory block.
|
---|
151 | */
|
---|
152 | RTDECL(void) RTMemFree(void *pv) RT_NO_THROW;
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Allocates memory which may contain code.
|
---|
156 | *
|
---|
157 | * @returns Pointer to the allocated memory.
|
---|
158 | * @returns NULL on failure.
|
---|
159 | * @param cb Size in bytes of the memory block to allocate.
|
---|
160 | */
|
---|
161 | RTDECL(void *) RTMemExecAlloc(size_t cb) RT_NO_THROW;
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Free executable/read/write memory allocated by RTMemExecAlloc().
|
---|
165 | *
|
---|
166 | * @param pv Pointer to memory block.
|
---|
167 | */
|
---|
168 | RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW;
|
---|
169 |
|
---|
170 | #if defined(IN_RING0) && defined(RT_ARCH_AMD64) && defined(RT_OS_LINUX)
|
---|
171 | /**
|
---|
172 | * Donate read+write+execute memory to the exec heap.
|
---|
173 | *
|
---|
174 | * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
|
---|
175 | * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
|
---|
176 | * allocated memory in the module if it wishes for GCC generated code to work.
|
---|
177 | * GCC can only generate modules that work in the address range ~2GB to ~0
|
---|
178 | * currently.
|
---|
179 | *
|
---|
180 | * The API only accept one single donation.
|
---|
181 | *
|
---|
182 | * @returns IPRT status code.
|
---|
183 | * @param pvMemory Pointer to the memory block.
|
---|
184 | * @param cb The size of the memory block.
|
---|
185 | */
|
---|
186 | RTR0DECL(int) RTR0MemExecDonate(void *pvMemory, size_t cb) RT_NO_THROW;
|
---|
187 | #endif /* R0+AMD64+LINUX */
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Allocate page aligned memory.
|
---|
191 | *
|
---|
192 | * @returns Pointer to the allocated memory.
|
---|
193 | * @returns NULL if we're out of memory.
|
---|
194 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
195 | */
|
---|
196 | RTDECL(void *) RTMemPageAlloc(size_t cb) RT_NO_THROW;
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Allocate zero'ed page aligned memory.
|
---|
200 | *
|
---|
201 | * @returns Pointer to the allocated memory.
|
---|
202 | * @returns NULL if we're out of memory.
|
---|
203 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
204 | */
|
---|
205 | RTDECL(void *) RTMemPageAllocZ(size_t cb) RT_NO_THROW;
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
|
---|
209 | *
|
---|
210 | * @param pv Pointer to the block as it was returned by the allocation function.
|
---|
211 | * NULL will be ignored.
|
---|
212 | */
|
---|
213 | RTDECL(void) RTMemPageFree(void *pv) RT_NO_THROW;
|
---|
214 |
|
---|
215 | /** Page level protection flags for RTMemProtect().
|
---|
216 | * @{
|
---|
217 | */
|
---|
218 | /** Read access. */
|
---|
219 | #define RTMEM_PROT_NONE 0
|
---|
220 | /** Read access. */
|
---|
221 | #define RTMEM_PROT_READ 1
|
---|
222 | /** Write access. */
|
---|
223 | #define RTMEM_PROT_WRITE 2
|
---|
224 | /** Execute access. */
|
---|
225 | #define RTMEM_PROT_EXEC 4
|
---|
226 | /** @} */
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Change the page level protection of a memory region.
|
---|
230 | *
|
---|
231 | * @returns iprt status code.
|
---|
232 | * @param pv Start of the region. Will be rounded down to nearest page boundary.
|
---|
233 | * @param cb Size of the region. Will be rounded up to the nearest page boundary.
|
---|
234 | * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
|
---|
235 | */
|
---|
236 | RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW;
|
---|
237 |
|
---|
238 |
|
---|
239 | #ifdef IN_RING0
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Allocates physical contiguous memory (below 4GB).
|
---|
243 | * The allocation is page aligned and the content is undefined.
|
---|
244 | *
|
---|
245 | * @returns Pointer to the memory block. This is page aligned.
|
---|
246 | * @param pPhys Where to store the physical address.
|
---|
247 | * @param cb The allocation size in bytes. This is always
|
---|
248 | * rounded up to PAGE_SIZE.
|
---|
249 | */
|
---|
250 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb) RT_NO_THROW;
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Frees memory allocated ysing RTMemContAlloc().
|
---|
254 | *
|
---|
255 | * @param pv Pointer to return from RTMemContAlloc().
|
---|
256 | * @param cb The cb parameter passed to RTMemContAlloc().
|
---|
257 | */
|
---|
258 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb) RT_NO_THROW;
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Copy memory from an user mode buffer into a kernel buffer.
|
---|
262 | *
|
---|
263 | * @retval VINF_SUCCESS on success.
|
---|
264 | * @retval VERR_ACCESS_DENIED on error.
|
---|
265 | *
|
---|
266 | * @param pvDst The kernel mode destination address.
|
---|
267 | * @param R3PtrDst The user mode source address.
|
---|
268 | * @param cb The number of bytes to copy.
|
---|
269 | */
|
---|
270 | RTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst, RTR3PTR R3PtrSrc, size_t cb);
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Copy memory from a kernel buffer into a user mode one.
|
---|
274 | *
|
---|
275 | * @retval VINF_SUCCESS on success.
|
---|
276 | * @retval VERR_ACCESS_DENIED on error.
|
---|
277 | *
|
---|
278 | * @param R3PtrDst The user mode destination address.
|
---|
279 | * @param pvSrc The kernel mode source address.
|
---|
280 | * @param cb The number of bytes to copy.
|
---|
281 | */
|
---|
282 | RTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb);
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Tests if the specified address is in the user addressable range.
|
---|
286 | *
|
---|
287 | * This function does not check whether the memory at that address is accessible
|
---|
288 | * or anything of that sort, only if the address it self is in the user mode
|
---|
289 | * range.
|
---|
290 | *
|
---|
291 | * @returns true if it's in the user addressable range. false if not.
|
---|
292 | * @param R3Ptr The user mode pointer to test.
|
---|
293 | *
|
---|
294 | * @remarks Some systems may have overlapping kernel and user address ranges.
|
---|
295 | * One prominent example of this is the x86 version of Mac OS X. Use
|
---|
296 | * RTR0MemAreKernelAndUserRangesDifferent() to check.
|
---|
297 | */
|
---|
298 | RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr);
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Tests if the specified address is in the kernel mode range.
|
---|
302 | *
|
---|
303 | * This function does not check whether the memory at that address is accessible
|
---|
304 | * or anything of that sort, only if the address it self is in the kernel mode
|
---|
305 | * range.
|
---|
306 | *
|
---|
307 | * @returns true if it's in the kernel range. false if not.
|
---|
308 | * @param pv The alleged kernel mode pointer.
|
---|
309 | *
|
---|
310 | * @remarks Some systems may have overlapping kernel and user address ranges.
|
---|
311 | * One prominent example of this is the x86 version of Mac OS X. Use
|
---|
312 | * RTR0MemAreKernelAndUserRangesDifferent() to check.
|
---|
313 | */
|
---|
314 | RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv);
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Are user mode and kernel mode address ranges distinctly different.
|
---|
318 | *
|
---|
319 | * This determins whether RTR0MemKernelIsValidAddr and RTR0MemUserIsValidAddr
|
---|
320 | * can be used for deciding whether some arbitrary address is a user mode or a
|
---|
321 | * kernel mode one.
|
---|
322 | *
|
---|
323 | * @returns true if they are, false if not.
|
---|
324 | */
|
---|
325 | RTR0DECL(bool) RTR0MemAreKernelAndUserRangesDifferent(void);
|
---|
326 |
|
---|
327 | #endif /* IN_RING0 */
|
---|
328 |
|
---|
329 |
|
---|
330 | /** @name Electrical Fence Version of some APIs.
|
---|
331 | * @{
|
---|
332 | */
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Same as RTMemTmpAlloc() except that it's fenced.
|
---|
336 | *
|
---|
337 | * @returns Pointer to the allocated memory.
|
---|
338 | * @returns NULL on failure.
|
---|
339 | * @param cb Size in bytes of the memory block to allocate.
|
---|
340 | */
|
---|
341 | RTDECL(void *) RTMemEfTmpAlloc(size_t cb) RT_NO_THROW;
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Same as RTMemTmpAllocZ() except that it's fenced.
|
---|
345 | *
|
---|
346 | * @returns Pointer to the allocated memory.
|
---|
347 | * @returns NULL on failure.
|
---|
348 | * @param cb Size in bytes of the memory block to allocate.
|
---|
349 | */
|
---|
350 | RTDECL(void *) RTMemEfTmpAllocZ(size_t cb) RT_NO_THROW;
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Same as RTMemTmpFree() except that it's for fenced memory.
|
---|
354 | *
|
---|
355 | * @param pv Pointer to memory block.
|
---|
356 | */
|
---|
357 | RTDECL(void) RTMemEfTmpFree(void *pv) RT_NO_THROW;
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Same as RTMemAlloc() except that it's fenced.
|
---|
361 | *
|
---|
362 | * @returns Pointer to the allocated memory. Free with RTMemEfFree().
|
---|
363 | * @returns NULL on failure.
|
---|
364 | * @param cb Size in bytes of the memory block to allocate.
|
---|
365 | */
|
---|
366 | RTDECL(void *) RTMemEfAlloc(size_t cb) RT_NO_THROW;
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Same as RTMemAllocZ() except that it's fenced.
|
---|
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 | */
|
---|
375 | RTDECL(void *) RTMemEfAllocZ(size_t cb) RT_NO_THROW;
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Same as RTMemRealloc() except that it's fenced.
|
---|
379 | *
|
---|
380 | * @returns Pointer to the allocated memory.
|
---|
381 | * @returns NULL on failure.
|
---|
382 | * @param pvOld The memory block to reallocate.
|
---|
383 | * @param cbNew The new block size (in bytes).
|
---|
384 | */
|
---|
385 | RTDECL(void *) RTMemEfRealloc(void *pvOld, size_t cbNew) RT_NO_THROW;
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Free memory allocated by any of the RTMemEf* allocators.
|
---|
389 | *
|
---|
390 | * @param pv Pointer to memory block.
|
---|
391 | */
|
---|
392 | RTDECL(void) RTMemEfFree(void *pv) RT_NO_THROW;
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Same as RTMemDup() except that it's fenced.
|
---|
396 | *
|
---|
397 | * @returns New heap block with the duplicate data.
|
---|
398 | * @returns NULL if we're out of memory.
|
---|
399 | * @param pvSrc The memory to duplicate.
|
---|
400 | * @param cb The amount of memory to duplicate.
|
---|
401 | */
|
---|
402 | RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb) RT_NO_THROW;
|
---|
403 |
|
---|
404 | /**
|
---|
405 | * Same as RTMemEfDupEx except that it's fenced.
|
---|
406 | *
|
---|
407 | * @returns New heap block with the duplicate data.
|
---|
408 | * @returns NULL if we're out of memory.
|
---|
409 | * @param pvSrc The memory to duplicate.
|
---|
410 | * @param cbSrc The amount of memory to duplicate.
|
---|
411 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
412 | */
|
---|
413 | RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW;
|
---|
414 |
|
---|
415 | /** @def RTMEM_WRAP_TO_EF_APIS
|
---|
416 | * Define RTMEM_WRAP_TO_EF_APIS to wrap RTMem APIs to RTMemEf APIs.
|
---|
417 | */
|
---|
418 | #ifdef RTMEM_WRAP_TO_EF_APIS
|
---|
419 | # define RTMemTmpAlloc RTMemEfTmpAlloc
|
---|
420 | # define RTMemTmpAllocZ RTMemEfTmpAllocZ
|
---|
421 | # define RTMemTmpFree RTMemEfTmpFree
|
---|
422 | # define RTMemAlloc RTMemEfAlloc
|
---|
423 | # define RTMemAllocZ RTMemEfAllocZ
|
---|
424 | # define RTMemRealloc RTMemEfRealloc
|
---|
425 | # define RTMemFree RTMemEfFree
|
---|
426 | # define RTMemDup RTMemEfDup
|
---|
427 | # define RTMemDupEx RTMemEfDupEx
|
---|
428 | #endif
|
---|
429 | #ifdef DOXYGEN_RUNNING
|
---|
430 | # define RTMEM_WRAP_TO_EF_APIS
|
---|
431 | #endif
|
---|
432 |
|
---|
433 | /** @} */
|
---|
434 |
|
---|
435 | RT_C_DECLS_END
|
---|
436 |
|
---|
437 |
|
---|
438 | #ifdef __cplusplus
|
---|
439 | # include <iprt/assert.h>
|
---|
440 |
|
---|
441 | /**
|
---|
442 | * Template function wrapping RTMemFree to get the correct Destruct
|
---|
443 | * signature for RTAutoRes.
|
---|
444 | *
|
---|
445 | * We can't use a more complex template here, because the g++ on RHEL 3
|
---|
446 | * chokes on it with an internal compiler error.
|
---|
447 | *
|
---|
448 | * @param T The data type that's being managed.
|
---|
449 | * @param aMem Pointer to the memory that should be free.
|
---|
450 | */
|
---|
451 | template <class T>
|
---|
452 | inline void RTMemAutoDestructor(T *aMem) RT_NO_THROW
|
---|
453 | {
|
---|
454 | RTMemFree(aMem);
|
---|
455 | }
|
---|
456 |
|
---|
457 |
|
---|
458 | /**
|
---|
459 | * RTMemAutoPtr allocator which uses RTMemTmpAlloc().
|
---|
460 | *
|
---|
461 | * @returns Allocated memory on success, NULL on failure.
|
---|
462 | * @param pvOld What to reallocate, shall always be NULL.
|
---|
463 | * @param cbNew The amount of memory to allocate (in bytes).
|
---|
464 | */
|
---|
465 | inline void *RTMemTmpAutoAllocator(void *pvOld, size_t cbNew) RT_NO_THROW
|
---|
466 | {
|
---|
467 | AssertReturn(!pvOld, NULL);
|
---|
468 | return RTMemTmpAlloc(cbNew);
|
---|
469 | }
|
---|
470 |
|
---|
471 |
|
---|
472 | /**
|
---|
473 | * Template function wrapping RTMemTmpFree to get the correct Destruct
|
---|
474 | * signature for RTAutoRes.
|
---|
475 | *
|
---|
476 | * We can't use a more complex template here, because the g++ on RHEL 3
|
---|
477 | * chokes on it with an internal compiler error.
|
---|
478 | *
|
---|
479 | * @param T The data type that's being managed.
|
---|
480 | * @param aMem Pointer to the memory that should be free.
|
---|
481 | */
|
---|
482 | template <class T>
|
---|
483 | inline void RTMemTmpAutoDestructor(T *aMem) RT_NO_THROW
|
---|
484 | {
|
---|
485 | RTMemTmpFree(aMem);
|
---|
486 | }
|
---|
487 |
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * Template function wrapping RTMemEfFree to get the correct Destruct
|
---|
491 | * signature for RTAutoRes.
|
---|
492 | *
|
---|
493 | * We can't use a more complex template here, because the g++ on RHEL 3
|
---|
494 | * chokes on it with an internal compiler error.
|
---|
495 | *
|
---|
496 | * @param T The data type that's being managed.
|
---|
497 | * @param aMem Pointer to the memory that should be free.
|
---|
498 | */
|
---|
499 | template <class T>
|
---|
500 | inline void RTMemEfAutoFree(T *aMem) RT_NO_THROW
|
---|
501 | {
|
---|
502 | RTMemEfFree(aMem);
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | /**
|
---|
507 | * Template function wrapping NULL to get the correct NilRes signature
|
---|
508 | * for RTAutoRes.
|
---|
509 | *
|
---|
510 | * @param T The data type that's being managed.
|
---|
511 | * @returns NULL with the right type.
|
---|
512 | */
|
---|
513 | template <class T>
|
---|
514 | inline T * RTMemAutoNil(void) RT_NO_THROW
|
---|
515 | {
|
---|
516 | return (T *)(NULL);
|
---|
517 | }
|
---|
518 |
|
---|
519 |
|
---|
520 | /**
|
---|
521 | * An auto pointer-type template class for managing memory allocating
|
---|
522 | * via C APIs like RTMem (the default).
|
---|
523 | *
|
---|
524 | * The main purpose of this class is to automatically free memory that
|
---|
525 | * isn't explicitly used (release()'ed) when the object goes out of scope.
|
---|
526 | *
|
---|
527 | * As an additional service it can also make the allocations and
|
---|
528 | * reallocations for you if you like, but it can also take of memory
|
---|
529 | * you hand it.
|
---|
530 | *
|
---|
531 | * @param T The data type to manage allocations for.
|
---|
532 | * @param Destruct The function to be used to free the resource.
|
---|
533 | * This will default to RTMemFree.
|
---|
534 | * @param Allocator The function to be used to allocate or reallocate
|
---|
535 | * the managed memory.
|
---|
536 | * This is standard realloc() like stuff, so it's possible
|
---|
537 | * to support simple allocation without actually having
|
---|
538 | * to support reallocating memory if that's a problem.
|
---|
539 | * This will default to RTMemRealloc.
|
---|
540 | */
|
---|
541 | template <class T, void Destruct(T *) = RTMemAutoDestructor<T>, void *Allocator(void *, size_t) = RTMemRealloc >
|
---|
542 | class RTMemAutoPtr
|
---|
543 | : public RTAutoRes<T *, Destruct, RTMemAutoNil<T> >
|
---|
544 | {
|
---|
545 | public:
|
---|
546 | /**
|
---|
547 | * Constructor.
|
---|
548 | *
|
---|
549 | * @param aPtr Memory pointer to manage. Defaults to NULL.
|
---|
550 | */
|
---|
551 | RTMemAutoPtr(T *aPtr = NULL)
|
---|
552 | : RTAutoRes<T *, Destruct, RTMemAutoNil<T> >(aPtr)
|
---|
553 | {
|
---|
554 | }
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * Constructor that allocates memory.
|
---|
558 | *
|
---|
559 | * @param a_cElements The number of elements (of the data type) to allocate.
|
---|
560 | * @param a_fZeroed Whether the memory should be memset with zeros after
|
---|
561 | * the allocation. Defaults to false.
|
---|
562 | */
|
---|
563 | RTMemAutoPtr(size_t a_cElements, bool a_fZeroed = false)
|
---|
564 | : RTAutoRes<T *, Destruct, RTMemAutoNil<T> >((T *)Allocator(NULL, a_cElements * sizeof(T)))
|
---|
565 | {
|
---|
566 | if (a_fZeroed && RT_LIKELY(this->get() != NULL))
|
---|
567 | memset(this->get(), '\0', a_cElements * sizeof(T));
|
---|
568 | }
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Free current memory and start managing aPtr.
|
---|
572 | *
|
---|
573 | * @param aPtr Memory pointer to manage.
|
---|
574 | */
|
---|
575 | RTMemAutoPtr &operator=(T *aPtr)
|
---|
576 | {
|
---|
577 | this->RTAutoRes<T *, Destruct, RTMemAutoNil<T> >::operator=(aPtr);
|
---|
578 | return *this;
|
---|
579 | }
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * Dereference with * operator.
|
---|
583 | */
|
---|
584 | T &operator*()
|
---|
585 | {
|
---|
586 | return *this->get();
|
---|
587 | }
|
---|
588 |
|
---|
589 | /**
|
---|
590 | * Dereference with -> operator.
|
---|
591 | */
|
---|
592 | T *operator->()
|
---|
593 | {
|
---|
594 | return this->get();
|
---|
595 | }
|
---|
596 |
|
---|
597 | /**
|
---|
598 | * Accessed with the subscript operator ([]).
|
---|
599 | *
|
---|
600 | * @returns Reference to the element.
|
---|
601 | * @param a_i The element to access.
|
---|
602 | */
|
---|
603 | T &operator[](size_t a_i)
|
---|
604 | {
|
---|
605 | return this->get()[a_i];
|
---|
606 | }
|
---|
607 |
|
---|
608 | /**
|
---|
609 | * Allocates memory and start manage it.
|
---|
610 | *
|
---|
611 | * Any previously managed memory will be freed before making
|
---|
612 | * the new allocation.
|
---|
613 | *
|
---|
614 | * @returns Success indicator.
|
---|
615 | * @retval true if the new allocation succeeds.
|
---|
616 | * @retval false on failure, no memory is associated with the object.
|
---|
617 | *
|
---|
618 | * @param a_cElements The number of elements (of the data type) to allocate.
|
---|
619 | * This defaults to 1.
|
---|
620 | * @param a_fZeroed Whether the memory should be memset with zeros after
|
---|
621 | * the allocation. Defaults to false.
|
---|
622 | */
|
---|
623 | bool alloc(size_t a_cElements = 1, bool a_fZeroed = false)
|
---|
624 | {
|
---|
625 | this->reset(NULL);
|
---|
626 | T *pNewMem = (T *)Allocator(NULL, a_cElements * sizeof(T));
|
---|
627 | if (a_fZeroed && RT_LIKELY(pNewMem != NULL))
|
---|
628 | memset(pNewMem, '\0', a_cElements * sizeof(T));
|
---|
629 | this->reset(pNewMem);
|
---|
630 | return pNewMem != NULL;
|
---|
631 | }
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * Reallocate or allocates the memory resource.
|
---|
635 | *
|
---|
636 | * Free the old value if allocation fails.
|
---|
637 | *
|
---|
638 | * The content of any additional memory that was allocated is
|
---|
639 | * undefined when using the default allocator.
|
---|
640 | *
|
---|
641 | * @returns Success indicator.
|
---|
642 | * @retval true if the new allocation succeeds.
|
---|
643 | * @retval false on failure, no memory is associated with the object.
|
---|
644 | *
|
---|
645 | * @param cElements The new number of elements (of the data type) to allocate.
|
---|
646 | * The size of the allocation is the number of elements times
|
---|
647 | * the size of the data type - this is currently what's passed
|
---|
648 | * down to the Allocator.
|
---|
649 | * This defaults to 1.
|
---|
650 | */
|
---|
651 | bool realloc(size_t a_cElements = 1)
|
---|
652 | {
|
---|
653 | T *aNewValue = (T *)Allocator(this->get(), a_cElements * sizeof(T));
|
---|
654 | if (RT_LIKELY(aNewValue != NULL))
|
---|
655 | this->release();
|
---|
656 | /* We want this both if aNewValue is non-NULL and if it is NULL. */
|
---|
657 | this->reset(aNewValue);
|
---|
658 | return aNewValue != NULL;
|
---|
659 | }
|
---|
660 | };
|
---|
661 |
|
---|
662 |
|
---|
663 | #endif /* __cplusplus */
|
---|
664 |
|
---|
665 |
|
---|
666 | /** @} */
|
---|
667 |
|
---|
668 |
|
---|
669 | #endif
|
---|
670 |
|
---|