COROIO: coroio/actors/output_buffer.hpp Source File
COROIO
Loading...
Searching...
No Matches
output_buffer.hpp
1#pragma once
2
3#include <algorithm>
4#include <cstdlib>
5#include <cstring>
6#include <memory>
7#include <new>
8#include <span>
9#include <utility>
10
11namespace NNet {
12namespace NActors {
13
27class TOutputBuffer {
28public:
30 static constexpr size_t MinCapacity = 4096;
32 static constexpr size_t GrowthFactor = 2;
35 static constexpr size_t StaleRatio = 2;
36
37 TOutputBuffer() = default;
38 TOutputBuffer(const TOutputBuffer&) = delete;
39 TOutputBuffer& operator=(const TOutputBuffer&) = delete;
40
42 TOutputBuffer(TOutputBuffer&& other) noexcept
43 : Ptr(std::move(other.Ptr))
44 , Cap(std::exchange(other.Cap, 0))
45 { }
46
48 TOutputBuffer& operator=(TOutputBuffer&& other) noexcept {
49 if (this != &other) {
50 Ptr = std::move(other.Ptr);
51 Cap = std::exchange(other.Cap, 0);
52 }
53 return *this;
54 }
55
57 char* Data() {
58 return Ptr.get();
59 }
60
62 size_t Capacity() const {
63 return Cap;
64 }
65
68 void Reserve(size_t used, size_t size) {
69 if (size > Cap) [[unlikely]] {
70 Grow(used, size);
71 }
72 }
73
75 void Swap(TOutputBuffer& other) noexcept {
76 Ptr.swap(other.Ptr);
77 std::swap(Cap, other.Cap);
78 }
79
80private:
81 struct TFree {
82 void operator()(char* p) const {
83 std::free(p);
84 }
85 };
86
87 void Grow(size_t used, size_t size) {
88 const size_t cap = std::max({size, Cap * GrowthFactor, MinCapacity});
89 if (used * StaleRatio < Cap) {
90 std::unique_ptr<char, TFree> block(static_cast<char*>(std::malloc(cap)));
91 if (!block) {
92 throw std::bad_alloc();
93 }
94 if (used > 0) {
95 std::memcpy(block.get(), Ptr.get(), used);
96 }
97 Ptr = std::move(block);
98 } else {
99 // realloc(nullptr, n) is malloc(n); on failure the old block stays valid
100 char* block = static_cast<char*>(std::realloc(Ptr.get(), cap));
101 if (!block) {
102 throw std::bad_alloc();
103 }
104 Ptr.release();
105 Ptr.reset(block);
106 }
107 Cap = cap;
108 }
109
110 std::unique_ptr<char, TFree> Ptr;
111 size_t Cap = 0;
112};
113
121inline void PoisonAcquired([[maybe_unused]] std::span<char> range) {
122#ifndef NDEBUG
123 if (!range.empty()) {
124 std::memset(range.data(), 0xA5, range.size());
125 }
126#endif
127}
128
129} // namespace NActors
130} // namespace NNet
TOutputBuffer & operator=(TOutputBuffer &&other) noexcept
Frees the block of this and takes the block of other, which is left empty.
Definition output_buffer.hpp:48
TOutputBuffer(TOutputBuffer &&other) noexcept
Takes the block; other is left empty.
Definition output_buffer.hpp:42
void Swap(TOutputBuffer &other) noexcept
Exchanges the blocks of the two buffers.
Definition output_buffer.hpp:75
static constexpr size_t MinCapacity
The smallest block Reserve allocates: small frames do not cause a chain of tiny reallocations.
Definition output_buffer.hpp:30
size_t Capacity() const
The size of the block in bytes.
Definition output_buffer.hpp:62
static constexpr size_t GrowthFactor
A block grows to this many times its size at least: the total copy stays proportional to the final si...
Definition output_buffer.hpp:32
static constexpr size_t StaleRatio
Definition output_buffer.hpp:35
void Reserve(size_t used, size_t size)
Definition output_buffer.hpp:68
char * Data()
The start of the block, null while the capacity is 0. Valid until the next Reserve or Swap.
Definition output_buffer.hpp:57