Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 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 | |
Avi Drissman | c73aa5d2 | 2023-12-07 17:24:22 | [diff] [blame] | 7 | #include <bit> |
| 8 | |
Peter Boström | 5411965 | 2024-11-14 00:16:38 | [diff] [blame] | 9 | #include "base/check.h" |
Lei Zhang | 0d85a9d3 | 2021-05-06 19:21:47 | [diff] [blame] | 10 | #include "base/check_op.h" |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 11 | #include "base/logging.h" |
avi | 9beac25 | 2015-12-24 08:44:47 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 13 | |
Xiaohan Wang | 346275d | 2022-01-08 01:25:37 | [diff] [blame] | 14 | #if BUILDFLAG(IS_ANDROID) |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 15 | #include <malloc.h> |
| 16 | #endif |
| 17 | |
| 18 | namespace base { |
| 19 | |
| 20 | void* AlignedAlloc(size_t size, size_t alignment) { |
| 21 | DCHECK_GT(size, 0U); |
Avi Drissman | c73aa5d2 | 2023-12-07 17:24:22 | [diff] [blame] | 22 | DCHECK(std::has_single_bit(alignment)); |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 23 | DCHECK_EQ(alignment % sizeof(void*), 0U); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 24 | void* ptr = nullptr; |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 25 | #if defined(COMPILER_MSVC) |
| 26 | ptr = _aligned_malloc(size, alignment); |
Xiaohan Wang | 346275d | 2022-01-08 01:25:37 | [diff] [blame] | 27 | #elif BUILDFLAG(IS_ANDROID) |
Lei Zhang | d425ff9 | 2020-06-10 16:32:42 | [diff] [blame] | 28 | // Android technically supports posix_memalign(), but does not expose it in |
| 29 | // the current version of the library headers used by Chromium. Luckily, |
| 30 | // memalign() on Android returns pointers which can safely be used with |
| 31 | // free(), so we can use it instead. Issue filed to document this: |
| 32 | // http://code.google.com/p/android/issues/detail?id=35391 |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 33 | ptr = memalign(alignment, size); |
| 34 | #else |
Lei Zhang | d425ff9 | 2020-06-10 16:32:42 | [diff] [blame] | 35 | int ret = posix_memalign(&ptr, alignment, size); |
| 36 | if (ret != 0) { |
Alexandre Courbot | 95a3cedc | 2019-01-23 05:08:48 | [diff] [blame] | 37 | DLOG(ERROR) << "posix_memalign() returned with error " << ret; |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 38 | ptr = nullptr; |
Alexandre Courbot | 95a3cedc | 2019-01-23 05:08:48 | [diff] [blame] | 39 | } |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 40 | #endif |
Lei Zhang | d425ff9 | 2020-06-10 16:32:42 | [diff] [blame] | 41 | |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 42 | // Since aligned allocations may fail for non-memory related reasons, force a |
| 43 | // crash if we encounter a failed allocation; maintaining consistent behavior |
| 44 | // with a normal allocation failure in Chrome. |
Peter Boström | 5411965 | 2024-11-14 00:16:38 | [diff] [blame] | 45 | CHECK(ptr) << "If you crashed here, your aligned allocation is incorrect: " |
| 46 | << "size=" << size << ", alignment=" << alignment; |
| 47 | |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 48 | // Sanity check alignment just to be safe. |
Lei Zhang | d425ff9 | 2020-06-10 16:32:42 | [diff] [blame] | 49 | DCHECK(IsAligned(ptr, alignment)); |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 50 | return ptr; |
| 51 | } |
| 52 | |
| 53 | } // namespace base |