-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.h
358 lines (286 loc) · 9.72 KB
/
types.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <string.h>
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
#include <android-base/logging.h>
#include "fdevent/fdevent.h"
#include "sysdeps/uio.h"
// Essentially std::vector<char>, except without zero initialization or reallocation.
struct Block {
using iterator = char*;
Block() = default;
explicit Block(size_t size) { allocate(size); }
template <typename Iterator>
Block(Iterator begin, Iterator end) : Block(end - begin) {
std::copy(begin, end, data_.get());
}
Block(const Block& copy) = delete;
Block(Block&& move) noexcept
: data_(std::exchange(move.data_, nullptr)),
capacity_(std::exchange(move.capacity_, 0)),
size_(std::exchange(move.size_, 0)) {}
Block& operator=(const Block& copy) = delete;
Block& operator=(Block&& move) noexcept {
clear();
data_ = std::exchange(move.data_, nullptr);
capacity_ = std::exchange(move.capacity_, 0);
size_ = std::exchange(move.size_, 0);
return *this;
}
~Block() = default;
void resize(size_t new_size) {
if (!data_) {
allocate(new_size);
} else {
CHECK_GE(capacity_, new_size);
size_ = new_size;
}
}
template <typename InputIt>
void assign(InputIt begin, InputIt end) {
clear();
allocate(end - begin);
std::copy(begin, end, data_.get());
}
void clear() {
data_.reset();
capacity_ = 0;
size_ = 0;
}
size_t capacity() const { return capacity_; }
size_t size() const { return size_; }
bool empty() const { return size() == 0; }
char* data() { return data_.get(); }
const char* data() const { return data_.get(); }
char* begin() { return data_.get(); }
const char* begin() const { return data_.get(); }
char* end() { return data() + size_; }
const char* end() const { return data() + size_; }
char& operator[](size_t idx) { return data()[idx]; }
const char& operator[](size_t idx) const { return data()[idx]; }
bool operator==(const Block& rhs) const {
return size() == rhs.size() && memcmp(data(), rhs.data(), size()) == 0;
}
private:
void allocate(size_t size) {
CHECK(data_ == nullptr);
CHECK_EQ(0ULL, capacity_);
CHECK_EQ(0ULL, size_);
if (size != 0) {
// This isn't std::make_unique because that's equivalent to `new char[size]()`, which
// value-initializes the array instead of leaving it uninitialized. As an optimization,
// call new without parentheses to avoid this costly initialization.
data_.reset(new char[size]);
capacity_ = size;
size_ = size;
}
}
std::unique_ptr<char[]> data_;
size_t capacity_ = 0;
size_t size_ = 0;
};
struct amessage {
uint32_t command; /* command identifier constant */
uint32_t arg0; /* first argument */
uint32_t arg1; /* second argument */
uint32_t data_length; /* length of payload (0 is allowed) */
uint32_t data_check; /* checksum of data payload */
uint32_t magic; /* command ^ 0xffffffff */
};
struct apacket {
using payload_type = Block;
amessage msg;
payload_type payload;
};
struct IOVector {
using value_type = char;
using block_type = Block;
using size_type = size_t;
IOVector() = default;
explicit IOVector(block_type&& block) { append(std::move(block)); }
IOVector(const IOVector& copy) = delete;
IOVector(IOVector&& move) noexcept : IOVector() { *this = std::move(move); }
IOVector& operator=(const IOVector& copy) = delete;
IOVector& operator=(IOVector&& move) noexcept;
const value_type* front_data() const {
if (chain_.empty()) {
return nullptr;
}
return chain_[start_index_].data() + begin_offset_;
}
size_type front_size() const {
if (chain_.empty()) {
return 0;
}
return chain_[start_index_].size() - begin_offset_;
}
size_type size() const { return chain_length_ - begin_offset_; }
bool empty() const { return size() == 0; }
// Return the last block so the caller can still reuse its allocated capacity
// or it can be simply ignored.
block_type clear();
void drop_front(size_type len);
// Split the first |len| bytes out of this chain into its own.
IOVector take_front(size_type len);
// Add a nonempty block to the chain.
void append(block_type&& block) {
if (block.size() == 0) {
return;
}
CHECK_NE(0ULL, block.size());
chain_length_ += block.size();
chain_.emplace_back(std::move(block));
}
void trim_front();
private:
void trim_chain_front();
// Drop the front block from the chain, and update chain_length_ appropriately.
void pop_front_block();
// Iterate over the blocks with a callback with an operator()(const char*, size_t).
template <typename Fn>
void iterate_blocks(Fn&& callback) const {
if (size() == 0) {
return;
}
for (size_t i = start_index_; i < chain_.size(); ++i) {
const auto& block = chain_[i];
const char* begin = block.data();
size_t length = block.size();
if (i == start_index_) {
CHECK_GE(block.size(), begin_offset_);
begin += begin_offset_;
length -= begin_offset_;
}
callback(begin, length);
}
}
public:
// Copy all of the blocks into a single block.
template <typename CollectionType = block_type>
CollectionType coalesce() const& {
CollectionType result;
if (size() == 0) {
return result;
}
result.resize(size());
size_t offset = 0;
iterate_blocks([&offset, &result](const char* data, size_t len) {
memcpy(&result[offset], data, len);
offset += len;
});
return result;
}
block_type coalesce() &&;
template <typename FunctionType>
auto coalesced(FunctionType&& f) const {
if (chain_.size() == start_index_ + 1) {
// If we only have one block, we can use it directly.
return f(chain_[start_index_].data() + begin_offset_, size());
} else {
// Otherwise, copy to a single block.
auto data = coalesce();
return f(data.data(), data.size());
}
}
// Get a list of iovecs that can be used to write out all of the blocks.
std::vector<adb_iovec> iovecs() const;
private:
// Total length of all of the blocks in the chain.
size_t chain_length_ = 0;
size_t begin_offset_ = 0;
size_t start_index_ = 0;
std::vector<block_type> chain_;
};
// An implementation of weak pointers tied to the fdevent run loop.
//
// This allows for code to submit a request for an object, and upon receiving
// a response, know whether the object is still alive, or has been destroyed
// because of other reasons. We keep a list of living weak_ptrs in each object,
// and clear the weak_ptrs when the object is destroyed. This is safe, because
// we require that both the destructor of the referent and the get method on
// the weak_ptr are executed on the main thread.
template <typename T>
struct enable_weak_from_this;
template <typename T>
struct weak_ptr {
weak_ptr() = default;
explicit weak_ptr(T* ptr) { reset(ptr); }
weak_ptr(const weak_ptr& copy) { reset(copy.get()); }
weak_ptr(weak_ptr&& move) {
reset(move.get());
move.reset();
}
~weak_ptr() { reset(); }
weak_ptr& operator=(const weak_ptr& copy) {
if (© == this) {
return *this;
}
reset(copy.get());
return *this;
}
weak_ptr& operator=(weak_ptr&& move) {
if (&move == this) {
return *this;
}
reset(move.get());
move.reset();
return *this;
}
T* get() const {
check_main_thread();
return ptr_;
}
void reset(T* ptr = nullptr) {
check_main_thread();
if (ptr == ptr_) {
return;
}
if (ptr_) {
ptr_->weak_ptrs_.erase(
std::remove(ptr_->weak_ptrs_.begin(), ptr_->weak_ptrs_.end(), this));
}
ptr_ = ptr;
if (ptr_) {
ptr_->weak_ptrs_.push_back(this);
}
}
private:
friend struct enable_weak_from_this<T>;
T* ptr_ = nullptr;
};
template <typename T>
struct enable_weak_from_this {
~enable_weak_from_this() {
if (!weak_ptrs_.empty()) {
check_main_thread();
for (auto& weak : weak_ptrs_) {
weak->ptr_ = nullptr;
}
weak_ptrs_.clear();
}
}
weak_ptr<T> weak() { return weak_ptr<T>(static_cast<T*>(this)); }
void schedule_deletion() {
fdevent_run_on_main_thread([this]() { delete static_cast<T*>(this); });
}
private:
friend struct weak_ptr<T>;
std::vector<weak_ptr<T>*> weak_ptrs_;
};