Index: base/memory/aligned_memory.h |
diff --git a/base/memory/aligned_memory.h b/base/memory/aligned_memory.h |
index 0a176347a6dd47768c20c9585870fcd9cef417f1..550dcdd5d8dc723a77c2cacfb8d6eb17b7f2bb5a 100644 |
--- a/base/memory/aligned_memory.h |
+++ b/base/memory/aligned_memory.h |
@@ -18,13 +18,30 @@ |
// |
// // ... 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, ScopedAlignedFree> my_array( |
+// reinterpret_cast<float*>(AlignedAlloc(size, alignment))); |
#ifndef BASE_MEMORY_ALIGNED_MEMORY_H_ |
#define BASE_MEMORY_ALIGNED_MEMORY_H_ |
+#if defined(COMPILER_MSVC) || defined(OS_ANDROID) |
+#include <malloc.h> |
+#endif |
+#include <stdlib.h> |
+ |
+#include "base/base_export.h" |
#include "base/basictypes.h" |
#include "base/compiler_specific.h" |
-#include "base/logging.h" |
namespace base { |
@@ -37,7 +54,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 +65,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 +88,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); |
Jeffrey Yasskin
2012/07/21 20:38:49
Please make this signature match C11's aligned_all
DaleCurtis
2012/07/21 22:18:41
I used this since it matches the original AlignedM
Jeffrey Yasskin
2012/07/21 22:42:07
It does seem reasonable to keep them the same. Unf
|
+ |
+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 ScopedAlignedFree { |
Jeffrey Yasskin
2012/07/21 20:38:49
This name seems awkward, but I don't have a concre
DaleCurtis
2012/07/21 22:18:41
AlignedFreeHelper, AlignedFreeScoper? The default
Jeffrey Yasskin
2012/07/21 22:42:07
ScopedPtrAlignedFree seems the least bad of those,
DaleCurtis
2012/07/22 00:45:40
Changed to ScopedPtrAlignedFree for consistency wi
|
+ public: |
+ inline void operator()(void* x) const { |
+ AlignedFree(x); |
+ } |
+}; |
+ |
+} // namespace base |
#endif // BASE_MEMORY_ALIGNED_MEMORY_H_ |