Chromium Code Reviews| 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 = reinterpret_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, ScopedAlignedFree> my_array( | |
| 32 // reinterpret_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 #if defined(COMPILER_MSVC) || defined(OS_ANDROID) | |
| 38 #include <malloc.h> | |
| 39 #endif | |
| 40 #include <stdlib.h> | |
| 41 | |
| 42 #include "base/base_export.h" | |
| 25 #include "base/basictypes.h" | 43 #include "base/basictypes.h" |
| 26 #include "base/compiler_specific.h" | 44 #include "base/compiler_specific.h" |
| 27 #include "base/logging.h" | |
| 28 | 45 |
| 29 namespace base { | 46 namespace base { |
| 30 | 47 |
| 31 // AlignedMemory is specialized for all supported alignments. | 48 // AlignedMemory is specialized for all supported alignments. |
| 32 // Make sure we get a compiler error if someone uses an unsupported alignment. | 49 // Make sure we get a compiler error if someone uses an unsupported alignment. |
| 33 template <size_t Size, size_t ByteAlignment> | 50 template <size_t Size, size_t ByteAlignment> |
|
Jeffrey Yasskin
2012/07/21 20:38:49
Could you revert the argument order flip, now that
DaleCurtis
2012/07/21 22:18:41
I already did; this is the original order.
Jeffrey Yasskin
2012/07/21 22:42:07
Oops, sorry, I compared the wrong versions of the
| |
| 34 struct AlignedMemory {}; | 51 struct AlignedMemory {}; |
| 35 | 52 |
| 36 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ | 53 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ |
| 37 template <size_t Size> \ | 54 template <size_t Size> \ |
| 38 class AlignedMemory<Size, byte_alignment> { \ | 55 class AlignedMemory<Size, byte_alignment> { \ |
| 39 public: \ | 56 public: \ |
| 40 ALIGNAS(byte_alignment) uint8 data_[Size]; \ | |
| 41 void* void_data() { return reinterpret_cast<void*>(data_); } \ | 57 void* void_data() { return reinterpret_cast<void*>(data_); } \ |
| 42 const void* void_data() const { \ | 58 const void* void_data() const { \ |
| 43 return reinterpret_cast<const void*>(data_); \ | 59 return reinterpret_cast<const void*>(data_); \ |
| 44 } \ | 60 } \ |
| 45 template<typename Type> \ | 61 template<typename Type> \ |
| 46 Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \ | 62 Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \ |
| 47 template<typename Type> \ | 63 template<typename Type> \ |
| 48 const Type* data_as() const { \ | 64 const Type* data_as() const { \ |
| 49 return reinterpret_cast<const Type*>(void_data()); \ | 65 return reinterpret_cast<const Type*>(void_data()); \ |
| 50 } \ | 66 } \ |
| 51 private: \ | 67 private: \ |
| 68 ALIGNAS(byte_alignment) uint8 data_[Size]; \ | |
| 52 void* operator new(size_t); \ | 69 void* operator new(size_t); \ |
| 53 void operator delete(void*); \ | 70 void operator delete(void*); \ |
| 54 } | 71 } |
| 55 | 72 |
| 56 // Specialization for all alignments is required because MSVC (as of VS 2008) | 73 // Specialization for all alignments is required because MSVC (as of VS 2008) |
| 57 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). | 74 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). |
| 58 // Greater than 4096 alignment is not supported by some compilers, so 4096 is | 75 // Greater than 4096 alignment is not supported by some compilers, so 4096 is |
| 59 // the maximum specified here. | 76 // the maximum specified here. |
| 60 BASE_DECL_ALIGNED_MEMORY(1); | 77 BASE_DECL_ALIGNED_MEMORY(1); |
| 61 BASE_DECL_ALIGNED_MEMORY(2); | 78 BASE_DECL_ALIGNED_MEMORY(2); |
| 62 BASE_DECL_ALIGNED_MEMORY(4); | 79 BASE_DECL_ALIGNED_MEMORY(4); |
| 63 BASE_DECL_ALIGNED_MEMORY(8); | 80 BASE_DECL_ALIGNED_MEMORY(8); |
| 64 BASE_DECL_ALIGNED_MEMORY(16); | 81 BASE_DECL_ALIGNED_MEMORY(16); |
| 65 BASE_DECL_ALIGNED_MEMORY(32); | 82 BASE_DECL_ALIGNED_MEMORY(32); |
| 66 BASE_DECL_ALIGNED_MEMORY(64); | 83 BASE_DECL_ALIGNED_MEMORY(64); |
| 67 BASE_DECL_ALIGNED_MEMORY(128); | 84 BASE_DECL_ALIGNED_MEMORY(128); |
| 68 BASE_DECL_ALIGNED_MEMORY(256); | 85 BASE_DECL_ALIGNED_MEMORY(256); |
| 69 BASE_DECL_ALIGNED_MEMORY(512); | 86 BASE_DECL_ALIGNED_MEMORY(512); |
| 70 BASE_DECL_ALIGNED_MEMORY(1024); | 87 BASE_DECL_ALIGNED_MEMORY(1024); |
| 71 BASE_DECL_ALIGNED_MEMORY(2048); | 88 BASE_DECL_ALIGNED_MEMORY(2048); |
| 72 BASE_DECL_ALIGNED_MEMORY(4096); | 89 BASE_DECL_ALIGNED_MEMORY(4096); |
| 73 | 90 |
| 74 } // base | 91 #undef BASE_DECL_ALIGNED_MEMORY |
| 92 | |
| 93 BASE_EXPORT void* AlignedAlloc(size_t size, size_t alignment); | |
|
Jeffrey Yasskin
2012/07/21 20:38:49
Please make this signature match C11's aligned_all
DaleCurtis
2012/07/21 22:18:41
I used this since it matches the original AlignedM
Jeffrey Yasskin
2012/07/21 22:42:07
It does seem reasonable to keep them the same. Unf
| |
| 94 | |
| 95 inline void AlignedFree(void* ptr) { | |
| 96 #if defined(COMPILER_MSVC) | |
| 97 _aligned_free(ptr); | |
| 98 #else | |
| 99 free(ptr); | |
| 100 #endif | |
| 101 } | |
| 102 | |
| 103 // Helper class for use with scoped_ptr_malloc. | |
| 104 class BASE_EXPORT ScopedAlignedFree { | |
|
Jeffrey Yasskin
2012/07/21 20:38:49
This name seems awkward, but I don't have a concre
DaleCurtis
2012/07/21 22:18:41
AlignedFreeHelper, AlignedFreeScoper? The default
Jeffrey Yasskin
2012/07/21 22:42:07
ScopedPtrAlignedFree seems the least bad of those,
DaleCurtis
2012/07/22 00:45:40
Changed to ScopedPtrAlignedFree for consistency wi
| |
| 105 public: | |
| 106 inline void operator()(void* x) const { | |
| 107 AlignedFree(x); | |
| 108 } | |
| 109 }; | |
| 110 | |
| 111 } // namespace base | |
| 75 | 112 |
| 76 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ | 113 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ |
| OLD | NEW |