blob: 076cff1169a67d6f1119b27dae8be1cb07408499 [file] [log] [blame]
Elliott Hughes8eac9af2014-05-09 19:12:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes5e62b342018-10-25 11:00:00 -070017#pragma once
Elliott Hughes8eac9af2014-05-09 19:12:08 -070018
Evgenii Stepanovd13e9a62016-07-15 16:31:42 -070019#include <stdint.h>
20
Elliott Hughes5e62b342018-10-25 11:00:00 -070021#define BIONIC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
22 TypeName(const TypeName&) = delete; \
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -070023 void operator=(const TypeName&) = delete
Elliott Hughes8eac9af2014-05-09 19:12:08 -070024
Elliott Hughes5e62b342018-10-25 11:00:00 -070025#define BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
26 TypeName() = delete; \
27 BIONIC_DISALLOW_COPY_AND_ASSIGN(TypeName)
Elliott Hughes8eac9af2014-05-09 19:12:08 -070028
Christopher Ferris03eebcb2014-06-13 13:57:51 -070029#define BIONIC_ROUND_UP_POWER_OF_2(value) \
Christopher Ferris0b13f292015-12-16 16:11:04 -080030 ((sizeof(value) == 8) \
Christopher Ferris27047fa2014-07-14 18:47:23 -070031 ? (1UL << (64 - __builtin_clzl(static_cast<unsigned long>(value)))) \
Christopher Ferris0b13f292015-12-16 16:11:04 -080032 : (1UL << (32 - __builtin_clz(static_cast<unsigned int>(value)))))
Christopher Ferris03eebcb2014-06-13 13:57:51 -070033
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070034static constexpr uintptr_t align_down(uintptr_t p, size_t align) {
Evgenii Stepanovd13e9a62016-07-15 16:31:42 -070035 return p & ~(align - 1);
36}
37
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070038static constexpr uintptr_t align_up(uintptr_t p, size_t align) {
Evgenii Stepanovd13e9a62016-07-15 16:31:42 -070039 return (p + align - 1) & ~(align - 1);
40}
41
42template <typename T>
43static inline T* align_down(T* p, size_t align) {
44 return reinterpret_cast<T*>(align_down(reinterpret_cast<uintptr_t>(p), align));
45}
46
47template <typename T>
48static inline T* align_up(T* p, size_t align) {
49 return reinterpret_cast<T*>(align_up(reinterpret_cast<uintptr_t>(p), align));
50}
51
Christopher Ferris93ea09f2017-10-05 15:18:47 -070052#if defined(__arm__)
Ryan Savitski9db34862019-08-27 21:54:18 +010053#define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined r14")
Christopher Ferris93ea09f2017-10-05 15:18:47 -070054#elif defined(__aarch64__)
55#define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined x30")
56#elif defined(__i386__)
57#define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined \%eip")
58#elif defined(__x86_64__)
59#define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined \%rip")
60#endif
61
Tom Cherry4362f892017-11-14 08:50:43 -080062// The arraysize(arr) macro returns the # of elements in an array arr.
63// The expression is a compile-time constant, and therefore can be
64// used in defining new arrays, for example. If you use arraysize on
65// a pointer by mistake, you will get a compile-time error.
66//
67// One caveat is that arraysize() doesn't accept any array of an
68// anonymous type or a type defined inside a function.
69//
70// This template function declaration is used in defining arraysize.
71// Note that the function doesn't need an implementation, as we only
72// use its type.
73template <typename T, size_t N>
74char (&ArraySizeHelper(T (&array)[N]))[N]; // NOLINT(readability/casting)
75
76#define arraysize(array) (sizeof(ArraySizeHelper(array)))
77
George Burgess IVfa5410f2018-08-13 17:44:06 -070078// Used to inform clang's -Wimplicit-fallthrough that a fallthrough is intended. There's no way to
79// silence (or enable, apparently) -Wimplicit-fallthrough in C yet.
80#ifdef __cplusplus
81#define __BIONIC_FALLTHROUGH [[clang::fallthrough]]
82#else
83#define __BIONIC_FALLTHROUGH
84#endif
Peter Collingbourne191ecdc2019-08-07 19:06:00 -070085
Peter Collingbourne2528dab2020-03-12 18:29:52 -070086static inline uintptr_t untag_address(uintptr_t p) {
Peter Collingbourne191ecdc2019-08-07 19:06:00 -070087#if defined(__aarch64__)
Peter Collingbourne2528dab2020-03-12 18:29:52 -070088 return p & ((1ULL << 56) - 1);
Peter Collingbourne191ecdc2019-08-07 19:06:00 -070089#else
90 return p;
91#endif
92}
Peter Collingbourne2528dab2020-03-12 18:29:52 -070093
94template <typename T>
95static inline T* untag_address(T* p) {
96 return reinterpret_cast<T*>(untag_address(reinterpret_cast<uintptr_t>(p)));
97}