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 10 matching lines...) Expand all Loading... |
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: | 29 // Or using scoped_ptr: |
30 // | 30 // |
31 // scoped_ptr<float, AlignedFreeDeleter> my_array( | 31 // std::unique_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> | 37 #include <stddef.h> |
38 #include <stdint.h> | 38 #include <stdint.h> |
39 | 39 |
40 #include "base/base_export.h" | 40 #include "base/base_export.h" |
41 #include "base/compiler_specific.h" | 41 #include "base/compiler_specific.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 98 |
99 inline void AlignedFree(void* ptr) { | 99 inline void AlignedFree(void* ptr) { |
100 #if defined(COMPILER_MSVC) | 100 #if defined(COMPILER_MSVC) |
101 _aligned_free(ptr); | 101 _aligned_free(ptr); |
102 #else | 102 #else |
103 free(ptr); | 103 free(ptr); |
104 #endif | 104 #endif |
105 } | 105 } |
106 | 106 |
107 // Deleter for use with scoped_ptr. E.g., use as | 107 // Deleter for use with scoped_ptr. E.g., use as |
108 // scoped_ptr<Foo, base::AlignedFreeDeleter> foo; | 108 // std::unique_ptr<Foo, base::AlignedFreeDeleter> foo; |
109 struct AlignedFreeDeleter { | 109 struct AlignedFreeDeleter { |
110 inline void operator()(void* ptr) const { | 110 inline void operator()(void* ptr) const { |
111 AlignedFree(ptr); | 111 AlignedFree(ptr); |
112 } | 112 } |
113 }; | 113 }; |
114 | 114 |
115 } // namespace base | 115 } // namespace base |
116 | 116 |
117 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ | 117 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ |
OLD | NEW |