OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
8 * | 8 * |
9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 { | 44 { |
45 } | 45 } |
46 TryMallocReturnValue(const TryMallocReturnValue& source) | 46 TryMallocReturnValue(const TryMallocReturnValue& source) |
47 : m_data(source.m_data) | 47 : m_data(source.m_data) |
48 { | 48 { |
49 source.m_data = 0; | 49 source.m_data = 0; |
50 } | 50 } |
51 ~TryMallocReturnValue() { ASSERT(!m_data); } | 51 ~TryMallocReturnValue() { ASSERT(!m_data); } |
52 template <typename T> bool getValue(T& data) WARN_UNUSED_RETURN; | 52 template <typename T> bool getValue(T& data) WARN_UNUSED_RETURN; |
53 template <typename T> operator PossiblyNull<T>() | 53 template <typename T> operator PossiblyNull<T>() |
54 { | 54 { |
55 T value; | 55 T value; |
56 getValue(value); | 56 getValue(value); |
57 return PossiblyNull<T>(value); | 57 return PossiblyNull<T>(value); |
58 } | 58 } |
59 private: | 59 private: |
60 mutable void* m_data; | 60 mutable void* m_data; |
61 }; | 61 }; |
62 | 62 |
63 template <typename T> bool TryMallocReturnValue::getValue(T& data) | 63 template <typename T> bool TryMallocReturnValue::getValue(T& data) |
64 { | 64 { |
65 union u { void* data; T target; } res; | 65 union u { void* data; T target; } res; |
66 res.data = m_data; | 66 res.data = m_data; |
67 data = res.target; | 67 data = res.target; |
68 bool returnValue = !!m_data; | 68 bool returnValue = !!m_data; |
69 m_data = 0; | 69 m_data = 0; |
70 return returnValue; | 70 return returnValue; |
71 } | 71 } |
72 | 72 |
73 WTF_EXPORT TryMallocReturnValue tryFastMalloc(size_t n); | 73 WTF_EXPORT TryMallocReturnValue tryFastMalloc(size_t n); |
74 WTF_EXPORT TryMallocReturnValue tryFastZeroedMalloc(size_t n); | 74 WTF_EXPORT TryMallocReturnValue tryFastZeroedMalloc(size_t n); |
75 WTF_EXPORT TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t elem
ent_size); | 75 WTF_EXPORT TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t elem
ent_size); |
76 WTF_EXPORT TryMallocReturnValue tryFastRealloc(void* p, size_t n); | 76 WTF_EXPORT TryMallocReturnValue tryFastRealloc(void* p, size_t n); |
77 | 77 |
78 WTF_EXPORT void fastFree(void*); | 78 WTF_EXPORT void fastFree(void*); |
79 | 79 |
80 #ifndef NDEBUG | 80 #ifndef NDEBUG |
81 WTF_EXPORT void fastMallocForbid(); | 81 WTF_EXPORT void fastMallocForbid(); |
82 WTF_EXPORT void fastMallocAllow(); | 82 WTF_EXPORT void fastMallocAllow(); |
83 #endif | 83 #endif |
84 | 84 |
85 WTF_EXPORT void releaseFastMallocFreeMemory(); | 85 WTF_EXPORT void releaseFastMallocFreeMemory(); |
86 | 86 |
87 struct FastMallocStatistics { | 87 struct FastMallocStatistics { |
88 size_t reservedVMBytes; | 88 size_t reservedVMBytes; |
89 size_t committedVMBytes; | 89 size_t committedVMBytes; |
90 size_t freeListBytes; | 90 size_t freeListBytes; |
91 }; | 91 }; |
92 WTF_EXPORT FastMallocStatistics fastMallocStatistics(); | 92 WTF_EXPORT FastMallocStatistics fastMallocStatistics(); |
93 | 93 |
94 // This defines a type which holds an unsigned integer and is the same | 94 // This defines a type which holds an unsigned integer and is the same |
95 // size as the minimally aligned memory allocation. | 95 // size as the minimally aligned memory allocation. |
96 typedef unsigned long long AllocAlignmentInteger; | 96 typedef unsigned long long AllocAlignmentInteger; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 // allocation operation. If memory is allocated with operator new[] | 131 // allocation operation. If memory is allocated with operator new[] |
132 // but freed with free or delete, this system would detect that. | 132 // but freed with free or delete, this system would detect that. |
133 // In the implementation here, the tag is an integer prepended to | 133 // In the implementation here, the tag is an integer prepended to |
134 // the allocation memory which is assigned one of the AllocType | 134 // the allocation memory which is assigned one of the AllocType |
135 // enumeration values. An alternative implementation of this | 135 // enumeration values. An alternative implementation of this |
136 // scheme could store the tag somewhere else or ignore it. | 136 // scheme could store the tag somewhere else or ignore it. |
137 // Users of FastMalloc don't need to know or care how this tagging | 137 // Users of FastMalloc don't need to know or care how this tagging |
138 // is implemented. | 138 // is implemented. |
139 | 139 |
140 namespace Internal { | 140 namespace Internal { |
141 | 141 |
142 // Handle a detected alloc/free mismatch. By default this calls CRASH(). | 142 // Handle a detected alloc/free mismatch. By default this calls CRASH(). |
143 void fastMallocMatchFailed(void* p); | 143 void fastMallocMatchFailed(void* p); |
144 | 144 |
145 inline ValidationHeader* fastMallocValidationHeader(void* p) | 145 inline ValidationHeader* fastMallocValidationHeader(void* p) |
146 { | 146 { |
147 return reinterpret_cast<ValidationHeader*>(static_cast<char*>(p) - s
izeof(ValidationHeader)); | 147 return reinterpret_cast<ValidationHeader*>(static_cast<char*>(p) - s
izeof(ValidationHeader)); |
148 } | 148 } |
149 | 149 |
150 inline ValidationTag* fastMallocValidationSuffix(void* p) | 150 inline ValidationTag* fastMallocValidationSuffix(void* p) |
151 { | 151 { |
152 ValidationHeader* header = fastMallocValidationHeader(p); | 152 ValidationHeader* header = fastMallocValidationHeader(p); |
153 if (header->m_prefix != static_cast<unsigned>(ValidationPrefix)) | 153 if (header->m_prefix != static_cast<unsigned>(ValidationPrefix)) |
154 fastMallocMatchFailed(p); | 154 fastMallocMatchFailed(p); |
155 | 155 |
156 return reinterpret_cast<ValidationTag*>(static_cast<char*>(p) + head
er->m_size); | 156 return reinterpret_cast<ValidationTag*>(static_cast<char*>(p) + head
er->m_size); |
157 } | 157 } |
158 | 158 |
159 // Return the AllocType tag associated with the allocated block p. | 159 // Return the AllocType tag associated with the allocated block p. |
160 inline AllocType fastMallocMatchValidationType(void* p) | 160 inline AllocType fastMallocMatchValidationType(void* p) |
161 { | 161 { |
162 return fastMallocValidationHeader(p)->m_type; | 162 return fastMallocValidationHeader(p)->m_type; |
163 } | 163 } |
164 | 164 |
165 // Set the AllocType tag to be associaged with the allocated block p. | 165 // Set the AllocType tag to be associaged with the allocated block p. |
(...skipping 11 matching lines...) Expand all Loading... |
177 return; | 177 return; |
178 | 178 |
179 Internal::setFastMallocMatchValidationType(p, allocType); | 179 Internal::setFastMallocMatchValidationType(p, allocType); |
180 } | 180 } |
181 | 181 |
182 // This is a higher level function which is used by FastMalloc-using code. | 182 // This is a higher level function which is used by FastMalloc-using code. |
183 inline void fastMallocMatchValidateFree(void* p, Internal::AllocType) | 183 inline void fastMallocMatchValidateFree(void* p, Internal::AllocType) |
184 { | 184 { |
185 if (!p) | 185 if (!p) |
186 return; | 186 return; |
187 | 187 |
188 Internal::ValidationHeader* header = Internal::fastMallocValidationHeade
r(p); | 188 Internal::ValidationHeader* header = Internal::fastMallocValidationHeade
r(p); |
189 if (header->m_prefix != static_cast<unsigned>(Internal::ValidationPrefix
)) | 189 if (header->m_prefix != static_cast<unsigned>(Internal::ValidationPrefix
)) |
190 Internal::fastMallocMatchFailed(p); | 190 Internal::fastMallocMatchFailed(p); |
191 | 191 |
192 if (*Internal::fastMallocValidationSuffix(p) != Internal::ValidationSuff
ix) | 192 if (*Internal::fastMallocValidationSuffix(p) != Internal::ValidationSuff
ix) |
193 Internal::fastMallocMatchFailed(p); | 193 Internal::fastMallocMatchFailed(p); |
194 | 194 |
195 Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc)
; // Set it to this so that fastFree thinks it's OK. | 195 Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc)
; // Set it to this so that fastFree thinks it's OK. |
196 } | 196 } |
197 | 197 |
198 inline void fastMallocValidate(void* p) | 198 inline void fastMallocValidate(void* p) |
199 { | 199 { |
200 if (!p) | 200 if (!p) |
201 return; | 201 return; |
202 | 202 |
203 Internal::ValidationHeader* header = Internal::fastMallocValidationHeade
r(p); | 203 Internal::ValidationHeader* header = Internal::fastMallocValidationHeade
r(p); |
204 if (header->m_prefix != static_cast<unsigned>(Internal::ValidationPrefix
)) | 204 if (header->m_prefix != static_cast<unsigned>(Internal::ValidationPrefix
)) |
205 Internal::fastMallocMatchFailed(p); | 205 Internal::fastMallocMatchFailed(p); |
206 | 206 |
207 if (*Internal::fastMallocValidationSuffix(p) != Internal::ValidationSuff
ix) | 207 if (*Internal::fastMallocValidationSuffix(p) != Internal::ValidationSuff
ix) |
208 Internal::fastMallocMatchFailed(p); | 208 Internal::fastMallocMatchFailed(p); |
209 } | 209 } |
210 | 210 |
211 #else | 211 #else |
212 | 212 |
213 inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType) | 213 inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType) |
214 { | 214 { |
215 } | 215 } |
216 | 216 |
(...skipping 10 matching lines...) Expand all Loading... |
227 using WTF::fastMalloc; | 227 using WTF::fastMalloc; |
228 using WTF::fastMallocGoodSize; | 228 using WTF::fastMallocGoodSize; |
229 using WTF::fastRealloc; | 229 using WTF::fastRealloc; |
230 using WTF::fastStrDup; | 230 using WTF::fastStrDup; |
231 using WTF::fastZeroedMalloc; | 231 using WTF::fastZeroedMalloc; |
232 using WTF::tryFastCalloc; | 232 using WTF::tryFastCalloc; |
233 using WTF::tryFastMalloc; | 233 using WTF::tryFastMalloc; |
234 using WTF::tryFastRealloc; | 234 using WTF::tryFastRealloc; |
235 using WTF::tryFastZeroedMalloc; | 235 using WTF::tryFastZeroedMalloc; |
236 | 236 |
237 #ifndef NDEBUG | 237 #ifndef NDEBUG |
238 using WTF::fastMallocForbid; | 238 using WTF::fastMallocForbid; |
239 using WTF::fastMallocAllow; | 239 using WTF::fastMallocAllow; |
240 #endif | 240 #endif |
241 | 241 |
242 #endif /* WTF_FastMalloc_h */ | 242 #endif /* WTF_FastMalloc_h */ |
OLD | NEW |