Chromium Code Reviews| Index: base/memory/aligned_memory.h |
| diff --git a/base/memory/aligned_memory.h b/base/memory/aligned_memory.h |
| index 0a176347a6dd47768c20c9585870fcd9cef417f1..2cb2929c0eb807eb883c6145be600290bac800e2 100644 |
| --- a/base/memory/aligned_memory.h |
| +++ b/base/memory/aligned_memory.h |
| @@ -8,7 +8,7 @@ |
| // is constructed and destructed (you don't want static initialization and |
| // destruction), use AlignedMemory: |
| // |
| -// static AlignedMemory<sizeof(MyClass), ALIGNOF(MyClass)> my_class; |
| +// static AlignedMemory<ALIGNOF(MyClass), sizeof(MyClass)> my_class; |
| // |
| // // ... at runtime: |
| // new(my_class.void_data()) MyClass(); |
| @@ -18,26 +18,67 @@ |
| // |
| // // ... later, to destruct my_class: |
| // my_class.data_as<MyClass>()->MyClass::~MyClass(); |
| +// |
| +// Alternatively, a runtime sized AlignedMemory instance can be created: |
| +// |
| +// AlignedMemory<|alignment|> my_memory(|size|); |
| +// |
| +// // ... use it: |
| +// float* float_array = my_memory.data_as<float>(); |
| +// |
| +// Memory will be freed when AlignedMemory is destructed. |
| #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_ |
| #define BASE_MEMORY_ALIGNED_MEMORY_H_ |
| +#if !defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 600) |
| +#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
|
| +#endif |
| + |
| +#if defined(COMPILER_MSVC) || defined(OS_ANDROID) |
| +#include <malloc.h> |
| +#endif |
| +#include <stdlib.h> |
| + |
| #include "base/basictypes.h" |
| #include "base/compiler_specific.h" |
| -#include "base/logging.h" |
| +#include "build/build_config.h" |
| + |
| +#if defined(COMPILER_MSVC) |
| +#define INTERNAL_ALIGNED_MALLOC(ptr, align, size) \ |
| + ptr = _aligned_malloc(size, align) |
| +#define INTERNAL_ALIGNED_FREE(ptr) \ |
| + _aligned_free(ptr) |
| +#else |
| +#if defined(OS_ANDROID) |
| +#define INTERNAL_ALIGNED_MALLOC(ptr, align, size) \ |
| + 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
|
| +#else |
| +// TODO(dalecurtis): Not sure why this is necessary, but without it none of the |
| +// 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
|
| +// locally with or without it. |
| +extern int posix_memalign(void **ptr, size_t align, size_t size); |
| +#define INTERNAL_ALIGNED_MALLOC(ptr, align, size) do { \ |
| + if (posix_memalign(&ptr, align, size)) \ |
| + ptr = NULL; \ |
| + } while (0) |
| +#endif |
| +#define INTERNAL_ALIGNED_FREE(ptr) \ |
| + free(ptr) |
| +#endif |
| namespace base { |
| // AlignedMemory is specialized for all supported alignments. |
| // Make sure we get a compiler error if someone uses an unsupported alignment. |
| -template <size_t Size, size_t ByteAlignment> |
| +template <size_t ByteAlignment, size_t Size = 0> |
| struct AlignedMemory {}; |
| -#define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ |
| +// Compile time based aligned memory allocator. |
| +#define BASE_DECL_ALIGNED_MEMORY_STATIC(byte_alignment) \ |
| template <size_t Size> \ |
| - class AlignedMemory<Size, byte_alignment> { \ |
| + class AlignedMemory<byte_alignment, Size> { \ |
| public: \ |
| - ALIGNAS(byte_alignment) uint8 data_[Size]; \ |
| void* void_data() { return reinterpret_cast<void*>(data_); } \ |
| const void* void_data() const { \ |
| return reinterpret_cast<const void*>(data_); \ |
| @@ -49,17 +90,61 @@ struct AlignedMemory {}; |
| return reinterpret_cast<const Type*>(void_data()); \ |
| } \ |
| private: \ |
| + ALIGNAS(byte_alignment) uint8 data_[Size]; \ |
| + void* operator new(size_t); \ |
| + void operator delete(void*); \ |
| + } |
| + |
| +// 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
|
| +// fail for non-memory related reasons, the code will force a crash immediately |
| +// via NULL dereference if the calls fail for any reason; maintaining consistent |
| +// behavior with a normal failed allocation in Chrome. |
| +#define BASE_DECL_ALIGNED_MEMORY_DYNAMIC(byte_alignment) \ |
| + template <> \ |
| + class AlignedMemory<byte_alignment, 0> { \ |
| + public: \ |
| + explicit AlignedMemory(size_t size) { \ |
| + INTERNAL_ALIGNED_MALLOC(data_, byte_alignment, size); \ |
| + /* If you crashed here, your aligned allocation is incorrect. */ \ |
| + 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
|
| + } \ |
| + ~AlignedMemory() { if (data_) INTERNAL_ALIGNED_FREE(data_); } \ |
| + void* void_data() { return data_; } \ |
| + const void* void_data() const { \ |
| + return reinterpret_cast<const void*>(data_); \ |
| + } \ |
| + template<typename Type> \ |
| + Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \ |
| + template<typename Type> \ |
| + const Type* data_as() const { \ |
| + return reinterpret_cast<const Type*>(void_data()); \ |
| + } \ |
| + private: \ |
| + void* data_; \ |
| void* operator new(size_t); \ |
| void operator delete(void*); \ |
| + DISALLOW_COPY_AND_ASSIGN(AlignedMemory); \ |
| } |
| +#define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ |
| + BASE_DECL_ALIGNED_MEMORY_STATIC(byte_alignment); \ |
| + BASE_DECL_ALIGNED_MEMORY_DYNAMIC(byte_alignment) |
| + |
| // Specialization for all alignments is required because MSVC (as of VS 2008) |
| // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). |
| // Greater than 4096 alignment is not supported by some compilers, so 4096 is |
| // the maximum specified here. |
| -BASE_DECL_ALIGNED_MEMORY(1); |
| -BASE_DECL_ALIGNED_MEMORY(2); |
| +BASE_DECL_ALIGNED_MEMORY_STATIC(1); |
| +BASE_DECL_ALIGNED_MEMORY_STATIC(2); |
| +// Minimum alignment for dynamic allocations is sizeof(void*) which is 4 on 32 |
| +// bit platforms and 8 on 64 bit. On NaCl sizeof(void*) is always 4. |
| +#if defined(ARCH_CPU_64_BITS) && !defined(OS_NACL) |
| +BASE_DECL_ALIGNED_MEMORY_STATIC(4); |
| +COMPILE_ASSERT(sizeof(void*) == 8, Minimum_Alignment_Should_Be_8); |
| +#else |
| +COMPILE_ASSERT(sizeof(void*) == 4, Minimum_Alignment_Should_Be_4); |
| BASE_DECL_ALIGNED_MEMORY(4); |
| +#endif |
| BASE_DECL_ALIGNED_MEMORY(8); |
| BASE_DECL_ALIGNED_MEMORY(16); |
| BASE_DECL_ALIGNED_MEMORY(32); |
| @@ -71,6 +156,12 @@ BASE_DECL_ALIGNED_MEMORY(1024); |
| BASE_DECL_ALIGNED_MEMORY(2048); |
| BASE_DECL_ALIGNED_MEMORY(4096); |
| +#undef INTERNAL_ALIGNED_MALLOC |
| +#undef INTERNAL_ALIGNED_FREE |
| +#undef BASE_DECL_ALIGNED_MEMORY_STATIC |
| +#undef BASE_DECL_ALIGNED_MEMORY_DYNAMIC |
| +#undef BASE_DECL_ALIGNED_MEMORY |
| + |
| } // base |
| #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ |