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

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

Minor style fixes in previous patch and changelog correction.

Reviewed by Mark Rowe

  • 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 {
61 union u { void* data; T target; } res;
62 res.data = m_data;
63 data = res.target;
64 bool returnValue = !!m_data;
65 m_data = 0;
66 return returnValue;
67 }
68
69 TryMallocReturnValue tryFastMalloc(size_t n);
70 TryMallocReturnValue tryFastZeroedMalloc(size_t n);
71 TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t element_size);
72 TryMallocReturnValue tryFastRealloc(void* p, size_t n);
73
74 void fastFree(void*);
75
76#ifndef NDEBUG
77 void fastMallocForbid();
78 void fastMallocAllow();
79#endif
80
81 void releaseFastMallocFreeMemory();
82
83 struct FastMallocStatistics {
84 size_t heapSize;
85 size_t freeSizeInHeap;
86 size_t freeSizeInCaches;
87 size_t returnedSize;
88 };
89 FastMallocStatistics fastMallocStatistics();
90
91 // This defines a type which holds an unsigned integer and is the same
92 // size as the minimally aligned memory allocation.
93 typedef unsigned long long AllocAlignmentInteger;
94
95 namespace Internal {
96 enum AllocType { // Start with an unusual number instead of zero, because zero is common.
97 AllocTypeMalloc = 0x375d6750, // Encompasses fastMalloc, fastZeroedMalloc, fastCalloc, fastRealloc.
98 AllocTypeClassNew, // Encompasses class operator new from FastAllocBase.
99 AllocTypeClassNewArray, // Encompasses class operator new[] from FastAllocBase.
100 AllocTypeFastNew, // Encompasses fastNew.
101 AllocTypeFastNewArray, // Encompasses fastNewArray.
102 AllocTypeNew, // Encompasses global operator new.
103 AllocTypeNewArray // Encompasses global operator new[].
104 };
105 }
106
107#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
108
109 // Malloc validation is a scheme whereby a tag is attached to an
110 // allocation which identifies how it was originally allocated.
111 // This allows us to verify that the freeing operation matches the
112 // allocation operation. If memory is allocated with operator new[]
113 // but freed with free or delete, this system would detect that.
114 // In the implementation here, the tag is an integer prepended to
115 // the allocation memory which is assigned one of the AllocType
116 // enumeration values. An alternative implementation of this
117 // scheme could store the tag somewhere else or ignore it.
118 // Users of FastMalloc don't need to know or care how this tagging
119 // is implemented.
120
121 namespace Internal {
122
123 // Return the AllocType tag associated with the allocated block p.
124 inline AllocType fastMallocMatchValidationType(const void* p)
125 {
126 const AllocAlignmentInteger* type = static_cast<const AllocAlignmentInteger*>(p) - 1;
127 return static_cast<AllocType>(*type);
128 }
129
130 // Return the address of the AllocType tag associated with the allocated block p.
131 inline AllocAlignmentInteger* fastMallocMatchValidationValue(void* p)
132 {
133 return reinterpret_cast<AllocAlignmentInteger*>(static_cast<char*>(p) - sizeof(AllocAlignmentInteger));
134 }
135
136 // Set the AllocType tag to be associaged with the allocated block p.
137 inline void setFastMallocMatchValidationType(void* p, AllocType allocType)
138 {
139 AllocAlignmentInteger* type = static_cast<AllocAlignmentInteger*>(p) - 1;
140 *type = static_cast<AllocAlignmentInteger>(allocType);
141 }
142
143 // Handle a detected alloc/free mismatch. By default this calls CRASH().
144 void fastMallocMatchFailed(void* p);
145
146 } // namespace Internal
147
148 // This is a higher level function which is used by FastMalloc-using code.
149 inline void fastMallocMatchValidateMalloc(void* p, Internal::AllocType allocType)
150 {
151 if (!p)
152 return;
153
154 Internal::setFastMallocMatchValidationType(p, allocType);
155 }
156
157 // This is a higher level function which is used by FastMalloc-using code.
158 inline void fastMallocMatchValidateFree(void* p, Internal::AllocType allocType)
159 {
160 if (!p)
161 return;
162
163 if (Internal::fastMallocMatchValidationType(p) != allocType)
164 Internal::fastMallocMatchFailed(p);
165 Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc); // Set it to this so that fastFree thinks it's OK.
166 }
167
168#else
169
170 inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType)
171 {
172 }
173
174 inline void fastMallocMatchValidateFree(void*, Internal::AllocType)
175 {
176 }
177
178#endif
179
180} // namespace WTF
181
182using WTF::fastMalloc;
183using WTF::fastZeroedMalloc;
184using WTF::fastCalloc;
185using WTF::fastRealloc;
186using WTF::tryFastMalloc;
187using WTF::tryFastZeroedMalloc;
188using WTF::tryFastCalloc;
189using WTF::tryFastRealloc;
190using WTF::fastFree;
191
192#ifndef NDEBUG
193using WTF::fastMallocForbid;
194using WTF::fastMallocAllow;
195#endif
196
197#if COMPILER(GCC) && PLATFORM(DARWIN)
198#define WTF_PRIVATE_INLINE __private_extern__ inline __attribute__((always_inline))
199#elif COMPILER(GCC)
200#define WTF_PRIVATE_INLINE inline __attribute__((always_inline))
201#elif COMPILER(MSVC)
202#define WTF_PRIVATE_INLINE __forceinline
203#else
204#define WTF_PRIVATE_INLINE inline
205#endif
206
207#if !defined(_CRTDBG_MAP_ALLOC) && !(defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC)
208
209// The nothrow functions here are actually not all that helpful, because fastMalloc will
210// call CRASH() rather than returning 0, and returning 0 is what nothrow is all about.
211// But since WebKit code never uses exceptions or nothrow at all, this is probably OK.
212// Long term we will adopt FastAllocBase.h everywhere, and and replace this with
213// debug-only code to make sure we don't use the system malloc via the default operator
214// new by accident.
215
216WTF_PRIVATE_INLINE void* operator new(size_t size) { return fastMalloc(size); }
217WTF_PRIVATE_INLINE void* operator new(size_t size, const std::nothrow_t&) throw() { return fastMalloc(size); }
218WTF_PRIVATE_INLINE void operator delete(void* p) { fastFree(p); }
219WTF_PRIVATE_INLINE void operator delete(void* p, const std::nothrow_t&) throw() { fastFree(p); }
220WTF_PRIVATE_INLINE void* operator new[](size_t size) { return fastMalloc(size); }
221WTF_PRIVATE_INLINE void* operator new[](size_t size, const std::nothrow_t&) throw() { return fastMalloc(size); }
222WTF_PRIVATE_INLINE void operator delete[](void* p) { fastFree(p); }
223WTF_PRIVATE_INLINE void operator delete[](void* p, const std::nothrow_t&) throw() { fastFree(p); }
224
225#endif
226
227#endif /* WTF_FastMalloc_h */
Note: See TracBrowser for help on using the repository browser.