|
1 /* |
|
2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
|
3 * Copyright (C) 2007-2009 Torch Mobile, Inc. All rights reserved |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Library General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Library General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Library General Public License |
|
16 * along with this library; see the file COPYING.LIB. If not, write to |
|
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
18 * Boston, MA 02110-1301, USA. |
|
19 * |
|
20 */ |
|
21 |
|
22 #ifndef FastMallocWince_h |
|
23 #define FastMallocWince_h |
|
24 |
|
25 #include <new.h> |
|
26 |
|
27 #ifdef __cplusplus |
|
28 #include <new> |
|
29 #include "MemoryManager.h" |
|
30 extern "C" { |
|
31 #endif |
|
32 |
|
33 void* fastMalloc(size_t n); |
|
34 void* fastCalloc(size_t n_elements, size_t element_size); |
|
35 void fastFree(void* p); |
|
36 void* fastRealloc(void* p, size_t n); |
|
37 void* fastZeroedMalloc(size_t n); |
|
38 // These functions return 0 if an allocation fails. |
|
39 void* tryFastMalloc(size_t n); |
|
40 void* tryFastZeroedMalloc(size_t n); |
|
41 void* tryFastCalloc(size_t n_elements, size_t element_size); |
|
42 void* tryFastRealloc(void* p, size_t n); |
|
43 char* fastStrDup(const char*); |
|
44 |
|
45 #ifndef NDEBUG |
|
46 void fastMallocForbid(); |
|
47 void fastMallocAllow(); |
|
48 #endif |
|
49 |
|
50 #if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC |
|
51 |
|
52 #define malloc(n) fastMalloc(n) |
|
53 #define calloc(n_elements, element_size) fastCalloc(n_elements, element_size) |
|
54 #define realloc(p, n) fastRealloc(p, n) |
|
55 #define free(p) fastFree(p) |
|
56 #define strdup(p) fastStrDup(p) |
|
57 |
|
58 #else |
|
59 |
|
60 #define strdup(p) _strdup(p) |
|
61 |
|
62 #endif |
|
63 |
|
64 #ifdef __cplusplus |
|
65 } |
|
66 #endif |
|
67 |
|
68 #ifdef __cplusplus |
|
69 #if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC |
|
70 static inline void* __cdecl operator new(size_t s) { return fastMalloc(s); } |
|
71 static inline void __cdecl operator delete(void* p) { fastFree(p); } |
|
72 static inline void* __cdecl operator new[](size_t s) { return fastMalloc(s); } |
|
73 static inline void __cdecl operator delete[](void* p) { fastFree(p); } |
|
74 static inline void* operator new(size_t s, const std::nothrow_t&) throw() { return fastMalloc(s); } |
|
75 static inline void operator delete(void* p, const std::nothrow_t&) throw() { fastFree(p); } |
|
76 static inline void* operator new[](size_t s, const std::nothrow_t&) throw() { return fastMalloc(s); } |
|
77 static inline void operator delete[](void* p, const std::nothrow_t&) throw() { fastFree(p); } |
|
78 #endif |
|
79 |
|
80 namespace WTF { |
|
81 // This defines a type which holds an unsigned integer and is the same |
|
82 // size as the minimally aligned memory allocation. |
|
83 typedef unsigned long long AllocAlignmentInteger; |
|
84 |
|
85 namespace Internal { |
|
86 enum AllocType { // Start with an unusual number instead of zero, because zero is common. |
|
87 AllocTypeMalloc = 0x375d6750, // Encompasses fastMalloc, fastZeroedMalloc, fastCalloc, fastRealloc. |
|
88 AllocTypeClassNew, // Encompasses class operator new from FastAllocBase. |
|
89 AllocTypeClassNewArray, // Encompasses class operator new[] from FastAllocBase. |
|
90 AllocTypeFastNew, // Encompasses fastNew. |
|
91 AllocTypeFastNewArray, // Encompasses fastNewArray. |
|
92 AllocTypeNew, // Encompasses global operator new. |
|
93 AllocTypeNewArray // Encompasses global operator new[]. |
|
94 }; |
|
95 } |
|
96 |
|
97 |
|
98 #if ENABLE(FAST_MALLOC_MATCH_VALIDATION) |
|
99 |
|
100 // Malloc validation is a scheme whereby a tag is attached to an |
|
101 // allocation which identifies how it was originally allocated. |
|
102 // This allows us to verify that the freeing operation matches the |
|
103 // allocation operation. If memory is allocated with operator new[] |
|
104 // but freed with free or delete, this system would detect that. |
|
105 // In the implementation here, the tag is an integer prepended to |
|
106 // the allocation memory which is assigned one of the AllocType |
|
107 // enumeration values. An alternative implementation of this |
|
108 // scheme could store the tag somewhere else or ignore it. |
|
109 // Users of FastMalloc don't need to know or care how this tagging |
|
110 // is implemented. |
|
111 |
|
112 namespace Internal { |
|
113 |
|
114 // Return the AllocType tag associated with the allocated block p. |
|
115 inline AllocType fastMallocMatchValidationType(const void* p) |
|
116 { |
|
117 const AllocAlignmentInteger* type = static_cast<const AllocAlignmentInteger*>(p) - 1; |
|
118 return static_cast<AllocType>(*type); |
|
119 } |
|
120 |
|
121 // Return the address of the AllocType tag associated with the allocated block p. |
|
122 inline AllocAlignmentInteger* fastMallocMatchValidationValue(void* p) |
|
123 { |
|
124 return reinterpret_cast<AllocAlignmentInteger*>(static_cast<char*>(p) - sizeof(AllocAlignmentInteger)); |
|
125 } |
|
126 |
|
127 // Set the AllocType tag to be associaged with the allocated block p. |
|
128 inline void setFastMallocMatchValidationType(void* p, AllocType allocType) |
|
129 { |
|
130 AllocAlignmentInteger* type = static_cast<AllocAlignmentInteger*>(p) - 1; |
|
131 *type = static_cast<AllocAlignmentInteger>(allocType); |
|
132 } |
|
133 |
|
134 // Handle a detected alloc/free mismatch. By default this calls CRASH(). |
|
135 void fastMallocMatchFailed(void* p); |
|
136 |
|
137 } // namespace Internal |
|
138 |
|
139 // This is a higher level function which is used by FastMalloc-using code. |
|
140 inline void fastMallocMatchValidateMalloc(void* p, Internal::AllocType allocType) |
|
141 { |
|
142 if (!p) |
|
143 return; |
|
144 |
|
145 Internal::setFastMallocMatchValidationType(p, allocType); |
|
146 } |
|
147 |
|
148 // This is a higher level function which is used by FastMalloc-using code. |
|
149 inline void fastMallocMatchValidateFree(void* p, Internal::AllocType allocType) |
|
150 { |
|
151 if (!p) |
|
152 return; |
|
153 |
|
154 if (Internal::fastMallocMatchValidationType(p) != allocType) |
|
155 Internal::fastMallocMatchFailed(p); |
|
156 Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc); // Set it to this so that fastFree thinks it's OK. |
|
157 } |
|
158 |
|
159 #else |
|
160 |
|
161 inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType) |
|
162 { |
|
163 } |
|
164 |
|
165 inline void fastMallocMatchValidateFree(void*, Internal::AllocType) |
|
166 { |
|
167 } |
|
168 |
|
169 #endif |
|
170 |
|
171 } // namespace WTF |
|
172 |
|
173 #endif |
|
174 |
|
175 #endif // FastMallocWince_h |
|
176 |