1 | /* $Id: alloc.cpp 33269 2010-10-20 15:42:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. 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 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Defined Constants And Macros *
|
---|
30 | *******************************************************************************/
|
---|
31 | #if defined(RTMEM_WRAP_TO_EF_APIS) && !defined(RTMEM_NO_WRAP_TO_EF_APIS)
|
---|
32 | # undef RTMEM_WRAP_TO_EF_APIS
|
---|
33 | # define RTALLOC_USE_EFENCE 1
|
---|
34 | #endif
|
---|
35 |
|
---|
36 |
|
---|
37 | /*******************************************************************************
|
---|
38 | * Header Files *
|
---|
39 | *******************************************************************************/
|
---|
40 | #include "alloc-ef.h"
|
---|
41 | #include <iprt/mem.h>
|
---|
42 |
|
---|
43 | #include <iprt/asm.h>
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #ifdef RTMEMALLOC_USE_TRACKER
|
---|
46 | # include <iprt/memtracker.h>
|
---|
47 | #endif
|
---|
48 | #include <iprt/param.h>
|
---|
49 | #include <iprt/string.h>
|
---|
50 | #include "internal/mem.h"
|
---|
51 |
|
---|
52 | #include <stdlib.h>
|
---|
53 |
|
---|
54 | #undef RTMemTmpAlloc
|
---|
55 | #undef RTMemTmpAllocTag
|
---|
56 | #undef RTMemTmpAllocZ
|
---|
57 | #undef RTMemTmpAllocZTag
|
---|
58 | #undef RTMemTmpFree
|
---|
59 | #undef RTMemAlloc
|
---|
60 | #undef RTMemAllocTag
|
---|
61 | #undef RTMemAllocZ
|
---|
62 | #undef RTMemAllocZTag
|
---|
63 | #undef RTMemAllocVar
|
---|
64 | #undef RTMemAllocVarTag
|
---|
65 | #undef RTMemAllocZVar
|
---|
66 | #undef RTMemAllocZVarTag
|
---|
67 | #undef RTMemRealloc
|
---|
68 | #undef RTMemReallocTag
|
---|
69 | #undef RTMemFree
|
---|
70 | #undef RTMemDup
|
---|
71 | #undef RTMemDupTag
|
---|
72 | #undef RTMemDupEx
|
---|
73 | #undef RTMemDupExTag
|
---|
74 |
|
---|
75 |
|
---|
76 | RTDECL(void *) RTMemTmpAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
77 | {
|
---|
78 | return RTMemAllocTag(cb, pszTag);
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | RTDECL(void *) RTMemTmpAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
83 | {
|
---|
84 | return RTMemAllocZTag(cb, pszTag);
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW
|
---|
89 | {
|
---|
90 | RTMemFree(pv);
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | RTDECL(void *) RTMemAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
95 | {
|
---|
96 | #ifdef RTALLOC_USE_EFENCE
|
---|
97 | void *pv = rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
98 |
|
---|
99 | #else /* !RTALLOC_USE_EFENCE */
|
---|
100 |
|
---|
101 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
102 | # ifdef RTMEMALLOC_USE_TRACKER
|
---|
103 | void *pv = RTMemTrackerHdrAlloc(malloc(cb + sizeof(RTMEMTRACKERHDR)), cb, pszTag, RTMEMTRACKERMETHOD_ALLOC);
|
---|
104 | # else
|
---|
105 | void *pv = malloc(cb);
|
---|
106 | # endif
|
---|
107 | AssertMsg(pv, ("malloc(%#zx) failed!!!\n", cb));
|
---|
108 | AssertMsg( cb < RTMEM_ALIGNMENT
|
---|
109 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
110 | || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
111 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
112 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
113 | return pv;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | RTDECL(void *) RTMemAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
118 | {
|
---|
119 | #ifdef RTALLOC_USE_EFENCE
|
---|
120 | void *pv = rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
121 |
|
---|
122 | #else /* !RTALLOC_USE_EFENCE */
|
---|
123 |
|
---|
124 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
125 |
|
---|
126 | # ifdef RTMEMALLOC_USE_TRACKER
|
---|
127 | void *pv = RTMemTrackerHdrAlloc(calloc(1, cb + sizeof(RTMEMTRACKERHDR)), cb, pszTag, RTMEMTRACKERMETHOD_ALLOCZ);
|
---|
128 | #else
|
---|
129 | void *pv = calloc(1, cb);
|
---|
130 | #endif
|
---|
131 | AssertMsg(pv, ("calloc(1,%#zx) failed!!!\n", cb));
|
---|
132 | AssertMsg( cb < RTMEM_ALIGNMENT
|
---|
133 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
134 | || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
135 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
136 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
137 | return pv;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | RTDECL(void *) RTMemAllocVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW
|
---|
142 | {
|
---|
143 | size_t cbAligned;
|
---|
144 | if (cbUnaligned >= 16)
|
---|
145 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
146 | else
|
---|
147 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
148 | #ifdef RTALLOC_USE_EFENCE
|
---|
149 | void *pv = rtR3MemAlloc("AllocVar", RTMEMTYPE_RTMEMALLOC, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
150 | #else
|
---|
151 | void *pv = RTMemAllocTag(cbAligned, pszTag);
|
---|
152 | #endif
|
---|
153 | return pv;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | RTDECL(void *) RTMemAllocZVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW
|
---|
158 | {
|
---|
159 | size_t cbAligned;
|
---|
160 | if (cbUnaligned >= 16)
|
---|
161 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
162 | else
|
---|
163 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
164 | #ifdef RTALLOC_USE_EFENCE
|
---|
165 | void *pv = rtR3MemAlloc("AllocZVar", RTMEMTYPE_RTMEMALLOCZ, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
166 | #else
|
---|
167 | void *pv = RTMemAllocZTag(cbAligned, pszTag);
|
---|
168 | #endif
|
---|
169 | return pv;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW
|
---|
174 | {
|
---|
175 | #ifdef RTALLOC_USE_EFENCE
|
---|
176 | void *pv = rtR3MemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
177 |
|
---|
178 | #else /* !RTALLOC_USE_EFENCE */
|
---|
179 |
|
---|
180 | # ifdef RTALLOC_USE_TRACKER
|
---|
181 | void *pv;
|
---|
182 | if (!pvOld)
|
---|
183 | {
|
---|
184 | if (cbNew)
|
---|
185 | pv = RTMemTrackerHdrAlloc(realloc(pvOld, cbNew + sizeof(RTMEMTRACKERHDR)), cbNew,
|
---|
186 | pszTag, RTMEMTRACKERMETHOD_REALLOC);
|
---|
187 | else
|
---|
188 | pv = NULL;
|
---|
189 | }
|
---|
190 | else
|
---|
191 | {
|
---|
192 | RTMemTrackerHdrReallocPrep(pvOld, 0, pszTag, RTMEMTRACKERMETHOD_REALLOC);
|
---|
193 | pv = RTMemTrackerHdrRealloc(realloc(pvOld, cbNew + sizeof(RTMEMTRACKERHDR)), cbNew, pvOld,
|
---|
194 | pszTag, RTMEMTRACKERMETHOD_REALLOC);
|
---|
195 | }
|
---|
196 | # else
|
---|
197 | void *pv = realloc(pvOld, cbNew);
|
---|
198 | # endif
|
---|
199 | AssertMsg(pv || !cbNew, ("realloc(%p, %#zx) failed!!!\n", pvOld, cbNew));
|
---|
200 | AssertMsg( cbNew < RTMEM_ALIGNMENT
|
---|
201 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
202 | || ( (cbNew & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
203 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
204 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
205 | return pv;
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | RTDECL(void) RTMemFree(void *pv) RT_NO_THROW
|
---|
210 | {
|
---|
211 | if (pv)
|
---|
212 | #ifdef RTALLOC_USE_EFENCE
|
---|
213 | rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ASMReturnAddress(), NULL, 0, NULL);
|
---|
214 | #else
|
---|
215 | # ifdef RTALLOC_USE_TRACKER
|
---|
216 | pv = RTMemTrackerHdrFree(pv, 0, NULL, RTMEMTRACKERMETHOD_FREE);
|
---|
217 | # endif
|
---|
218 | free(pv);
|
---|
219 | #endif
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 |
|
---|
224 | DECLHIDDEN(void *) rtMemBaseAlloc(size_t cb)
|
---|
225 | {
|
---|
226 | Assert(cb > 0 && cb < _1M);
|
---|
227 | return malloc(cb);
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | DECLHIDDEN(void) rtMemBaseFree(void *pv)
|
---|
232 | {
|
---|
233 | free(pv);
|
---|
234 | }
|
---|
235 |
|
---|