1/*
2 * Copyright (C) 2005-2009, 2015 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 <new>
25#include <stdlib.h>
26#include <wtf/StdLibExtras.h>
27
28namespace WTF {
29
30class TryMallocReturnValue {
31public:
32 TryMallocReturnValue(void*);
33 TryMallocReturnValue(TryMallocReturnValue&&);
34 ~TryMallocReturnValue();
35 template<typename T> bool getValue(T*&) WARN_UNUSED_RETURN;
36private:
37 void operator=(TryMallocReturnValue&&) = delete;
38 mutable void* m_data;
39};
40
41// These functions call CRASH() if an allocation fails.
42WTF_EXPORT_PRIVATE void* fastMalloc(size_t);
43WTF_EXPORT_PRIVATE void* fastZeroedMalloc(size_t);
44WTF_EXPORT_PRIVATE void* fastCalloc(size_t numElements, size_t elementSize);
45WTF_EXPORT_PRIVATE void* fastRealloc(void*, size_t);
46WTF_EXPORT_PRIVATE char* fastStrDup(const char*);
47
48WTF_EXPORT_PRIVATE TryMallocReturnValue tryFastMalloc(size_t);
49TryMallocReturnValue tryFastZeroedMalloc(size_t);
50WTF_EXPORT_PRIVATE TryMallocReturnValue tryFastCalloc(size_t numElements, size_t elementSize);
51
52WTF_EXPORT_PRIVATE void fastFree(void*);
53
54// Allocations from fastAlignedMalloc() must be freed using fastAlignedFree().
55WTF_EXPORT_PRIVATE void* fastAlignedMalloc(size_t alignment, size_t);
56WTF_EXPORT_PRIVATE void fastAlignedFree(void*);
57
58WTF_EXPORT_PRIVATE size_t fastMallocSize(const void*);
59
60// FIXME: This is non-helpful; fastMallocGoodSize will be removed soon.
61WTF_EXPORT_PRIVATE size_t fastMallocGoodSize(size_t);
62
63WTF_EXPORT_PRIVATE void releaseFastMallocFreeMemory();
64WTF_EXPORT_PRIVATE void releaseFastMallocFreeMemoryForThisThread();
65
66struct FastMallocStatistics {
67 size_t reservedVMBytes;
68 size_t committedVMBytes;
69 size_t freeListBytes;
70};
71WTF_EXPORT_PRIVATE FastMallocStatistics fastMallocStatistics();
72
73// This defines a type which holds an unsigned integer and is the same
74// size as the minimally aligned memory allocation.
75typedef unsigned long long AllocAlignmentInteger;
76
77inline TryMallocReturnValue::TryMallocReturnValue(void* data)
78 : m_data(data)
79{
80}
81
82inline TryMallocReturnValue::TryMallocReturnValue(TryMallocReturnValue&& source)
83 : m_data(source.m_data)
84{
85 source.m_data = nullptr;
86}
87
88inline TryMallocReturnValue::~TryMallocReturnValue()
89{
90 ASSERT(!m_data);
91}
92
93template<typename T> inline bool TryMallocReturnValue::getValue(T*& data)
94{
95 data = static_cast<T*>(m_data);
96 m_data = nullptr;
97 return data;
98}
99
100} // namespace WTF
101
102using WTF::fastCalloc;
103using WTF::fastFree;
104using WTF::fastMalloc;
105using WTF::fastMallocGoodSize;
106using WTF::fastMallocSize;
107using WTF::fastRealloc;
108using WTF::fastStrDup;
109using WTF::fastZeroedMalloc;
110using WTF::tryFastCalloc;
111using WTF::tryFastMalloc;
112using WTF::tryFastZeroedMalloc;
113using WTF::fastAlignedMalloc;
114using WTF::fastAlignedFree;
115
116#if COMPILER(GCC_OR_CLANG) && OS(DARWIN)
117#define WTF_PRIVATE_INLINE __private_extern__ inline __attribute__((always_inline))
118#elif COMPILER(GCC_OR_CLANG)
119#define WTF_PRIVATE_INLINE inline __attribute__((always_inline))
120#elif COMPILER(MSVC)
121#define WTF_PRIVATE_INLINE __forceinline
122#else
123#define WTF_PRIVATE_INLINE inline
124#endif
125
126#define WTF_MAKE_FAST_ALLOCATED \
127public: \
128 void* operator new(size_t, void* p) { return p; } \
129 void* operator new[](size_t, void* p) { return p; } \
130 \
131 void* operator new(size_t size) \
132 { \
133 return ::WTF::fastMalloc(size); \
134 } \
135 \
136 void operator delete(void* p) \
137 { \
138 ::WTF::fastFree(p); \
139 } \
140 \
141 void* operator new[](size_t size) \
142 { \
143 return ::WTF::fastMalloc(size); \
144 } \
145 \
146 void operator delete[](void* p) \
147 { \
148 ::WTF::fastFree(p); \
149 } \
150 void* operator new(size_t, NotNullTag, void* location) \
151 { \
152 ASSERT(location); \
153 return location; \
154 } \
155private: \
156typedef int __thisIsHereToForceASemicolonAfterThisMacro
157
158#endif /* WTF_FastMalloc_h */
159