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 #if defined(OS_ANDROID) || defined(OS_NACL) | |
|
willchan no longer on Chromium
2012/07/23 18:48:45
As per Chromium style guide, please move platform
DaleCurtis
2012/07/23 19:04:27
Done.
| |
| 8 #include <malloc.h> | |
| 9 #endif | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 | |
| 13 namespace base { | |
| 14 | |
| 15 void* AlignedAlloc(size_t size, size_t alignment) { | |
| 16 DCHECK_GT(size, 0U); | |
| 17 DCHECK_EQ(alignment & (alignment - 1), 0U); | |
| 18 DCHECK_EQ(alignment % sizeof(void*), 0U); | |
| 19 void* ptr = NULL; | |
| 20 #if defined(COMPILER_MSVC) | |
| 21 ptr = _aligned_malloc(size, alignment); | |
| 22 // Both Android and NaCl technically support posix_memalign(), but do not expose | |
| 23 // it in the current version of the library headers used by Chrome. Luckily, | |
| 24 // memalign() on both platforms returns pointers which can safely be used with | |
|
Jeffrey Yasskin
2012/07/23 18:34:40
I've seen too many incorrect claims of this form t
DaleCurtis
2012/07/23 19:04:27
The absolute worst case here is free() silently ac
willchan no longer on Chromium
2012/07/23 19:15:44
AIUI, passing non-malloc'd addresses to free() is
DaleCurtis
2012/07/23 19:32:21
We have confirmation from NaCl and Android that th
| |
| 25 // free(), so we can use it instead. | |
| 26 #elif defined(OS_ANDROID) || defined(OS_NACL) | |
| 27 ptr = memalign(alignment, size); | |
| 28 #else | |
| 29 if (posix_memalign(&ptr, alignment, size)) | |
| 30 ptr = NULL; | |
| 31 #endif | |
| 32 // Since aligned allocations may fail for non-memory related reasons, force a | |
| 33 // crash if we encounter a failed allocation; maintaining consistent behavior | |
| 34 // with a normal allocation failure in Chrome. | |
| 35 CHECK(ptr) << "If you crashed here, your aligned allocation is incorrect: " | |
|
willchan no longer on Chromium
2012/07/23 18:48:45
I know Jeffrey asked for more info here, but in or
DaleCurtis
2012/07/23 19:04:27
Done.
| |
| 36 << "size=" << size << ", alignment=" << alignment; | |
| 37 // Sanity check alignment just to be safe. | |
| 38 DCHECK_EQ(reinterpret_cast<uintptr_t>(ptr) & (alignment - 1), 0U); | |
| 39 return ptr; | |
| 40 } | |
| 41 | |
| 42 } // namespace base | |
| OLD | NEW |