1 | /* $Id: alloc.cpp 28271 2010-04-13 19:29:42Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 |
|
---|
41 | #ifdef RTMEM_WRAP_TO_EF_APIS
|
---|
42 | # undef RTMemAllocVar
|
---|
43 | # undef RTMemAllocZVar
|
---|
44 | # undef RTMemDup
|
---|
45 | # undef RTMemDupEx
|
---|
46 | #endif
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Wrapper around RTMemAlloc for automatically aligning variable sized
|
---|
52 | * allocations so that the various electric fence heaps works correctly.
|
---|
53 | *
|
---|
54 | * @returns See RTMemAlloc.
|
---|
55 | * @param cbUnaligned The unaligned size.
|
---|
56 | */
|
---|
57 | RTDECL(void *) RTMemAllocVar(size_t cbUnaligned)
|
---|
58 | {
|
---|
59 | size_t cbAligned;
|
---|
60 | if (cbUnaligned >= 16)
|
---|
61 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
62 | else
|
---|
63 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
64 | return RTMemAlloc(cbAligned);
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Wrapper around RTMemAllocZ for automatically aligning variable sized
|
---|
70 | * allocations so that the various electric fence heaps works correctly.
|
---|
71 | *
|
---|
72 | * @returns See RTMemAllocZ.
|
---|
73 | * @param cbUnaligned The unaligned size.
|
---|
74 | */
|
---|
75 | RTDECL(void *) RTMemAllocZVar(size_t cbUnaligned)
|
---|
76 | {
|
---|
77 | size_t cbAligned;
|
---|
78 | if (cbUnaligned >= 16)
|
---|
79 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
80 | else
|
---|
81 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
82 | return RTMemAllocZ(cbAligned);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Duplicates a chunk of memory into a new heap block.
|
---|
88 | *
|
---|
89 | * @returns New heap block with the duplicate data.
|
---|
90 | * @returns NULL if we're out of memory.
|
---|
91 | * @param pvSrc The memory to duplicate.
|
---|
92 | * @param cb The amount of memory to duplicate.
|
---|
93 | */
|
---|
94 | RTDECL(void *) RTMemDup(const void *pvSrc, size_t cb) RT_NO_THROW
|
---|
95 | {
|
---|
96 | void *pvDst = RTMemAlloc(cb);
|
---|
97 | if (pvDst)
|
---|
98 | memcpy(pvDst, pvSrc, cb);
|
---|
99 | return pvDst;
|
---|
100 | }
|
---|
101 | RT_EXPORT_SYMBOL(RTMemDup);
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Duplicates a chunk of memory into a new heap block with some
|
---|
106 | * additional zeroed memory.
|
---|
107 | *
|
---|
108 | * @returns New heap block with the duplicate data.
|
---|
109 | * @returns NULL if we're out of memory.
|
---|
110 | * @param pvSrc The memory to duplicate.
|
---|
111 | * @param cbSrc The amount of memory to duplicate.
|
---|
112 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
113 | */
|
---|
114 | RTDECL(void *) RTMemDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW
|
---|
115 | {
|
---|
116 | void *pvDst = RTMemAlloc(cbSrc + cbExtra);
|
---|
117 | if (pvDst)
|
---|
118 | {
|
---|
119 | memcpy(pvDst, pvSrc, cbSrc);
|
---|
120 | memset((uint8_t *)pvDst + cbSrc, 0, cbExtra);
|
---|
121 | }
|
---|
122 | return pvDst;
|
---|
123 | }
|
---|
124 | RT_EXPORT_SYMBOL(RTMemDupEx);
|
---|
125 |
|
---|