|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 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 <stddef.h> // for size_t |
| 20 | +#include <unistd.h> // for TEMP_FAILURE_RETRY |
| 21 | + |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +// bionic and glibc both have TEMP_FAILURE_RETRY, but eg Mac OS' libc doesn't. |
| 25 | +#ifndef TEMP_FAILURE_RETRY |
| 26 | +#define TEMP_FAILURE_RETRY(exp) \ |
| 27 | + ({ \ |
| 28 | + decltype(exp) _rc; \ |
| 29 | + do { \ |
| 30 | + _rc = (exp); \ |
| 31 | + } while (_rc == -1 && errno == EINTR); \ |
| 32 | + _rc; \ |
| 33 | + }) |
| 34 | +#endif |
| 35 | + |
| 36 | +// A macro to disallow the copy constructor and operator= functions |
| 37 | +// This must be placed in the private: declarations for a class. |
| 38 | +// |
| 39 | +// For disallowing only assign or copy, delete the relevant operator or |
| 40 | +// constructor, for example: |
| 41 | +// void operator=(const TypeName&) = delete; |
| 42 | +// Note, that most uses of DISALLOW_ASSIGN and DISALLOW_COPY are broken |
| 43 | +// semantically, one should either use disallow both or neither. Try to |
| 44 | +// avoid these in new code. |
| 45 | +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 46 | + TypeName(const TypeName &) = delete; \ |
| 47 | + void operator=(const TypeName &) = delete |
| 48 | + |
| 49 | +// A macro to disallow all the implicit constructors, namely the |
| 50 | +// default constructor, copy constructor and operator= functions. |
| 51 | +// |
| 52 | +// This should be used in the private: declarations for a class |
| 53 | +// that wants to prevent anyone from instantiating it. This is |
| 54 | +// especially useful for classes containing only static methods. |
| 55 | +#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
| 56 | + TypeName() = delete; \ |
| 57 | + DISALLOW_COPY_AND_ASSIGN(TypeName) |
| 58 | + |
| 59 | +// The arraysize(arr) macro returns the # of elements in an array arr. |
| 60 | +// The expression is a compile-time constant, and therefore can be |
| 61 | +// used in defining new arrays, for example. If you use arraysize on |
| 62 | +// a pointer by mistake, you will get a compile-time error. |
| 63 | +// |
| 64 | +// One caveat is that arraysize() doesn't accept any array of an |
| 65 | +// anonymous type or a type defined inside a function. In these rare |
| 66 | +// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is |
| 67 | +// due to a limitation in C++'s template system. The limitation might |
| 68 | +// eventually be removed, but it hasn't happened yet. |
| 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. |
| 73 | +template <typename T, size_t N> |
| 74 | +char (&ArraySizeHelper(T (&array)[N]))[N]; // NOLINT(readability/casting) |
| 75 | + |
| 76 | +#define arraysize(array) (sizeof(ArraySizeHelper(array))) |
| 77 | + |
| 78 | +#define SIZEOF_MEMBER(t, f) sizeof(std::declval<t>().f) |
| 79 | + |
| 80 | +// Changing this definition will cause you a lot of pain. A majority of |
| 81 | +// vendor code defines LIKELY and UNLIKELY this way, and includes |
| 82 | +// this header through an indirect path. |
| 83 | +#define LIKELY(exp) (__builtin_expect((exp) != 0, true)) |
| 84 | +#define UNLIKELY(exp) (__builtin_expect((exp) != 0, false)) |
| 85 | + |
| 86 | +#define WARN_UNUSED __attribute__((warn_unused_result)) |
| 87 | + |
| 88 | +// A deprecated function to call to create a false use of the parameter, for |
| 89 | +// example: |
| 90 | +// int foo(int x) { UNUSED(x); return 10; } |
| 91 | +// to avoid compiler warnings. Going forward we prefer ATTRIBUTE_UNUSED. |
| 92 | +template <typename... T> |
| 93 | +void UNUSED(const T &...) {} |
| 94 | + |
| 95 | +// An attribute to place on a parameter to a function, for example: |
| 96 | +// int foo(int x ATTRIBUTE_UNUSED) { return 10; } |
| 97 | +// to avoid compiler warnings. |
| 98 | +#define ATTRIBUTE_UNUSED __attribute__((__unused__)) |
| 99 | + |
| 100 | +// The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through |
| 101 | +// between switch labels: |
| 102 | +// switch (x) { |
| 103 | +// case 40: |
| 104 | +// case 41: |
| 105 | +// if (truth_is_out_there) { |
| 106 | +// ++x; |
| 107 | +// FALLTHROUGH_INTENDED; // Use instead of/along with annotations in |
| 108 | +// // comments. |
| 109 | +// } else { |
| 110 | +// return x; |
| 111 | +// } |
| 112 | +// case 42: |
| 113 | +// ... |
| 114 | +// |
| 115 | +// As shown in the example above, the FALLTHROUGH_INTENDED macro should be |
| 116 | +// followed by a semicolon. It is designed to mimic control-flow statements |
| 117 | +// like 'break;', so it can be placed in most places where 'break;' can, but |
| 118 | +// only if there are no statements on the execution path between it and the |
| 119 | +// next switch label. |
| 120 | +// |
| 121 | +// When compiled with clang, the FALLTHROUGH_INTENDED macro is expanded to |
| 122 | +// [[clang::fallthrough]] attribute, which is analysed when performing switch |
| 123 | +// labels fall-through diagnostic ('-Wimplicit-fallthrough'). See clang |
| 124 | +// documentation on language extensions for details: |
| 125 | +// http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough |
| 126 | +// |
| 127 | +// When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no |
| 128 | +// effect on diagnostics. |
| 129 | +// |
| 130 | +// In either case this macro has no effect on runtime behavior and performance |
| 131 | +// of code. |
| 132 | +#ifndef FALLTHROUGH_INTENDED |
| 133 | +#define FALLTHROUGH_INTENDED [[fallthrough]] // NOLINT |
| 134 | +#endif |
| 135 | + |
| 136 | +// Current ABI string |
| 137 | +#if defined(__arm__) |
| 138 | +#define ABI_STRING "arm" |
| 139 | +#elif defined(__aarch64__) |
| 140 | +#define ABI_STRING "arm64" |
| 141 | +#elif defined(__i386__) |
| 142 | +#define ABI_STRING "x86" |
| 143 | +#elif defined(__riscv) |
| 144 | +#define ABI_STRING "riscv64" |
| 145 | +#elif defined(__x86_64__) |
| 146 | +#define ABI_STRING "x86_64" |
| 147 | +#endif |
0 commit comments