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 // 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 // | |
22 // Alternatively, a runtime sized aligned allocation can be created: | |
23 // | |
24 // float* my_array = static_cast<float*>(AlignedAlloc(size, alignment)); | |
25 // | |
26 // // ... later, to release the memory: | |
27 // AlignedFree(my_array); | |
28 // | |
29 // Or using scoped_ptr_malloc: | |
30 // | |
31 // scoped_ptr_malloc<float, ScopedPtrAlignedFree> my_array( | |
32 // static_cast<float*>(AlignedAlloc(size, alignment))); | |
21 | 33 |
22 #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_ | 34 #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_ |
23 #define BASE_MEMORY_ALIGNED_MEMORY_H_ | 35 #define BASE_MEMORY_ALIGNED_MEMORY_H_ |
24 | 36 |
37 #include "base/base_export.h" | |
25 #include "base/basictypes.h" | 38 #include "base/basictypes.h" |
26 #include "base/compiler_specific.h" | 39 #include "base/compiler_specific.h" |
27 #include "base/logging.h" | 40 |
41 #if defined(COMPILER_MSVC) | |
42 #include <malloc.h> | |
43 #else | |
44 #include <stdlib.h> | |
45 #endif | |
28 | 46 |
29 namespace base { | 47 namespace base { |
30 | 48 |
31 // AlignedMemory is specialized for all supported alignments. | 49 // AlignedMemory is specialized for all supported alignments. |
32 // Make sure we get a compiler error if someone uses an unsupported alignment. | 50 // Make sure we get a compiler error if someone uses an unsupported alignment. |
33 template <size_t Size, size_t ByteAlignment> | 51 template <size_t Size, size_t ByteAlignment> |
34 struct AlignedMemory {}; | 52 struct AlignedMemory {}; |
35 | 53 |
36 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ | 54 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ |
37 template <size_t Size> \ | 55 template <size_t Size> \ |
38 class AlignedMemory<Size, byte_alignment> { \ | 56 class AlignedMemory<Size, byte_alignment> { \ |
39 public: \ | 57 public: \ |
40 ALIGNAS(byte_alignment) uint8 data_[Size]; \ | 58 void* void_data() { return static_cast<void*>(data_); } \ |
41 void* void_data() { return reinterpret_cast<void*>(data_); } \ | |
42 const void* void_data() const { \ | 59 const void* void_data() const { \ |
43 return reinterpret_cast<const void*>(data_); \ | 60 return static_cast<const void*>(data_); \ |
44 } \ | 61 } \ |
45 template<typename Type> \ | 62 template<typename Type> \ |
46 Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \ | 63 Type* data_as() { return static_cast<Type*>(void_data()); } \ |
47 template<typename Type> \ | 64 template<typename Type> \ |
48 const Type* data_as() const { \ | 65 const Type* data_as() const { \ |
49 return reinterpret_cast<const Type*>(void_data()); \ | 66 return static_cast<const Type*>(void_data()); \ |
50 } \ | 67 } \ |
51 private: \ | 68 private: \ |
69 ALIGNAS(byte_alignment) uint8 data_[Size]; \ | |
jbates
2012/07/24 01:30:24
Making this private actually changes AlignedMemory
| |
52 void* operator new(size_t); \ | 70 void* operator new(size_t); \ |
53 void operator delete(void*); \ | 71 void operator delete(void*); \ |
54 } | 72 } |
55 | 73 |
56 // Specialization for all alignments is required because MSVC (as of VS 2008) | 74 // Specialization for all alignments is required because MSVC (as of VS 2008) |
57 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). | 75 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). |
58 // Greater than 4096 alignment is not supported by some compilers, so 4096 is | 76 // Greater than 4096 alignment is not supported by some compilers, so 4096 is |
59 // the maximum specified here. | 77 // the maximum specified here. |
60 BASE_DECL_ALIGNED_MEMORY(1); | 78 BASE_DECL_ALIGNED_MEMORY(1); |
61 BASE_DECL_ALIGNED_MEMORY(2); | 79 BASE_DECL_ALIGNED_MEMORY(2); |
62 BASE_DECL_ALIGNED_MEMORY(4); | 80 BASE_DECL_ALIGNED_MEMORY(4); |
63 BASE_DECL_ALIGNED_MEMORY(8); | 81 BASE_DECL_ALIGNED_MEMORY(8); |
64 BASE_DECL_ALIGNED_MEMORY(16); | 82 BASE_DECL_ALIGNED_MEMORY(16); |
65 BASE_DECL_ALIGNED_MEMORY(32); | 83 BASE_DECL_ALIGNED_MEMORY(32); |
66 BASE_DECL_ALIGNED_MEMORY(64); | 84 BASE_DECL_ALIGNED_MEMORY(64); |
67 BASE_DECL_ALIGNED_MEMORY(128); | 85 BASE_DECL_ALIGNED_MEMORY(128); |
68 BASE_DECL_ALIGNED_MEMORY(256); | 86 BASE_DECL_ALIGNED_MEMORY(256); |
69 BASE_DECL_ALIGNED_MEMORY(512); | 87 BASE_DECL_ALIGNED_MEMORY(512); |
70 BASE_DECL_ALIGNED_MEMORY(1024); | 88 BASE_DECL_ALIGNED_MEMORY(1024); |
71 BASE_DECL_ALIGNED_MEMORY(2048); | 89 BASE_DECL_ALIGNED_MEMORY(2048); |
72 BASE_DECL_ALIGNED_MEMORY(4096); | 90 BASE_DECL_ALIGNED_MEMORY(4096); |
73 | 91 |
74 } // base | 92 #undef BASE_DECL_ALIGNED_MEMORY |
93 | |
94 BASE_EXPORT void* AlignedAlloc(size_t size, size_t alignment); | |
95 | |
96 inline void AlignedFree(void* ptr) { | |
97 #if defined(COMPILER_MSVC) | |
98 _aligned_free(ptr); | |
99 #else | |
100 free(ptr); | |
101 #endif | |
102 } | |
103 | |
104 // Helper class for use with scoped_ptr_malloc. | |
105 class BASE_EXPORT ScopedPtrAlignedFree { | |
106 public: | |
107 inline void operator()(void* ptr) const { | |
108 AlignedFree(ptr); | |
109 } | |
110 }; | |
111 | |
112 } // namespace base | |
75 | 113 |
76 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ | 114 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ |
OLD | NEW |