blob: df8a4ce1f1476dbbaa468a182d13d2b82eead374 [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
Tomasz Wasilczyk7ba2e7e2023-11-13 13:18:57 -080017#pragma once
18
Steven Morelandf183fdd2020-10-27 00:12:12 +000019#include <stddef.h>
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000020#include <sys/uio.h>
Frederick Mayledc07cf82022-05-26 20:30:12 +000021#include <cstdint>
22#include <optional>
Steven Morelandf183fdd2020-10-27 00:12:12 +000023
Frederick Maylef7b65d12024-05-14 16:55:14 -070024#include <binder/Common.h>
Yifan Hong18ac9472021-09-09 19:55:38 -070025#include <log/log.h>
Andrei Homescuc24c8792022-04-19 00:24:51 +000026#include <utils/Errors.h>
Yifan Hong18ac9472021-09-09 19:55:38 -070027
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070028#define PLOGE(fmt, ...) \
29 do { \
30 auto savedErrno = errno; \
31 ALOGE(fmt ": %s" __VA_OPT__(, ) __VA_ARGS__, strerror(savedErrno)); \
32 } while (0)
33#define PLOGF(fmt, ...) \
34 do { \
35 auto savedErrno = errno; \
36 LOG_ALWAYS_FATAL(fmt ": %s" __VA_OPT__(, ) __VA_ARGS__, strerror(savedErrno)); \
37 } while (0)
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070038
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -070039/* TEMP_FAILURE_RETRY is not available on macOS and Trusty. */
40#ifndef TEMP_FAILURE_RETRY
41/* Used to retry syscalls that can return EINTR. */
42#define TEMP_FAILURE_RETRY(exp) \
43 ({ \
44 __typeof__(exp) _rc; \
45 do { \
46 _rc = (exp); \
47 } while (_rc == -1 && errno == EINTR); \
48 _rc; \
49 })
50#endif
51
Yifan Hong18ac9472021-09-09 19:55:38 -070052#define TEST_AND_RETURN(value, expr) \
53 do { \
54 if (!(expr)) { \
55 ALOGE("Failed to call: %s", #expr); \
56 return value; \
57 } \
58 } while (0)
Yifan Hongb675ffe2021-08-05 16:37:17 -070059
Steven Morelandf183fdd2020-10-27 00:12:12 +000060namespace android {
61
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070062/**
63 * Get the size of a statically initialized array.
64 *
65 * \param N the array to get the size of.
66 * \return the size of the array.
67 */
68template <typename T, size_t N>
69constexpr size_t countof(T (&)[N]) {
70 return N;
71}
72
Steven Morelandf183fdd2020-10-27 00:12:12 +000073// avoid optimizations
74void zeroMemory(uint8_t* data, size_t size);
75
Frederick Mayledc07cf82022-05-26 20:30:12 +000076// View of contiguous sequence. Similar to std::span.
77template <typename T>
78struct Span {
79 T* data = nullptr;
80 size_t size = 0;
81
82 size_t byteSize() { return size * sizeof(T); }
83
84 iovec toIovec() { return {const_cast<std::remove_const_t<T>*>(data), byteSize()}; }
85
86 // Truncates `this` to a length of `offset` and returns a span with the
87 // remainder.
88 //
Frederick Mayle16a12ae2022-07-15 00:04:33 +000089 // `std::nullopt` iff offset > size.
90 std::optional<Span<T>> splitOff(size_t offset) {
91 if (offset > size) {
92 return std::nullopt;
93 }
Frederick Mayledc07cf82022-05-26 20:30:12 +000094 Span<T> rest = {data + offset, size - offset};
95 size = offset;
96 return rest;
97 }
Frederick Mayle69a0c992022-05-26 20:38:39 +000098
99 // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U).
100 template <typename U>
101 std::optional<Span<U>> reinterpret() const {
102 // Only allow casting from bytes for simplicity.
103 static_assert(std::is_same_v<std::remove_const_t<T>, uint8_t>);
104 if (size % sizeof(U) != 0) {
105 return std::nullopt;
106 }
107 return Span<U>{reinterpret_cast<U*>(data), size / sizeof(U)};
108 }
Frederick Mayledc07cf82022-05-26 20:30:12 +0000109};
110
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000111// Converts binary data into a hexString.
112//
113// Hex values are printed in order, e.g. 0xDEAD will result in 'adde' because
114// Android is little-endian.
Frederick Maylef7b65d12024-05-14 16:55:14 -0700115LIBBINDER_INTERNAL_EXPORTED std::string HexString(const void* bytes, size_t len);
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000116
Steven Morelandf183fdd2020-10-27 00:12:12 +0000117} // namespace android