1 /*
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
17 #pragma once
18
19 #include <stdint.h>
20
21 #define BIONIC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
22 TypeName(const TypeName&) = delete; \
23 void operator=(const TypeName&) = delete
24
25 #define BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
26 TypeName() = delete; \
27 BIONIC_DISALLOW_COPY_AND_ASSIGN(TypeName)
28
29 #define BIONIC_ROUND_UP_POWER_OF_2(value) \
30 ((sizeof(value) == 8) \
31 ? (1UL << (64 - __builtin_clzl(static_cast<unsigned long>(value)))) \
32 : (1UL << (32 - __builtin_clz(static_cast<unsigned int>(value)))))
33
align_down(uintptr_t p,size_t align)34 static constexpr uintptr_t align_down(uintptr_t p, size_t align) {
35 return p & ~(align - 1);
36 }
37
align_up(uintptr_t p,size_t align)38 static constexpr uintptr_t align_up(uintptr_t p, size_t align) {
39 return (p + align - 1) & ~(align - 1);
40 }
41
42 template <typename T>
align_down(T * _Nonnull p,size_t align)43 static inline T* _Nonnull align_down(T* _Nonnull p, size_t align) {
44 return reinterpret_cast<T*>(align_down(reinterpret_cast<uintptr_t>(p), align));
45 }
46
47 template <typename T>
align_up(T * _Nonnull p,size_t align)48 static inline T* _Nonnull align_up(T* _Nonnull p, size_t align) {
49 return reinterpret_cast<T*>(align_up(reinterpret_cast<uintptr_t>(p), align));
50 }
51
52 #if defined(__arm__)
53 #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined r14")
54 #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(__riscv)
59 #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined ra")
60 #elif defined(__x86_64__)
61 #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined \%rip")
62 #endif
63
64 // The arraysize(arr) macro returns the # of elements in an array arr.
65 // The expression is a compile-time constant, and therefore can be
66 // used in defining new arrays, for example. If you use arraysize on
67 // a pointer by mistake, you will get a compile-time error.
68 //
69 // One caveat is that arraysize() doesn't accept any array of an
70 // anonymous type or a type defined inside a function.
71 //
72 // This template function declaration is used in defining arraysize.
73 // Note that the function doesn't need an implementation, as we only
74 // use its type.
75 template <typename T, size_t N>
76 char (&ArraySizeHelper(T (&array)[N]))[N]; // NOLINT(readability/casting)
77
78 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
79
80 // Used to inform clang's -Wimplicit-fallthrough that a fallthrough is intended. There's no way to
81 // silence (or enable, apparently) -Wimplicit-fallthrough in C yet.
82 #ifdef __cplusplus
83 #define __BIONIC_FALLTHROUGH [[clang::fallthrough]]
84 #else
85 #define __BIONIC_FALLTHROUGH
86 #endif
87
untag_address(uintptr_t p)88 static inline uintptr_t untag_address(uintptr_t p) {
89 #if defined(__aarch64__)
90 return p & ((1ULL << 56) - 1);
91 #else
92 return p;
93 #endif
94 }
95
96 template <typename T>
untag_address(T * _Nonnull p)97 static inline T* _Nonnull untag_address(T* _Nonnull p) {
98 return reinterpret_cast<T*>(untag_address(reinterpret_cast<uintptr_t>(p)));
99 }
100