diff options
author | dalecurtis@chromium.org <dalecurtis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-23 23:26:50 +0000 |
---|---|---|
committer | dalecurtis@chromium.org <dalecurtis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-23 23:26:50 +0000 |
commit | f8f5593b34f0cf7563186381124172a336eebf99 (patch) | |
tree | e74300a161f45a5712b74a18f9fd965c776b12b9 /base/memory/aligned_memory.h | |
parent | 6075da4aecf7073f270cc0dd1306cc57b102671b (diff) | |
download | chromium_src-f8f5593b34f0cf7563186381124172a336eebf99.zip chromium_src-f8f5593b34f0cf7563186381124172a336eebf99.tar.gz chromium_src-f8f5593b34f0cf7563186381124172a336eebf99.tar.bz2 |
Upgrade AlignedMemory to support dynamic allocations.
Adds two new methods: AlignedAlloc and AlignedFree for creating and
destroying dynamic aligned allocations respectively. Also adds a
helper class, ScopedPtrAlignedFree, for use with scoped_ptr_malloc.
AlignedAlloc uses posix_memalign for OS X (now that we're targeting
10.6), Linux and _aligned_alloc() on Windows. Android and NaCl use
memalign() since they do not expose posix_memalign() and memalign()
is safe to use with free() on those platforms.
Also hacks around a bug in Visual C++ where __alignof will sometimes
return zero:
http://connect.microsoft.com/VisualStudio/feedback/details/682695/c-alignof-fails-to-properly-evalute-alignment-of-dependent-types
BUG=none
TEST=base_unittests + new test, trybots.
Review URL: https://chromiumcodereview.appspot.com/10796020
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@147988 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/memory/aligned_memory.h')
-rw-r--r-- | base/memory/aligned_memory.h | 52 |
1 files changed, 45 insertions, 7 deletions
diff --git a/base/memory/aligned_memory.h b/base/memory/aligned_memory.h index 0a17634..166fbcb 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 = static_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( +// static_cast<float*>(AlignedAlloc(size, alignment))); #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_ #define BASE_MEMORY_ALIGNED_MEMORY_H_ +#include "base/base_export.h" #include "base/basictypes.h" #include "base/compiler_specific.h" -#include "base/logging.h" + +#if defined(COMPILER_MSVC) +#include <malloc.h> +#else +#include <stdlib.h> +#endif namespace base { @@ -37,18 +55,18 @@ 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_); } \ + void* void_data() { return static_cast<void*>(data_); } \ const void* void_data() const { \ - return reinterpret_cast<const void*>(data_); \ + return static_cast<const void*>(data_); \ } \ template<typename Type> \ - Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \ + Type* data_as() { return static_cast<Type*>(void_data()); } \ template<typename Type> \ const Type* data_as() const { \ - return reinterpret_cast<const Type*>(void_data()); \ + return static_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* ptr) const { + AlignedFree(ptr); + } +}; + +} // namespace base #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ |