1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef HEAP_STUBS_H_
6 #define HEAP_STUBS_H_
7 
8 #include "stddef.h"
9 
10 #define WTF_MAKE_FAST_ALLOCATED                 \
11     public:                                     \
12     void* operator new(size_t, void* p);        \
13     void* operator new[](size_t, void* p);      \
14     void* operator new(size_t size);            \
15     private:                                    \
16     typedef int __thisIsHereToForceASemicolonAfterThisMacro
17 
18 namespace WTF {
19 
20 template<typename T> class RefCounted { };
21 
22 template<typename T> class RawPtr {
23 public:
24     operator T*() const { return 0; }
25     T* operator->() { return 0; }
26 };
27 
28 template<typename T> class RefPtr {
29 public:
~RefPtr()30     ~RefPtr() { }
31     operator T*() const { return 0; }
32     T* operator->() { return 0; }
33 };
34 
35 template<typename T> class OwnPtr {
36 public:
~OwnPtr()37     ~OwnPtr() { }
38     operator T*() const { return 0; }
39     T* operator->() { return 0; }
40 };
41 
42 class DefaultAllocator {
43 public:
44     static const bool isGarbageCollected = false;
45 };
46 
47 template<typename T>
48 struct VectorTraits {
49     static const bool needsDestruction = true;
50 };
51 
52 template<size_t inlineCapacity, bool isGarbageCollected, bool tNeedsDestruction>
53 class VectorDestructorBase {
54 public:
~VectorDestructorBase()55     ~VectorDestructorBase() {}
56 };
57 
58 template<size_t inlineCapacity>
59 class VectorDestructorBase<inlineCapacity, true, false> {};
60 
61 template<>
62 class VectorDestructorBase<0, true, true> {};
63 
64 template<
65     typename T,
66     size_t inlineCapacity = 0,
67     typename Allocator = DefaultAllocator>
68 class Vector : public VectorDestructorBase<inlineCapacity,
69                                            Allocator::isGarbageCollected,
70                                            VectorTraits<T>::needsDestruction> {
71 public:
72     size_t size();
73     T& operator[](size_t);
74 };
75 
76 template<
77     typename T,
78     size_t inlineCapacity = 0,
79     typename Allocator = DefaultAllocator>
80 class Deque {};
81 
82 template<
83     typename ValueArg,
84     typename HashArg = void,
85     typename TraitsArg = void,
86     typename Allocator = DefaultAllocator>
87 class HashSet {};
88 
89 template<
90     typename ValueArg,
91     typename HashArg = void,
92     typename TraitsArg = void,
93     typename Allocator = DefaultAllocator>
94 class ListHashSet {};
95 
96 template<
97     typename ValueArg,
98     typename HashArg = void,
99     typename TraitsArg = void,
100     typename Allocator = DefaultAllocator>
101 class LinkedHashSet {};
102 
103 template<
104     typename ValueArg,
105     typename HashArg = void,
106     typename TraitsArg = void,
107     typename Allocator = DefaultAllocator>
108 class HashCountedSet {};
109 
110 template<
111     typename KeyArg,
112     typename MappedArg,
113     typename HashArg = void,
114     typename KeyTraitsArg = void,
115     typename MappedTraitsArg = void,
116     typename Allocator = DefaultAllocator>
117 class HashMap {};
118 
119 }
120 
121 // Empty namespace declaration to exercise internal
122 // handling of namespace equality.
123 namespace std {
124   /* empty */
125 }
126 
127 namespace std {
128 
129 template<typename T> class unique_ptr {
130 public:
~unique_ptr()131     ~unique_ptr() { }
132     operator T*() const { return 0; }
133     T* operator->() { return 0; }
134 };
135 
136 }
137 
138 namespace blink {
139 
140 using namespace WTF;
141 
142 #define DISALLOW_NEW()                   \
143     private:                                    \
144     void* operator new(size_t) = delete;        \
145     void* operator new(size_t, void*) = delete;
146 
147 #define STACK_ALLOCATED()                                   \
148     private:                                                \
149     __attribute__((annotate("blink_stack_allocated")))      \
150     void* operator new(size_t) = delete;                    \
151     void* operator new(size_t, void*) = delete;
152 
153 #define ALLOW_ONLY_INLINE_ALLOCATION()    \
154     public:                               \
155     void* operator new(size_t, void*);    \
156     private:                              \
157     void* operator new(size_t) = delete;
158 
159 #define GC_PLUGIN_IGNORE(bug)                           \
160     __attribute__((annotate("blink_gc_plugin_ignore")))
161 
162 #define USING_GARBAGE_COLLECTED_MIXIN(type)                     \
163 public:                                                         \
164     virtual void adjustAndMark(Visitor*) const override { }     \
165     virtual bool isHeapObjectAlive(Visitor*) const override { return 0; }
166 
167 #define EAGERLY_FINALIZED() typedef int IsEagerlyFinalizedMarker
168 
169 template<typename T> class GarbageCollected { };
170 
171 template<typename T>
172 class GarbageCollectedFinalized : public GarbageCollected<T> { };
173 
174 template<typename T>
175 class RefCountedGarbageCollected : public GarbageCollectedFinalized<T> { };
176 
177 template<typename T> class Member {
178 public:
179     operator T*() const { return 0; }
180     T* operator->() { return 0; }
181     bool operator!() const { return false; }
182 };
183 
184 template<typename T> class WeakMember {
185 public:
186     operator T*() const { return 0; }
187     T* operator->() { return 0; }
188     bool operator!() const { return false; }
189 };
190 
191 template<typename T> class Persistent {
192 public:
193     operator T*() const { return 0; }
194     T* operator->() { return 0; }
195     bool operator!() const { return false; }
196 };
197 
198 template<typename T> class WeakPersistent {
199 public:
200     operator T*() const { return 0; }
201     T* operator->() { return 0; }
202     bool operator!() const { return false; }
203 };
204 
205 template<typename T> class CrossThreadPersistent {
206 public:
207     operator T*() const { return 0; }
208     T* operator->() { return 0; }
209     bool operator!() const { return false; }
210 };
211 
212 template<typename T> class CrossThreadWeakPersistent {
213 public:
214     operator T*() const { return 0; }
215     T* operator->() { return 0; }
216     bool operator!() const { return false; }
217 };
218 
219 class HeapAllocator {
220 public:
221     static const bool isGarbageCollected = true;
222 };
223 
224 template<typename T, size_t inlineCapacity = 0>
225 class HeapVector : public Vector<T, inlineCapacity, HeapAllocator> { };
226 
227 template<typename T, size_t inlineCapacity = 0>
228 class HeapDeque : public Vector<T, inlineCapacity, HeapAllocator> { };
229 
230 template<typename T>
231 class HeapHashSet : public HashSet<T, void, void, HeapAllocator> { };
232 
233 template<typename T>
234 class HeapListHashSet : public ListHashSet<T, void, void, HeapAllocator> { };
235 
236 template<typename T>
237 class HeapLinkedHashSet : public LinkedHashSet<T, void, void, HeapAllocator> {
238 };
239 
240 template<typename T>
241 class HeapHashCountedSet : public HashCountedSet<T, void, void, HeapAllocator> {
242 };
243 
244 template<typename K, typename V>
245 class HeapHashMap : public HashMap<K, V, void, void, void, HeapAllocator> { };
246 
247 template<typename T>
248 class PersistentHeapVector : public Vector<T, 0, HeapAllocator> { };
249 
250 template <typename Derived>
251 class VisitorHelper {
252 public:
253     template<typename T>
254     void trace(const T&);
255 };
256 
257 class Visitor : public VisitorHelper<Visitor> {
258 public:
259     template<typename T, void (T::*method)(Visitor*)>
260     void registerWeakMembers(const T* obj);
261 };
262 
263 class InlinedGlobalMarkingVisitor
264     : public VisitorHelper<InlinedGlobalMarkingVisitor> {
265 public:
266     InlinedGlobalMarkingVisitor* operator->() { return this; }
267 
268     template<typename T, void (T::*method)(Visitor*)>
269     void registerWeakMembers(const T* obj);
270 };
271 
272 class GarbageCollectedMixin {
273 public:
274     virtual void adjustAndMark(Visitor*) const = 0;
275     virtual bool isHeapObjectAlive(Visitor*) const = 0;
trace(Visitor *)276     virtual void trace(Visitor*) { }
277 };
278 
279 template<typename T>
280 struct TraceIfNeeded {
281     static void trace(Visitor*, T*);
282 };
283 
284 }
285 
286 namespace WTF {
287 
288 template<typename T>
289 struct VectorTraits<blink::Member<T> > {
290     static const bool needsDestruction = false;
291 };
292 
293 }
294 
295 #endif
296