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

Side by Side Diff: base/memory/manual_constructor.h

Issue 2932053002: Use C++11 alignment primitives (Closed)
Patch Set: Put back ALIGNAS Created 3 years, 6 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
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 // 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 //
11 // (When I say ManualConstructor statically allocates space, I mean that 11 // (When I say ManualConstructor statically allocates space, I mean that
12 // the ManualConstructor object itself is forced to be the right size.) 12 // the ManualConstructor object itself is forced to be the right size.)
13 // 13 //
14 // For example usage, check out base/containers/small_map.h. 14 // For example usage, check out base/containers/small_map.h.
15 15
16 #ifndef BASE_MEMORY_MANUAL_CONSTRUCTOR_H_ 16 #ifndef BASE_MEMORY_MANUAL_CONSTRUCTOR_H_
17 #define BASE_MEMORY_MANUAL_CONSTRUCTOR_H_ 17 #define BASE_MEMORY_MANUAL_CONSTRUCTOR_H_
18 18
19 #include <stddef.h> 19 #include <stddef.h>
20 #include <type_traits>
20 21
21 #include "base/compiler_specific.h" 22 #include "base/compiler_specific.h"
22 #include "base/memory/aligned_memory.h" 23 #include "base/memory/aligned_memory.h"
23 24
24 namespace base { 25 namespace base {
25 26
26 template <typename Type> 27 template <typename Type>
27 class ManualConstructor { 28 class ManualConstructor {
28 public: 29 public:
29 // No constructor or destructor because one of the most useful uses of 30 // 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 31 // 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 32 // constructors or destructors. And, anyway, the whole point of this
32 // class is to bypass these. 33 // class is to bypass these.
33 34
34 // Support users creating arrays of ManualConstructor<>s. This ensures that 35 // Support users creating arrays of ManualConstructor<>s. This ensures that
35 // the array itself has the correct alignment. 36 // the array itself has the correct alignment.
36 static void* operator new[](size_t size) { 37 static void* operator new[](size_t size) {
37 return AlignedAlloc(size, ALIGNOF(Type)); 38 return AlignedAlloc(size, alignof(Type));
38 } 39 }
39 static void operator delete[](void* mem) { 40 static void operator delete[](void* mem) {
40 AlignedFree(mem); 41 AlignedFree(mem);
41 } 42 }
42 43
43 inline Type* get() { 44 inline Type* get() { return reinterpret_cast<Type*>(&space_); }
44 return space_.template data_as<Type>();
45 }
46 inline const Type* get() const { 45 inline const Type* get() const {
47 return space_.template data_as<Type>(); 46 return reinterpret_cast<const Type*>(&space_);
48 } 47 }
49 48
50 inline Type* operator->() { return get(); } 49 inline Type* operator->() { return get(); }
51 inline const Type* operator->() const { return get(); } 50 inline const Type* operator->() const { return get(); }
52 51
53 inline Type& operator*() { return *get(); } 52 inline Type& operator*() { return *get(); }
54 inline const Type& operator*() const { return *get(); } 53 inline const Type& operator*() const { return *get(); }
55 54
56 template <typename... Ts> 55 template <typename... Ts>
57 inline void Init(Ts&&... params) { 56 inline void Init(Ts&&... params) {
58 new(space_.void_data()) Type(std::forward<Ts>(params)...); 57 new (&space_) Type(std::forward<Ts>(params)...);
59 } 58 }
60 59
61 inline void InitFromMove(ManualConstructor<Type>&& o) { 60 inline void InitFromMove(ManualConstructor<Type>&& o) {
62 Init(std::move(*o)); 61 Init(std::move(*o));
63 } 62 }
64 63
65 inline void Destroy() { 64 inline void Destroy() {
66 get()->~Type(); 65 get()->~Type();
67 } 66 }
68 67
69 private: 68 private:
70 AlignedMemory<sizeof(Type), ALIGNOF(Type)> space_; 69 typename std::aligned_storage<sizeof(Type), alignof(Type)>::type space_;
71 }; 70 };
72 71
73 } // namespace base 72 } // namespace base
74 73
75 #endif // BASE_MEMORY_MANUAL_CONSTRUCTOR_H_ 74 #endif // BASE_MEMORY_MANUAL_CONSTRUCTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698