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