| 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 // |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 // AlignedFree(my_array); | 27 // AlignedFree(my_array); |
| 28 // | 28 // |
| 29 // Or using scoped_ptr: | 29 // Or using scoped_ptr: |
| 30 // | 30 // |
| 31 // scoped_ptr<float, AlignedFreeDeleter> my_array( | 31 // scoped_ptr<float, AlignedFreeDeleter> my_array( |
| 32 // static_cast<float*>(AlignedAlloc(size, alignment))); | 32 // static_cast<float*>(AlignedAlloc(size, alignment))); |
| 33 | 33 |
| 34 #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_ | 34 #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_ |
| 35 #define BASE_MEMORY_ALIGNED_MEMORY_H_ | 35 #define BASE_MEMORY_ALIGNED_MEMORY_H_ |
| 36 | 36 |
| 37 #include <stddef.h> |
| 38 #include <stdint.h> |
| 39 |
| 37 #include "base/base_export.h" | 40 #include "base/base_export.h" |
| 38 #include "base/basictypes.h" | |
| 39 #include "base/compiler_specific.h" | 41 #include "base/compiler_specific.h" |
| 40 | 42 |
| 41 #if defined(COMPILER_MSVC) | 43 #if defined(COMPILER_MSVC) |
| 42 #include <malloc.h> | 44 #include <malloc.h> |
| 43 #else | 45 #else |
| 44 #include <stdlib.h> | 46 #include <stdlib.h> |
| 45 #endif | 47 #endif |
| 46 | 48 |
| 47 namespace base { | 49 namespace base { |
| 48 | 50 |
| 49 // AlignedMemory is specialized for all supported alignments. | 51 // AlignedMemory is specialized for all supported alignments. |
| 50 // Make sure we get a compiler error if someone uses an unsupported alignment. | 52 // Make sure we get a compiler error if someone uses an unsupported alignment. |
| 51 template <size_t Size, size_t ByteAlignment> | 53 template <size_t Size, size_t ByteAlignment> |
| 52 struct AlignedMemory {}; | 54 struct AlignedMemory {}; |
| 53 | 55 |
| 54 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ | 56 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ |
| 55 template <size_t Size> \ | 57 template <size_t Size> \ |
| 56 class AlignedMemory<Size, byte_alignment> { \ | 58 class AlignedMemory<Size, byte_alignment> { \ |
| 57 public: \ | 59 public: \ |
| 58 ALIGNAS(byte_alignment) uint8 data_[Size]; \ | 60 ALIGNAS(byte_alignment) uint8_t data_[Size]; \ |
| 59 void* void_data() { return static_cast<void*>(data_); } \ | 61 void* void_data() { return static_cast<void*>(data_); } \ |
| 60 const void* void_data() const { \ | 62 const void* void_data() const { return static_cast<const void*>(data_); } \ |
| 61 return static_cast<const void*>(data_); \ | 63 template <typename Type> \ |
| 62 } \ | 64 Type* data_as() { \ |
| 63 template<typename Type> \ | 65 return static_cast<Type*>(void_data()); \ |
| 64 Type* data_as() { return static_cast<Type*>(void_data()); } \ | 66 } \ |
| 65 template<typename Type> \ | 67 template <typename Type> \ |
| 66 const Type* data_as() const { \ | 68 const Type* data_as() const { \ |
| 67 return static_cast<const Type*>(void_data()); \ | 69 return static_cast<const Type*>(void_data()); \ |
| 68 } \ | 70 } \ |
| 69 private: \ | 71 \ |
| 70 void* operator new(size_t); \ | 72 private: \ |
| 71 void operator delete(void*); \ | 73 void* operator new(size_t); \ |
| 72 } | 74 void operator delete(void*); \ |
| 75 } |
| 73 | 76 |
| 74 // Specialization for all alignments is required because MSVC (as of VS 2008) | 77 // Specialization for all alignments is required because MSVC (as of VS 2008) |
| 75 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). | 78 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). |
| 76 // Greater than 4096 alignment is not supported by some compilers, so 4096 is | 79 // Greater than 4096 alignment is not supported by some compilers, so 4096 is |
| 77 // the maximum specified here. | 80 // the maximum specified here. |
| 78 BASE_DECL_ALIGNED_MEMORY(1); | 81 BASE_DECL_ALIGNED_MEMORY(1); |
| 79 BASE_DECL_ALIGNED_MEMORY(2); | 82 BASE_DECL_ALIGNED_MEMORY(2); |
| 80 BASE_DECL_ALIGNED_MEMORY(4); | 83 BASE_DECL_ALIGNED_MEMORY(4); |
| 81 BASE_DECL_ALIGNED_MEMORY(8); | 84 BASE_DECL_ALIGNED_MEMORY(8); |
| 82 BASE_DECL_ALIGNED_MEMORY(16); | 85 BASE_DECL_ALIGNED_MEMORY(16); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 105 // scoped_ptr<Foo, base::AlignedFreeDeleter> foo; | 108 // scoped_ptr<Foo, base::AlignedFreeDeleter> foo; |
| 106 struct AlignedFreeDeleter { | 109 struct AlignedFreeDeleter { |
| 107 inline void operator()(void* ptr) const { | 110 inline void operator()(void* ptr) const { |
| 108 AlignedFree(ptr); | 111 AlignedFree(ptr); |
| 109 } | 112 } |
| 110 }; | 113 }; |
| 111 | 114 |
| 112 } // namespace base | 115 } // namespace base |
| 113 | 116 |
| 114 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ | 117 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ |
| OLD | NEW |