| Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 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 | #ifndef _BIONIC_MACROS_H_ | 
|  | 18 | #define _BIONIC_MACROS_H_ | 
|  | 19 |  | 
| Evgenii Stepanov | d13e9a6 | 2016-07-15 16:31:42 -0700 | [diff] [blame] | 20 | #include <stdint.h> | 
|  | 21 |  | 
| Andreas Gampe | 00bbc7f | 2014-11-10 22:04:08 -0800 | [diff] [blame] | 22 | // Frameworks OpenGL code currently leaks this header and allows | 
|  | 23 | // collisions with other declarations, e.g., from libnativehelper. | 
|  | 24 | // TODO: Remove once cleaned up. b/18334516 | 
|  | 25 | #if !defined(DISALLOW_COPY_AND_ASSIGN) | 
| Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 26 | // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. | 
|  | 27 | // It goes in the private: declarations in a class. | 
|  | 28 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ | 
| Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 29 | TypeName(const TypeName&) = delete;      \ | 
|  | 30 | void operator=(const TypeName&) = delete | 
| Andreas Gampe | 00bbc7f | 2014-11-10 22:04:08 -0800 | [diff] [blame] | 31 | #endif  // !defined(DISALLOW_COPY_AND_ASSIGN) | 
| Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | // A macro to disallow all the implicit constructors, namely the | 
|  | 34 | // default constructor, copy constructor and operator= functions. | 
|  | 35 | // | 
|  | 36 | // This should be used in the private: declarations for a class | 
|  | 37 | // that wants to prevent anyone from instantiating it. This is | 
|  | 38 | // especially useful for classes containing only static methods. | 
|  | 39 | #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ | 
| Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 40 | TypeName() = delete;                           \ | 
| Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 41 | DISALLOW_COPY_AND_ASSIGN(TypeName) | 
|  | 42 |  | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 43 | #define BIONIC_ROUND_UP_POWER_OF_2(value) \ | 
| Christopher Ferris | 0b13f29 | 2015-12-16 16:11:04 -0800 | [diff] [blame] | 44 | ((sizeof(value) == 8) \ | 
| Christopher Ferris | 27047fa | 2014-07-14 18:47:23 -0700 | [diff] [blame] | 45 | ? (1UL << (64 - __builtin_clzl(static_cast<unsigned long>(value)))) \ | 
| Christopher Ferris | 0b13f29 | 2015-12-16 16:11:04 -0800 | [diff] [blame] | 46 | : (1UL << (32 - __builtin_clz(static_cast<unsigned int>(value))))) | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 47 |  | 
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 48 | static constexpr uintptr_t align_down(uintptr_t p, size_t align) { | 
| Evgenii Stepanov | d13e9a6 | 2016-07-15 16:31:42 -0700 | [diff] [blame] | 49 | return p & ~(align - 1); | 
|  | 50 | } | 
|  | 51 |  | 
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 52 | static constexpr uintptr_t align_up(uintptr_t p, size_t align) { | 
| Evgenii Stepanov | d13e9a6 | 2016-07-15 16:31:42 -0700 | [diff] [blame] | 53 | return (p + align - 1) & ~(align - 1); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | template <typename T> | 
|  | 57 | static inline T* align_down(T* p, size_t align) { | 
|  | 58 | return reinterpret_cast<T*>(align_down(reinterpret_cast<uintptr_t>(p), align)); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | template <typename T> | 
|  | 62 | static inline T* align_up(T* p, size_t align) { | 
|  | 63 | return reinterpret_cast<T*>(align_up(reinterpret_cast<uintptr_t>(p), align)); | 
|  | 64 | } | 
|  | 65 |  | 
| Christopher Ferris | 93ea09f | 2017-10-05 15:18:47 -0700 | [diff] [blame] | 66 | #if defined(__arm__) | 
|  | 67 | // Do not emit anything for arm, clang does not allow emiting an arm unwind | 
|  | 68 | // directive. | 
|  | 69 | // #define BIONIC_STOP_UNWIND asm volatile(".cantunwind") | 
|  | 70 | #define BIONIC_STOP_UNWIND | 
|  | 71 | #elif defined(__aarch64__) | 
|  | 72 | #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined x30") | 
|  | 73 | #elif defined(__i386__) | 
|  | 74 | #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined \%eip") | 
|  | 75 | #elif defined(__x86_64__) | 
|  | 76 | #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined \%rip") | 
| Goran Jakovljevic | ea82792 | 2017-10-13 14:22:10 +0200 | [diff] [blame] | 77 | #elif defined (__mips__) | 
|  | 78 | #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined $ra") | 
| Christopher Ferris | 93ea09f | 2017-10-05 15:18:47 -0700 | [diff] [blame] | 79 | #endif | 
|  | 80 |  | 
| Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 81 | // The arraysize(arr) macro returns the # of elements in an array arr. | 
|  | 82 | // The expression is a compile-time constant, and therefore can be | 
|  | 83 | // used in defining new arrays, for example.  If you use arraysize on | 
|  | 84 | // a pointer by mistake, you will get a compile-time error. | 
|  | 85 | // | 
|  | 86 | // One caveat is that arraysize() doesn't accept any array of an | 
|  | 87 | // anonymous type or a type defined inside a function. | 
|  | 88 | // | 
|  | 89 | // This template function declaration is used in defining arraysize. | 
|  | 90 | // Note that the function doesn't need an implementation, as we only | 
|  | 91 | // use its type. | 
|  | 92 | template <typename T, size_t N> | 
|  | 93 | char (&ArraySizeHelper(T (&array)[N]))[N];  // NOLINT(readability/casting) | 
|  | 94 |  | 
|  | 95 | #define arraysize(array) (sizeof(ArraySizeHelper(array))) | 
|  | 96 |  | 
| Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 97 | #endif // _BIONIC_MACROS_H_ |