VirtualBox

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

Last change on this file since 47006 was 46567, checked in by vboxsync, 12 years ago

RTMemAllocEx for ring-3.

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