diff options
author | acolwell@chromium.org <acolwell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-24 01:12:22 +0000 |
---|---|---|
committer | acolwell@chromium.org <acolwell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-24 01:12:22 +0000 |
commit | 7f5b892399727380e06d084189795b1dc7669d37 (patch) | |
tree | e12fe3bb6409afaf1a1e0d330652684b255bfd07 /base | |
parent | 2c95e24628e8d61bfe77e0d0d6ef1d75b85bfd7c (diff) | |
download | chromium_src-7f5b892399727380e06d084189795b1dc7669d37.zip chromium_src-7f5b892399727380e06d084189795b1dc7669d37.tar.gz chromium_src-7f5b892399727380e06d084189795b1dc7669d37.tar.bz2 |
Revert 147988 - 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
TBR=dalecurtis@chromium.org
Review URL: https://chromiumcodereview.appspot.com/10817021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148016 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gypi | 2 | ||||
-rw-r--r-- | base/compiler_specific.h | 6 | ||||
-rw-r--r-- | base/memory/aligned_memory.cc | 47 | ||||
-rw-r--r-- | base/memory/aligned_memory.h | 52 | ||||
-rw-r--r-- | base/memory/aligned_memory_unittest.cc | 29 |
5 files changed, 9 insertions, 127 deletions
diff --git a/base/base.gypi b/base/base.gypi index 4d16af0..5fdbe97 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -203,8 +203,6 @@ 'mac/scoped_sending_event.mm', 'mach_ipc_mac.h', 'mach_ipc_mac.mm', - 'memory/aligned_memory.cc', - 'memory/aligned_memory.h', 'memory/linked_ptr.h', 'memory/mru_cache.h', 'memory/raw_scoped_refptr_mismatch_checker.h', diff --git a/base/compiler_specific.h b/base/compiler_specific.h index 5d7d9d1..64273a9 100644 --- a/base/compiler_specific.h +++ b/base/compiler_specific.h @@ -114,13 +114,11 @@ #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) #endif -// Return the byte alignment of the given type (available at compile time). Use -// sizeof(type) prior to checking __alignof to workaround Visual C++ bug: -// http://goo.gl/isH0C +// Return the byte alignment of the given type (available at compile time). // Use like: // ALIGNOF(int32) // this would be 4 #if defined(COMPILER_MSVC) -#define ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type)) +#define ALIGNOF(type) __alignof(type) #elif defined(COMPILER_GCC) #define ALIGNOF(type) __alignof__(type) #endif diff --git a/base/memory/aligned_memory.cc b/base/memory/aligned_memory.cc deleted file mode 100644 index b6278ab..0000000 --- a/base/memory/aligned_memory.cc +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/memory/aligned_memory.h" - -#include "base/logging.h" - -#if defined(OS_ANDROID) || defined(OS_NACL) -#include <malloc.h> -#endif - -namespace base { - -void* AlignedAlloc(size_t size, size_t alignment) { - DCHECK_GT(size, 0U); - DCHECK_EQ(alignment & (alignment - 1), 0U); - DCHECK_EQ(alignment % sizeof(void*), 0U); - void* ptr = NULL; -#if defined(COMPILER_MSVC) - ptr = _aligned_malloc(size, alignment); -// Both Android and NaCl technically support posix_memalign(), but do not expose -// it in the current version of the library headers used by Chrome. Luckily, -// memalign() on both platforms returns pointers which can safely be used with -// free(), so we can use it instead. Issues filed with each project for docs: -// http://code.google.com/p/android/issues/detail?id=35391 -// http://code.google.com/p/chromium/issues/detail?id=138579 -#elif defined(OS_ANDROID) || defined(OS_NACL) - ptr = memalign(alignment, size); -#else - if (posix_memalign(&ptr, alignment, size)) - ptr = NULL; -#endif - // Since aligned allocations may fail for non-memory related reasons, force a - // crash if we encounter a failed allocation; maintaining consistent behavior - // with a normal allocation failure in Chrome. - if (!ptr) { - DLOG(ERROR) << "If you crashed here, your aligned allocation is incorrect: " - << "size=" << size << ", alignment=" << alignment; - CHECK(false); - } - // Sanity check alignment just to be safe. - DCHECK_EQ(reinterpret_cast<uintptr_t>(ptr) & (alignment - 1), 0U); - return ptr; -} - -} // namespace base diff --git a/base/memory/aligned_memory.h b/base/memory/aligned_memory.h index 166fbcb..0a17634 100644 --- a/base/memory/aligned_memory.h +++ b/base/memory/aligned_memory.h @@ -18,31 +18,13 @@ // // // ... 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" - -#if defined(COMPILER_MSVC) -#include <malloc.h> -#else -#include <stdlib.h> -#endif +#include "base/logging.h" namespace base { @@ -55,18 +37,18 @@ struct AlignedMemory {}; template <size_t Size> \ class AlignedMemory<Size, byte_alignment> { \ public: \ - void* void_data() { return static_cast<void*>(data_); } \ + ALIGNAS(byte_alignment) uint8 data_[Size]; \ + void* void_data() { return reinterpret_cast<void*>(data_); } \ const void* void_data() const { \ - return static_cast<const void*>(data_); \ + return reinterpret_cast<const void*>(data_); \ } \ template<typename Type> \ - Type* data_as() { return static_cast<Type*>(void_data()); } \ + Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \ template<typename Type> \ const Type* data_as() const { \ - return static_cast<const Type*>(void_data()); \ + return reinterpret_cast<const Type*>(void_data()); \ } \ private: \ - ALIGNAS(byte_alignment) uint8 data_[Size]; \ void* operator new(size_t); \ void operator delete(void*); \ } @@ -89,26 +71,6 @@ BASE_DECL_ALIGNED_MEMORY(1024); BASE_DECL_ALIGNED_MEMORY(2048); BASE_DECL_ALIGNED_MEMORY(4096); -#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 +} // base #endif // BASE_MEMORY_ALIGNED_MEMORY_H_ diff --git a/base/memory/aligned_memory_unittest.cc b/base/memory/aligned_memory_unittest.cc index c337751..8c1eb61 100644 --- a/base/memory/aligned_memory_unittest.cc +++ b/base/memory/aligned_memory_unittest.cc @@ -51,33 +51,4 @@ TEST(AlignedMemoryTest, StackAlignment) { #endif // !(defined(OS_IOS) && defined(ARCH_CPU_ARM_FAMILY)) } -TEST(AlignedMemoryTest, DynamicAllocation) { - void* p = base::AlignedAlloc(8, 8); - EXPECT_TRUE(p); - EXPECT_ALIGNED(p, 8); - base::AlignedFree(p); - - p = base::AlignedAlloc(8, 16); - EXPECT_TRUE(p); - EXPECT_ALIGNED(p, 16); - base::AlignedFree(p); - - p = base::AlignedAlloc(8, 256); - EXPECT_TRUE(p); - EXPECT_ALIGNED(p, 256); - base::AlignedFree(p); - - p = base::AlignedAlloc(8, 4096); - EXPECT_TRUE(p); - EXPECT_ALIGNED(p, 4096); - base::AlignedFree(p); -} - -TEST(AlignedMemoryTest, ScopedDynamicAllocation) { - scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> p( - static_cast<float*>(base::AlignedAlloc(8, 8))); - EXPECT_TRUE(p.get()); - EXPECT_ALIGNED(p.get(), 8); -} - } // namespace |