Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | #pragma once |
| 18 | |
Anatol Pomazau | 45a2518 | 2019-08-08 14:37:59 -0700 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 21 | #include <algorithm> |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 22 | #include <memory> |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 23 | #include <utility> |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 24 | #include <vector> |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 25 | |
| 26 | #include <android-base/logging.h> |
| 27 | |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 28 | #include "sysdeps/uio.h" |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 29 | |
| 30 | // Essentially std::vector<char>, except without zero initialization or reallocation. |
| 31 | struct Block { |
| 32 | using iterator = char*; |
| 33 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 34 | Block() = default; |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 35 | |
| 36 | explicit Block(size_t size) { allocate(size); } |
| 37 | |
| 38 | template <typename Iterator> |
| 39 | Block(Iterator begin, Iterator end) : Block(end - begin) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 40 | std::copy(begin, end, data_.get()); |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | Block(const Block& copy) = delete; |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 44 | Block(Block&& move) noexcept |
| 45 | : data_(std::exchange(move.data_, nullptr)), |
| 46 | capacity_(std::exchange(move.capacity_, 0)), |
| 47 | size_(std::exchange(move.size_, 0)) {} |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 48 | |
| 49 | Block& operator=(const Block& copy) = delete; |
Chih-Hung Hsieh | 3c99c39 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 50 | Block& operator=(Block&& move) noexcept { |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 51 | clear(); |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 52 | data_ = std::exchange(move.data_, nullptr); |
| 53 | capacity_ = std::exchange(move.capacity_, 0); |
| 54 | size_ = std::exchange(move.size_, 0); |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 55 | return *this; |
| 56 | } |
| 57 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 58 | ~Block() = default; |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 59 | |
| 60 | void resize(size_t new_size) { |
| 61 | if (!data_) { |
| 62 | allocate(new_size); |
| 63 | } else { |
| 64 | CHECK_GE(capacity_, new_size); |
| 65 | size_ = new_size; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | template <typename InputIt> |
| 70 | void assign(InputIt begin, InputIt end) { |
| 71 | clear(); |
| 72 | allocate(end - begin); |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 73 | std::copy(begin, end, data_.get()); |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void clear() { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 77 | data_.reset(); |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 78 | capacity_ = 0; |
| 79 | size_ = 0; |
| 80 | } |
| 81 | |
| 82 | size_t capacity() const { return capacity_; } |
| 83 | size_t size() const { return size_; } |
| 84 | bool empty() const { return size() == 0; } |
| 85 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 86 | char* data() { return data_.get(); } |
| 87 | const char* data() const { return data_.get(); } |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 88 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 89 | char* begin() { return data_.get(); } |
| 90 | const char* begin() const { return data_.get(); } |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 91 | |
| 92 | char* end() { return data() + size_; } |
| 93 | const char* end() const { return data() + size_; } |
| 94 | |
| 95 | char& operator[](size_t idx) { return data()[idx]; } |
| 96 | const char& operator[](size_t idx) const { return data()[idx]; } |
| 97 | |
| 98 | bool operator==(const Block& rhs) const { |
| 99 | return size() == rhs.size() && memcmp(data(), rhs.data(), size()) == 0; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | void allocate(size_t size) { |
| 104 | CHECK(data_ == nullptr); |
| 105 | CHECK_EQ(0ULL, capacity_); |
| 106 | CHECK_EQ(0ULL, size_); |
| 107 | if (size != 0) { |
Josh Gao | 5b47082 | 2018-11-12 13:44:38 -0800 | [diff] [blame] | 108 | // This isn't std::make_unique because that's equivalent to `new char[size]()`, which |
| 109 | // value-initializes the array instead of leaving it uninitialized. As an optimization, |
| 110 | // call new without parentheses to avoid this costly initialization. |
| 111 | data_.reset(new char[size]); |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 112 | capacity_ = size; |
| 113 | size_ = size; |
| 114 | } |
| 115 | } |
| 116 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 117 | std::unique_ptr<char[]> data_; |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 118 | size_t capacity_ = 0; |
| 119 | size_t size_ = 0; |
| 120 | }; |
| 121 | |
| 122 | struct amessage { |
| 123 | uint32_t command; /* command identifier constant */ |
| 124 | uint32_t arg0; /* first argument */ |
| 125 | uint32_t arg1; /* second argument */ |
| 126 | uint32_t data_length; /* length of payload (0 is allowed) */ |
| 127 | uint32_t data_check; /* checksum of data payload */ |
| 128 | uint32_t magic; /* command ^ 0xffffffff */ |
| 129 | }; |
| 130 | |
| 131 | struct apacket { |
| 132 | using payload_type = Block; |
| 133 | amessage msg; |
| 134 | payload_type payload; |
| 135 | }; |
| 136 | |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 137 | struct IOVector { |
| 138 | using value_type = char; |
| 139 | using block_type = Block; |
| 140 | using size_type = size_t; |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 141 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 142 | IOVector() = default; |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 143 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 144 | explicit IOVector(block_type&& block) { append(std::move(block)); } |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 145 | |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 146 | IOVector(const IOVector& copy) = delete; |
Chih-Hung Hsieh | 3c99c39 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 147 | IOVector(IOVector&& move) noexcept : IOVector() { *this = std::move(move); } |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 148 | |
| 149 | IOVector& operator=(const IOVector& copy) = delete; |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 150 | IOVector& operator=(IOVector&& move) noexcept; |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 151 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 152 | size_type size() const { return chain_length_ - begin_offset_; } |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 153 | bool empty() const { return size() == 0; } |
| 154 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 155 | // Return the last block so the caller can still reuse its allocated capacity |
| 156 | // or it can be simply ignored. |
| 157 | block_type clear(); |
| 158 | |
| 159 | void drop_front(size_type len); |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 160 | |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 161 | // Split the first |len| bytes out of this chain into its own. |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 162 | IOVector take_front(size_type len); |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 163 | |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 164 | // Add a nonempty block to the chain. |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 165 | void append(block_type&& block) { |
| 166 | if (block.size() == 0) { |
Josh Gao | 15fa133 | 2019-04-03 12:56:22 -0700 | [diff] [blame] | 167 | return; |
| 168 | } |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 169 | CHECK_NE(0ULL, block.size()); |
| 170 | chain_length_ += block.size(); |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 171 | chain_.emplace_back(std::move(block)); |
| 172 | } |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 173 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 174 | void trim_front(); |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 175 | |
| 176 | private: |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 177 | void trim_chain_front(); |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 178 | |
| 179 | // Drop the front block from the chain, and update chain_length_ appropriately. |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 180 | void pop_front_block(); |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 181 | |
| 182 | // Iterate over the blocks with a callback with an operator()(const char*, size_t). |
| 183 | template <typename Fn> |
| 184 | void iterate_blocks(Fn&& callback) const { |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 185 | if (size() == 0) { |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 189 | for (size_t i = start_index_; i < chain_.size(); ++i) { |
| 190 | const auto& block = chain_[i]; |
| 191 | const char* begin = block.data(); |
| 192 | size_t length = block.size(); |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 193 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 194 | if (i == start_index_) { |
| 195 | CHECK_GE(block.size(), begin_offset_); |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 196 | begin += begin_offset_; |
| 197 | length -= begin_offset_; |
| 198 | } |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 199 | callback(begin, length); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | public: |
| 204 | // Copy all of the blocks into a single block. |
| 205 | template <typename CollectionType = block_type> |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 206 | CollectionType coalesce() const& { |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 207 | CollectionType result; |
| 208 | if (size() == 0) { |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | result.resize(size()); |
| 213 | |
| 214 | size_t offset = 0; |
| 215 | iterate_blocks([&offset, &result](const char* data, size_t len) { |
| 216 | memcpy(&result[offset], data, len); |
| 217 | offset += len; |
| 218 | }); |
| 219 | |
| 220 | return result; |
| 221 | } |
| 222 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 223 | block_type coalesce() &&; |
| 224 | |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 225 | template <typename FunctionType> |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 226 | auto coalesced(FunctionType&& f) const { |
| 227 | if (chain_.size() == start_index_ + 1) { |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 228 | // If we only have one block, we can use it directly. |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 229 | return f(chain_[start_index_].data() + begin_offset_, size()); |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 230 | } else { |
| 231 | // Otherwise, copy to a single block. |
| 232 | auto data = coalesce(); |
| 233 | return f(data.data(), data.size()); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Get a list of iovecs that can be used to write out all of the blocks. |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 238 | std::vector<adb_iovec> iovecs() const; |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 239 | |
| 240 | private: |
| 241 | // Total length of all of the blocks in the chain. |
| 242 | size_t chain_length_ = 0; |
| 243 | |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 244 | size_t begin_offset_ = 0; |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame^] | 245 | size_t start_index_ = 0; |
| 246 | std::vector<block_type> chain_; |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 247 | }; |