1 | /* $Id: alloc.cpp 32125 2010-08-31 10:16:05Z 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/alloc.h>
|
---|
42 | #include <iprt/asm.h>
|
---|
43 | #include <iprt/assert.h>
|
---|
44 | #ifdef RTMEMALLOC_USE_TRACKER
|
---|
45 | # include <iprt/memtracker.h>
|
---|
46 | #endif
|
---|
47 | #include <iprt/param.h>
|
---|
48 | #include <iprt/string.h>
|
---|
49 |
|
---|
50 | #include <stdlib.h>
|
---|
51 |
|
---|
52 | #undef RTMemTmpAlloc
|
---|
53 | #undef RTMemTmpAllocTag
|
---|
54 | #undef RTMemTmpAllocZ
|
---|
55 | #undef RTMemTmpAllocZTag
|
---|
56 | #undef RTMemTmpFree
|
---|
57 | #undef RTMemAlloc
|
---|
58 | #undef RTMemAllocTag
|
---|
59 | #undef RTMemAllocZ
|
---|
60 | #undef RTMemAllocZTag
|
---|
61 | #undef RTMemAllocVar
|
---|
62 | #undef RTMemAllocVarTag
|
---|
63 | #undef RTMemAllocZVar
|
---|
64 | #undef RTMemAllocZVarTag
|
---|
65 | #undef RTMemRealloc
|
---|
66 | #undef RTMemReallocTag
|
---|
67 | #undef RTMemFree
|
---|
68 | #undef RTMemDup
|
---|
69 | #undef RTMemDupTag
|
---|
70 | #undef RTMemDupEx
|
---|
71 | #undef RTMemDupExTag
|
---|
72 |
|
---|
73 |
|
---|
74 | RTDECL(void *) RTMemTmpAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
75 | {
|
---|
76 | return RTMemAllocTag(cb, pszTag);
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | RTDECL(void *) RTMemTmpAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
81 | {
|
---|
82 | return RTMemAllocZTag(cb, pszTag);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW
|
---|
87 | {
|
---|
88 | RTMemFree(pv);
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(void *) RTMemAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
93 | {
|
---|
94 | #ifdef RTALLOC_USE_EFENCE
|
---|
95 | void *pv = rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
96 |
|
---|
97 | #else /* !RTALLOC_USE_EFENCE */
|
---|
98 |
|
---|
99 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
100 | # ifdef RTMEMALLOC_USE_TRACKER
|
---|
101 | void *pv = RTMemTrackerHdrAlloc(malloc(cb + sizeof(RTMEMTRACKERHDR)), cb, pszTag, RTMEMTRACKERMETHOD_ALLOC);
|
---|
102 | # else
|
---|
103 | void *pv = malloc(cb);
|
---|
104 | # endif
|
---|
105 | AssertMsg(pv, ("malloc(%#zx) failed!!!\n", cb));
|
---|
106 | AssertMsg( cb < RTMEM_ALIGNMENT
|
---|
107 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
108 | || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
109 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
110 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
111 | return pv;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | RTDECL(void *) RTMemAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
116 | {
|
---|
117 | #ifdef RTALLOC_USE_EFENCE
|
---|
118 | void *pv = rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
119 |
|
---|
120 | #else /* !RTALLOC_USE_EFENCE */
|
---|
121 |
|
---|
122 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
123 |
|
---|
124 | # ifdef RTMEMALLOC_USE_TRACKER
|
---|
125 | void *pv = RTMemTrackerHdrAlloc(calloc(1, cb + sizeof(RTMEMTRACKERHDR)), cb, pszTag, RTMEMTRACKERMETHOD_ALLOCZ);
|
---|
126 | #else
|
---|
127 | void *pv = calloc(1, cb);
|
---|
128 | #endif
|
---|
129 | AssertMsg(pv, ("calloc(1,%#zx) failed!!!\n", cb));
|
---|
130 | AssertMsg( cb < RTMEM_ALIGNMENT
|
---|
131 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
132 | || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
133 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
134 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
135 | return pv;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | RTDECL(void *) RTMemAllocVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW
|
---|
140 | {
|
---|
141 | size_t cbAligned;
|
---|
142 | if (cbUnaligned >= 16)
|
---|
143 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
144 | else
|
---|
145 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
146 | #ifdef RTALLOC_USE_EFENCE
|
---|
147 | void *pv = rtR3MemAlloc("AllocVar", RTMEMTYPE_RTMEMALLOC, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
148 | #else
|
---|
149 | void *pv = RTMemAllocTag(cbAligned, pszTag);
|
---|
150 | #endif
|
---|
151 | return pv;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | RTDECL(void *) RTMemAllocZVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW
|
---|
156 | {
|
---|
157 | size_t cbAligned;
|
---|
158 | if (cbUnaligned >= 16)
|
---|
159 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
160 | else
|
---|
161 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
162 | #ifdef RTALLOC_USE_EFENCE
|
---|
163 | void *pv = rtR3MemAlloc("AllocZVar", RTMEMTYPE_RTMEMALLOCZ, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
164 | #else
|
---|
165 | void *pv = RTMemAllocZTag(cbAligned, pszTag);
|
---|
166 | #endif
|
---|
167 | return pv;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW
|
---|
172 | {
|
---|
173 | #ifdef RTALLOC_USE_EFENCE
|
---|
174 | void *pv = rtR3MemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
175 |
|
---|
176 | #else /* !RTALLOC_USE_EFENCE */
|
---|
177 |
|
---|
178 | # ifdef RTALLOC_USE_TRACKER
|
---|
179 | void *pv;
|
---|
180 | if (!pvOld)
|
---|
181 | {
|
---|
182 | if (cbNew)
|
---|
183 | pv = RTMemTrackerHdrAlloc(realloc(pvOld, cbNew + sizeof(RTMEMTRACKERHDR)), cbNew,
|
---|
184 | pszTag, RTMEMTRACKERMETHOD_REALLOC);
|
---|
185 | else
|
---|
186 | pv = NULL;
|
---|
187 | }
|
---|
188 | else
|
---|
189 | {
|
---|
190 | RTMemTrackerHdrReallocPrep(pvOld, 0, pszTag, RTMEMTRACKERMETHOD_REALLOC);
|
---|
191 | pv = RTMemTrackerHdrRealloc(realloc(pvOld, cbNew + sizeof(RTMEMTRACKERHDR)), cbNew, pvOld,
|
---|
192 | pszTag, RTMEMTRACKERMETHOD_REALLOC);
|
---|
193 | }
|
---|
194 | # else
|
---|
195 | void *pv = realloc(pvOld, cbNew);
|
---|
196 | # endif
|
---|
197 | AssertMsg(pv || !cbNew, ("realloc(%p, %#zx) failed!!!\n", pvOld, cbNew));
|
---|
198 | AssertMsg( cbNew < RTMEM_ALIGNMENT
|
---|
199 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
200 | || ( (cbNew & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
201 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
202 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
203 | return pv;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | RTDECL(void) RTMemFree(void *pv) RT_NO_THROW
|
---|
208 | {
|
---|
209 | if (pv)
|
---|
210 | #ifdef RTALLOC_USE_EFENCE
|
---|
211 | rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ASMReturnAddress(), NULL, 0, NULL);
|
---|
212 | #else
|
---|
213 | # ifdef RTALLOC_USE_TRACKER
|
---|
214 | pv = RTMemTrackerHdrFree(pv, 0, NULL, RTMEMTRACKERMETHOD_FREE);
|
---|
215 | # endif
|
---|
216 | free(pv);
|
---|
217 | #endif
|
---|
218 | }
|
---|
219 |
|
---|