Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / DefaultAllocator.h
blob48160e93b6778b05f91662effde83ccbd8acbdfb
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef WTF_DefaultAllocator_h
32 #define WTF_DefaultAllocator_h
34 // This is the allocator that is used for allocations that are not on the
35 // traced, garbage collected heap. It uses FastMalloc for collections,
36 // but uses the partition allocator for the backing store of the collections.
38 #include "wtf/Assertions.h"
39 #include "wtf/FastAllocBase.h"
40 #include "wtf/PartitionAlloc.h"
41 #include "wtf/Partitions.h"
43 #include <string.h>
45 namespace WTF {
47 class DefaultAllocatorDummyVisitor;
49 class WTF_EXPORT DefaultAllocator {
50 public:
51 typedef DefaultAllocatorDummyVisitor Visitor;
52 static const bool isGarbageCollected = false;
54 template<typename T>
55 static size_t quantizedSize(size_t count)
57 RELEASE_ASSERT(count <= kGenericMaxDirectMapped / sizeof(T));
58 return partitionAllocActualSize(Partitions::bufferPartition(), count * sizeof(T));
60 template <typename T>
61 static T* allocateVectorBacking(size_t size)
63 return reinterpret_cast<T*>(allocateBacking(size));
65 template <typename T>
66 static T* allocateExpandedVectorBacking(size_t size)
68 return reinterpret_cast<T*>(allocateBacking(size));
70 static void freeVectorBacking(void* address);
71 static inline bool expandVectorBacking(void*, size_t)
73 return false;
75 static inline bool shrinkVectorBacking(void* address, size_t quantizedCurrentSize, size_t quantizedShrunkSize)
77 // Optimization: if we're downsizing inside the same allocator bucket,
78 // we can skip reallocation.
79 return quantizedCurrentSize == quantizedShrunkSize;
81 template <typename T>
82 static T* allocateInlineVectorBacking(size_t size) { return allocateVectorBacking<T>(size); }
83 static inline void freeInlineVectorBacking(void* address) { freeVectorBacking(address); }
84 static inline bool expandInlineVectorBacking(void*, size_t) { return false; }
85 static inline bool shrinkInlineVectorBacking(void* address, size_t quantizedCurrentSize, size_t quantizedShrunkSize) { return shrinkVectorBacking(address, quantizedCurrentSize, quantizedShrunkSize); }
87 template <typename T, typename HashTable>
88 static T* allocateHashTableBacking(size_t size)
90 return reinterpret_cast<T*>(allocateBacking(size));
92 template <typename T, typename HashTable>
93 static T* allocateZeroedHashTableBacking(size_t size)
95 void* result = allocateBacking(size);
96 memset(result, 0, size);
97 return reinterpret_cast<T*>(result);
99 static void freeHashTableBacking(void* address);
101 template <typename Return, typename Metadata>
102 static Return malloc(size_t size)
104 return reinterpret_cast<Return>(fastMalloc(size));
107 static inline bool expandHashTableBacking(void*, size_t)
109 return false;
111 static void free(void* address)
113 fastFree(address);
115 template<typename T>
116 static void* newArray(size_t bytes)
118 return malloc<void*, void>(bytes);
120 static void
121 deleteArray(void* ptr)
123 free(ptr); // Not the system free, the one from this class.
126 static bool isAllocationAllowed() { return true; }
127 template<typename T>
128 static bool isHeapObjectAlive(T* object)
130 ASSERT_NOT_REACHED();
131 return false;
134 static void markNoTracing(...)
136 ASSERT_NOT_REACHED();
139 static void registerDelayedMarkNoTracing(...)
141 ASSERT_NOT_REACHED();
144 static void registerWeakMembers(...)
146 ASSERT_NOT_REACHED();
149 static void registerWeakTable(...)
151 ASSERT_NOT_REACHED();
154 #if ENABLE(ASSERT)
155 static bool weakTableRegistered(...)
157 ASSERT_NOT_REACHED();
158 return false;
160 #endif
162 template<typename T, typename Traits>
163 static void trace(...)
165 ASSERT_NOT_REACHED();
168 template<typename T>
169 struct OtherType {
170 typedef T* Type;
173 template<typename T>
174 static T& getOther(T* other)
176 return *other;
179 static void enterNoAllocationScope() { }
180 static void leaveNoAllocationScope() { }
181 static void enterGCForbiddenScope() { }
182 static void leaveGCForbiddenScope() { }
184 private:
185 static void* allocateBacking(size_t);
188 // The Windows compiler seems to be very eager to instantiate things it won't
189 // need, so unless we have this class we get compile errors.
190 class DefaultAllocatorDummyVisitor {
191 public:
192 template<typename T> inline bool isHeapObjectAlive(T obj)
194 ASSERT_NOT_REACHED();
195 return false;
199 } // namespace WTF
201 #define WTF_USE_ALLOCATOR(ClassName, Allocator) \
202 public: \
203 void* operator new(size_t size) \
205 return Allocator::template malloc<void*, ClassName>(size); \
207 void operator delete(void* p) { Allocator::free(p); } \
208 void* operator new[](size_t size) { return Allocator::template newArray<ClassName>(size); } \
209 void operator delete[](void* p) { Allocator::deleteArray(p); } \
210 void* operator new(size_t, NotNullTag, void* location) \
212 ASSERT(location); \
213 return location; \
215 private: \
216 typedef int __thisIsHereToForceASemicolonAfterThisMacro
218 using WTF::DefaultAllocator;
220 #endif // WTF_DefaultAllocator_h