| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // - "Dynamic mode". In this mode, the instance is dynamically allocated and | 84 // - "Dynamic mode". In this mode, the instance is dynamically allocated and |
| 85 // constructed (using new) by default. This mode is useful if you have to | 85 // constructed (using new) by default. This mode is useful if you have to |
| 86 // deal with some code already allocating the instance for you (e.g. | 86 // deal with some code already allocating the instance for you (e.g. |
| 87 // OS::Mutex() which returns a new private OS-dependent subclass of Mutex). | 87 // OS::Mutex() which returns a new private OS-dependent subclass of Mutex). |
| 88 // The macro LAZY_DYNAMIC_INSTANCE_INITIALIZER must be used to initialize | 88 // The macro LAZY_DYNAMIC_INSTANCE_INITIALIZER must be used to initialize |
| 89 // dynamic lazy instances. | 89 // dynamic lazy instances. |
| 90 | 90 |
| 91 #ifndef V8_LAZY_INSTANCE_H_ | 91 #ifndef V8_LAZY_INSTANCE_H_ |
| 92 #define V8_LAZY_INSTANCE_H_ | 92 #define V8_LAZY_INSTANCE_H_ |
| 93 | 93 |
| 94 #include "checks.h" |
| 94 #include "once.h" | 95 #include "once.h" |
| 95 | 96 |
| 96 namespace v8 { | 97 namespace v8 { |
| 97 namespace internal { | 98 namespace internal { |
| 98 | 99 |
| 99 #define LAZY_STATIC_INSTANCE_INITIALIZER { V8_ONCE_INIT, {} } | 100 #define LAZY_STATIC_INSTANCE_INITIALIZER { V8_ONCE_INIT, { {} } } |
| 100 #define LAZY_DYNAMIC_INSTANCE_INITIALIZER { V8_ONCE_INIT, 0 } | 101 #define LAZY_DYNAMIC_INSTANCE_INITIALIZER { V8_ONCE_INIT, 0 } |
| 101 | 102 |
| 102 // Default to static mode. | 103 // Default to static mode. |
| 103 #define LAZY_INSTANCE_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER | 104 #define LAZY_INSTANCE_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER |
| 104 | 105 |
| 105 | 106 |
| 106 template <typename T> | 107 template <typename T> |
| 107 struct LeakyInstanceTrait { | 108 struct LeakyInstanceTrait { |
| 108 static void Destroy(T* /* instance */) {} | 109 static void Destroy(T* /* instance */) {} |
| 109 }; | 110 }; |
| 110 | 111 |
| 111 | 112 |
| 112 // Traits that define how an instance is allocated and accessed. | 113 // Traits that define how an instance is allocated and accessed. |
| 113 | 114 |
| 114 // TODO(kalmard): __alignof__ is only defined for GCC > 4.2. Fix alignment issue | |
| 115 // on MIPS with other compilers. | |
| 116 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) | |
| 117 #define LAZY_ALIGN(x) __attribute__((aligned(__alignof__(x)))) | |
| 118 #else | |
| 119 #define LAZY_ALIGN(x) | |
| 120 #endif | |
| 121 | 115 |
| 122 template <typename T> | 116 template <typename T> |
| 123 struct StaticallyAllocatedInstanceTrait { | 117 struct StaticallyAllocatedInstanceTrait { |
| 124 typedef char StorageType[sizeof(T)] LAZY_ALIGN(T); | 118 // 16-byte alignment fallback to be on the safe side here. |
| 119 struct V8_ALIGNAS(T, 16) StorageType { |
| 120 char x[sizeof(T)]; |
| 121 }; |
| 122 |
| 123 STATIC_ASSERT(V8_ALIGNOF(StorageType) >= V8_ALIGNOF(T)); |
| 125 | 124 |
| 126 static T* MutableInstance(StorageType* storage) { | 125 static T* MutableInstance(StorageType* storage) { |
| 127 return reinterpret_cast<T*>(storage); | 126 return reinterpret_cast<T*>(storage); |
| 128 } | 127 } |
| 129 | 128 |
| 130 template <typename ConstructTrait> | 129 template <typename ConstructTrait> |
| 131 static void InitStorageUsingTrait(StorageType* storage) { | 130 static void InitStorageUsingTrait(StorageType* storage) { |
| 132 ConstructTrait::Construct(MutableInstance(storage)); | 131 ConstructTrait::Construct(MutableInstance(storage)); |
| 133 } | 132 } |
| 134 }; | 133 }; |
| 135 | 134 |
| 136 #undef LAZY_ALIGN | |
| 137 | |
| 138 | 135 |
| 139 template <typename T> | 136 template <typename T> |
| 140 struct DynamicallyAllocatedInstanceTrait { | 137 struct DynamicallyAllocatedInstanceTrait { |
| 141 typedef T* StorageType; | 138 typedef T* StorageType; |
| 142 | 139 |
| 143 static T* MutableInstance(StorageType* storage) { | 140 static T* MutableInstance(StorageType* storage) { |
| 144 return *storage; | 141 return *storage; |
| 145 } | 142 } |
| 146 | 143 |
| 147 template <typename CreateTrait> | 144 template <typename CreateTrait> |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 typename InitOnceTrait = SingleThreadInitOnceTrait, | 251 typename InitOnceTrait = SingleThreadInitOnceTrait, |
| 255 typename DestroyTrait = LeakyInstanceTrait<T> > | 252 typename DestroyTrait = LeakyInstanceTrait<T> > |
| 256 struct LazyDynamicInstance { | 253 struct LazyDynamicInstance { |
| 257 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, | 254 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, |
| 258 CreateTrait, InitOnceTrait, DestroyTrait> type; | 255 CreateTrait, InitOnceTrait, DestroyTrait> type; |
| 259 }; | 256 }; |
| 260 | 257 |
| 261 } } // namespace v8::internal | 258 } } // namespace v8::internal |
| 262 | 259 |
| 263 #endif // V8_LAZY_INSTANCE_H_ | 260 #endif // V8_LAZY_INSTANCE_H_ |
| OLD | NEW |