| OLD | NEW |
| 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 // ManualConstructor statically-allocates space in which to store some | 5 // ManualConstructor statically-allocates space in which to store some |
| 6 // object, but does not initialize it. You can then call the constructor | 6 // object, but does not initialize it. You can then call the constructor |
| 7 // and destructor for the object yourself as you see fit. This is useful | 7 // and destructor for the object yourself as you see fit. This is useful |
| 8 // for memory management optimizations, where you want to initialize and | 8 // for memory management optimizations, where you want to initialize and |
| 9 // destroy an object multiple times but only allocate it once. | 9 // destroy an object multiple times but only allocate it once. |
| 10 // | 10 // |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 class ManualConstructor { | 27 class ManualConstructor { |
| 28 public: | 28 public: |
| 29 // No constructor or destructor because one of the most useful uses of | 29 // No constructor or destructor because one of the most useful uses of |
| 30 // this class is as part of a union, and members of a union cannot have | 30 // this class is as part of a union, and members of a union cannot have |
| 31 // constructors or destructors. And, anyway, the whole point of this | 31 // constructors or destructors. And, anyway, the whole point of this |
| 32 // class is to bypass these. | 32 // class is to bypass these. |
| 33 | 33 |
| 34 // Support users creating arrays of ManualConstructor<>s. This ensures that | 34 // Support users creating arrays of ManualConstructor<>s. This ensures that |
| 35 // the array itself has the correct alignment. | 35 // the array itself has the correct alignment. |
| 36 static void* operator new[](size_t size) { | 36 static void* operator new[](size_t size) { |
| 37 return AlignedAlloc(size, ALIGNOF(Type)); | 37 return AlignedAlloc(size, alignof(Type)); |
| 38 } | 38 } |
| 39 static void operator delete[](void* mem) { | 39 static void operator delete[](void* mem) { |
| 40 AlignedFree(mem); | 40 AlignedFree(mem); |
| 41 } | 41 } |
| 42 | 42 |
| 43 inline Type* get() { | 43 inline Type* get() { |
| 44 return space_.template data_as<Type>(); | 44 return space_.template data_as<Type>(); |
| 45 } | 45 } |
| 46 inline const Type* get() const { | 46 inline const Type* get() const { |
| 47 return space_.template data_as<Type>(); | 47 return space_.template data_as<Type>(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 60 | 60 |
| 61 inline void InitFromMove(ManualConstructor<Type>&& o) { | 61 inline void InitFromMove(ManualConstructor<Type>&& o) { |
| 62 Init(std::move(*o)); | 62 Init(std::move(*o)); |
| 63 } | 63 } |
| 64 | 64 |
| 65 inline void Destroy() { | 65 inline void Destroy() { |
| 66 get()->~Type(); | 66 get()->~Type(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 private: | 69 private: |
| 70 AlignedMemory<sizeof(Type), ALIGNOF(Type)> space_; | 70 AlignedMemory<sizeof(Type), alignof(Type)> space_; |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 } // namespace base | 73 } // namespace base |
| 74 | 74 |
| 75 #endif // BASE_MEMORY_MANUAL_CONSTRUCTOR_H_ | 75 #endif // BASE_MEMORY_MANUAL_CONSTRUCTOR_H_ |
| OLD | NEW |