blob: 37c1262c9b629f932c1baa1f714eb5b856df741d [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>
Frederick Mayledc07cf82022-05-26 20:30:12 +000018#include <cstdint>
19#include <optional>
Steven Morelandf183fdd2020-10-27 00:12:12 +000020
Yifan Hongb675ffe2021-08-05 16:37:17 -070021#include <android-base/result.h>
22#include <android-base/unique_fd.h>
Yifan Hong18ac9472021-09-09 19:55:38 -070023#include <log/log.h>
Andrei Homescuc24c8792022-04-19 00:24:51 +000024#include <utils/Errors.h>
Yifan Hong18ac9472021-09-09 19:55:38 -070025
26#define TEST_AND_RETURN(value, expr) \
27 do { \
28 if (!(expr)) { \
29 ALOGE("Failed to call: %s", #expr); \
30 return value; \
31 } \
32 } while (0)
Yifan Hongb675ffe2021-08-05 16:37:17 -070033
Steven Morelandf183fdd2020-10-27 00:12:12 +000034namespace android {
35
36// avoid optimizations
37void zeroMemory(uint8_t* data, size_t size);
38
Yifan Hongb675ffe2021-08-05 16:37:17 -070039android::base::Result<void> setNonBlocking(android::base::borrowed_fd fd);
40
Andrei Homescuc24c8792022-04-19 00:24:51 +000041status_t getRandomBytes(uint8_t* data, size_t size);
42
Frederick Mayledc07cf82022-05-26 20:30:12 +000043// View of contiguous sequence. Similar to std::span.
44template <typename T>
45struct Span {
46 T* data = nullptr;
47 size_t size = 0;
48
49 size_t byteSize() { return size * sizeof(T); }
50
51 iovec toIovec() { return {const_cast<std::remove_const_t<T>*>(data), byteSize()}; }
52
53 // Truncates `this` to a length of `offset` and returns a span with the
54 // remainder.
55 //
56 // Aborts if offset > size.
57 Span<T> splitOff(size_t offset) {
58 LOG_ALWAYS_FATAL_IF(offset > size);
59 Span<T> rest = {data + offset, size - offset};
60 size = offset;
61 return rest;
62 }
Frederick Mayle69a0c992022-05-26 20:38:39 +000063
64 // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U).
65 template <typename U>
66 std::optional<Span<U>> reinterpret() const {
67 // Only allow casting from bytes for simplicity.
68 static_assert(std::is_same_v<std::remove_const_t<T>, uint8_t>);
69 if (size % sizeof(U) != 0) {
70 return std::nullopt;
71 }
72 return Span<U>{reinterpret_cast<U*>(data), size / sizeof(U)};
73 }
Frederick Mayledc07cf82022-05-26 20:30:12 +000074};
75
Steven Morelandf183fdd2020-10-27 00:12:12 +000076} // namespace android