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