xref: /aosp_15_r20/frameworks/native/libs/binder/Utils.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2020 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>
20 #include <sys/uio.h>
21 #include <chrono>
22 #include <cstdint>
23 #include <optional>
24 
25 #include <binder/Common.h>
26 #include <log/log.h>
27 #include <utils/Errors.h>
28 
29 #define PLOGE(fmt, ...)                                                     \
30     do {                                                                    \
31         auto savedErrno = errno;                                            \
32         ALOGE(fmt ": %s" __VA_OPT__(, ) __VA_ARGS__, strerror(savedErrno)); \
33     } while (0)
34 #define PLOGF(fmt, ...)                                                                \
35     do {                                                                               \
36         auto savedErrno = errno;                                                       \
37         LOG_ALWAYS_FATAL(fmt ": %s" __VA_OPT__(, ) __VA_ARGS__, strerror(savedErrno)); \
38     } while (0)
39 
40 /* TEMP_FAILURE_RETRY is not available on macOS and Trusty. */
41 #ifndef TEMP_FAILURE_RETRY
42 /* Used to retry syscalls that can return EINTR. */
43 #define TEMP_FAILURE_RETRY(exp)                \
44     ({                                         \
45         __typeof__(exp) _rc;                   \
46         do {                                   \
47             _rc = (exp);                       \
48         } while (_rc == -1 && errno == EINTR); \
49         _rc;                                   \
50     })
51 #endif
52 
53 #define TEST_AND_RETURN(value, expr)            \
54     do {                                        \
55         if (!(expr)) {                          \
56             ALOGE("Failed to call: %s", #expr); \
57             return value;                       \
58         }                                       \
59     } while (0)
60 
61 #define LIBBINDER_PRAGMA(arg) _Pragma(#arg)
62 #if defined(__clang__)
63 #define LIBBINDER_PRAGMA_FOR_COMPILER(arg) LIBBINDER_PRAGMA(clang arg)
64 #elif defined(__GNUC__)
65 #define LIBBINDER_PRAGMA_FOR_COMPILER(arg) LIBBINDER_PRAGMA(GCC arg)
66 #else
67 #define LIBBINDER_PRAGMA_FOR_COMPILER(arg)
68 #endif
69 #define LIBBINDER_IGNORE(warning_flag)             \
70     LIBBINDER_PRAGMA_FOR_COMPILER(diagnostic push) \
71     LIBBINDER_PRAGMA_FOR_COMPILER(diagnostic ignored warning_flag)
72 #define LIBBINDER_IGNORE_END() LIBBINDER_PRAGMA_FOR_COMPILER(diagnostic pop)
73 
74 namespace android {
75 
76 /**
77  * Get the size of a statically initialized array.
78  *
79  * \param N the array to get the size of.
80  * \return the size of the array.
81  */
82 template <typename T, size_t N>
countof(T (&)[N])83 constexpr size_t countof(T (&)[N]) {
84     return N;
85 }
86 
87 // avoid optimizations
88 void zeroMemory(uint8_t* data, size_t size);
89 
90 // View of contiguous sequence. Similar to std::span.
91 template <typename T>
92 struct Span {
93     T* data = nullptr;
94     size_t size = 0;
95 
byteSizeSpan96     size_t byteSize() { return size * sizeof(T); }
97 
toIovecSpan98     iovec toIovec() { return {const_cast<std::remove_const_t<T>*>(data), byteSize()}; }
99 
100     // Truncates `this` to a length of `offset` and returns a span with the
101     // remainder.
102     //
103     // `std::nullopt` iff offset > size.
splitOffSpan104     std::optional<Span<T>> splitOff(size_t offset) {
105         if (offset > size) {
106             return std::nullopt;
107         }
108         Span<T> rest = {data + offset, size - offset};
109         size = offset;
110         return rest;
111     }
112 
113     // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U).
114     template <typename U>
reinterpretSpan115     std::optional<Span<U>> reinterpret() const {
116         // Only allow casting from bytes for simplicity.
117         static_assert(std::is_same_v<std::remove_const_t<T>, uint8_t>);
118         if (size % sizeof(U) != 0) {
119             return std::nullopt;
120         }
121         return Span<U>{reinterpret_cast<U*>(data), size / sizeof(U)};
122     }
123 };
124 
125 // Converts binary data into a hexString.
126 //
127 // Hex values are printed in order, e.g. 0xDEAD will result in 'adde' because
128 // Android is little-endian.
129 LIBBINDER_INTERNAL_EXPORTED std::string HexString(const void* bytes, size_t len);
130 
131 // Converts any std::chrono duration to the number of milliseconds
132 template <class Rep, class Period>
to_ms(std::chrono::duration<Rep,Period> duration)133 uint64_t to_ms(std::chrono::duration<Rep, Period> duration) {
134     return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
135 }
136 
137 }   // namespace android
138