VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/alloc.cpp@ 45733

Last change on this file since 45733 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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