Chromium Code Reviews| Index: base/memory/aligned_memory.cc |
| diff --git a/base/memory/aligned_memory.cc b/base/memory/aligned_memory.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2c0b043d6bbd6091e43119fcca41083d2e09f5c6 |
| --- /dev/null |
| +++ b/base/memory/aligned_memory.cc |
| @@ -0,0 +1,38 @@ |
| +// 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" |
| + |
| +// NaCl currently does not have posix_memalign in its headers, though the malloc |
| +// implementation (dlmalloc) does support it. |
| +// http://code.google.com/p/nativeclient/issues/detail?id=2505 |
| +#if defined(OS_NACL) |
| +extern int posix_memalign(void **memptr, size_t alignment, size_t size); |
| +#endif |
| + |
| +namespace base { |
| + |
| +void* AlignedAlloc(size_t size, size_t alignment) { |
| + DCHECK_GT(size, static_cast<size_t>(0)); |
|
Jeffrey Yasskin
2012/07/21 22:42:06
FWIW, "0U" usually works instead of static_cast<si
DaleCurtis
2012/07/22 00:45:40
Done.
|
| + DCHECK_EQ(alignment & (alignment - 1), static_cast<size_t>(0)); |
| + DCHECK_EQ(alignment % sizeof(void*), static_cast<size_t>(0)); |
| + void* ptr = NULL; |
| +#if defined(COMPILER_MSVC) |
| + ptr = _aligned_malloc(size, alignment); |
| +#elif defined(OS_ANDROID) |
| + ptr = memalign(alignment, size); |
|
Jeffrey Yasskin
2012/07/21 22:42:06
I do want to find some sort of link to documentati
DaleCurtis
2012/07/22 00:45:40
I suspect aside from the commit message I linked p
|
| +#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. |
| + CHECK(ptr) << "If you crashed here, your aligned allocation is incorrect."; |
|
Jeffrey Yasskin
2012/07/21 22:42:07
It might be helpful to include 'size' and 'alignme
DaleCurtis
2012/07/22 00:45:40
Done.
|
| + return ptr; |
| +} |
| + |
| +} // namespace base |