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

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: Comments. Remove unused imports. 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
« no previous file with comments | « base/lazy_instance.h ('k') | base/memory/aligned_memory_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
Jeffrey Yasskin 2012/07/20 16:41:42 If _XOPEN_SOURCE was already defined as <600, this
DaleCurtis 2012/07/20 18:25:28 Well if we keep the extern below this isn't really
Jeffrey Yasskin 2012/07/20 23:36:56 Make sure linux and mac set _XOPEN_SOURCE >=600 or
DaleCurtis 2012/07/21 03:04:40 I'm wary of setting this all over the code base an
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 "build/build_config.h"
46
47 #if defined(COMPILER_MSVC)
48 #define INTERNAL_ALIGNED_MALLOC(ptr, align, size) \
49 ptr = _aligned_malloc(size, align)
50 #define INTERNAL_ALIGNED_FREE(ptr) \
51 _aligned_free(ptr)
52 #else
53 #if defined(OS_ANDROID)
54 #define INTERNAL_ALIGNED_MALLOC(ptr, align, size) \
55 ptr = memalign(align, size)
Jeffrey Yasskin 2012/07/20 16:41:42 http://linux.die.net/man/3/memalign says, "Some sy
DaleCurtis 2012/07/20 18:25:28 Sadly, NDK documentation is incredibly sparse. I t
DaleCurtis 2012/07/20 20:33:37 Actually, NOTREACHED is probably a bad idea, we co
Jeffrey Yasskin 2012/07/20 23:36:56 Yeah, NOTREACHED would be bad. Your option is an o
DaleCurtis 2012/07/21 03:04:40 Per https://github.com/android/platform_bionic/com
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
Jeffrey Yasskin 2012/07/20 16:41:42 What error do they give? It's very weird that <std
DaleCurtis 2012/07/20 18:25:28 "error: ‘posix_memalign’ was not declared in this
Jeffrey Yasskin 2012/07/20 23:36:56 Definitely figure out what's going wrong with the
DaleCurtis 2012/07/21 03:04:40 Turns out this is a NaCl issue, http://code.google
jvoung (off chromium) 2012/07/21 04:45:26 Hmm, so this is while building the nacl-ized versi
DaleCurtis 2012/07/21 18:30:32 Thanks for the quick response! SGTM. I'll add OS_N
Jeffrey Yasskin 2012/07/21 20:38:48 Thanks for the reply. Note that AFAIK Dale wasn't
DaleCurtis 2012/07/22 00:45:40 Looks like the extern definition is still necessar
DaleCurtis 2012/07/22 01:16:13 Actually scratch that, I just forgot the header.
jvoung (off chromium) 2012/07/23 17:06:36 Hrmmm... just checked the newlib sources. I don't
jvoung (off chromium) 2012/07/23 17:06:36 My guess is that the trybots are also building aga
dmichael (off chromium) 2012/07/23 17:19:28 We're building some bits of Chrome in base, ipc, a
jvoung - send to chromium... 2012/07/23 17:25:23 n/m, it's in malloc.h there
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) do { \
62 if (posix_memalign(&ptr, align, size)) \
63 ptr = NULL; \
64 } while (0)
65 #endif
66 #define INTERNAL_ALIGNED_FREE(ptr) \
67 free(ptr)
68 #endif
28 69
29 namespace base { 70 namespace base {
30 71
31 // AlignedMemory is specialized for all supported alignments. 72 // AlignedMemory is specialized for all supported alignments.
32 // Make sure we get a compiler error if someone uses an unsupported alignment. 73 // Make sure we get a compiler error if someone uses an unsupported alignment.
33 template <size_t Size, size_t ByteAlignment> 74 template <size_t ByteAlignment, size_t Size = 0>
34 struct AlignedMemory {}; 75 struct AlignedMemory {};
35 76
36 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ 77 // Compile time based aligned memory allocator.
78 #define BASE_DECL_ALIGNED_MEMORY_STATIC(byte_alignment) \
37 template <size_t Size> \ 79 template <size_t Size> \
38 class AlignedMemory<Size, byte_alignment> { \ 80 class AlignedMemory<byte_alignment, Size> { \
39 public: \ 81 public: \
40 ALIGNAS(byte_alignment) uint8 data_[Size]; \
41 void* void_data() { return reinterpret_cast<void*>(data_); } \ 82 void* void_data() { return reinterpret_cast<void*>(data_); } \
42 const void* void_data() const { \ 83 const void* void_data() const { \
43 return reinterpret_cast<const void*>(data_); \ 84 return reinterpret_cast<const void*>(data_); \
44 } \ 85 } \
45 template<typename Type> \ 86 template<typename Type> \
46 Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \ 87 Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \
47 template<typename Type> \ 88 template<typename Type> \
48 const Type* data_as() const { \ 89 const Type* data_as() const { \
49 return reinterpret_cast<const Type*>(void_data()); \ 90 return reinterpret_cast<const Type*>(void_data()); \
50 } \ 91 } \
51 private: \ 92 private: \
93 ALIGNAS(byte_alignment) uint8 data_[Size]; \
52 void* operator new(size_t); \ 94 void* operator new(size_t); \
53 void operator delete(void*); \ 95 void operator delete(void*); \
54 } 96 }
55 97
98 // Run time based aligned memory allocator. Since the alignment allocators may
Jeffrey Yasskin 2012/07/20 16:41:42 Why do you want the posix_memalign wrapper to be n
DaleCurtis 2012/07/20 18:25:28 1. Type safety. scoped_ptr_malloc prevents you fro
Jeffrey Yasskin 2012/07/20 23:36:56 What you have doesn't actually work for this becau
DaleCurtis 2012/07/21 03:04:40 After discussing over chat with Jeffrey I came to
99 // fail for non-memory related reasons, the code will force a crash immediately
100 // via NULL dereference if the calls fail for any reason; maintaining consistent
101 // behavior with a normal failed allocation in Chrome.
102 #define BASE_DECL_ALIGNED_MEMORY_DYNAMIC(byte_alignment) \
103 template <> \
104 class AlignedMemory<byte_alignment, 0> { \
105 public: \
106 explicit AlignedMemory(size_t size) { \
107 INTERNAL_ALIGNED_MALLOC(data_, byte_alignment, size); \
108 /* If you crashed here, your aligned allocation is incorrect. */ \
109 if (!data_) *reinterpret_cast<uint8*>(data_) = 0; \
Jeffrey Yasskin 2012/07/20 16:41:42 Assigning to a null pointer is undefined behavior
DaleCurtis 2012/07/20 18:25:28 jbates was worried about the weight of CHECK/DCHEC
Jeffrey Yasskin 2012/07/20 23:36:56 Anything other than dereferencing null would be fi
DaleCurtis 2012/07/21 03:04:40 Since this is now in a .cc file, switched to CHECK
110 } \
111 ~AlignedMemory() { if (data_) INTERNAL_ALIGNED_FREE(data_); } \
112 void* void_data() { return data_; } \
113 const void* void_data() const { \
114 return reinterpret_cast<const void*>(data_); \
115 } \
116 template<typename Type> \
117 Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \
118 template<typename Type> \
119 const Type* data_as() const { \
120 return reinterpret_cast<const Type*>(void_data()); \
121 } \
122 private: \
123 void* data_; \
124 void* operator new(size_t); \
125 void operator delete(void*); \
126 DISALLOW_COPY_AND_ASSIGN(AlignedMemory); \
127 }
128
129 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \
130 BASE_DECL_ALIGNED_MEMORY_STATIC(byte_alignment); \
131 BASE_DECL_ALIGNED_MEMORY_DYNAMIC(byte_alignment)
132
56 // Specialization for all alignments is required because MSVC (as of VS 2008) 133 // Specialization for all alignments is required because MSVC (as of VS 2008)
57 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). 134 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param).
58 // Greater than 4096 alignment is not supported by some compilers, so 4096 is 135 // Greater than 4096 alignment is not supported by some compilers, so 4096 is
59 // the maximum specified here. 136 // the maximum specified here.
60 BASE_DECL_ALIGNED_MEMORY(1); 137 BASE_DECL_ALIGNED_MEMORY_STATIC(1);
61 BASE_DECL_ALIGNED_MEMORY(2); 138 BASE_DECL_ALIGNED_MEMORY_STATIC(2);
139 // Minimum alignment for dynamic allocations is sizeof(void*) which is 4 on 32
140 // bit platforms and 8 on 64 bit. On NaCl sizeof(void*) is always 4.
141 #if defined(ARCH_CPU_64_BITS) && !defined(OS_NACL)
142 BASE_DECL_ALIGNED_MEMORY_STATIC(4);
143 COMPILE_ASSERT(sizeof(void*) == 8, Minimum_Alignment_Should_Be_8);
144 #else
145 COMPILE_ASSERT(sizeof(void*) == 4, Minimum_Alignment_Should_Be_4);
62 BASE_DECL_ALIGNED_MEMORY(4); 146 BASE_DECL_ALIGNED_MEMORY(4);
147 #endif
63 BASE_DECL_ALIGNED_MEMORY(8); 148 BASE_DECL_ALIGNED_MEMORY(8);
64 BASE_DECL_ALIGNED_MEMORY(16); 149 BASE_DECL_ALIGNED_MEMORY(16);
65 BASE_DECL_ALIGNED_MEMORY(32); 150 BASE_DECL_ALIGNED_MEMORY(32);
66 BASE_DECL_ALIGNED_MEMORY(64); 151 BASE_DECL_ALIGNED_MEMORY(64);
67 BASE_DECL_ALIGNED_MEMORY(128); 152 BASE_DECL_ALIGNED_MEMORY(128);
68 BASE_DECL_ALIGNED_MEMORY(256); 153 BASE_DECL_ALIGNED_MEMORY(256);
69 BASE_DECL_ALIGNED_MEMORY(512); 154 BASE_DECL_ALIGNED_MEMORY(512);
70 BASE_DECL_ALIGNED_MEMORY(1024); 155 BASE_DECL_ALIGNED_MEMORY(1024);
71 BASE_DECL_ALIGNED_MEMORY(2048); 156 BASE_DECL_ALIGNED_MEMORY(2048);
72 BASE_DECL_ALIGNED_MEMORY(4096); 157 BASE_DECL_ALIGNED_MEMORY(4096);
73 158
159 #undef INTERNAL_ALIGNED_MALLOC
160 #undef INTERNAL_ALIGNED_FREE
161 #undef BASE_DECL_ALIGNED_MEMORY_STATIC
162 #undef BASE_DECL_ALIGNED_MEMORY_DYNAMIC
163 #undef BASE_DECL_ALIGNED_MEMORY
164
74 } // base 165 } // base
75 166
76 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ 167 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_
OLDNEW
« no previous file with comments | « base/lazy_instance.h ('k') | base/memory/aligned_memory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698