OLD | NEW |
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 #ifndef BASE_MEMORY_SINGLETON_H_ | 5 #ifndef BASE_MEMORY_SINGLETON_H_ |
6 #define BASE_MEMORY_SINGLETON_H_ | 6 #define BASE_MEMORY_SINGLETON_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 private: | 103 private: |
104 static const size_t kBufferSize = (sizeof(Type) + | 104 static const size_t kBufferSize = (sizeof(Type) + |
105 sizeof(intptr_t) - 1) / sizeof(intptr_t); | 105 sizeof(intptr_t) - 1) / sizeof(intptr_t); |
106 static intptr_t buffer_[kBufferSize]; | 106 static intptr_t buffer_[kBufferSize]; |
107 | 107 |
108 // Signal the object was already deleted, so it is not revived. | 108 // Signal the object was already deleted, so it is not revived. |
109 static base::subtle::Atomic32 dead_; | 109 static base::subtle::Atomic32 dead_; |
110 }; | 110 }; |
111 | 111 |
| 112 // Alternate traits for use with the Singleton<Type>. Identical to |
| 113 // DefaultSingletonTraits except that AddRef() will be called on the singleton |
| 114 // upon construction and Release() will be called instead of delete when |
| 115 // the singleton is destructed. |
| 116 template<typename Type> |
| 117 struct RefCountedSingletonTraits { |
| 118 // Allocates the reference counted object. |
| 119 static Type* New() { |
| 120 DCHECK(Type::ImplementsThreadSafeReferenceCounting()); |
| 121 Type* ptr = new Type(); |
| 122 ptr->AddRef(); |
| 123 return ptr; |
| 124 } |
| 125 |
| 126 // Destroys the reference counted object. |
| 127 static void Delete(Type* x) { |
| 128 DCHECK(Type::ImplementsThreadSafeReferenceCounting()); |
| 129 x->Release(); |
| 130 } |
| 131 |
| 132 static const bool kRegisterAtExit = true; |
| 133 static const bool kAllowedToAccessOnNonjoinableThread = false; |
| 134 }; |
| 135 |
112 template <typename Type> intptr_t | 136 template <typename Type> intptr_t |
113 StaticMemorySingletonTraits<Type>::buffer_[kBufferSize]; | 137 StaticMemorySingletonTraits<Type>::buffer_[kBufferSize]; |
114 template <typename Type> base::subtle::Atomic32 | 138 template <typename Type> base::subtle::Atomic32 |
115 StaticMemorySingletonTraits<Type>::dead_ = 0; | 139 StaticMemorySingletonTraits<Type>::dead_ = 0; |
116 | 140 |
117 // The Singleton<Type, Traits, DifferentiatingType> class manages a single | 141 // The Singleton<Type, Traits, DifferentiatingType> class manages a single |
118 // instance of Type which will be created on first use and will be destroyed at | 142 // instance of Type which will be created on first use and will be destroyed at |
119 // normal process exit). The Trait::Delete function will not be called on | 143 // normal process exit). The Trait::Delete function will not be called on |
120 // abnormal process exit. | 144 // abnormal process exit. |
121 // | 145 // |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 instance_ = 0; | 286 instance_ = 0; |
263 } | 287 } |
264 static base::subtle::AtomicWord instance_; | 288 static base::subtle::AtomicWord instance_; |
265 }; | 289 }; |
266 | 290 |
267 template <typename Type, typename Traits, typename DifferentiatingType> | 291 template <typename Type, typename Traits, typename DifferentiatingType> |
268 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 292 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
269 instance_ = 0; | 293 instance_ = 0; |
270 | 294 |
271 #endif // BASE_MEMORY_SINGLETON_H_ | 295 #endif // BASE_MEMORY_SINGLETON_H_ |
OLD | NEW |