Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(659)

Side by Side Diff: base/lazy_instance.h

Issue 2670873002: Remove base's ALIGNOF/ALIGNAS in favor of alignof/alignas. (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/containers/stack_container_unittest.cc ('k') | base/lazy_instance_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // The LazyInstance<Type, Traits> class manages a single instance of Type, 5 // The LazyInstance<Type, Traits> class manages a single instance of Type,
6 // which will be lazily created on the first time it's accessed. This class is 6 // which will be lazily created on the first time it's accessed. This class is
7 // useful for places you would normally use a function-level static, but you 7 // useful for places you would normally use a function-level static, but you
8 // need to have guaranteed thread-safety. The Type constructor will only ever 8 // need to have guaranteed thread-safety. The Type constructor will only ever
9 // be called once, even if two threads are racing to create the object. Get() 9 // be called once, even if two threads are racing to create the object. Get()
10 // and Pointer() will always return the same, completely initialized instance. 10 // and Pointer() will always return the same, completely initialized instance.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 namespace base { 53 namespace base {
54 54
55 template <typename Type> 55 template <typename Type>
56 struct DefaultLazyInstanceTraits { 56 struct DefaultLazyInstanceTraits {
57 static const bool kRegisterOnExit = true; 57 static const bool kRegisterOnExit = true;
58 #if DCHECK_IS_ON() 58 #if DCHECK_IS_ON()
59 static const bool kAllowedToAccessOnNonjoinableThread = false; 59 static const bool kAllowedToAccessOnNonjoinableThread = false;
60 #endif 60 #endif
61 61
62 static Type* New(void* instance) { 62 static Type* New(void* instance) {
63 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (ALIGNOF(Type) - 1), 0u) 63 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (alignof(Type) - 1), 0u)
64 << ": Bad boy, the buffer passed to placement new is not aligned!\n" 64 << "The buffer passed to placement new is not aligned!\n"
65 "This may break some stuff like SSE-based optimizations assuming the " 65 "This may break some stuff like SSE-based optimizations assuming "
66 "<Type> objects are word aligned."; 66 "the <Type> objects are word aligned.";
67 // Use placement new to initialize our instance in our preallocated space. 67 // Use placement new to initialize our instance in our preallocated space.
68 // The parenthesis is very important here to force POD type initialization. 68 // The parenthesis is very important here to force POD type initialization.
69 return new (instance) Type(); 69 return new (instance) Type();
70 } 70 }
71 static void Delete(Type* instance) { 71 static void Delete(Type* instance) {
72 // Explicitly call the destructor. 72 // Explicitly call the destructor.
73 instance->~Type(); 73 instance->~Type();
74 } 74 }
75 }; 75 };
76 76
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 return p == instance(); 177 return p == instance();
178 } 178 }
179 } 179 }
180 180
181 // Effectively private: member data is only public to allow the linker to 181 // Effectively private: member data is only public to allow the linker to
182 // statically initialize it and to maintain a POD class. DO NOT USE FROM 182 // statically initialize it and to maintain a POD class. DO NOT USE FROM
183 // OUTSIDE THIS CLASS. 183 // OUTSIDE THIS CLASS.
184 184
185 subtle::AtomicWord private_instance_; 185 subtle::AtomicWord private_instance_;
186 // Preallocated space for the Type instance. 186 // Preallocated space for the Type instance.
187 base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> private_buf_; 187 base::AlignedMemory<sizeof(Type), alignof(Type)> private_buf_;
188 188
189 private: 189 private:
190 Type* instance() { 190 Type* instance() {
191 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); 191 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_));
192 } 192 }
193 193
194 // Adapter function for use with AtExit. This should be called single 194 // Adapter function for use with AtExit. This should be called single
195 // threaded, so don't synchronize across threads. 195 // threaded, so don't synchronize across threads.
196 // Calling OnExit while the instance is in use by other threads is a mistake. 196 // Calling OnExit while the instance is in use by other threads is a mistake.
197 static void OnExit(void* lazy_instance) { 197 static void OnExit(void* lazy_instance) {
198 LazyInstance<Type, Traits>* me = 198 LazyInstance<Type, Traits>* me =
199 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); 199 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance);
200 Traits::Delete(me->instance()); 200 Traits::Delete(me->instance());
201 subtle::NoBarrier_Store(&me->private_instance_, 0); 201 subtle::NoBarrier_Store(&me->private_instance_, 0);
202 } 202 }
203 }; 203 };
204 204
205 } // namespace base 205 } // namespace base
206 206
207 #endif // BASE_LAZY_INSTANCE_H_ 207 #endif // BASE_LAZY_INSTANCE_H_
OLDNEW
« no previous file with comments | « base/containers/stack_container_unittest.cc ('k') | base/lazy_instance_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698