MySQL 8.4.0
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/// @file rw_buffer.h
25///
26/// Non-owning manager for a fixed memory buffer, which is split into
27/// a read part and a write part, with a movable split position.
28
29#ifndef MYSQL_BINLOG_EVENT_COMPRESSION_BUFFER_RW_BUFFER_H
30#define MYSQL_BINLOG_EVENT_COMPRESSION_BUFFER_RW_BUFFER_H
31
32#include <cassert>
33
34#include "buffer_view.h"
35
36/// @addtogroup GroupLibsMysqlBinlogEvent
37/// @{
38
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 *;
69
70 Rw_buffer() = default;
71
72 /// Create a new Rw_buffer from the specified size and buffer.
73 ///
74 /// The read part will be 0 bytes at the beginning of the buffer,
75 /// and the write part will be the full buffer.
77 : m_read_part(buffer.begin(), 0),
79
80 /// Deleted copy constructor.
81 Rw_buffer(Rw_buffer &) = delete;
82
83 /// Default move constructor.
84 Rw_buffer(Rw_buffer &&) noexcept = default;
85
86 /// Deleted copy assignment operator.
87 Rw_buffer &operator=(Rw_buffer &) = delete;
88
89 /// Default move assignment operator.
90 Rw_buffer &operator=(Rw_buffer &&) noexcept = default;
91
92 /// Default delete operator.
93 virtual ~Rw_buffer() = default;
94
95 /// Return the read part.
96 const Buffer_view_t &read_part() const { return m_read_part; }
97
98 /// Return the read part.
100
101 /// Return the write part.
102 const Buffer_view_t &write_part() const { return m_write_part; }
103
104 /// Return the write part.
106
107 /// Return the total size of the read part and the write part.
108 Size_t capacity() const { return read_part().size() + write_part().size(); }
109
110 /// Set the position to a fixed number.
111 ///
112 /// The position is the same as the size of the read part.
113 ///
114 /// This adjusts the read part and the write part, so that the read
115 /// part size becomes equal to position, and the write part begins
116 /// where the read part ends.
117 ///
118 /// The specified position must be between 0 and capacity(),
119 /// inclusive.
120 ///
121 /// @note This alters the end iterator for the read part and the
122 /// begin iterator for the write part.
123 void set_position(Size_t new_position) {
124 auto capacity = this->capacity();
125 assert(new_position <= capacity);
126 new_position = std::min(new_position, capacity);
127
128 read_part() = Buffer_view_t(read_part().begin(), new_position);
129 write_part() = Buffer_view_t(read_part().end(), capacity - new_position);
130 }
131
132 /// Increase the position right, relative to the currrent position.
133 ///
134 /// The position is the same as the size of the read part.
135 ///
136 /// This adjusts the read part and the write part, so that the read
137 /// part size becomes equal to position, and the write part begins
138 /// where the read part ends.
139 ///
140 /// The resulting new position must be less than or equal to
141 /// capacity().
142 ///
143 /// @note This alters the end iterator for the read part and the
144 /// begin iterator for the write part.
145 void increase_position(Size_t increment) {
146 auto read_size = read_part().size();
147 assert(increment <= this->capacity() - read_size);
148 auto new_position = read_size + increment;
149 set_position(new_position);
150 }
151
152 /// Move the position left or right, relative to the current position.
153 ///
154 /// The position is the same as the size of the read part.
155 ///
156 /// This increments the right end of the read part by delta, and
157 /// increments the left end of the write part by delta.
158 ///
159 /// The resulting new position must be between 0 and capacity(),
160 /// inclusive.
161 ///
162 /// @note This alters the end iterator for the read part and the
163 /// begin iterator for the write part.
165 auto new_position = Difference_t(read_part().size()) + delta;
166 assert(new_position >= 0);
167 new_position = std::max(new_position, Difference_t(0));
168 set_position(Size_t(new_position));
169 }
170
171 protected:
174};
175
176} // namespace mysql::binlog::event::compression::buffer
177
178/// @}
179
180#endif // MYSQL_BINLOG_EVENT_COMPRESSION_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
const Char_t * Const_iterator_t
Definition: rw_buffer.h:66
const Buffer_view_t & read_part() const
Return the read part.
Definition: rw_buffer.h:96
void increase_position(Size_t increment)
Increase the position right, relative to the currrent position.
Definition: rw_buffer.h:145
Char_tp Char_t
Definition: rw_buffer.h:62
Buffer_view_t & write_part()
Return the write part.
Definition: rw_buffer.h:105
const Buffer_view_t & write_part() const
Return the write part.
Definition: rw_buffer.h:102
std::ptrdiff_t Difference_t
Definition: rw_buffer.h:64
void move_position(Difference_t delta)
Move the position left or right, relative to the current position.
Definition: rw_buffer.h:164
Rw_buffer(Rw_buffer &)=delete
Deleted copy constructor.
Rw_buffer(Rw_buffer &&) noexcept=default
Default move constructor.
Buffer_view_t & read_part()
Return the read part.
Definition: rw_buffer.h:99
mysql::binlog::event::compression::buffer::Buffer_view< Char_t > Buffer_view_t
Definition: rw_buffer.h:68
std::size_t Size_t
Definition: rw_buffer.h:63
Rw_buffer(Buffer_view_t buffer)
Create a new Rw_buffer from the specified size and buffer.
Definition: rw_buffer.h:76
Buffer_view_t m_write_part
Definition: rw_buffer.h:173
Buffer_view_t m_read_part
Definition: rw_buffer.h:172
void set_position(Size_t new_position)
Set the position to a fixed number.
Definition: rw_buffer.h:123
Size_t capacity() const
Return the total size of the read part and the write part.
Definition: rw_buffer.h:108
Char_t * Iterator_t
Definition: rw_buffer.h:65
Definition: buffer_sequence_view.h:51
const char * begin(const char *const c)
Definition: base64.h:44
size_t size(const char *const c)
Definition: base64.h:46
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418