Home | History | Annotate | Download | only in heap
      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:
     30     ~RefPtr() { }
     31     operator T*() const { return 0; }
     32     T* operator->() { return 0; }
     33 };
     34 
     35 template<typename T> class OwnPtr {
     36 public:
     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:
     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   using iterator = T*;
     73   using const_iterator = const T*;
     74   using reverse_iterator = T*;
     75   using const_reverse_iterator = const T*;
     76 
     77   size_t size();
     78   T& operator[](size_t);
     79 };
     80 
     81 template <typename T,
     82           size_t inlineCapacity = 0,
     83           typename Allocator = DefaultAllocator>
     84 class Deque {
     85  public:
     86   using iterator = T*;
     87   using const_iterator = const T*;
     88   using reverse_iterator = T*;
     89   using const_reverse_iterator = const T*;
     90 };
     91 
     92 template <typename ValueArg,
     93           typename HashArg = void,
     94           typename TraitsArg = void,
     95           typename Allocator = DefaultAllocator>
     96 class HashSet {
     97  public:
     98   typedef ValueArg* iterator;
     99   typedef const ValueArg* const_iterator;
    100   typedef ValueArg* reverse_iterator;
    101   typedef const ValueArg* const_reverse_iterator;
    102 };
    103 
    104 template <typename ValueArg,
    105           typename HashArg = void,
    106           typename TraitsArg = void,
    107           typename Allocator = DefaultAllocator>
    108 class ListHashSet {
    109  public:
    110   typedef ValueArg* iterator;
    111   typedef const ValueArg* const_iterator;
    112   typedef ValueArg* reverse_iterator;
    113   typedef const ValueArg* const_reverse_iterator;
    114 };
    115 
    116 template <typename ValueArg,
    117           typename HashArg = void,
    118           typename TraitsArg = void,
    119           typename Allocator = DefaultAllocator>
    120 class LinkedHashSet {
    121  public:
    122   typedef ValueArg* iterator;
    123   typedef const ValueArg* const_iterator;
    124   typedef ValueArg* reverse_iterator;
    125   typedef const ValueArg* const_reverse_iterator;
    126 };
    127 
    128 template<
    129     typename ValueArg,
    130     typename HashArg = void,
    131     typename TraitsArg = void,
    132     typename Allocator = DefaultAllocator>
    133 class HashCountedSet {};
    134 
    135 template <typename KeyArg,
    136           typename MappedArg,
    137           typename HashArg = void,
    138           typename KeyTraitsArg = void,
    139           typename MappedTraitsArg = void,
    140           typename Allocator = DefaultAllocator>
    141 class HashMap {
    142  public:
    143   typedef MappedArg* iterator;
    144   typedef const MappedArg* const_iterator;
    145   typedef MappedArg* reverse_iterator;
    146   typedef const MappedArg* const_reverse_iterator;
    147 };
    148 }
    149 
    150 // Empty namespace declaration to exercise internal
    151 // handling of namespace equality.
    152 namespace std {
    153   /* empty */
    154 }
    155 
    156 namespace std {
    157 
    158 template<typename T> class unique_ptr {
    159 public:
    160     ~unique_ptr() { }
    161     operator T*() const { return 0; }
    162     T* operator->() { return 0; }
    163 };
    164 
    165 }
    166 
    167 namespace blink {
    168 
    169 using namespace WTF;
    170 
    171 #define DISALLOW_NEW()                   \
    172     private:                                    \
    173     void* operator new(size_t) = delete;        \
    174     void* operator new(size_t, void*) = delete;
    175 
    176 #define STACK_ALLOCATED()                                   \
    177     private:                                                \
    178     __attribute__((annotate("blink_stack_allocated")))      \
    179     void* operator new(size_t) = delete;                    \
    180     void* operator new(size_t, void*) = delete;
    181 
    182 #define ALLOW_ONLY_INLINE_ALLOCATION()    \
    183     public:                               \
    184     void* operator new(size_t, void*);    \
    185     private:                              \
    186     void* operator new(size_t) = delete;
    187 
    188 #define GC_PLUGIN_IGNORE(bug)                           \
    189     __attribute__((annotate("blink_gc_plugin_ignore")))
    190 
    191 #define USING_GARBAGE_COLLECTED_MIXIN(type)                     \
    192 public:                                                         \
    193     virtual void AdjustAndMark(Visitor*) const override { }     \
    194     virtual bool IsHeapObjectAlive(Visitor*) const override { return 0; }
    195 
    196 #define EAGERLY_FINALIZED() typedef int IsEagerlyFinalizedMarker
    197 
    198 template<typename T> class GarbageCollected { };
    199 
    200 template<typename T>
    201 class GarbageCollectedFinalized : public GarbageCollected<T> { };
    202 
    203 template<typename T>
    204 class RefCountedGarbageCollected : public GarbageCollectedFinalized<T> { };
    205 
    206 template<typename T> class Member {
    207 public:
    208     operator T*() const { return 0; }
    209     T* operator->() { return 0; }
    210     bool operator!() const { return false; }
    211 };
    212 
    213 template<typename T> class WeakMember {
    214 public:
    215     operator T*() const { return 0; }
    216     T* operator->() { return 0; }
    217     bool operator!() const { return false; }
    218 };
    219 
    220 template<typename T> class Persistent {
    221 public:
    222     operator T*() const { return 0; }
    223     T* operator->() { return 0; }
    224     bool operator!() const { return false; }
    225 };
    226 
    227 template<typename T> class WeakPersistent {
    228 public:
    229     operator T*() const { return 0; }
    230     T* operator->() { return 0; }
    231     bool operator!() const { return false; }
    232 };
    233 
    234 template<typename T> class CrossThreadPersistent {
    235 public:
    236     operator T*() const { return 0; }
    237     T* operator->() { return 0; }
    238     bool operator!() const { return false; }
    239 };
    240 
    241 template<typename T> class CrossThreadWeakPersistent {
    242 public:
    243     operator T*() const { return 0; }
    244     T* operator->() { return 0; }
    245     bool operator!() const { return false; }
    246 };
    247 
    248 class HeapAllocator {
    249 public:
    250     static const bool isGarbageCollected = true;
    251 };
    252 
    253 template<typename T, size_t inlineCapacity = 0>
    254 class HeapVector : public Vector<T, inlineCapacity, HeapAllocator> { };
    255 
    256 template<typename T, size_t inlineCapacity = 0>
    257 class HeapDeque : public Vector<T, inlineCapacity, HeapAllocator> { };
    258 
    259 template<typename T>
    260 class HeapHashSet : public HashSet<T, void, void, HeapAllocator> { };
    261 
    262 template<typename T>
    263 class HeapListHashSet : public ListHashSet<T, void, void, HeapAllocator> { };
    264 
    265 template<typename T>
    266 class HeapLinkedHashSet : public LinkedHashSet<T, void, void, HeapAllocator> {
    267 };
    268 
    269 template<typename T>
    270 class HeapHashCountedSet : public HashCountedSet<T, void, void, HeapAllocator> {
    271 };
    272 
    273 template<typename K, typename V>
    274 class HeapHashMap : public HashMap<K, V, void, void, void, HeapAllocator> { };
    275 
    276 template<typename T>
    277 class PersistentHeapVector : public Vector<T, 0, HeapAllocator> { };
    278 
    279 template <typename Derived>
    280 class VisitorHelper {
    281 public:
    282     template<typename T>
    283     void Trace(const T&);
    284 };
    285 
    286 class Visitor : public VisitorHelper<Visitor> {
    287 public:
    288     template<typename T, void (T::*method)(Visitor*)>
    289     void RegisterWeakMembers(const T* obj);
    290 };
    291 
    292 class InlinedGlobalMarkingVisitor
    293     : public VisitorHelper<InlinedGlobalMarkingVisitor> {
    294 public:
    295     InlinedGlobalMarkingVisitor* operator->() { return this; }
    296 
    297     template<typename T, void (T::*method)(Visitor*)>
    298     void RegisterWeakMembers(const T* obj);
    299 };
    300 
    301 class GarbageCollectedMixin {
    302 public:
    303     virtual void AdjustAndMark(Visitor*) const = 0;
    304     virtual bool IsHeapObjectAlive(Visitor*) const = 0;
    305     virtual void Trace(Visitor*) { }
    306 };
    307 
    308 template<typename T>
    309 struct TraceIfNeeded {
    310     static void Trace(Visitor*, T*);
    311 };
    312 
    313 }
    314 
    315 namespace WTF {
    316 
    317 template<typename T>
    318 struct VectorTraits<blink::Member<T> > {
    319     static const bool needsDestruction = false;
    320 };
    321 
    322 }
    323 
    324 #endif
    325