• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
UNUSED(const T &...)93 void UNUSED(const T&...) {
94 }
95 
96 // An attribute to place on a parameter to a function, for example:
97 //   int foo(int x ATTRIBUTE_UNUSED) { return 10; }
98 // to avoid compiler warnings.
99 #define ATTRIBUTE_UNUSED __attribute__((__unused__))
100 
101 // The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
102 // between switch labels:
103 //  switch (x) {
104 //    case 40:
105 //    case 41:
106 //      if (truth_is_out_there) {
107 //        ++x;
108 //        FALLTHROUGH_INTENDED;  // Use instead of/along with annotations in
109 //                               // comments.
110 //      } else {
111 //        return x;
112 //      }
113 //    case 42:
114 //      ...
115 //
116 // As shown in the example above, the FALLTHROUGH_INTENDED macro should be
117 // followed by a semicolon. It is designed to mimic control-flow statements
118 // like 'break;', so it can be placed in most places where 'break;' can, but
119 // only if there are no statements on the execution path between it and the
120 // next switch label.
121 //
122 // When compiled with clang, the FALLTHROUGH_INTENDED macro is expanded to
123 // [[clang::fallthrough]] attribute, which is analysed when performing switch
124 // labels fall-through diagnostic ('-Wimplicit-fallthrough'). See clang
125 // documentation on language extensions for details:
126 // http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough
127 //
128 // When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no
129 // effect on diagnostics.
130 //
131 // In either case this macro has no effect on runtime behavior and performance
132 // of code.
133 #ifndef FALLTHROUGH_INTENDED
134 #define FALLTHROUGH_INTENDED [[clang::fallthrough]]  // NOLINT
135 #endif
136 
137 // Current ABI string
138 #if defined(__arm__)
139 #define ABI_STRING "arm"
140 #elif defined(__aarch64__)
141 #define ABI_STRING "arm64"
142 #elif defined(__i386__)
143 #define ABI_STRING "x86"
144 #elif defined(__x86_64__)
145 #define ABI_STRING "x86_64"
146 #elif defined(__mips__) && !defined(__LP64__)
147 #define ABI_STRING "mips"
148 #elif defined(__mips__) && defined(__LP64__)
149 #define ABI_STRING "mips64"
150 #endif
151