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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: base/memory/aligned_memory.h
diff --git a/base/memory/aligned_memory.h b/base/memory/aligned_memory.h
index 0a176347a6dd47768c20c9585870fcd9cef417f1..97225b1e8a0769b1a3af10549bf5045481e695e7 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,65 @@
//
// // ... 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
+#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"
+#if defined(COMPILER_MSVC)
+#define INTERNAL_ALIGNED_MALLOC(ptr, align, size) \
+ 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.
+#define INTERNAL_ALIGNED_FREE(ptr) \
+ _aligned_free(ptr);
jbates 2012/07/19 03:45:37 no semicolon?
DaleCurtis 2012/07/20 02:38:56 Done.
+#else
+#if defined(OS_ANDROID)
+#define INTERNAL_ALIGNED_MALLOC(ptr, align, size) \
+ ptr = memalign(align, size);
jbates 2012/07/19 03:45:37 no semicolon?
DaleCurtis 2012/07/20 02:38:56 Done.
+#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
+// locally with or without it.
+extern int posix_memalign(void **ptr, size_t align, size_t size);
+#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.
+ if (posix_memalign(&ptr, align, size)) \
+ ptr = NULL;
+#endif
+#define INTERNAL_ALIGNED_FREE(ptr) \
+ free(ptr);
jbates 2012/07/19 03:45:37 no semicolon?
DaleCurtis 2012/07/20 02:38:56 Done.
+#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) \
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,27 +88,60 @@ 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*); \
+ }; \
+ template <> \
+ class AlignedMemory<byte_alignment, 0> { \
+ public: \
+ explicit AlignedMemory(size_t size) { \
+ DCHECK_GT(size, static_cast<size_t>(0)); \
+ 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.
+ 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
+ INTERNAL_ALIGNED_MALLOC(data_, byte_alignment, size) \
+ } \
+ ~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*); \
- }
+ };
// 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(4);
-BASE_DECL_ALIGNED_MEMORY(8);
-BASE_DECL_ALIGNED_MEMORY(16);
-BASE_DECL_ALIGNED_MEMORY(32);
-BASE_DECL_ALIGNED_MEMORY(64);
-BASE_DECL_ALIGNED_MEMORY(128);
-BASE_DECL_ALIGNED_MEMORY(256);
-BASE_DECL_ALIGNED_MEMORY(512);
-BASE_DECL_ALIGNED_MEMORY(1024);
-BASE_DECL_ALIGNED_MEMORY(2048);
-BASE_DECL_ALIGNED_MEMORY(4096);
+BASE_DECL_ALIGNED_MEMORY(1)
+BASE_DECL_ALIGNED_MEMORY(2)
+BASE_DECL_ALIGNED_MEMORY(4)
+BASE_DECL_ALIGNED_MEMORY(8)
+BASE_DECL_ALIGNED_MEMORY(16)
+BASE_DECL_ALIGNED_MEMORY(32)
+BASE_DECL_ALIGNED_MEMORY(64)
+BASE_DECL_ALIGNED_MEMORY(128)
+BASE_DECL_ALIGNED_MEMORY(256)
+BASE_DECL_ALIGNED_MEMORY(512)
+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
} // base

Powered by Google App Engine
This is Rietveld 408576698