Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(257)

Side by Side Diff: base/memory/aligned_memory.h

Issue 10796020: Upgrade AlignedMemory to support dynamic allocations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Documentation. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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<ALIGNOF(MyClass), sizeof(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 AlignedMemory instance can be created:
23 //
24 // AlignedMemory<|alignment|> my_memory(|size|);
25 //
26 // // ... use it:
27 // float* float_array = my_memory.data_as<float>();
28 //
29 // Memory will be freed when AlignedMemory is destructed.
21 30
22 #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_ 31 #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_
23 #define BASE_MEMORY_ALIGNED_MEMORY_H_ 32 #define BASE_MEMORY_ALIGNED_MEMORY_H_
24 33
34 #if !defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 600)
35 #define _XOPEN_SOURCE 600 // for posix_memalign
36 #endif
37
38 #if defined(COMPILER_MSVC) || defined(OS_ANDROID)
39 #include <malloc.h>
40 #endif
41 #include <stdlib.h>
42
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" 45 #include "base/logging.h"
28 46
47 #if defined(COMPILER_MSVC)
48 #define INTERNAL_ALIGNED_MALLOC(ptr, align, size) \
49 ptr = _aligned_malloc(size, align);
jbates 2012/07/19 03:45:37 typically don't put the semicolon here
DaleCurtis 2012/07/20 02:38:56 Done.
50 #define INTERNAL_ALIGNED_FREE(ptr) \
51 _aligned_free(ptr);
jbates 2012/07/19 03:45:37 no semicolon?
DaleCurtis 2012/07/20 02:38:56 Done.
52 #else
53 #if defined(OS_ANDROID)
54 #define INTERNAL_ALIGNED_MALLOC(ptr, align, size) \
55 ptr = memalign(align, size);
jbates 2012/07/19 03:45:37 no semicolon?
DaleCurtis 2012/07/20 02:38:56 Done.
56 #else
57 // TODO(dalecurtis): Not sure why this is necessary, but without it none of the
58 // Buildbots can find the posix_memalign definition. Everything builds fine
59 // locally with or without it.
60 extern int posix_memalign(void **ptr, size_t align, size_t size);
61 #define INTERNAL_ALIGNED_MALLOC(ptr, align, size) \
jbates 2012/07/19 03:45:37 just to be pedantic in case someone uses this in a
DaleCurtis 2012/07/20 02:38:56 Done.
62 if (posix_memalign(&ptr, align, size)) \
63 ptr = NULL;
64 #endif
65 #define INTERNAL_ALIGNED_FREE(ptr) \
66 free(ptr);
jbates 2012/07/19 03:45:37 no semicolon?
DaleCurtis 2012/07/20 02:38:56 Done.
67 #endif
68
29 namespace base { 69 namespace base {
30 70
31 // AlignedMemory is specialized for all supported alignments. 71 // AlignedMemory is specialized for all supported alignments.
32 // Make sure we get a compiler error if someone uses an unsupported alignment. 72 // Make sure we get a compiler error if someone uses an unsupported alignment.
33 template <size_t Size, size_t ByteAlignment> 73 template <size_t ByteAlignment, size_t Size = 0>
34 struct AlignedMemory {}; 74 struct AlignedMemory {};
35 75
36 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ 76 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \
37 template <size_t Size> \ 77 template <size_t Size> \
38 class AlignedMemory<Size, byte_alignment> { \ 78 class AlignedMemory<byte_alignment, Size> { \
39 public: \ 79 public: \
40 ALIGNAS(byte_alignment) uint8 data_[Size]; \
41 void* void_data() { return reinterpret_cast<void*>(data_); } \ 80 void* void_data() { return reinterpret_cast<void*>(data_); } \
42 const void* void_data() const { \ 81 const void* void_data() const { \
43 return reinterpret_cast<const void*>(data_); \ 82 return reinterpret_cast<const void*>(data_); \
44 } \ 83 } \
45 template<typename Type> \ 84 template<typename Type> \
46 Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \ 85 Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \
47 template<typename Type> \ 86 template<typename Type> \
48 const Type* data_as() const { \ 87 const Type* data_as() const { \
49 return reinterpret_cast<const Type*>(void_data()); \ 88 return reinterpret_cast<const Type*>(void_data()); \
50 } \ 89 } \
51 private: \ 90 private: \
91 ALIGNAS(byte_alignment) uint8 data_[Size]; \
52 void* operator new(size_t); \ 92 void* operator new(size_t); \
53 void operator delete(void*); \ 93 void operator delete(void*); \
54 } 94 }; \
95 template <> \
96 class AlignedMemory<byte_alignment, 0> { \
97 public: \
98 explicit AlignedMemory(size_t size) { \
99 DCHECK_GT(size, static_cast<size_t>(0)); \
100 DCHECK_EQ(byte_alignment & (byte_alignment - 1), 0); \
jbates 2012/07/19 03:45:37 is this necessary here since all the alignments ar
DaleCurtis 2012/07/20 02:38:56 Good point. Removed.
101 DCHECK_EQ(byte_alignment % sizeof(void*), static_cast<size_t>(0)); \
jbates 2012/07/19 03:45:37 would it make sense to just do a MAX(byte_alignmen
DaleCurtis 2012/07/20 02:38:56 I've removed all of this in favor of forcing a NUL
102 INTERNAL_ALIGNED_MALLOC(data_, byte_alignment, size) \
103 } \
104 ~AlignedMemory() { \
105 if (data_) \
106 INTERNAL_ALIGNED_FREE(data_) \
107 } \
108 void* void_data() { return data_; } \
109 const void* void_data() const { \
110 return reinterpret_cast<const void*>(data_); \
111 } \
112 template<typename Type> \
113 Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \
114 template<typename Type> \
115 const Type* data_as() const { \
116 return reinterpret_cast<const Type*>(void_data()); \
117 } \
118 private: \
119 void* data_; \
120 void* operator new(size_t); \
121 void operator delete(void*); \
122 };
55 123
56 // Specialization for all alignments is required because MSVC (as of VS 2008) 124 // Specialization for all alignments is required because MSVC (as of VS 2008)
57 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). 125 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param).
58 // Greater than 4096 alignment is not supported by some compilers, so 4096 is 126 // Greater than 4096 alignment is not supported by some compilers, so 4096 is
59 // the maximum specified here. 127 // the maximum specified here.
60 BASE_DECL_ALIGNED_MEMORY(1); 128 BASE_DECL_ALIGNED_MEMORY(1)
61 BASE_DECL_ALIGNED_MEMORY(2); 129 BASE_DECL_ALIGNED_MEMORY(2)
62 BASE_DECL_ALIGNED_MEMORY(4); 130 BASE_DECL_ALIGNED_MEMORY(4)
63 BASE_DECL_ALIGNED_MEMORY(8); 131 BASE_DECL_ALIGNED_MEMORY(8)
64 BASE_DECL_ALIGNED_MEMORY(16); 132 BASE_DECL_ALIGNED_MEMORY(16)
65 BASE_DECL_ALIGNED_MEMORY(32); 133 BASE_DECL_ALIGNED_MEMORY(32)
66 BASE_DECL_ALIGNED_MEMORY(64); 134 BASE_DECL_ALIGNED_MEMORY(64)
67 BASE_DECL_ALIGNED_MEMORY(128); 135 BASE_DECL_ALIGNED_MEMORY(128)
68 BASE_DECL_ALIGNED_MEMORY(256); 136 BASE_DECL_ALIGNED_MEMORY(256)
69 BASE_DECL_ALIGNED_MEMORY(512); 137 BASE_DECL_ALIGNED_MEMORY(512)
70 BASE_DECL_ALIGNED_MEMORY(1024); 138 BASE_DECL_ALIGNED_MEMORY(1024)
71 BASE_DECL_ALIGNED_MEMORY(2048); 139 BASE_DECL_ALIGNED_MEMORY(2048)
72 BASE_DECL_ALIGNED_MEMORY(4096); 140 BASE_DECL_ALIGNED_MEMORY(4096)
141
142 #undef INTERNAL_ALIGNED_MALLOC
143 #undef INTERNAL_ALIGNED_FREE
144 #undef BASE_DECL_ALIGNED_MEMORY
73 145
74 } // base 146 } // base
75 147
76 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ 148 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698