blob: c8431aab4394b1852e726f0555917dbd33fdd436 [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 Wasilczyk88aa8c32023-11-01 09:46:07 -070025#define PLOGE_VA_ARGS(...) , ##__VA_ARGS__
26#define PLOGE(fmt, ...) ALOGE(fmt ": %s" PLOGE_VA_ARGS(__VA_ARGS__), strerror(errno))
27
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -070028/* TEMP_FAILURE_RETRY is not available on macOS and Trusty. */
29#ifndef TEMP_FAILURE_RETRY
30/* Used to retry syscalls that can return EINTR. */
31#define TEMP_FAILURE_RETRY(exp) \
32 ({ \
33 __typeof__(exp) _rc; \
34 do { \
35 _rc = (exp); \
36 } while (_rc == -1 && errno == EINTR); \
37 _rc; \
38 })
39#endif
40
Yifan Hong18ac9472021-09-09 19:55:38 -070041#define TEST_AND_RETURN(value, expr) \
42 do { \
43 if (!(expr)) { \
44 ALOGE("Failed to call: %s", #expr); \
45 return value; \
46 } \
47 } while (0)
Yifan Hongb675ffe2021-08-05 16:37:17 -070048
Steven Morelandf183fdd2020-10-27 00:12:12 +000049namespace android {
50
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070051/**
52 * Get the size of a statically initialized array.
53 *
54 * \param N the array to get the size of.
55 * \return the size of the array.
56 */
57template <typename T, size_t N>
58constexpr size_t countof(T (&)[N]) {
59 return N;
60}
61
Steven Morelandf183fdd2020-10-27 00:12:12 +000062// avoid optimizations
63void zeroMemory(uint8_t* data, size_t size);
64
Frederick Mayledc07cf82022-05-26 20:30:12 +000065// View of contiguous sequence. Similar to std::span.
66template <typename T>
67struct Span {
68 T* data = nullptr;
69 size_t size = 0;
70
71 size_t byteSize() { return size * sizeof(T); }
72
73 iovec toIovec() { return {const_cast<std::remove_const_t<T>*>(data), byteSize()}; }
74
75 // Truncates `this` to a length of `offset` and returns a span with the
76 // remainder.
77 //
Frederick Mayle16a12ae2022-07-15 00:04:33 +000078 // `std::nullopt` iff offset > size.
79 std::optional<Span<T>> splitOff(size_t offset) {
80 if (offset > size) {
81 return std::nullopt;
82 }
Frederick Mayledc07cf82022-05-26 20:30:12 +000083 Span<T> rest = {data + offset, size - offset};
84 size = offset;
85 return rest;
86 }
Frederick Mayle69a0c992022-05-26 20:38:39 +000087
88 // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U).
89 template <typename U>
90 std::optional<Span<U>> reinterpret() const {
91 // Only allow casting from bytes for simplicity.
92 static_assert(std::is_same_v<std::remove_const_t<T>, uint8_t>);
93 if (size % sizeof(U) != 0) {
94 return std::nullopt;
95 }
96 return Span<U>{reinterpret_cast<U*>(data), size / sizeof(U)};
97 }
Frederick Mayledc07cf82022-05-26 20:30:12 +000098};
99
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000100// Converts binary data into a hexString.
101//
102// Hex values are printed in order, e.g. 0xDEAD will result in 'adde' because
103// Android is little-endian.
104std::string HexString(const void* bytes, size_t len);
105
Steven Morelandf183fdd2020-10-27 00:12:12 +0000106} // namespace android