| 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 | 100 |
| 101 | 101 |
| 102 template <typename T> | 102 template <typename T> |
| 103 struct LeakyInstanceTrait { | 103 struct LeakyInstanceTrait { |
| 104 static void Destroy(T* /* instance */) {} | 104 static void Destroy(T* /* instance */) {} |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 | 107 |
| 108 // Traits that define how an instance is allocated and accessed. | 108 // Traits that define how an instance is allocated and accessed. |
| 109 | 109 |
| 110 // TODO(kalmard): __alignof__ is only defined for GCC > 4.2. Fix alignment issue |
| 111 // on MIPS with other compilers. |
| 112 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) |
| 113 #define LAZY_ALIGN(x) __attribute__((aligned(__alignof__(x)))) |
| 114 #else |
| 115 #define LAZY_ALIGN(x) |
| 116 #endif |
| 117 |
| 110 template <typename T> | 118 template <typename T> |
| 111 struct StaticallyAllocatedInstanceTrait { | 119 struct StaticallyAllocatedInstanceTrait { |
| 112 typedef char StorageType[sizeof(T)]; | 120 typedef char StorageType[sizeof(T)] LAZY_ALIGN(T); |
| 113 | 121 |
| 114 static T* MutableInstance(StorageType* storage) { | 122 static T* MutableInstance(StorageType* storage) { |
| 115 return reinterpret_cast<T*>(storage); | 123 return reinterpret_cast<T*>(storage); |
| 116 } | 124 } |
| 117 | 125 |
| 118 template <typename ConstructTrait> | 126 template <typename ConstructTrait> |
| 119 static void InitStorageUsingTrait(StorageType* storage) { | 127 static void InitStorageUsingTrait(StorageType* storage) { |
| 120 ConstructTrait::Construct(MutableInstance(storage)); | 128 ConstructTrait::Construct(MutableInstance(storage)); |
| 121 } | 129 } |
| 122 }; | 130 }; |
| 123 | 131 |
| 132 #undef LAZY_ALIGN |
| 133 |
| 124 | 134 |
| 125 template <typename T> | 135 template <typename T> |
| 126 struct DynamicallyAllocatedInstanceTrait { | 136 struct DynamicallyAllocatedInstanceTrait { |
| 127 typedef T* StorageType; | 137 typedef T* StorageType; |
| 128 | 138 |
| 129 static T* MutableInstance(StorageType* storage) { | 139 static T* MutableInstance(StorageType* storage) { |
| 130 return *storage; | 140 return *storage; |
| 131 } | 141 } |
| 132 | 142 |
| 133 template <typename CreateTrait> | 143 template <typename CreateTrait> |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 return AllocationTrait::MutableInstance(&storage_); | 211 return AllocationTrait::MutableInstance(&storage_); |
| 202 } | 212 } |
| 203 | 213 |
| 204 const T& Get() const { | 214 const T& Get() const { |
| 205 Init(); | 215 Init(); |
| 206 return *AllocationTrait::MutableInstance(&storage_); | 216 return *AllocationTrait::MutableInstance(&storage_); |
| 207 } | 217 } |
| 208 | 218 |
| 209 mutable OnceType once_; | 219 mutable OnceType once_; |
| 210 // Note that the previous field, OnceType, is an AtomicWord which guarantees | 220 // Note that the previous field, OnceType, is an AtomicWord which guarantees |
| 211 // the correct alignment of the storage field below. | 221 // 4-byte alignment of the storage field below. If compiling with GCC (>4.2), |
| 222 // the LAZY_ALIGN macro above will guarantee correctness for any alignment. |
| 212 mutable StorageType storage_; | 223 mutable StorageType storage_; |
| 213 }; | 224 }; |
| 214 | 225 |
| 215 | 226 |
| 216 template <typename T, | 227 template <typename T, |
| 217 typename CreateTrait = DefaultConstructTrait<T>, | 228 typename CreateTrait = DefaultConstructTrait<T>, |
| 218 typename InitOnceTrait = SingleThreadInitOnceTrait, | 229 typename InitOnceTrait = SingleThreadInitOnceTrait, |
| 219 typename DestroyTrait = LeakyInstanceTrait<T> > | 230 typename DestroyTrait = LeakyInstanceTrait<T> > |
| 220 struct LazyStaticInstance { | 231 struct LazyStaticInstance { |
| 221 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>, | 232 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 239 typename InitOnceTrait = SingleThreadInitOnceTrait, | 250 typename InitOnceTrait = SingleThreadInitOnceTrait, |
| 240 typename DestroyTrait = LeakyInstanceTrait<T> > | 251 typename DestroyTrait = LeakyInstanceTrait<T> > |
| 241 struct LazyDynamicInstance { | 252 struct LazyDynamicInstance { |
| 242 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, | 253 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, |
| 243 CreateTrait, InitOnceTrait, DestroyTrait> type; | 254 CreateTrait, InitOnceTrait, DestroyTrait> type; |
| 244 }; | 255 }; |
| 245 | 256 |
| 246 } } // namespace v8::internal | 257 } } // namespace v8::internal |
| 247 | 258 |
| 248 #endif // V8_LAZY_INSTANCE_H_ | 259 #endif // V8_LAZY_INSTANCE_H_ |
| OLD | NEW |