MySQL 9.1.0
Source Code Documentation
owned_buffer.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_HTTP_SRC_HTTP_BASE_DETAILS_OWNED_BUFFER_H_
27#define ROUTER_SRC_HTTP_SRC_HTTP_BASE_DETAILS_OWNED_BUFFER_H_
28
29#include <string.h>
30
31#include <algorithm>
32#include <cstring> // memcpy
33#include <memory>
34#include <utility>
35
37
38namespace http {
39namespace base {
40namespace details {
41
42template <typename T>
44 public:
47 ref_buffer(const ref_buffer &other) : ref_{other.ref_} {}
48
49 void *data() const noexcept { return ref_.data(); }
50 size_t size() const noexcept { return ref_.size(); }
51
52 void reset() { ref_.reset(); }
53
54 This &operator+=(size_t n) {
55 ref_ += n;
56 return *this;
57 }
58
59 operator net::const_buffer() const {
60 return net::const_buffer(data(), size());
61 }
62
63 private:
64 T &ref_;
65};
66
67template <typename T>
69 public:
72 ref_buffers(const ref_buffers &other) : ref_{other.ref_} {}
73
74 auto begin() const { return ref_->begin(); }
75 auto end() const { return ref_->end(); }
76
77 This &operator=(const This &t) { ref_ = t.ref_; }
78
79 private:
80 T *ref_;
81};
82
84 public:
85 owned_buffer(size_t n = 0)
86 : buffer_{n ? new uint8_t[n] : nullptr},
89 data_size_{0} {}
90
92 : buffer_{std::move(other.buffer_)},
95 data_size_{other.data_size_} {}
96
97 public: // Methods needed by net_ts stream template
98 void *data() const noexcept { return data_movable_; }
99 size_t size() const noexcept { return data_size_; }
100
101 void reset() {
102 data_movable_ = buffer_.get();
103 data_size_ = 0;
104 }
105
108 data_size_ = data_size_ > n ? data_size_ - n : 0;
109 return *this;
110 }
111
112 operator net::const_buffer() const {
113 return net::const_buffer(data(), size());
114 }
115
116 public: // Other methods
117 size_t space_left() const { return buffer_size_ - data_size_; }
118 bool empty() const { return 0 == data_size_; }
119
120 size_t write(const uint8_t *source, size_t source_size) {
121 const auto bytes_to_copy = std::min(source_size, space_left());
122 memcpy(buffer_.get() + data_size_, source, bytes_to_copy);
123 data_size_ += bytes_to_copy;
124
125 return bytes_to_copy;
126 }
127
128 private:
129 std::unique_ptr<uint8_t[]> buffer_;
130 const size_t buffer_size_;
131
134};
135
136} // namespace details
137} // namespace base
138} // namespace http
139
140#endif // ROUTER_SRC_HTTP_SRC_HTTP_BASE_DETAILS_OWNED_BUFFER_H_
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Definition: owned_buffer.h:83
owned_buffer & operator+=(size_t n)
Definition: owned_buffer.h:106
owned_buffer(size_t n=0)
Definition: owned_buffer.h:85
const size_t buffer_size_
Definition: owned_buffer.h:130
uint8_t * data_movable_
Definition: owned_buffer.h:132
size_t space_left() const
Definition: owned_buffer.h:117
size_t data_size_
Definition: owned_buffer.h:133
owned_buffer(owned_buffer &&other)
Definition: owned_buffer.h:91
void * data() const noexcept
Definition: owned_buffer.h:98
size_t write(const uint8_t *source, size_t source_size)
Definition: owned_buffer.h:120
bool empty() const
Definition: owned_buffer.h:118
size_t size() const noexcept
Definition: owned_buffer.h:99
void reset()
Definition: owned_buffer.h:101
std::unique_ptr< uint8_t[]> buffer_
Definition: owned_buffer.h:129
Definition: owned_buffer.h:43
ref_buffer(T &ref)
Definition: owned_buffer.h:46
This & operator+=(size_t n)
Definition: owned_buffer.h:54
void * data() const noexcept
Definition: owned_buffer.h:49
ref_buffer(const ref_buffer &other)
Definition: owned_buffer.h:47
void reset()
Definition: owned_buffer.h:52
T & ref_
Definition: owned_buffer.h:64
size_t size() const noexcept
Definition: owned_buffer.h:50
Definition: owned_buffer.h:68
ref_buffers(T &ref)
Definition: owned_buffer.h:71
This & operator=(const This &t)
Definition: owned_buffer.h:77
auto begin() const
Definition: owned_buffer.h:74
auto end() const
Definition: owned_buffer.h:75
T * ref_
Definition: owned_buffer.h:80
ref_buffers(const ref_buffers &other)
Definition: owned_buffer.h:72
Definition: buffer.h:135
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
Definition: connection.h:56
static mysql_service_status_t get(THD **thd) noexcept
Definition: mysql_current_thread_reader_all_empty.cc:31
Definition: gcs_xcom_synode.h:64
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42
int n
Definition: xcom_base.cc:509