blob: 5e1012af017b6c44eacab094e5601d514c3cfcac [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>
Tomasz Wasilczyk1d46f582024-05-21 15:06:29 -070021#include <chrono>
Frederick Mayledc07cf82022-05-26 20:30:12 +000022#include <cstdint>
23#include <optional>
Steven Morelandf183fdd2020-10-27 00:12:12 +000024
Frederick Maylef7b65d12024-05-14 16:55:14 -070025#include <binder/Common.h>
Yifan Hong18ac9472021-09-09 19:55:38 -070026#include <log/log.h>
Andrei Homescuc24c8792022-04-19 00:24:51 +000027#include <utils/Errors.h>
Yifan Hong18ac9472021-09-09 19:55:38 -070028
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070029#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 Wasilczyk88aa8c32023-11-01 09:46:07 -070039
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -070040/* 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 Hong18ac9472021-09-09 19:55:38 -070053#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 Hongb675ffe2021-08-05 16:37:17 -070060
Steven Morelandf183fdd2020-10-27 00:12:12 +000061namespace android {
62
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070063/**
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 */
69template <typename T, size_t N>
70constexpr size_t countof(T (&)[N]) {
71 return N;
72}
73
Steven Morelandf183fdd2020-10-27 00:12:12 +000074// avoid optimizations
75void zeroMemory(uint8_t* data, size_t size);
76
Frederick Mayledc07cf82022-05-26 20:30:12 +000077// View of contiguous sequence. Similar to std::span.
78template <typename T>
79struct 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 Mayle16a12ae2022-07-15 00:04:33 +000090 // `std::nullopt` iff offset > size.
91 std::optional<Span<T>> splitOff(size_t offset) {
92 if (offset > size) {
93 return std::nullopt;
94 }
Frederick Mayledc07cf82022-05-26 20:30:12 +000095 Span<T> rest = {data + offset, size - offset};
96 size = offset;
97 return rest;
98 }
Frederick Mayle69a0c992022-05-26 20:38:39 +000099
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 Mayledc07cf82022-05-26 20:30:12 +0000110};
111
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000112// 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 Maylef7b65d12024-05-14 16:55:14 -0700116LIBBINDER_INTERNAL_EXPORTED std::string HexString(const void* bytes, size_t len);
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000117
Tomasz Wasilczyk1d46f582024-05-21 15:06:29 -0700118// Converts any std::chrono duration to the number of milliseconds
119template <class Rep, class Period>
120uint64_t to_ms(std::chrono::duration<Rep, Period> duration) {
121 return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
122}
123
Steven Morelandf183fdd2020-10-27 00:12:12 +0000124} // namespace android