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