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