Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 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 | |
Tomasz Wasilczyk | 7ba2e7e | 2023-11-13 13:18:57 -0800 | [diff] [blame] | 17 | #pragma once |
| 18 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 19 | #include <stddef.h> |
Andrei Homescu | 7c0b79f | 2022-06-30 02:00:46 +0000 | [diff] [blame] | 20 | #include <sys/uio.h> |
Tomasz Wasilczyk | 1d46f58 | 2024-05-21 15:06:29 -0700 | [diff] [blame] | 21 | #include <chrono> |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 22 | #include <cstdint> |
| 23 | #include <optional> |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 24 | |
Frederick Mayle | f7b65d1 | 2024-05-14 16:55:14 -0700 | [diff] [blame] | 25 | #include <binder/Common.h> |
Yifan Hong | 18ac947 | 2021-09-09 19:55:38 -0700 | [diff] [blame] | 26 | #include <log/log.h> |
Andrei Homescu | c24c879 | 2022-04-19 00:24:51 +0000 | [diff] [blame] | 27 | #include <utils/Errors.h> |
Yifan Hong | 18ac947 | 2021-09-09 19:55:38 -0700 | [diff] [blame] | 28 | |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 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) |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 39 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 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 | |
Yifan Hong | 18ac947 | 2021-09-09 19:55:38 -0700 | [diff] [blame] | 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) |
Yifan Hong | b675ffe | 2021-08-05 16:37:17 -0700 | [diff] [blame] | 60 | |
Tomasz Wasilczyk | 052c513 | 2024-06-21 15:45:26 -0700 | [diff] [blame^] | 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 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 74 | namespace android { |
| 75 | |
Tomasz Wasilczyk | df07f94 | 2023-11-02 15:07:45 -0700 | [diff] [blame] | 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> |
| 83 | constexpr size_t countof(T (&)[N]) { |
| 84 | return N; |
| 85 | } |
| 86 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 87 | // avoid optimizations |
| 88 | void zeroMemory(uint8_t* data, size_t size); |
| 89 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 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 | |
| 96 | size_t byteSize() { return size * sizeof(T); } |
| 97 | |
| 98 | 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 | // |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 103 | // `std::nullopt` iff offset > size. |
| 104 | std::optional<Span<T>> splitOff(size_t offset) { |
| 105 | if (offset > size) { |
| 106 | return std::nullopt; |
| 107 | } |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 108 | Span<T> rest = {data + offset, size - offset}; |
| 109 | size = offset; |
| 110 | return rest; |
| 111 | } |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 112 | |
| 113 | // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U). |
| 114 | template <typename U> |
| 115 | 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 | } |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Tomasz Wasilczyk | 891f6b0 | 2023-10-11 18:35:42 +0000 | [diff] [blame] | 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. |
Frederick Mayle | f7b65d1 | 2024-05-14 16:55:14 -0700 | [diff] [blame] | 129 | LIBBINDER_INTERNAL_EXPORTED std::string HexString(const void* bytes, size_t len); |
Tomasz Wasilczyk | 891f6b0 | 2023-10-11 18:35:42 +0000 | [diff] [blame] | 130 | |
Tomasz Wasilczyk | 1d46f58 | 2024-05-21 15:06:29 -0700 | [diff] [blame] | 131 | // Converts any std::chrono duration to the number of milliseconds |
| 132 | template <class Rep, class Period> |
| 133 | uint64_t to_ms(std::chrono::duration<Rep, Period> duration) { |
| 134 | return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(); |
| 135 | } |
| 136 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 137 | } // namespace android |