blob: 68d639208ea93785bf9574f97a6d0d3670f1c5ac [file] [log] [blame]
Steven Morelandf183fdd2020-10-27 00:12:12 +00001/*
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 Morelandf183fdd2020-10-27 00:12:12 +000017#include <stddef.h>
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000018#include <sys/uio.h>
Frederick Mayledc07cf82022-05-26 20:30:12 +000019#include <cstdint>
20#include <optional>
Steven Morelandf183fdd2020-10-27 00:12:12 +000021
Yifan Hong18ac9472021-09-09 19:55:38 -070022#include <log/log.h>
Andrei Homescuc24c8792022-04-19 00:24:51 +000023#include <utils/Errors.h>
Yifan Hong18ac9472021-09-09 19:55:38 -070024
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -070025/* 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 Hong18ac9472021-09-09 19:55:38 -070038#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 Hongb675ffe2021-08-05 16:37:17 -070045
Steven Morelandf183fdd2020-10-27 00:12:12 +000046namespace android {
47
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070048/**
49 * Get the size of a statically initialized array.
50 *
51 * \param N the array to get the size of.
52 * \return the size of the array.
53 */
54template <typename T, size_t N>
55constexpr size_t countof(T (&)[N]) {
56 return N;
57}
58
Steven Morelandf183fdd2020-10-27 00:12:12 +000059// avoid optimizations
60void zeroMemory(uint8_t* data, size_t size);
61
Frederick Mayledc07cf82022-05-26 20:30:12 +000062// View of contiguous sequence. Similar to std::span.
63template <typename T>
64struct Span {
65 T* data = nullptr;
66 size_t size = 0;
67
68 size_t byteSize() { return size * sizeof(T); }
69
70 iovec toIovec() { return {const_cast<std::remove_const_t<T>*>(data), byteSize()}; }
71
72 // Truncates `this` to a length of `offset` and returns a span with the
73 // remainder.
74 //
Frederick Mayle16a12ae2022-07-15 00:04:33 +000075 // `std::nullopt` iff offset > size.
76 std::optional<Span<T>> splitOff(size_t offset) {
77 if (offset > size) {
78 return std::nullopt;
79 }
Frederick Mayledc07cf82022-05-26 20:30:12 +000080 Span<T> rest = {data + offset, size - offset};
81 size = offset;
82 return rest;
83 }
Frederick Mayle69a0c992022-05-26 20:38:39 +000084
85 // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U).
86 template <typename U>
87 std::optional<Span<U>> reinterpret() const {
88 // Only allow casting from bytes for simplicity.
89 static_assert(std::is_same_v<std::remove_const_t<T>, uint8_t>);
90 if (size % sizeof(U) != 0) {
91 return std::nullopt;
92 }
93 return Span<U>{reinterpret_cast<U*>(data), size / sizeof(U)};
94 }
Frederick Mayledc07cf82022-05-26 20:30:12 +000095};
96
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +000097// Converts binary data into a hexString.
98//
99// Hex values are printed in order, e.g. 0xDEAD will result in 'adde' because
100// Android is little-endian.
101std::string HexString(const void* bytes, size_t len);
102
Steven Morelandf183fdd2020-10-27 00:12:12 +0000103} // namespace android