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

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: Fix headers. 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..201337a6cba34f1233e6a2f691b0205c58807151 100644
--- a/base/memory/aligned_memory.h
+++ b/base/memory/aligned_memory.h
@@ -18,13 +18,31 @@
//
// // ... later, to destruct my_class:
// my_class.data_as<MyClass>()->MyClass::~MyClass();
+//
+// Alternatively, a runtime sized aligned allocation can be created:
+//
+// float* my_array = reinterpret_cast<float*>(AlignedAlloc(size, alignment));
+//
+// // ... later, to release the memory:
+// AlignedFree(my_array);
+//
+// Or using scoped_ptr_malloc:
+//
+// scoped_ptr_malloc<float, ScopedPtrAlignedFree> my_array(
+// reinterpret_cast<float*>(AlignedAlloc(size, alignment)));
Jeffrey Yasskin 2012/07/23 18:34:40 These reinterpret_casts only need to be static_cas
DaleCurtis 2012/07/23 19:04:27 Fixed all over.
#ifndef BASE_MEMORY_ALIGNED_MEMORY_H_
#define BASE_MEMORY_ALIGNED_MEMORY_H_
+#if defined(COMPILER_MSVC)
willchan no longer on Chromium 2012/07/23 18:48:45 Platform specific #includes should be behind the c
DaleCurtis 2012/07/23 19:04:27 Done. Causes a lint error though.
+#include <malloc.h>
+#else
+#include <stdlib.h>
+#endif
+
+#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
-#include "base/logging.h"
namespace base {
@@ -37,7 +55,6 @@ struct AlignedMemory {};
template <size_t Size> \
class AlignedMemory<Size, byte_alignment> { \
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,6 +66,7 @@ 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*); \
}
@@ -71,6 +89,26 @@ BASE_DECL_ALIGNED_MEMORY(1024);
BASE_DECL_ALIGNED_MEMORY(2048);
BASE_DECL_ALIGNED_MEMORY(4096);
-} // base
+#undef BASE_DECL_ALIGNED_MEMORY
+
+BASE_EXPORT void* AlignedAlloc(size_t size, size_t alignment);
+
+inline void AlignedFree(void* ptr) {
+#if defined(COMPILER_MSVC)
+ _aligned_free(ptr);
+#else
+ free(ptr);
+#endif
+}
+
+// Helper class for use with scoped_ptr_malloc.
+class BASE_EXPORT ScopedPtrAlignedFree {
+ public:
+ inline void operator()(void* x) const {
+ AlignedFree(x);
+ }
+};
+
+} // namespace base
#endif // BASE_MEMORY_ALIGNED_MEMORY_H_

Powered by Google App Engine
This is Rietveld 408576698