Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/memory/aligned_memory.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 // NaCl currently does not have posix_memalign in its headers, though the malloc | |
| 10 // implementation (dlmalloc) does support it. | |
| 11 // http://code.google.com/p/nativeclient/issues/detail?id=2505 | |
| 12 #if defined(OS_NACL) | |
| 13 extern int posix_memalign(void **memptr, size_t alignment, size_t size); | |
| 14 #endif | |
| 15 | |
| 16 namespace base { | |
| 17 | |
| 18 void* AlignedAlloc(size_t size, size_t alignment) { | |
| 19 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.
| |
| 20 DCHECK_EQ(alignment & (alignment - 1), static_cast<size_t>(0)); | |
| 21 DCHECK_EQ(alignment % sizeof(void*), static_cast<size_t>(0)); | |
| 22 void* ptr = NULL; | |
| 23 #if defined(COMPILER_MSVC) | |
| 24 ptr = _aligned_malloc(size, alignment); | |
| 25 #elif defined(OS_ANDROID) | |
| 26 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
| |
| 27 #else | |
| 28 if (posix_memalign(&ptr, alignment, size)) | |
| 29 ptr = NULL; | |
| 30 #endif | |
| 31 // Since aligned allocations may fail for non-memory related reasons, force a | |
| 32 // crash if we encounter a failed allocation; maintaining consistent behavior | |
| 33 // with a normal allocation failure in Chrome. | |
| 34 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.
| |
| 35 return ptr; | |
| 36 } | |
| 37 | |
| 38 } // namespace base | |
| OLD | NEW |