blob: 881cdf3f4c122ebf1e071656852091e235c8c109 [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
Tomasz Wasilczyk052c5132024-06-21 15:45:26 -070061#define LIBBINDER_PRAGMA(arg) _Pragma(#arg)
62#if defined(__clang__)
63#define LIBBINDER_PRAGMA_FOR_COMPILER(arg) LIBBINDER_PRAGMA(clang arg)
64#elif defined(__GNUC__)
65#define LIBBINDER_PRAGMA_FOR_COMPILER(arg) LIBBINDER_PRAGMA(GCC arg)
66#else
67#define LIBBINDER_PRAGMA_FOR_COMPILER(arg)
68#endif
69#define LIBBINDER_IGNORE(warning_flag) \
70 LIBBINDER_PRAGMA_FOR_COMPILER(diagnostic push) \
71 LIBBINDER_PRAGMA_FOR_COMPILER(diagnostic ignored warning_flag)
72#define LIBBINDER_IGNORE_END() LIBBINDER_PRAGMA_FOR_COMPILER(diagnostic pop)
73
Steven Morelandf183fdd2020-10-27 00:12:12 +000074namespace android {
75
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070076/**
77 * Get the size of a statically initialized array.
78 *
79 * \param N the array to get the size of.
80 * \return the size of the array.
81 */
82template <typename T, size_t N>
83constexpr size_t countof(T (&)[N]) {
84 return N;
85}
86
Steven Morelandf183fdd2020-10-27 00:12:12 +000087// avoid optimizations
88void zeroMemory(uint8_t* data, size_t size);
89
Frederick Mayledc07cf82022-05-26 20:30:12 +000090// View of contiguous sequence. Similar to std::span.
91template <typename T>
92struct Span {
93 T* data = nullptr;
94 size_t size = 0;
95
96 size_t byteSize() { return size * sizeof(T); }
97
98 iovec toIovec() { return {const_cast<std::remove_const_t<T>*>(data), byteSize()}; }
99
100 // Truncates `this` to a length of `offset` and returns a span with the
101 // remainder.
102 //
Frederick Mayle16a12ae2022-07-15 00:04:33 +0000103 // `std::nullopt` iff offset > size.
104 std::optional<Span<T>> splitOff(size_t offset) {
105 if (offset > size) {
106 return std::nullopt;
107 }
Frederick Mayledc07cf82022-05-26 20:30:12 +0000108 Span<T> rest = {data + offset, size - offset};
109 size = offset;
110 return rest;
111 }
Frederick Mayle69a0c992022-05-26 20:38:39 +0000112
113 // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U).
114 template <typename U>
115 std::optional<Span<U>> reinterpret() const {
116 // Only allow casting from bytes for simplicity.
117 static_assert(std::is_same_v<std::remove_const_t<T>, uint8_t>);
118 if (size % sizeof(U) != 0) {
119 return std::nullopt;
120 }
121 return Span<U>{reinterpret_cast<U*>(data), size / sizeof(U)};
122 }
Frederick Mayledc07cf82022-05-26 20:30:12 +0000123};
124
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000125// Converts binary data into a hexString.
126//
127// Hex values are printed in order, e.g. 0xDEAD will result in 'adde' because
128// Android is little-endian.
Frederick Maylef7b65d12024-05-14 16:55:14 -0700129LIBBINDER_INTERNAL_EXPORTED std::string HexString(const void* bytes, size_t len);
Tomasz Wasilczyk891f6b02023-10-11 18:35:42 +0000130
Tomasz Wasilczyk1d46f582024-05-21 15:06:29 -0700131// Converts any std::chrono duration to the number of milliseconds
132template <class Rep, class Period>
133uint64_t to_ms(std::chrono::duration<Rep, Period> duration) {
134 return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
135}
136
Steven Morelandf183fdd2020-10-27 00:12:12 +0000137} // namespace android