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 | |
28 | namespace WTF { |
29 | |
30 | class TryMallocReturnValue { |
31 | public: |
32 | TryMallocReturnValue(void*); |
33 | TryMallocReturnValue(TryMallocReturnValue&&); |
34 | ~TryMallocReturnValue(); |
35 | template<typename T> bool getValue(T*&) WARN_UNUSED_RETURN; |
36 | private: |
37 | void operator=(TryMallocReturnValue&&) = delete; |
38 | mutable void* m_data; |
39 | }; |
40 | |
41 | // These functions call CRASH() if an allocation fails. |
42 | WTF_EXPORT_PRIVATE void* fastMalloc(size_t); |
43 | WTF_EXPORT_PRIVATE void* fastZeroedMalloc(size_t); |
44 | WTF_EXPORT_PRIVATE void* fastCalloc(size_t numElements, size_t elementSize); |
45 | WTF_EXPORT_PRIVATE void* fastRealloc(void*, size_t); |
46 | WTF_EXPORT_PRIVATE char* fastStrDup(const char*); |
47 | |
48 | WTF_EXPORT_PRIVATE TryMallocReturnValue tryFastMalloc(size_t); |
49 | TryMallocReturnValue tryFastZeroedMalloc(size_t); |
50 | WTF_EXPORT_PRIVATE TryMallocReturnValue tryFastCalloc(size_t numElements, size_t elementSize); |
51 | |
52 | WTF_EXPORT_PRIVATE void fastFree(void*); |
53 | |
54 | // Allocations from fastAlignedMalloc() must be freed using fastAlignedFree(). |
55 | WTF_EXPORT_PRIVATE void* fastAlignedMalloc(size_t alignment, size_t); |
56 | WTF_EXPORT_PRIVATE void fastAlignedFree(void*); |
57 | |
58 | WTF_EXPORT_PRIVATE size_t fastMallocSize(const void*); |
59 | |
60 | // FIXME: This is non-helpful; fastMallocGoodSize will be removed soon. |
61 | WTF_EXPORT_PRIVATE size_t fastMallocGoodSize(size_t); |
62 | |
63 | WTF_EXPORT_PRIVATE void releaseFastMallocFreeMemory(); |
64 | WTF_EXPORT_PRIVATE void releaseFastMallocFreeMemoryForThisThread(); |
65 | |
66 | struct FastMallocStatistics { |
67 | size_t reservedVMBytes; |
68 | size_t committedVMBytes; |
69 | size_t freeListBytes; |
70 | }; |
71 | WTF_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. |
75 | typedef unsigned long long AllocAlignmentInteger; |
76 | |
77 | inline TryMallocReturnValue::TryMallocReturnValue(void* data) |
78 | : m_data(data) |
79 | { |
80 | } |
81 | |
82 | inline TryMallocReturnValue::TryMallocReturnValue(TryMallocReturnValue&& source) |
83 | : m_data(source.m_data) |
84 | { |
85 | source.m_data = nullptr; |
86 | } |
87 | |
88 | inline TryMallocReturnValue::~TryMallocReturnValue() |
89 | { |
90 | ASSERT(!m_data); |
91 | } |
92 | |
93 | template<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 | |
102 | using WTF::fastCalloc; |
103 | using WTF::fastFree; |
104 | using WTF::fastMalloc; |
105 | using WTF::fastMallocGoodSize; |
106 | using WTF::fastMallocSize; |
107 | using WTF::fastRealloc; |
108 | using WTF::fastStrDup; |
109 | using WTF::fastZeroedMalloc; |
110 | using WTF::tryFastCalloc; |
111 | using WTF::tryFastMalloc; |
112 | using WTF::tryFastZeroedMalloc; |
113 | using WTF::fastAlignedMalloc; |
114 | using 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 \ |
127 | public: \ |
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 | } \ |
155 | private: \ |
156 | typedef int __thisIsHereToForceASemicolonAfterThisMacro |
157 | |
158 | #endif /* WTF_FastMalloc_h */ |
159 | |