| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #include "platform/wtf/Allocator.h" |
| 6 #define WTF_Allocator_h | |
| 7 | 6 |
| 8 #include "wtf/Assertions.h" | 7 // The contents of this header was moved to platform/wtf as part of |
| 9 #include "wtf/TypeTraits.h" | 8 // WTF migration project. See the following post for details: |
| 10 #include "wtf/allocator/Partitions.h" | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
| 11 | |
| 12 namespace WTF { | |
| 13 | |
| 14 // Classes that contain references to garbage-collected objects but aren't | |
| 15 // themselves garbaged allocated, have some extra macros available which | |
| 16 // allows their use to be restricted to cases where the garbage collector | |
| 17 // is able to discover their references. These macros will be useful for | |
| 18 // non-garbage-collected objects to avoid unintended allocations. | |
| 19 // | |
| 20 // STACK_ALLOCATED(): Use if the object is only stack allocated. | |
| 21 // Garbage-collected objects should be in Members but you do not need the | |
| 22 // trace method as they are on the stack. (Down the line these might turn | |
| 23 // in to raw pointers, but for now Members indicate that we have thought | |
| 24 // about them and explicitly taken care of them.) | |
| 25 // | |
| 26 // DISALLOW_NEW(): Cannot be allocated with new operators but can be a | |
| 27 // part of object. If it has Members you need a trace method and the containing | |
| 28 // object needs to call that trace method. | |
| 29 // | |
| 30 // DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(): Allows only placement new operator. This | |
| 31 // disallows general allocation of this object but allows to put the object as a | |
| 32 // value object in collections. If these have Members you need to have a trace | |
| 33 // method. That trace method will be called automatically by the on-heap | |
| 34 // collections. | |
| 35 // | |
| 36 #define DISALLOW_NEW() \ | |
| 37 private: \ | |
| 38 void* operator new(size_t) = delete; \ | |
| 39 void* operator new(size_t, NotNullTag, void*) = delete; \ | |
| 40 void* operator new(size_t, void*) = delete; \ | |
| 41 \ | |
| 42 public: | |
| 43 | |
| 44 #define DISALLOW_NEW_EXCEPT_PLACEMENT_NEW() \ | |
| 45 public: \ | |
| 46 using IsAllowOnlyPlacementNew = int; \ | |
| 47 void* operator new(size_t, NotNullTag, void* location) { return location; } \ | |
| 48 void* operator new(size_t, void* location) { return location; } \ | |
| 49 \ | |
| 50 private: \ | |
| 51 void* operator new(size_t) = delete; \ | |
| 52 \ | |
| 53 public: | |
| 54 | |
| 55 #define STATIC_ONLY(Type) \ | |
| 56 private: \ | |
| 57 Type() = delete; \ | |
| 58 Type(const Type&) = delete; \ | |
| 59 Type& operator=(const Type&) = delete; \ | |
| 60 void* operator new(size_t) = delete; \ | |
| 61 void* operator new(size_t, NotNullTag, void*) = delete; \ | |
| 62 void* operator new(size_t, void*) = delete; \ | |
| 63 \ | |
| 64 public: | |
| 65 | |
| 66 #define IS_GARBAGE_COLLECTED_TYPE() \ | |
| 67 public: \ | |
| 68 using IsGarbageCollectedTypeMarker = int; \ | |
| 69 \ | |
| 70 private: | |
| 71 | |
| 72 #if COMPILER(CLANG) | |
| 73 #define STACK_ALLOCATED() \ | |
| 74 private: \ | |
| 75 __attribute__((annotate("blink_stack_allocated"))) void* operator new( \ | |
| 76 size_t) = delete; \ | |
| 77 void* operator new(size_t, NotNullTag, void*) = delete; \ | |
| 78 void* operator new(size_t, void*) = delete; \ | |
| 79 \ | |
| 80 public: | |
| 81 #else | |
| 82 #define STACK_ALLOCATED() DISALLOW_NEW() | |
| 83 #endif | |
| 84 | |
| 85 // Provides customizable overrides of fastMalloc/fastFree and operator | |
| 86 // new/delete | |
| 87 // | |
| 88 // Provided functionality: | |
| 89 // Macro: USING_FAST_MALLOC | |
| 90 // | |
| 91 // Example usage: | |
| 92 // class Widget { | |
| 93 // USING_FAST_MALLOC(Widget) | |
| 94 // ... | |
| 95 // }; | |
| 96 // | |
| 97 // struct Data { | |
| 98 // USING_FAST_MALLOC(Data) | |
| 99 // public: | |
| 100 // ... | |
| 101 // }; | |
| 102 // | |
| 103 | |
| 104 #define USING_FAST_MALLOC_INTERNAL(type, typeName) \ | |
| 105 public: \ | |
| 106 void* operator new(size_t, void* p) { return p; } \ | |
| 107 void* operator new[](size_t, void* p) { return p; } \ | |
| 108 \ | |
| 109 void* operator new(size_t size) { \ | |
| 110 return ::WTF::Partitions::fastMalloc(size, typeName); \ | |
| 111 } \ | |
| 112 \ | |
| 113 void operator delete(void* p) { ::WTF::Partitions::fastFree(p); } \ | |
| 114 \ | |
| 115 void* operator new[](size_t size) { \ | |
| 116 return ::WTF::Partitions::fastMalloc(size, typeName); \ | |
| 117 } \ | |
| 118 \ | |
| 119 void operator delete[](void* p) { ::WTF::Partitions::fastFree(p); } \ | |
| 120 void* operator new(size_t, NotNullTag, void* location) { \ | |
| 121 DCHECK(location); \ | |
| 122 return location; \ | |
| 123 } \ | |
| 124 \ | |
| 125 private: \ | |
| 126 typedef int __thisIsHereToForceASemicolonAfterThisMacro | |
| 127 | |
| 128 // In official builds, do not include type info string literals to avoid | |
| 129 // bloating the binary. | |
| 130 #if defined(OFFICIAL_BUILD) | |
| 131 #define WTF_HEAP_PROFILER_TYPE_NAME(T) nullptr | |
| 132 #else | |
| 133 #define WTF_HEAP_PROFILER_TYPE_NAME(T) ::WTF::getStringWithTypeName<T>() | |
| 134 #endif | |
| 135 | |
| 136 // Both of these macros enable fast malloc and provide type info to the heap | |
| 137 // profiler. The regular macro does not provide type info in official builds, | |
| 138 // to avoid bloating the binary with type name strings. The |WITH_TYPE_NAME| | |
| 139 // variant provides type info unconditionally, so it should be used sparingly. | |
| 140 // Furthermore, the |WITH_TYPE_NAME| variant does not work if |type| is a | |
| 141 // template argument; |USING_FAST_MALLOC| does. | |
| 142 #define USING_FAST_MALLOC(type) \ | |
| 143 USING_FAST_MALLOC_INTERNAL(type, WTF_HEAP_PROFILER_TYPE_NAME(type)) | |
| 144 #define USING_FAST_MALLOC_WITH_TYPE_NAME(type) \ | |
| 145 USING_FAST_MALLOC_INTERNAL(type, #type) | |
| 146 | |
| 147 } // namespace WTF | |
| 148 | |
| 149 // This version of placement new omits a 0 check. | |
| 150 enum NotNullTag { NotNull }; | |
| 151 inline void* operator new(size_t, NotNullTag, void* location) { | |
| 152 DCHECK(location); | |
| 153 return location; | |
| 154 } | |
| 155 | |
| 156 #endif /* WTF_Allocator_h */ | |
| OLD | NEW |