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

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

Issue 2670873002: Remove base's ALIGNOF/ALIGNAS in favor of alignof/alignas. (Closed)
Patch Set: rebase Created 3 years, 10 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/lazy_instance_unittest.cc ('k') | base/memory/aligned_memory_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 // AlignedMemory is a POD type that gives you a portable way to specify static 5 // AlignedMemory is a POD type that gives you a portable way to specify static
6 // or local stack data of a given alignment and size. For example, if you need 6 // or local stack data of a given alignment and size. For example, if you need
7 // static storage for a class, but you want manual control over when the object 7 // static storage for a class, but you want manual control over when the object
8 // is constructed and destructed (you don't want static initialization and 8 // is constructed and destructed (you don't want static initialization and
9 // destruction), use AlignedMemory: 9 // destruction), use AlignedMemory:
10 // 10 //
11 // static AlignedMemory<sizeof(MyClass), ALIGNOF(MyClass)> my_class; 11 // static AlignedMemory<sizeof(MyClass), alignof(MyClass)> my_class;
12 // 12 //
13 // // ... at runtime: 13 // // ... at runtime:
14 // new(my_class.void_data()) MyClass(); 14 // new(my_class.void_data()) MyClass();
15 // 15 //
16 // // ... use it: 16 // // ... use it:
17 // MyClass* mc = my_class.data_as<MyClass>(); 17 // MyClass* mc = my_class.data_as<MyClass>();
18 // 18 //
19 // // ... later, to destruct my_class: 19 // // ... later, to destruct my_class:
20 // my_class.data_as<MyClass>()->MyClass::~MyClass(); 20 // my_class.data_as<MyClass>()->MyClass::~MyClass();
21 // 21 //
(...skipping 19 matching lines...) Expand all
41 #include "base/compiler_specific.h" 41 #include "base/compiler_specific.h"
42 42
43 #if defined(COMPILER_MSVC) 43 #if defined(COMPILER_MSVC)
44 #include <malloc.h> 44 #include <malloc.h>
45 #else 45 #else
46 #include <stdlib.h> 46 #include <stdlib.h>
47 #endif 47 #endif
48 48
49 namespace base { 49 namespace base {
50 50
51 // AlignedMemory is specialized for all supported alignments.
52 // Make sure we get a compiler error if someone uses an unsupported alignment.
53 template <size_t Size, size_t ByteAlignment> 51 template <size_t Size, size_t ByteAlignment>
54 struct AlignedMemory {}; 52 class AlignedMemory {
55 53 public:
56 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ 54 alignas(ByteAlignment) uint8_t data_[Size];
57 template <size_t Size> \ 55 void* void_data() { return static_cast<void*>(data_); }
58 class AlignedMemory<Size, byte_alignment> { \ 56 const void* void_data() const { return static_cast<const void*>(data_); }
59 public: \ 57 template <typename Type>
60 ALIGNAS(byte_alignment) uint8_t data_[Size]; \ 58 Type* data_as() {
61 void* void_data() { return static_cast<void*>(data_); } \ 59 return static_cast<Type*>(void_data());
62 const void* void_data() const { return static_cast<const void*>(data_); } \ 60 }
63 template <typename Type> \ 61 template <typename Type>
64 Type* data_as() { \ 62 const Type* data_as() const {
65 return static_cast<Type*>(void_data()); \ 63 return static_cast<const Type*>(void_data());
66 } \
67 template <typename Type> \
68 const Type* data_as() const { \
69 return static_cast<const Type*>(void_data()); \
70 } \
71 \
72 private: \
73 void* operator new(size_t); \
74 void operator delete(void*); \
75 } 64 }
76 65
77 // Specialization for all alignments is required because MSVC (as of VS 2008) 66 private:
78 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). 67 void* operator new(size_t);
79 // Greater than 4096 alignment is not supported by some compilers, so 4096 is 68 void operator delete(void*);
80 // the maximum specified here. 69 };
81 BASE_DECL_ALIGNED_MEMORY(1);
82 BASE_DECL_ALIGNED_MEMORY(2);
83 BASE_DECL_ALIGNED_MEMORY(4);
84 BASE_DECL_ALIGNED_MEMORY(8);
85 BASE_DECL_ALIGNED_MEMORY(16);
86 BASE_DECL_ALIGNED_MEMORY(32);
87 BASE_DECL_ALIGNED_MEMORY(64);
88 BASE_DECL_ALIGNED_MEMORY(128);
89 BASE_DECL_ALIGNED_MEMORY(256);
90 BASE_DECL_ALIGNED_MEMORY(512);
91 BASE_DECL_ALIGNED_MEMORY(1024);
92 BASE_DECL_ALIGNED_MEMORY(2048);
93 BASE_DECL_ALIGNED_MEMORY(4096);
94
95 #undef BASE_DECL_ALIGNED_MEMORY
96 70
97 BASE_EXPORT void* AlignedAlloc(size_t size, size_t alignment); 71 BASE_EXPORT void* AlignedAlloc(size_t size, size_t alignment);
98 72
99 inline void AlignedFree(void* ptr) { 73 inline void AlignedFree(void* ptr) {
100 #if defined(COMPILER_MSVC) 74 #if defined(COMPILER_MSVC)
101 _aligned_free(ptr); 75 _aligned_free(ptr);
102 #else 76 #else
103 free(ptr); 77 free(ptr);
104 #endif 78 #endif
105 } 79 }
106 80
107 // Deleter for use with unique_ptr. E.g., use as 81 // Deleter for use with unique_ptr. E.g., use as
108 // std::unique_ptr<Foo, base::AlignedFreeDeleter> foo; 82 // std::unique_ptr<Foo, base::AlignedFreeDeleter> foo;
109 struct AlignedFreeDeleter { 83 struct AlignedFreeDeleter {
110 inline void operator()(void* ptr) const { 84 inline void operator()(void* ptr) const {
111 AlignedFree(ptr); 85 AlignedFree(ptr);
112 } 86 }
113 }; 87 };
114 88
115 } // namespace base 89 } // namespace base
116 90
117 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ 91 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_
OLDNEW
« no previous file with comments | « base/lazy_instance_unittest.cc ('k') | base/memory/aligned_memory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698