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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright (C) 2014 Cloudius Systems, Ltd.
24 #include <seastar/core/deleter.hh>
25 #include <seastar/util/eclipse.hh>
31 /// \addtogroup memory-module
34 /// Temporary, self-managed byte buffer.
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.
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.
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.
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.
51 /// Use cases for a \c temporary_buffer include:
52 /// - passing a substring of a tcp packet for the user to consume (zero-copy
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
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");
66 /// Creates a \c temporary_buffer of a specified size. The buffer is not shared
67 /// with anyone, and is not initialized.
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();
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.
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)) {
88 /// Creates a \c temporary_buffer with a specific deleter.
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
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);
103 void operator=(const temporary_buffer&) = delete;
104 /// Moves a \c temporary_buffer.
105 temporary_buffer& operator=(temporary_buffer&& x) noexcept {
109 _deleter = std::move(x._deleter);
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.
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);
136 /// Reads a character from a specific position in the buffer.
138 /// \param pos position to read character from; must be less than size.
139 CharType operator[](size_t pos) const {
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.
150 /// \return a clone of the buffer object.
151 temporary_buffer share() {
152 return temporary_buffer(_buffer, _size, _deleter.share());
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.
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) {
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};
173 /// Remove a prefix from the buffer. The underlying data
176 /// \param pos Position of first character to retain.
177 void trim_front(size_t pos) {
181 /// Remove a suffix from the buffer. The underlying data
184 /// \param pos Position of first character to drop.
185 void trim(size_t pos) {
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.
193 /// \return \ref deleter object managing the data's lifetime.
195 return std::move(_deleter);
197 /// Creates a \c temporary_buffer object with a specified size, with
198 /// memory aligned to a specific boundary.
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) {
205 auto ret = ::posix_memalign(&ptr, alignment, size * sizeof(CharType));
206 auto buf = static_cast<CharType*>(ptr);
208 throw std::bad_alloc();
210 return temporary_buffer(buf, size, make_free_deleter(buf));
213 /// Compare contents of this buffer with another buffer for equality
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());
221 /// Compare contents of this buffer with another buffer for inequality
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);