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 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 61 | namespace android { |
| 62 | |
Tomasz Wasilczyk | df07f94 | 2023-11-02 15:07:45 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Get the size of a statically initialized array. |
| 65 | * |
| 66 | * \param N the array to get the size of. |
| 67 | * \return the size of the array. |
| 68 | */ |
| 69 | template <typename T, size_t N> |
| 70 | constexpr size_t countof(T (&)[N]) { |
| 71 | return N; |
| 72 | } |
| 73 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 74 | // avoid optimizations |
| 75 | void zeroMemory(uint8_t* data, size_t size); |
| 76 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 77 | // View of contiguous sequence. Similar to std::span. |
| 78 | template <typename T> |
| 79 | struct Span { |
| 80 | T* data = nullptr; |
| 81 | size_t size = 0; |
| 82 | |
| 83 | size_t byteSize() { return size * sizeof(T); } |
| 84 | |
| 85 | iovec toIovec() { return {const_cast<std::remove_const_t<T>*>(data), byteSize()}; } |
| 86 | |
| 87 | // Truncates `this` to a length of `offset` and returns a span with the |
| 88 | // remainder. |
| 89 | // |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 90 | // `std::nullopt` iff offset > size. |
| 91 | std::optional<Span<T>> splitOff(size_t offset) { |
| 92 | if (offset > size) { |
| 93 | return std::nullopt; |
| 94 | } |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 95 | Span<T> rest = {data + offset, size - offset}; |
| 96 | size = offset; |
| 97 | return rest; |
| 98 | } |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 99 | |
| 100 | // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U). |
| 101 | template <typename U> |
| 102 | std::optional<Span<U>> reinterpret() const { |
| 103 | // Only allow casting from bytes for simplicity. |
| 104 | static_assert(std::is_same_v<std::remove_const_t<T>, uint8_t>); |
| 105 | if (size % sizeof(U) != 0) { |
| 106 | return std::nullopt; |
| 107 | } |
| 108 | return Span<U>{reinterpret_cast<U*>(data), size / sizeof(U)}; |
| 109 | } |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
Tomasz Wasilczyk | 891f6b0 | 2023-10-11 18:35:42 +0000 | [diff] [blame] | 112 | // Converts binary data into a hexString. |
| 113 | // |
| 114 | // Hex values are printed in order, e.g. 0xDEAD will result in 'adde' because |
| 115 | // Android is little-endian. |
Frederick Mayle | f7b65d1 | 2024-05-14 16:55:14 -0700 | [diff] [blame] | 116 | LIBBINDER_INTERNAL_EXPORTED std::string HexString(const void* bytes, size_t len); |
Tomasz Wasilczyk | 891f6b0 | 2023-10-11 18:35:42 +0000 | [diff] [blame] | 117 | |
Tomasz Wasilczyk | 1d46f58 | 2024-05-21 15:06:29 -0700 | [diff] [blame^] | 118 | // Converts any std::chrono duration to the number of milliseconds |
| 119 | template <class Rep, class Period> |
| 120 | uint64_t to_ms(std::chrono::duration<Rep, Period> duration) { |
| 121 | return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(); |
| 122 | } |
| 123 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 124 | } // namespace android |