| 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 // | 21 // |
| 22 // Alternatively, a runtime sized aligned allocation can be created: | 22 // Alternatively, a runtime sized aligned allocation can be created: |
| 23 // | 23 // |
| 24 // float* my_array = static_cast<float*>(AlignedAlloc(size, alignment)); | 24 // float* my_array = static_cast<float*>(AlignedAlloc(size, alignment)); |
| 25 // | 25 // |
| 26 // // ... later, to release the memory: | 26 // // ... later, to release the memory: |
| 27 // AlignedFree(my_array); | 27 // AlignedFree(my_array); |
| 28 // | 28 // |
| 29 // Or using scoped_ptr_malloc: | 29 // Or using scoped_ptr: |
| 30 // | 30 // |
| 31 // scoped_ptr_malloc<float, ScopedPtrAlignedFree> 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 "base/base_export.h" | 37 #include "base/base_export.h" |
| 38 #include "base/basictypes.h" | 38 #include "base/basictypes.h" |
| 39 #include "base/compiler_specific.h" | 39 #include "base/compiler_specific.h" |
| 40 | 40 |
| 41 #if defined(COMPILER_MSVC) | 41 #if defined(COMPILER_MSVC) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 // scoped_ptr<Foo, base::AlignedFreeDeleter> foo; | 105 // scoped_ptr<Foo, base::AlignedFreeDeleter> foo; |
| 106 struct AlignedFreeDeleter { | 106 struct AlignedFreeDeleter { |
| 107 inline void operator()(void* ptr) const { | 107 inline void operator()(void* ptr) const { |
| 108 AlignedFree(ptr); | 108 AlignedFree(ptr); |
| 109 } | 109 } |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 } // namespace base | 112 } // namespace base |
| 113 | 113 |
| 114 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ | 114 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ |
| OLD | NEW |