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

Side by Side Diff: base/lazy_instance.h

Issue 8366041: Make the placement-new buffer in LazyInstance<Type> aligned. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | base/logging.h » ('j') | base/logging.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 23 matching lines...) Expand all
34 34
35 #ifndef BASE_LAZY_INSTANCE_H_ 35 #ifndef BASE_LAZY_INSTANCE_H_
36 #define BASE_LAZY_INSTANCE_H_ 36 #define BASE_LAZY_INSTANCE_H_
37 #pragma once 37 #pragma once
38 38
39 #include <new> // For placement new. 39 #include <new> // For placement new.
40 40
41 #include "base/atomicops.h" 41 #include "base/atomicops.h"
42 #include "base/base_export.h" 42 #include "base/base_export.h"
43 #include "base/basictypes.h" 43 #include "base/basictypes.h"
44 #include "base/logging.h"
44 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 45 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
45 #include "base/threading/thread_restrictions.h" 46 #include "base/threading/thread_restrictions.h"
46 47
47 namespace base { 48 namespace base {
48 49
49 template <typename Type> 50 template <typename Type>
50 struct DefaultLazyInstanceTraits { 51 struct DefaultLazyInstanceTraits {
51 static const bool kAllowedToAccessOnNonjoinableThread = false; 52 static const bool kAllowedToAccessOnNonjoinableThread = false;
52 53
53 static Type* New(void* instance) { 54 static Type* New(void* instance) {
55 DCHECK_ALIGNED(instance) << ": Bad boy, the buffer is not aligned!";
54 // Use placement new to initialize our instance in our preallocated space. 56 // Use placement new to initialize our instance in our preallocated space.
55 // The parenthesis is very important here to force POD type initialization. 57 // The parenthesis is very important here to force POD type initialization.
56 return new (instance) Type(); 58 return new (instance) Type();
57 } 59 }
58 static void Delete(void* instance) { 60 static void Delete(void* instance) {
59 // Explicitly call the destructor. 61 // Explicitly call the destructor.
60 reinterpret_cast<Type*>(instance)->~Type(); 62 reinterpret_cast<Type*>(instance)->~Type();
61 } 63 }
62 }; 64 };
63 65
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 // threaded, so don't use atomic operations. 181 // threaded, so don't use atomic operations.
180 // Calling OnExit while the instance is in use by other threads is a mistake. 182 // Calling OnExit while the instance is in use by other threads is a mistake.
181 static void OnExit(void* lazy_instance) { 183 static void OnExit(void* lazy_instance) {
182 LazyInstance<Type, Traits>* me = 184 LazyInstance<Type, Traits>* me =
183 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); 185 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance);
184 Traits::Delete(me->instance_); 186 Traits::Delete(me->instance_);
185 me->instance_ = NULL; 187 me->instance_ = NULL;
186 base::subtle::Release_Store(&me->state_, STATE_EMPTY); 188 base::subtle::Release_Store(&me->state_, STATE_EMPTY);
187 } 189 }
188 190
191 Type *instance_;
189 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. 192 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance.
190 Type *instance_;
191 193
192 DISALLOW_COPY_AND_ASSIGN(LazyInstance); 194 DISALLOW_COPY_AND_ASSIGN(LazyInstance);
193 }; 195 };
194 196
195 } // namespace base 197 } // namespace base
196 198
197 #endif // BASE_LAZY_INSTANCE_H_ 199 #endif // BASE_LAZY_INSTANCE_H_
OLDNEW
« no previous file with comments | « no previous file | base/logging.h » ('j') | base/logging.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698