]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/core/temporary_buffer.hh
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / include / seastar / core / temporary_buffer.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright (C) 2014 Cloudius Systems, Ltd.
20 */
21
22 #pragma once
23
24 #include <seastar/core/deleter.hh>
25 #include <seastar/util/eclipse.hh>
26 #include <malloc.h>
27 #include <algorithm>
28
29 namespace seastar {
30
31 /// \addtogroup memory-module
32 /// @{
33
34 /// Temporary, self-managed byte buffer.
35 ///
36 /// A \c temporary_buffer is similar to an \c std::string or a \c std::unique_ptr<char[]>,
37 /// but provides more flexible memory management. A \c temporary_buffer can own the memory
38 /// it points to, or it can be shared with another \c temporary_buffer, or point at a substring
39 /// of a buffer. It uses a \ref deleter to manage its memory.
40 ///
41 /// A \c temporary_buffer should not be held indefinitely. It can be held while a request
42 /// is processed, or for a similar duration, but not longer, as it can tie up more memory
43 /// that its size indicates.
44 ///
45 /// A buffer can be shared: two \c temporary_buffer objects will point to the same data,
46 /// or a subset of it. See the \ref temporary_buffer::share() method.
47 ///
48 /// Unless you created a \c temporary_buffer yourself, do not modify its contents, as they
49 /// may be shared with another user that does not expect the data to change.
50 ///
51 /// Use cases for a \c temporary_buffer include:
52 /// - passing a substring of a tcp packet for the user to consume (zero-copy
53 /// tcp input)
54 /// - passing a refcounted blob held in memory to tcp, ensuring that when the TCP ACK
55 /// is received, the blob is released (by decrementing its reference count) (zero-copy
56 /// tcp output)
57 ///
58 /// \tparam CharType underlying character type (must be a variant of \c char).
59 template <typename CharType>
60 class temporary_buffer {
61 static_assert(sizeof(CharType) == 1, "must buffer stream of bytes");
62 CharType* _buffer;
63 size_t _size;
64 deleter _deleter;
65 public:
66 /// Creates a \c temporary_buffer of a specified size. The buffer is not shared
67 /// with anyone, and is not initialized.
68 ///
69 /// \param size buffer size, in bytes
70 explicit temporary_buffer(size_t size)
71 : _buffer(static_cast<CharType*>(malloc(size * sizeof(CharType)))), _size(size)
72 , _deleter(make_free_deleter(_buffer)) {
73 if (size && !_buffer) {
74 throw std::bad_alloc();
75 }
76 }
77 //explicit temporary_buffer(CharType* borrow, size_t size) : _buffer(borrow), _size(size) {}
78 /// Creates an empty \c temporary_buffer that does not point at anything.
79 temporary_buffer()
80 : _buffer(nullptr)
81 , _size(0) {}
82 temporary_buffer(const temporary_buffer&) = delete;
83 /// Moves a \c temporary_buffer.
84 temporary_buffer(temporary_buffer&& x) noexcept : _buffer(x._buffer), _size(x._size), _deleter(std::move(x._deleter)) {
85 x._buffer = nullptr;
86 x._size = 0;
87 }
88 /// Creates a \c temporary_buffer with a specific deleter.
89 ///
90 /// \param buf beginning of the buffer held by this \c temporary_buffer
91 /// \param size size of the buffer
92 /// \param d deleter controlling destruction of the buffer. The deleter
93 /// will be destroyed when there are no longer any users for the buffer.
94 temporary_buffer(CharType* buf, size_t size, deleter d)
95 : _buffer(buf), _size(size), _deleter(std::move(d)) {}
96 /// Creates a `temporary_buffer` containing a copy of the provided data
97 ///
98 /// \param src data buffer to be copied
99 /// \param size size of data buffer in `src`
100 temporary_buffer(const CharType* src, size_t size) : temporary_buffer(size) {
101 std::copy_n(src, size, _buffer);
102 }
103 void operator=(const temporary_buffer&) = delete;
104 /// Moves a \c temporary_buffer.
105 temporary_buffer& operator=(temporary_buffer&& x) noexcept {
106 if (this != &x) {
107 _buffer = x._buffer;
108 _size = x._size;
109 _deleter = std::move(x._deleter);
110 x._buffer = nullptr;
111 x._size = 0;
112 }
113 return *this;
114 }
115 /// Gets a pointer to the beginning of the buffer.
116 const CharType* get() const { return _buffer; }
117 /// Gets a writable pointer to the beginning of the buffer. Use only
118 /// when you are certain no user expects the buffer data not to change.
119 CharType* get_write() { return _buffer; }
120 /// Gets the buffer size.
121 size_t size() const { return _size; }
122 /// Gets a pointer to the beginning of the buffer.
123 const CharType* begin() const { return _buffer; }
124 /// Gets a pointer to the end of the buffer.
125 const CharType* end() const { return _buffer + _size; }
126 /// Returns the buffer, but with a reduced size. The original
127 /// buffer is consumed by this call and can no longer be used.
128 ///
129 /// \param size New size; must be smaller than current size.
130 /// \return the same buffer, with a prefix removed.
131 temporary_buffer prefix(size_t size) && {
132 auto ret = std::move(*this);
133 ret._size = size;
134 return ret;
135 }
136 /// Reads a character from a specific position in the buffer.
137 ///
138 /// \param pos position to read character from; must be less than size.
139 CharType operator[](size_t pos) const {
140 return _buffer[pos];
141 }
142 /// Checks whether the buffer is empty.
143 bool empty() const { return !size(); }
144 /// Checks whether the buffer is not empty.
145 explicit operator bool() const { return size(); }
146 /// Create a new \c temporary_buffer object referring to the same
147 /// underlying data. The underlying \ref deleter will not be destroyed
148 /// until both the original and the clone have been destroyed.
149 ///
150 /// \return a clone of the buffer object.
151 temporary_buffer share() {
152 return temporary_buffer(_buffer, _size, _deleter.share());
153 }
154 /// Create a new \c temporary_buffer object referring to a substring of the
155 /// same underlying data. The underlying \ref deleter will not be destroyed
156 /// until both the original and the clone have been destroyed.
157 ///
158 /// \param pos Position of the first character to share.
159 /// \param len Length of substring to share.
160 /// \return a clone of the buffer object, referring to a substring.
161 temporary_buffer share(size_t pos, size_t len) {
162 auto ret = share();
163 ret._buffer += pos;
164 ret._size = len;
165 return ret;
166 }
167 /// Clone the current \c temporary_buffer object into a new one.
168 /// This creates a temporary buffer with the same length and data but not
169 /// pointing to the memory of the original object.
170 temporary_buffer clone() const {
171 return {_buffer, _size};
172 }
173 /// Remove a prefix from the buffer. The underlying data
174 /// is not modified.
175 ///
176 /// \param pos Position of first character to retain.
177 void trim_front(size_t pos) {
178 _buffer += pos;
179 _size -= pos;
180 }
181 /// Remove a suffix from the buffer. The underlying data
182 /// is not modified.
183 ///
184 /// \param pos Position of first character to drop.
185 void trim(size_t pos) {
186 _size = pos;
187 }
188 /// Stops automatic memory management. When the \c temporary_buffer
189 /// object is destroyed, the underlying \ref deleter will not be called.
190 /// Instead, it is the caller's responsibility to destroy the deleter object
191 /// when the data is no longer needed.
192 ///
193 /// \return \ref deleter object managing the data's lifetime.
194 deleter release() {
195 return std::move(_deleter);
196 }
197 /// Creates a \c temporary_buffer object with a specified size, with
198 /// memory aligned to a specific boundary.
199 ///
200 /// \param alignment Required alignment; must be a power of two.
201 /// \param size Required size.
202 /// \return a new \c temporary_buffer object.
203 static temporary_buffer aligned(size_t alignment, size_t size) {
204 void *ptr = nullptr;
205 auto ret = ::posix_memalign(&ptr, alignment, size * sizeof(CharType));
206 auto buf = static_cast<CharType*>(ptr);
207 if (ret) {
208 throw std::bad_alloc();
209 }
210 return temporary_buffer(buf, size, make_free_deleter(buf));
211 }
212
213 /// Compare contents of this buffer with another buffer for equality
214 ///
215 /// \param o buffer to compare with
216 /// \return true if and only if contents are the same
217 bool operator==(const temporary_buffer<char>& o) const {
218 return size() == o.size() && std::equal(begin(), end(), o.begin());
219 }
220
221 /// Compare contents of this buffer with another buffer for inequality
222 ///
223 /// \param o buffer to compare with
224 /// \return true if and only if contents are not the same
225 bool operator!=(const temporary_buffer<char>& o) const {
226 return !(*this == o);
227 }
228 };
229
230 /// @}
231
232 }