MySQL 8.0.37
Source Code Documentation
rw_buffer.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/// @addtogroup Replication
25/// @{
26///
27/// @file rw_buffer.h
28///
29/// Non-owning manager for a fixed memory buffer, which is split into
30/// a read part and a write part, with a movable split position.
31
32#ifndef MYSQL_BUFFER_RW_BUFFER_H_
33#define MYSQL_BUFFER_RW_BUFFER_H_
34
35#include <cassert>
36
37#include "buffer_view.h"
38
39namespace mysqlns::buffer {
40
41/// Non-owning read/write memory buffer manager with a fixed size.
42///
43/// This has a read/write position (which @c Buffer_view does not
44/// have). It does not have functionailty to grow the buffer (as @c
45/// Managed_buffer has).
46///
47/// Objects have one internal contiguous buffer which is split into
48/// two parts, each of which is a Buffer_view: the first part is the
49/// read part and the second part is the write part. API clients may
50/// write to the write part and then move the position forwards: this
51/// will increase the read part so that API clients can read what was
52/// just written, and decrease the write part so that next write will
53/// happen after the position that was just written.
54///
55/// Generally, std::stringstream is a safer interface for buffers and
56/// should be preferred when possible. This class is intended for
57/// interaction with C-like APIs that request a (possibly
58/// uninitialized) memory buffer which they write to.
59template <class Char_tp = unsigned char>
60class Rw_buffer {
61 public:
62 using Char_t = Char_tp;
63 using Size_t = std::size_t;
64 using Difference_t = std::ptrdiff_t;
65 using Iterator_t = Char_t *;
66 using Const_iterator_t = const Char_t *;
68
69 Rw_buffer() = default;
70
71 /// Create a new Rw_buffer from the specified size and buffer.
72 ///
73 /// The read part will be 0 bytes at the beginning of the buffer,
74 /// and the write part will be the full buffer.
76 : m_read_part(buffer.begin(), 0),
77 m_write_part(buffer.begin(), buffer.size()) {}
78
79 /// Deleted copy constructor.
80 Rw_buffer(Rw_buffer &) = delete;
81
82 /// Default move constructor.
83 Rw_buffer(Rw_buffer &&) noexcept = default;
84
85 /// Deleted copy assignment operator.
86 Rw_buffer &operator=(Rw_buffer &) = delete;
87
88 /// Default move assignment operator.
89 Rw_buffer &operator=(Rw_buffer &&) noexcept = default;
90
91 /// Default delete operator.
92 virtual ~Rw_buffer() = default;
93
94 /// Return the read part.
95 const Buffer_view_t &read_part() const { return m_read_part; }
96
97 /// Return the read part.
99
100 /// Return the write part.
101 const Buffer_view_t &write_part() const { return m_write_part; }
102
103 /// Return the write part.
105
106 /// Return the total size of the read part and the write part.
107 Size_t capacity() const { return read_part().size() + write_part().size(); }
108
109 /// Set the position to a fixed number.
110 ///
111 /// The position is the same as the size of the read part.
112 ///
113 /// This adjusts the read part and the write part, so that the read
114 /// part size becomes equal to position, and the write part begins
115 /// where the read part ends.
116 ///
117 /// The specified position must be between 0 and capacity(),
118 /// inclusive.
119 ///
120 /// @note This alters the end iterator for the read part and the
121 /// begin iterator for the write part.
122 void set_position(Size_t new_position) {
123 auto capacity = this->capacity();
124 assert(new_position <= capacity);
125 new_position = std::min(new_position, capacity);
126
127 read_part() = Buffer_view_t(read_part().begin(), new_position);
128 write_part() = Buffer_view_t(read_part().end(), capacity - new_position);
129 }
130
131 /// Increase the position right, relative to the currrent position.
132 ///
133 /// The position is the same as the size of the read part.
134 ///
135 /// This adjusts the read part and the write part, so that the read
136 /// part size becomes equal to position, and the write part begins
137 /// where the read part ends.
138 ///
139 /// The resulting new position must be less than or equal to
140 /// capacity().
141 ///
142 /// @note This alters the end iterator for the read part and the
143 /// begin iterator for the write part.
144 void increase_position(Size_t increment) {
145 auto read_size = read_part().size();
146 assert(increment <= this->capacity() - read_size);
147 auto new_position = read_size + increment;
148 set_position(new_position);
149 }
150
151 /// Move the position left or right, relative to the current position.
152 ///
153 /// The position is the same as the size of the read part.
154 ///
155 /// This increments the right end of the read part by delta, and
156 /// increments the left end of the write part by delta.
157 ///
158 /// The resulting new position must be between 0 and capacity(),
159 /// inclusive.
160 ///
161 /// @note This alters the end iterator for the read part and the
162 /// begin iterator for the write part.
164 auto new_position = Difference_t(read_part().size()) + delta;
165 assert(new_position >= 0);
166 new_position = std::max(new_position, Difference_t(0));
167 set_position(Size_t(new_position));
168 }
169
170 protected:
173};
174
175} // namespace mysqlns::buffer
176
177/// @} (end of group Replication)
178
179#endif // MYSQL_BUFFER_RW_BUFFER_H_
Class that groups a pointer+size as one object, without managing the memory for it.
Size_t size() const
Return the number of bytes.
Definition: buffer_view.h:112
Non-owning read/write memory buffer manager with a fixed size.
Definition: rw_buffer.h:60
Rw_buffer(Buffer_view_t buffer)
Create a new Rw_buffer from the specified size and buffer.
Definition: rw_buffer.h:75
void move_position(Difference_t delta)
Move the position left or right, relative to the current position.
Definition: rw_buffer.h:163
Rw_buffer(Rw_buffer &&) noexcept=default
Default move constructor.
const Char_t * Const_iterator_t
Definition: rw_buffer.h:66
Buffer_view_t & read_part()
Return the read part.
Definition: rw_buffer.h:98
Char_t * Iterator_t
Definition: rw_buffer.h:65
Rw_buffer(Rw_buffer &)=delete
Deleted copy constructor.
mysqlns::buffer::Buffer_view< Char_t > Buffer_view_t
Definition: rw_buffer.h:67
Buffer_view_t m_write_part
Definition: rw_buffer.h:172
void set_position(Size_t new_position)
Set the position to a fixed number.
Definition: rw_buffer.h:122
const Buffer_view_t & read_part() const
Return the read part.
Definition: rw_buffer.h:95
void increase_position(Size_t increment)
Increase the position right, relative to the currrent position.
Definition: rw_buffer.h:144
Buffer_view_t & write_part()
Return the write part.
Definition: rw_buffer.h:104
std::ptrdiff_t Difference_t
Definition: rw_buffer.h:64
Size_t capacity() const
Return the total size of the read part and the write part.
Definition: rw_buffer.h:107
Buffer_view_t m_read_part
Definition: rw_buffer.h:171
const Buffer_view_t & write_part() const
Return the write part.
Definition: rw_buffer.h:101
std::size_t Size_t
Definition: rw_buffer.h:63
Char_tp Char_t
Definition: rw_buffer.h:62
Definition: buffer_sequence_view.h:51
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:420