forked from zhllxt/asio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_buffer.hpp
More file actions
588 lines (434 loc) · 17.4 KB
/
multi_buffer.hpp
File metadata and controls
588 lines (434 loc) · 17.4 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BEAST_MULTI_BUFFER_HPP
#define BEAST_MULTI_BUFFER_HPP
#include <beast/core/detail/config.hpp>
#include <beast/core/detail/allocator.hpp>
#include <asio/buffer.hpp>
#include <beast/core/empty_value.hpp>
#include <beast/core/type_with_alignment.hpp>
#include <iterator>
#include <limits>
#include <memory>
#include <type_traits>
#include <list>
namespace beast {
/** A dynamic buffer providing sequences of variable length.
A dynamic buffer encapsulates memory storage that may be
automatically resized as required, where the memory is
divided into two regions: readable bytes followed by
writable bytes. These memory regions are internal to
the dynamic buffer, but direct access to the elements
is provided to permit them to be efficiently used with
I/O operations.
The implementation uses a sequence of one or more byte
arrays of varying sizes to represent the readable and
writable bytes. Additional byte array objects are
appended to the sequence to accommodate changes in the
desired size. The behavior and implementation of this
container is most similar to `std::deque`.
Objects of this type meet the requirements of <em>DynamicBuffer</em>
and have the following additional properties:
@li A mutable buffer sequence representing the readable
bytes is returned by @ref data when `this` is non-const.
@li Buffer sequences representing the readable and writable
bytes, returned by @ref data and @ref prepare, may have
length greater than one.
@li A configurable maximum size may be set upon construction
and adjusted afterwards. Calls to @ref prepare that would
exceed this size will throw `std::length_error`.
@li Sequences previously obtained using @ref data remain
valid after calls to @ref prepare or @ref commit.
@tparam Allocator The allocator to use for managing memory.
*/
template<class Allocator>
class basic_multi_buffer
#if ! BEAST_DOXYGEN
: private beast::empty_value<Allocator>
#endif
{
// Fancy pointers are not supported
static_assert(std::is_pointer<typename
std::allocator_traits<Allocator>::pointer>::value,
"Allocator must use regular pointers");
static bool constexpr default_nothrow =
std::is_nothrow_default_constructible<Allocator>::value;
// Storage for the list of buffers representing the input
// and output sequences. The allocation for each element
// contains `element` followed by raw storage bytes.
class element
{
using size_type = typename
detail::allocator_traits<Allocator>::size_type;
size_type const size_;
public:
element(element const&) = delete;
explicit
element(size_type n) noexcept
: size_(n)
{
}
size_type
size() const noexcept
{
return size_;
}
char*
data() const noexcept
{
return const_cast<char*>(
reinterpret_cast<char const*>(this + 1));
}
};
template<bool>
class subrange;
using size_type = typename
detail::allocator_traits<Allocator>::size_type;
using align_type = typename
beast::type_with_alignment<alignof(element)>::type;
using rebind_type = typename
beast::detail::allocator_traits<Allocator>::
template rebind_alloc<align_type>;
using alloc_traits =
beast::detail::allocator_traits<rebind_type>;
using list_type = typename std::list<element*>;
using iter = typename list_type::iterator;
using const_iter = typename list_type::const_iterator;
using pocma = typename
alloc_traits::propagate_on_container_move_assignment;
using pocca = typename
alloc_traits::propagate_on_container_copy_assignment;
static_assert(std::is_base_of<std::bidirectional_iterator_tag,
typename std::iterator_traits<iter>::iterator_category>::value,
"BidirectionalIterator type requirements not met");
static_assert(std::is_base_of<std::bidirectional_iterator_tag,
typename std::iterator_traits<const_iter>::iterator_category>::value,
"BidirectionalIterator type requirements not met");
std::size_t max_;
list_type list_; // list of allocated buffers
iter out_; // element that contains out_pos_
size_type in_size_ = 0; // size of the input sequence
size_type in_pos_ = 0; // input offset in list_.front()
size_type out_pos_ = 0; // output offset in *out_
size_type out_end_ = 0; // output end offset in list_.back()
public:
#if BEAST_DOXYGEN
/// The ConstBufferSequence used to represent the readable bytes.
using const_buffers_type = __implementation_defined__;
/// The MutableBufferSequence used to represent the writable bytes.
using mutable_buffers_type = __implementation_defined__;
#else
using const_buffers_type = subrange<false>;
using mutable_buffers_type = subrange<true>;
#endif
/// The type of allocator used.
using allocator_type = Allocator;
/// Destructor
~basic_multi_buffer();
/** Constructor
After construction, @ref capacity will return zero, and
@ref max_size will return the largest value which may
be passed to the allocator's `allocate` function.
*/
basic_multi_buffer() noexcept(default_nothrow);
/** Constructor
After construction, @ref capacity will return zero, and
@ref max_size will return the specified value of `limit`.
@param limit The desired maximum size.
*/
explicit
basic_multi_buffer(
std::size_t limit) noexcept(default_nothrow);
/** Constructor
After construction, @ref capacity will return zero, and
@ref max_size will return the largest value which may
be passed to the allocator's `allocate` function.
@param alloc The allocator to use for the object.
@esafe
No-throw guarantee.
*/
explicit
basic_multi_buffer(Allocator const& alloc) noexcept;
/** Constructor
After construction, @ref capacity will return zero, and
@ref max_size will return the specified value of `limit`.
@param limit The desired maximum size.
@param alloc The allocator to use for the object.
@esafe
No-throw guarantee.
*/
basic_multi_buffer(
std::size_t limit, Allocator const& alloc) noexcept;
/** Move Constructor
The container is constructed with the contents of `other`
using move semantics. The maximum size will be the same
as the moved-from object.
Buffer sequences previously obtained from `other` using
@ref data or @ref prepare remain valid after the move.
@param other The object to move from. After the move, the
moved-from object will have zero capacity, zero readable
bytes, and zero writable bytes.
@esafe
No-throw guarantee.
*/
basic_multi_buffer(basic_multi_buffer&& other) noexcept;
/** Move Constructor
Using `alloc` as the allocator for the new container, the
contents of `other` are moved. If `alloc != other.get_allocator()`,
this results in a copy. The maximum size will be the same
as the moved-from object.
Buffer sequences previously obtained from `other` using
@ref data or @ref prepare become invalid after the move.
@param other The object to move from. After the move,
the moved-from object will have zero capacity, zero readable
bytes, and zero writable bytes.
@param alloc The allocator to use for the object.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of `alloc`.
*/
basic_multi_buffer(
basic_multi_buffer&& other,
Allocator const& alloc);
/** Copy Constructor
This container is constructed with the contents of `other`
using copy semantics. The maximum size will be the same
as the copied object.
@param other The object to copy from.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of the allocator.
*/
basic_multi_buffer(basic_multi_buffer const& other);
/** Copy Constructor
This container is constructed with the contents of `other`
using copy semantics and the specified allocator. The maximum
size will be the same as the copied object.
@param other The object to copy from.
@param alloc The allocator to use for the object.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of `alloc`.
*/
basic_multi_buffer(basic_multi_buffer const& other,
Allocator const& alloc);
/** Copy Constructor
This container is constructed with the contents of `other`
using copy semantics. The maximum size will be the same
as the copied object.
@param other The object to copy from.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of the allocator.
*/
template<class OtherAlloc>
basic_multi_buffer(basic_multi_buffer<
OtherAlloc> const& other);
/** Copy Constructor
This container is constructed with the contents of `other`
using copy semantics. The maximum size will be the same
as the copied object.
@param other The object to copy from.
@param alloc The allocator to use for the object.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of `alloc`.
*/
template<class OtherAlloc>
basic_multi_buffer(
basic_multi_buffer<OtherAlloc> const& other,
allocator_type const& alloc);
/** Move Assignment
The container is assigned with the contents of `other`
using move semantics. The maximum size will be the same
as the moved-from object.
Buffer sequences previously obtained from `other` using
@ref data or @ref prepare remain valid after the move.
@param other The object to move from. After the move,
the moved-from object will have zero capacity, zero readable
bytes, and zero writable bytes.
*/
basic_multi_buffer&
operator=(basic_multi_buffer&& other);
/** Copy Assignment
The container is assigned with the contents of `other`
using copy semantics. The maximum size will be the same
as the copied object.
After the copy, `this` will have zero writable bytes.
@param other The object to copy from.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of the allocator.
*/
basic_multi_buffer& operator=(
basic_multi_buffer const& other);
/** Copy Assignment
The container is assigned with the contents of `other`
using copy semantics. The maximum size will be the same
as the copied object.
After the copy, `this` will have zero writable bytes.
@param other The object to copy from.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of the allocator.
*/
template<class OtherAlloc>
basic_multi_buffer& operator=(
basic_multi_buffer<OtherAlloc> const& other);
/// Returns a copy of the allocator used.
allocator_type
get_allocator() const
{
return this->get();
}
/** Set the maximum allowed capacity
This function changes the currently configured upper limit
on capacity to the specified value.
@param n The maximum number of bytes ever allowed for capacity.
@esafe
No-throw guarantee.
*/
void
max_size(std::size_t n) noexcept
{
max_ = n;
}
/** Guarantee a minimum capacity
This function adjusts the internal storage (if necessary)
to guarantee space for at least `n` bytes.
Buffer sequences previously obtained using @ref data remain
valid, while buffer sequences previously obtained using
@ref prepare become invalid.
@param n The minimum number of byte for the new capacity.
If this value is greater than the maximum size, then the
maximum size will be adjusted upwards to this value.
@throws std::length_error if n is larger than the maximum
allocation size of the allocator.
@esafe
Strong guarantee.
*/
void
reserve(std::size_t n);
/** Reallocate the buffer to fit the readable bytes exactly.
Buffer sequences previously obtained using @ref data or
@ref prepare become invalid.
@esafe
Strong guarantee.
*/
void
shrink_to_fit();
/** Set the size of the readable and writable bytes to zero.
This clears the buffer without changing capacity.
Buffer sequences previously obtained using @ref data or
@ref prepare become invalid.
@esafe
No-throw guarantee.
*/
void
clear() noexcept;
/// Exchange two dynamic buffers
template<class Alloc>
friend
void
swap(
basic_multi_buffer<Alloc>& lhs,
basic_multi_buffer<Alloc>& rhs) noexcept;
//--------------------------------------------------------------------------
/// Returns the number of readable bytes.
size_type
size() const noexcept
{
return in_size_;
}
/// Return the maximum number of bytes, both readable and writable, that can ever be held.
size_type
max_size() const noexcept
{
return max_;
}
/// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
std::size_t
capacity() const noexcept;
/** Returns a constant buffer sequence representing the readable bytes
@note The sequence may contain multiple contiguous memory regions.
*/
const_buffers_type
data() const noexcept;
/** Returns a constant buffer sequence representing the readable bytes
@note The sequence may contain multiple contiguous memory regions.
*/
const_buffers_type
cdata() const noexcept
{
return data();
}
/** Returns a mutable buffer sequence representing the readable bytes.
@note The sequence may contain multiple contiguous memory regions.
*/
mutable_buffers_type
data() noexcept;
/** Returns a mutable buffer sequence representing writable bytes.
Returns a mutable buffer sequence representing the writable
bytes containing exactly `n` bytes of storage. Memory may be
reallocated as needed.
All buffer sequences previously obtained using @ref prepare are
invalidated. Buffer sequences previously obtained using @ref data
remain valid.
@param n The desired number of bytes in the returned buffer
sequence.
@throws std::length_error if `size() + n` exceeds `max_size()`.
@esafe
Strong guarantee.
*/
mutable_buffers_type
prepare(size_type n);
/** Append writable bytes to the readable bytes.
Appends n bytes from the start of the writable bytes to the
end of the readable bytes. The remainder of the writable bytes
are discarded. If n is greater than the number of writable
bytes, all writable bytes are appended to the readable bytes.
All buffer sequences previously obtained using @ref prepare are
invalidated. Buffer sequences previously obtained using @ref data
remain valid.
@param n The number of bytes to append. If this number
is greater than the number of writable bytes, all
writable bytes are appended.
@esafe
No-throw guarantee.
*/
void
commit(size_type n) noexcept;
/** Remove bytes from beginning of the readable bytes.
Removes n bytes from the beginning of the readable bytes.
All buffers sequences previously obtained using
@ref data or @ref prepare are invalidated.
@param n The number of bytes to remove. If this number
is greater than the number of readable bytes, all
readable bytes are removed.
@esafe
No-throw guarantee.
*/
void
consume(size_type n) noexcept;
private:
template<class OtherAlloc>
friend class basic_multi_buffer;
template<class OtherAlloc>
void copy_from(basic_multi_buffer<OtherAlloc> const&);
void move_assign(basic_multi_buffer& other, std::false_type);
void move_assign(basic_multi_buffer& other, std::true_type) noexcept;
void copy_assign(basic_multi_buffer const& other, std::false_type);
void copy_assign(basic_multi_buffer const& other, std::true_type);
void swap(basic_multi_buffer&) noexcept;
void swap(basic_multi_buffer&, std::true_type) noexcept;
void swap(basic_multi_buffer&, std::false_type) noexcept;
void destroy(list_type& list) noexcept;
void destroy(const_iter it);
void destroy(element& e);
element& alloc(std::size_t size);
void debug_check() const;
};
/// A typical multi buffer
using multi_buffer = basic_multi_buffer<std::allocator<char>>;
} // beast
#include <beast/core/impl/multi_buffer.hpp>
#endif