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.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 | * 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 | * Defined Constants And Macros *
|
---|
34 | *******************************************************************************/
|
---|
35 | #ifdef RTMEM_WRAP_TO_EF_APIS
|
---|
36 | # undef RTMEM_WRAP_TO_EF_APIS
|
---|
37 | # define RTALLOC_USE_EFENCE 1
|
---|
38 | #endif
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Header Files *
|
---|
43 | *******************************************************************************/
|
---|
44 | #include "alloc-ef.h"
|
---|
45 | #include <iprt/alloc.h>
|
---|
46 | #include <iprt/assert.h>
|
---|
47 | #include <iprt/param.h>
|
---|
48 | #include <iprt/string.h>
|
---|
49 |
|
---|
50 | #include <stdlib.h>
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Allocates temporary memory.
|
---|
55 | *
|
---|
56 | * Temporary memory blocks are used for not too large memory blocks which
|
---|
57 | * are believed not to stick around for too long. Using this API instead
|
---|
58 | * of RTMemAlloc() not only gives the heap manager room for optimization
|
---|
59 | * but makes the code easier to read.
|
---|
60 | *
|
---|
61 | * @returns Pointer to the allocated memory.
|
---|
62 | * @returns NULL on failure.
|
---|
63 | * @param cb Size in bytes of the memory block to allocate.
|
---|
64 | */
|
---|
65 | RTDECL(void *) RTMemTmpAlloc(size_t cb) RT_NO_THROW
|
---|
66 | {
|
---|
67 | return RTMemAlloc(cb);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Allocates zero'ed temporary memory.
|
---|
73 | *
|
---|
74 | * Same as RTMemTmpAlloc() but the memory will be zero'ed.
|
---|
75 | *
|
---|
76 | * @returns Pointer to the allocated memory.
|
---|
77 | * @returns NULL on failure.
|
---|
78 | * @param cb Size in bytes of the memory block to allocate.
|
---|
79 | */
|
---|
80 | RTDECL(void *) RTMemTmpAllocZ(size_t cb) RT_NO_THROW
|
---|
81 | {
|
---|
82 | return RTMemAllocZ(cb);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Free temporary memory.
|
---|
88 | *
|
---|
89 | * @param pv Pointer to memory block.
|
---|
90 | */
|
---|
91 | RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW
|
---|
92 | {
|
---|
93 | RTMemFree(pv);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Allocates memory.
|
---|
99 | *
|
---|
100 | * @returns Pointer to the allocated memory.
|
---|
101 | * @returns NULL on failure.
|
---|
102 | * @param cb Size in bytes of the memory block to allocate.
|
---|
103 | */
|
---|
104 | RTDECL(void *) RTMemAlloc(size_t cb) RT_NO_THROW
|
---|
105 | {
|
---|
106 | #ifdef RTALLOC_USE_EFENCE
|
---|
107 | void *pv = rtMemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, ((void **)&cb)[-1], 0, NULL, NULL);
|
---|
108 |
|
---|
109 | #else /* !RTALLOC_USE_EFENCE */
|
---|
110 |
|
---|
111 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
112 | void *pv = malloc(cb);
|
---|
113 | AssertMsg(pv, ("malloc(%#zx) failed!!!\n", cb));
|
---|
114 | AssertMsg( cb < RTMEM_ALIGNMENT
|
---|
115 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
116 | || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
117 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
118 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
119 | return pv;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Allocates zero'ed memory.
|
---|
125 | *
|
---|
126 | * Instead of memset(pv, 0, sizeof()) use this when you want zero'ed
|
---|
127 | * memory. This keeps the code smaller and the heap can skip the memset
|
---|
128 | * in about 0.42% of the calls :-).
|
---|
129 | *
|
---|
130 | * @returns Pointer to the allocated memory.
|
---|
131 | * @returns NULL on failure.
|
---|
132 | * @param cb Size in bytes of the memory block to allocate.
|
---|
133 | */
|
---|
134 | RTDECL(void *) RTMemAllocZ(size_t cb) RT_NO_THROW
|
---|
135 | {
|
---|
136 | #ifdef RTALLOC_USE_EFENCE
|
---|
137 | void *pv = rtMemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, ((void **)&cb)[-1], 0, NULL, NULL);
|
---|
138 |
|
---|
139 | #else /* !RTALLOC_USE_EFENCE */
|
---|
140 |
|
---|
141 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
142 |
|
---|
143 | void *pv = calloc(1, cb);
|
---|
144 | AssertMsg(pv, ("calloc(1,%#zx) failed!!!\n", cb));
|
---|
145 | AssertMsg( cb < RTMEM_ALIGNMENT
|
---|
146 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
147 | || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
148 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
149 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
150 | return pv;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Reallocates memory.
|
---|
156 | *
|
---|
157 | * @returns Pointer to the allocated memory.
|
---|
158 | * @returns NULL on failure.
|
---|
159 | * @param pvOld The memory block to reallocate.
|
---|
160 | * @param cbNew The new block size (in bytes).
|
---|
161 | */
|
---|
162 | RTDECL(void *) RTMemRealloc(void *pvOld, size_t cbNew) RT_NO_THROW
|
---|
163 | {
|
---|
164 | #ifdef RTALLOC_USE_EFENCE
|
---|
165 | void *pv = rtMemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, ((void **)&pvOld)[-1], 0, NULL, NULL);
|
---|
166 |
|
---|
167 | #else /* !RTALLOC_USE_EFENCE */
|
---|
168 |
|
---|
169 | void *pv = realloc(pvOld, cbNew);
|
---|
170 | AssertMsg(pv && cbNew, ("realloc(%p, %#zx) failed!!!\n", pvOld, cbNew));
|
---|
171 | AssertMsg( cbNew < RTMEM_ALIGNMENT
|
---|
172 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
173 | || ( (cbNew & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
174 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
175 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
176 | return pv;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Free memory related to a virtual machine
|
---|
182 | *
|
---|
183 | * @param pv Pointer to memory block.
|
---|
184 | */
|
---|
185 | RTDECL(void) RTMemFree(void *pv) RT_NO_THROW
|
---|
186 | {
|
---|
187 | if (pv)
|
---|
188 | #ifdef RTALLOC_USE_EFENCE
|
---|
189 | rtMemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ((void **)&pv)[-1], 0, NULL, NULL);
|
---|
190 | #else
|
---|
191 | free(pv);
|
---|
192 | #endif
|
---|
193 | }
|
---|
194 |
|
---|