Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / Allocator.h
blob5b39698e5abc1a71762f0fffc422df4cacf945b6
1 // Copyright 2015 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.
5 #ifndef WTF_Allocator_h
6 #define WTF_Allocator_h
8 #include "wtf/StdLibExtras.h"
10 namespace WTF {
12 // Classes that contain references to garbage-collected objects but aren't
13 // themselves garbaged allocated, have some extra macros available which
14 // allows their use to be restricted to cases where the garbage collector
15 // is able to discover their references. These macros will be useful for
16 // non-garbage-collected objects to avoid unintended allocations.
18 // STACK_ALLOCATED(): Use if the object is only stack allocated.
19 // Garbage-collected objects should be in Members but you do not need the
20 // trace method as they are on the stack. (Down the line these might turn
21 // in to raw pointers, but for now Members indicate that we have thought
22 // about them and explicitly taken care of them.)
24 // DISALLOW_ALLOCATION(): Cannot be allocated with new operators but can be a
25 // part of object. If it has Members you need a trace method and the containing
26 // object needs to call that trace method.
28 // ALLOW_ONLY_INLINE_ALLOCATION(): Allows only placement new operator. This
29 // disallows general allocation of this object but allows to put the object as a
30 // value object in collections. If these have Members you need to have a trace
31 // method. That trace method will be called automatically by the on-heap
32 // collections.
34 #define DISALLOW_ALLOCATION() \
35 private: \
36 void* operator new(size_t) = delete; \
37 void* operator new(size_t, NotNullTag, void*) = delete; \
38 void* operator new(size_t, void*) = delete; \
39 public:
41 #define ALLOW_ONLY_INLINE_ALLOCATION() \
42 public: \
43 using IsAllowOnlyInlineAllocation = int; \
44 void* operator new(size_t, NotNullTag, void* location) { return location; } \
45 void* operator new(size_t, void* location) { return location; } \
46 private: \
47 void* operator new(size_t) = delete; \
48 public:
50 #define STATIC_ONLY(Type) \
51 private: \
52 Type() = delete; \
53 Type(const Type&) = delete; \
54 Type& operator=(const Type&) = delete; \
55 void* operator new(size_t) = delete; \
56 void* operator new(size_t, NotNullTag, void*) = delete; \
57 void* operator new(size_t, void*) = delete; \
58 public:
60 #if COMPILER(CLANG)
61 #define STACK_ALLOCATED() \
62 private: \
63 __attribute__((annotate("blink_stack_allocated"))) \
64 void* operator new(size_t) = delete; \
65 void* operator new(size_t, NotNullTag, void*) = delete; \
66 void* operator new(size_t, void*) = delete; \
67 public:
68 #else
69 #define STACK_ALLOCATED() DISALLOW_ALLOCATION()
70 #endif
72 } // namespace WTF
74 #endif /* WTF_Allocator_h */