Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: third_party/WebKit/Source/wtf/PartitionAllocator.h

Issue 1881983003: Move PartitionAlloc related things into wtf/allocator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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.
17 *
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.
29 */
30
31 #ifndef WTF_PartitionAllocator_h
32 #define WTF_PartitionAllocator_h
33
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.
37
38 #include "wtf/Allocator.h"
39 #include "wtf/Assertions.h"
40 #include "wtf/PartitionAlloc.h"
41 #include "wtf/Partitions.h"
42
43 #include <string.h>
44
45 namespace WTF {
46
47 class PartitionAllocatorDummyVisitor;
48
49 class WTF_EXPORT PartitionAllocator {
50 public:
51 typedef PartitionAllocatorDummyVisitor Visitor;
52 static const bool isGarbageCollected = false;
53
54 template<typename T>
55 static size_t quantizedSize(size_t count)
56 {
57 RELEASE_ASSERT(count <= kGenericMaxDirectMapped / sizeof(T));
58 return partitionAllocActualSize(Partitions::bufferPartition(), count * s izeof(T));
59 }
60 template <typename T>
61 static T* allocateVectorBacking(size_t size)
62 {
63 return reinterpret_cast<T*>(allocateBacking(size, WTF_HEAP_PROFILER_TYPE _NAME(T)));
64 }
65 template <typename T>
66 static T* allocateExpandedVectorBacking(size_t size)
67 {
68 return reinterpret_cast<T*>(allocateBacking(size, WTF_HEAP_PROFILER_TYPE _NAME(T)));
69 }
70 static void freeVectorBacking(void* address);
71 static inline bool expandVectorBacking(void*, size_t)
72 {
73 return false;
74 }
75 static inline bool shrinkVectorBacking(void* address, size_t quantizedCurren tSize, size_t quantizedShrunkSize)
76 {
77 // Optimization: if we're downsizing inside the same allocator bucket,
78 // we can skip reallocation.
79 return quantizedCurrentSize == quantizedShrunkSize;
80 }
81 template <typename T>
82 static T* allocateInlineVectorBacking(size_t size) { return allocateVectorBa cking<T>(size); }
83 static inline void freeInlineVectorBacking(void* address) { freeVectorBackin g(address); }
84 static inline bool expandInlineVectorBacking(void*, size_t) { return false; }
85 static inline bool shrinkInlineVectorBacking(void* address, size_t quantized CurrentSize, size_t quantizedShrunkSize) { return shrinkVectorBacking(address, q uantizedCurrentSize, quantizedShrunkSize); }
86
87 template <typename T, typename HashTable>
88 static T* allocateHashTableBacking(size_t size)
89 {
90 return reinterpret_cast<T*>(allocateBacking(size, WTF_HEAP_PROFILER_TYPE _NAME(T)));
91 }
92 template <typename T, typename HashTable>
93 static T* allocateZeroedHashTableBacking(size_t size)
94 {
95 void* result = allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T));
96 memset(result, 0, size);
97 return reinterpret_cast<T*>(result);
98 }
99 static void freeHashTableBacking(void* address);
100
101 template <typename Return, typename Metadata>
102 static Return malloc(size_t size, const char* typeName)
103 {
104 return reinterpret_cast<Return>(Partitions::fastMalloc(size, typeName));
105 }
106
107 static inline bool expandHashTableBacking(void*, size_t)
108 {
109 return false;
110 }
111 static void free(void* address)
112 {
113 Partitions::fastFree(address);
114 }
115 template<typename T>
116 static void* newArray(size_t bytes)
117 {
118 return malloc<void*, void>(bytes, WTF_HEAP_PROFILER_TYPE_NAME(T));
119 }
120 static void
121 deleteArray(void* ptr)
122 {
123 free(ptr); // Not the system free, the one from this class.
124 }
125
126 static bool isAllocationAllowed() { return true; }
127 template<typename T>
128 static bool isHeapObjectAlive(T* object)
129 {
130 ASSERT_NOT_REACHED();
131 return false;
132 }
133
134 static void markNoTracing(...)
135 {
136 ASSERT_NOT_REACHED();
137 }
138
139 static void registerDelayedMarkNoTracing(...)
140 {
141 ASSERT_NOT_REACHED();
142 }
143
144 static void registerWeakMembers(...)
145 {
146 ASSERT_NOT_REACHED();
147 }
148
149 static void registerWeakTable(...)
150 {
151 ASSERT_NOT_REACHED();
152 }
153
154 #if ENABLE(ASSERT)
155 static bool weakTableRegistered(...)
156 {
157 ASSERT_NOT_REACHED();
158 return false;
159 }
160 #endif
161
162 template<typename T, typename Traits>
163 static void trace(...)
164 {
165 ASSERT_NOT_REACHED();
166 }
167
168 template<typename T>
169 struct OtherType {
170 typedef T* Type;
171 };
172
173 template<typename T>
174 static T& getOther(T* other)
175 {
176 return *other;
177 }
178
179 static void enterGCForbiddenScope() { }
180 static void leaveGCForbiddenScope() { }
181
182 private:
183 static void* allocateBacking(size_t, const char* typeName);
184 };
185
186 // The Windows compiler seems to be very eager to instantiate things it won't
187 // need, so unless we have this class we get compile errors.
188 class PartitionAllocatorDummyVisitor {
189 DISALLOW_NEW();
190 public:
191 template<typename T> inline bool isHeapObjectAlive(T obj)
192 {
193 ASSERT_NOT_REACHED();
194 return false;
195 }
196 };
197
198 // Specializations for heap profiling, so type profiling of |char| is possible
199 // even in official builds (because |char| makes up a large portion of the heap. )
200 template <> WTF_EXPORT char* PartitionAllocator::allocateVectorBacking<char>(siz e_t size);
201 template <> WTF_EXPORT char* PartitionAllocator::allocateExpandedVectorBacking<c har>(size_t size);
202
203 } // namespace WTF
204
205 #define WTF_USE_ALLOCATOR(ClassName, Allocator) \
206 public: \
207 void* operator new(size_t size) \
208 { \
209 return Allocator::template malloc<void*, ClassName>(size, WTF_HEAP_PROFI LER_TYPE_NAME(ClassName)); \
210 } \
211 void operator delete(void* p) { Allocator::free(p); } \
212 void* operator new[](size_t size) { return Allocator::template newArray<Clas sName>(size); } \
213 void operator delete[](void* p) { Allocator::deleteArray(p); } \
214 void* operator new(size_t, NotNullTag, void* location) \
215 { \
216 ASSERT(location); \
217 return location; \
218 } \
219 private: \
220 typedef int __thisIsHereToForceASemicolonAfterThisMacro
221
222 using WTF::PartitionAllocator;
223
224 #endif // WTF_PartitionAllocator_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698