source: webkit/trunk/JavaScriptCore/wtf/FastMalloc.h@ 47092

Last change on this file since 47092 was 47092, checked in by [email protected], 16 years ago

Make it harder to misuse try* allocation routines
https://bugs.webkit.org/show_bug.cgi?id=27469

Reviewed by Gavin Barraclough

Jump through a few hoops to make it much harder to accidentally
miss null-checking of values returned by the try-* allocation
routines.

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
1/*
2 * Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#ifndef WTF_FastMalloc_h
22#define WTF_FastMalloc_h
23
24#include "Platform.h"
25#include "PossiblyNull.h"
26#include <stdlib.h>
27#include <new>
28
29namespace WTF {
30
31 // These functions call CRASH() if an allocation fails.
32 void* fastMalloc(size_t);
33 void* fastZeroedMalloc(size_t);
34 void* fastCalloc(size_t numElements, size_t elementSize);
35 void* fastRealloc(void*, size_t);
36
37 struct TryMallocReturnValue {
38 TryMallocReturnValue(void* data)
39 : m_data(data)
40 {
41 }
42 TryMallocReturnValue(const TryMallocReturnValue& source)
43 : m_data(source.m_data)
44 {
45 source.m_data = 0;
46 }
47 ~TryMallocReturnValue() { ASSERT(!m_data); }
48 template <typename T> bool getValue(T& data) WARN_UNUSED_RETURN;
49 template <typename T> operator PossiblyNull<T>()
50 {
51 T value;
52 getValue(value);
53 return PossiblyNull<T>(value);
54 }
55 private:
56 mutable void* m_data;
57 };
58
59 template <typename T> bool TryMallocReturnValue::getValue(T& data) {
60 union u { void* data; T target; } res;
61 res.data = m_data;
62 data = res.target;
63 bool returnValue = !!m_data;
64 m_data = 0;
65 return returnValue;
66 }
67
68 TryMallocReturnValue tryFastMalloc(size_t n);
69 TryMallocReturnValue tryFastZeroedMalloc(size_t n);
70 TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t element_size);
71 TryMallocReturnValue tryFastRealloc(void* p, size_t n);
72
73 void fastFree(void*);
74
75#ifndef NDEBUG
76 void fastMallocForbid();
77 void fastMallocAllow();
78#endif
79
80 void releaseFastMallocFreeMemory();
81
82 struct FastMallocStatistics {
83 size_t heapSize;
84 size_t freeSizeInHeap;
85 size_t freeSizeInCaches;
86 size_t returnedSize;
87 };
88 FastMallocStatistics fastMallocStatistics();
89
90 // This defines a type which holds an unsigned integer and is the same
91 // size as the minimally aligned memory allocation.
92 typedef unsigned long long AllocAlignmentInteger;
93
94 namespace Internal {
95 enum AllocType { // Start with an unusual number instead of zero, because zero is common.
96 AllocTypeMalloc = 0x375d6750, // Encompasses fastMalloc, fastZeroedMalloc, fastCalloc, fastRealloc.
97 AllocTypeClassNew, // Encompasses class operator new from FastAllocBase.
98 AllocTypeClassNewArray, // Encompasses class operator new[] from FastAllocBase.
99 AllocTypeFastNew, // Encompasses fastNew.
100 AllocTypeFastNewArray, // Encompasses fastNewArray.
101 AllocTypeNew, // Encompasses global operator new.
102 AllocTypeNewArray // Encompasses global operator new[].
103 };
104 }
105
106#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
107
108 // Malloc validation is a scheme whereby a tag is attached to an
109 // allocation which identifies how it was originally allocated.
110 // This allows us to verify that the freeing operation matches the
111 // allocation operation. If memory is allocated with operator new[]
112 // but freed with free or delete, this system would detect that.
113 // In the implementation here, the tag is an integer prepended to
114 // the allocation memory which is assigned one of the AllocType
115 // enumeration values. An alternative implementation of this
116 // scheme could store the tag somewhere else or ignore it.
117 // Users of FastMalloc don't need to know or care how this tagging
118 // is implemented.
119
120 namespace Internal {
121
122 // Return the AllocType tag associated with the allocated block p.
123 inline AllocType fastMallocMatchValidationType(const void* p)
124 {
125 const AllocAlignmentInteger* type = static_cast<const AllocAlignmentInteger*>(p) - 1;
126 return static_cast<AllocType>(*type);
127 }
128
129 // Return the address of the AllocType tag associated with the allocated block p.
130 inline AllocAlignmentInteger* fastMallocMatchValidationValue(void* p)
131 {
132 return reinterpret_cast<AllocAlignmentInteger*>(static_cast<char*>(p) - sizeof(AllocAlignmentInteger));
133 }
134
135 // Set the AllocType tag to be associaged with the allocated block p.
136 inline void setFastMallocMatchValidationType(void* p, AllocType allocType)
137 {
138 AllocAlignmentInteger* type = static_cast<AllocAlignmentInteger*>(p) - 1;
139 *type = static_cast<AllocAlignmentInteger>(allocType);
140 }
141
142 // Handle a detected alloc/free mismatch. By default this calls CRASH().
143 void fastMallocMatchFailed(void* p);
144
145 } // namespace Internal
146
147 // This is a higher level function which is used by FastMalloc-using code.
148 inline void fastMallocMatchValidateMalloc(void* p, Internal::AllocType allocType)
149 {
150 if (!p)
151 return;
152
153 Internal::setFastMallocMatchValidationType(p, allocType);
154 }
155
156 // This is a higher level function which is used by FastMalloc-using code.
157 inline void fastMallocMatchValidateFree(void* p, Internal::AllocType allocType)
158 {
159 if (!p)
160 return;
161
162 if (Internal::fastMallocMatchValidationType(p) != allocType)
163 Internal::fastMallocMatchFailed(p);
164 Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc); // Set it to this so that fastFree thinks it's OK.
165 }
166
167#else
168
169 inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType)
170 {
171 }
172
173 inline void fastMallocMatchValidateFree(void*, Internal::AllocType)
174 {
175 }
176
177#endif
178
179} // namespace WTF
180
181using WTF::fastMalloc;
182using WTF::fastZeroedMalloc;
183using WTF::fastCalloc;
184using WTF::fastRealloc;
185using WTF::tryFastMalloc;
186using WTF::tryFastZeroedMalloc;
187using WTF::tryFastCalloc;
188using WTF::tryFastRealloc;
189using WTF::fastFree;
190
191#ifndef NDEBUG
192using WTF::fastMallocForbid;
193using WTF::fastMallocAllow;
194#endif
195
196#if COMPILER(GCC) && PLATFORM(DARWIN)
197#define WTF_PRIVATE_INLINE __private_extern__ inline __attribute__((always_inline))
198#elif COMPILER(GCC)
199#define WTF_PRIVATE_INLINE inline __attribute__((always_inline))
200#elif COMPILER(MSVC)
201#define WTF_PRIVATE_INLINE __forceinline
202#else
203#define WTF_PRIVATE_INLINE inline
204#endif
205
206#if !defined(_CRTDBG_MAP_ALLOC) && !(defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC)
207
208// The nothrow functions here are actually not all that helpful, because fastMalloc will
209// call CRASH() rather than returning 0, and returning 0 is what nothrow is all about.
210// But since WebKit code never uses exceptions or nothrow at all, this is probably OK.
211// Long term we will adopt FastAllocBase.h everywhere, and and replace this with
212// debug-only code to make sure we don't use the system malloc via the default operator
213// new by accident.
214
215WTF_PRIVATE_INLINE void* operator new(size_t size) { return fastMalloc(size); }
216WTF_PRIVATE_INLINE void* operator new(size_t size, const std::nothrow_t&) throw() { return fastMalloc(size); }
217WTF_PRIVATE_INLINE void operator delete(void* p) { fastFree(p); }
218WTF_PRIVATE_INLINE void operator delete(void* p, const std::nothrow_t&) throw() { fastFree(p); }
219WTF_PRIVATE_INLINE void* operator new[](size_t size) { return fastMalloc(size); }
220WTF_PRIVATE_INLINE void* operator new[](size_t size, const std::nothrow_t&) throw() { return fastMalloc(size); }
221WTF_PRIVATE_INLINE void operator delete[](void* p) { fastFree(p); }
222WTF_PRIVATE_INLINE void operator delete[](void* p, const std::nothrow_t&) throw() { fastFree(p); }
223
224#endif
225
226#endif /* WTF_FastMalloc_h */
Note: See TracBrowser for help on using the repository browser.