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 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 17 | #include <stddef.h> |
Andrei Homescu | 7c0b79f | 2022-06-30 02:00:46 +0000 | [diff] [blame] | 18 | #include <sys/uio.h> |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 19 | #include <cstdint> |
| 20 | #include <optional> |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 21 | |
Yifan Hong | 18ac947 | 2021-09-09 19:55:38 -0700 | [diff] [blame] | 22 | #include <log/log.h> |
Andrei Homescu | c24c879 | 2022-04-19 00:24:51 +0000 | [diff] [blame] | 23 | #include <utils/Errors.h> |
Yifan Hong | 18ac947 | 2021-09-09 19:55:38 -0700 | [diff] [blame] | 24 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame^] | 25 | /* TEMP_FAILURE_RETRY is not available on macOS and Trusty. */ |
| 26 | #ifndef TEMP_FAILURE_RETRY |
| 27 | /* Used to retry syscalls that can return EINTR. */ |
| 28 | #define TEMP_FAILURE_RETRY(exp) \ |
| 29 | ({ \ |
| 30 | __typeof__(exp) _rc; \ |
| 31 | do { \ |
| 32 | _rc = (exp); \ |
| 33 | } while (_rc == -1 && errno == EINTR); \ |
| 34 | _rc; \ |
| 35 | }) |
| 36 | #endif |
| 37 | |
Yifan Hong | 18ac947 | 2021-09-09 19:55:38 -0700 | [diff] [blame] | 38 | #define TEST_AND_RETURN(value, expr) \ |
| 39 | do { \ |
| 40 | if (!(expr)) { \ |
| 41 | ALOGE("Failed to call: %s", #expr); \ |
| 42 | return value; \ |
| 43 | } \ |
| 44 | } while (0) |
Yifan Hong | b675ffe | 2021-08-05 16:37:17 -0700 | [diff] [blame] | 45 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 46 | namespace android { |
| 47 | |
| 48 | // avoid optimizations |
| 49 | void zeroMemory(uint8_t* data, size_t size); |
| 50 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 51 | // View of contiguous sequence. Similar to std::span. |
| 52 | template <typename T> |
| 53 | struct Span { |
| 54 | T* data = nullptr; |
| 55 | size_t size = 0; |
| 56 | |
| 57 | size_t byteSize() { return size * sizeof(T); } |
| 58 | |
| 59 | iovec toIovec() { return {const_cast<std::remove_const_t<T>*>(data), byteSize()}; } |
| 60 | |
| 61 | // Truncates `this` to a length of `offset` and returns a span with the |
| 62 | // remainder. |
| 63 | // |
Frederick Mayle | 16a12ae | 2022-07-15 00:04:33 +0000 | [diff] [blame] | 64 | // `std::nullopt` iff offset > size. |
| 65 | std::optional<Span<T>> splitOff(size_t offset) { |
| 66 | if (offset > size) { |
| 67 | return std::nullopt; |
| 68 | } |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 69 | Span<T> rest = {data + offset, size - offset}; |
| 70 | size = offset; |
| 71 | return rest; |
| 72 | } |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 73 | |
| 74 | // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U). |
| 75 | template <typename U> |
| 76 | std::optional<Span<U>> reinterpret() const { |
| 77 | // Only allow casting from bytes for simplicity. |
| 78 | static_assert(std::is_same_v<std::remove_const_t<T>, uint8_t>); |
| 79 | if (size % sizeof(U) != 0) { |
| 80 | return std::nullopt; |
| 81 | } |
| 82 | return Span<U>{reinterpret_cast<U*>(data), size / sizeof(U)}; |
| 83 | } |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Tomasz Wasilczyk | 891f6b0 | 2023-10-11 18:35:42 +0000 | [diff] [blame] | 86 | // Converts binary data into a hexString. |
| 87 | // |
| 88 | // Hex values are printed in order, e.g. 0xDEAD will result in 'adde' because |
| 89 | // Android is little-endian. |
| 90 | std::string HexString(const void* bytes, size_t len); |
| 91 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 92 | } // namespace android |