Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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#ifndef SEASTAR_MODULE
25#include <algorithm>
26#include <cstddef>
27#include <string_view>
28#include <malloc.h>
29#endif
30#include <seastar/core/deleter.hh>
31#include <seastar/util/eclipse.hh>
32#include <seastar/util/std-compat.hh>
33#include <seastar/util/modules.hh>
34
35namespace seastar {
36
39
65SEASTAR_MODULE_EXPORT
66template <typename CharType>
68 static_assert(sizeof(CharType) == 1, "must buffer stream of bytes");
69 CharType* _buffer;
70 size_t _size;
71 deleter _deleter;
72public:
77 explicit temporary_buffer(size_t size)
78 : _buffer(static_cast<CharType*>(malloc(size * sizeof(CharType)))), _size(size)
79 , _deleter(make_free_deleter(_buffer)) {
80 if (size && !_buffer) {
81 throw std::bad_alloc();
82 }
83 }
84 //explicit temporary_buffer(CharType* borrow, size_t size) : _buffer(borrow), _size(size) {}
87 : _buffer(nullptr)
88 , _size(0) {}
89 temporary_buffer(const temporary_buffer&) = delete;
90
92 temporary_buffer(temporary_buffer&& x) noexcept : _buffer(x._buffer), _size(x._size), _deleter(std::move(x._deleter)) {
93 x._buffer = nullptr;
94 x._size = 0;
95 }
96
103 temporary_buffer(CharType* buf, size_t size, deleter d) noexcept
104 : _buffer(buf), _size(size), _deleter(std::move(d)) {}
109 temporary_buffer(const CharType* src, size_t size) : temporary_buffer(size) {
110 std::copy_n(src, size, _buffer);
111 }
112 void operator=(const temporary_buffer&) = delete;
115 if (this != &x) {
116 _buffer = x._buffer;
117 _size = x._size;
118 _deleter = std::move(x._deleter);
119 x._buffer = nullptr;
120 x._size = 0;
121 }
122 return *this;
123 }
125 const CharType* get() const noexcept { return _buffer; }
128 CharType* get_write() noexcept { return _buffer; }
130 size_t size() const noexcept { return _size; }
132 const CharType* begin() const noexcept { return _buffer; }
134 const CharType* end() const noexcept { return _buffer + _size; }
140 temporary_buffer prefix(size_t size) && noexcept {
141 auto ret = std::move(*this);
142 ret._size = size;
143 return ret;
144 }
148 CharType operator[](size_t pos) const noexcept {
149 return _buffer[pos];
150 }
152 bool empty() const noexcept { return !size(); }
154 explicit operator bool() const noexcept { return size(); }
161 return temporary_buffer(_buffer, _size, _deleter.share());
162 }
170 temporary_buffer share(size_t pos, size_t len) {
171 auto ret = share();
172 ret._buffer += pos;
173 ret._size = len;
174 return ret;
175 }
180 return {_buffer, _size};
181 }
186 void trim_front(size_t pos) noexcept {
187 _buffer += pos;
188 _size -= pos;
189 }
194 void trim(size_t pos) noexcept {
195 _size = pos;
196 }
203 deleter release() noexcept {
204 return std::move(_deleter);
205 }
212 static temporary_buffer aligned(size_t alignment, size_t size) {
213 void *ptr = nullptr;
214 auto ret = ::posix_memalign(&ptr, alignment, size * sizeof(CharType));
215 auto buf = static_cast<CharType*>(ptr);
216 if (ret) {
217 throw std::bad_alloc();
218 }
219 return temporary_buffer(buf, size, make_free_deleter(buf));
220 }
221
222 static temporary_buffer copy_of(std::string_view view) {
223 void* ptr = ::malloc(view.size());
224 if (!ptr) {
225 throw std::bad_alloc();
226 }
227 auto buf = static_cast<CharType*>(ptr);
228 memcpy(buf, view.data(), view.size());
229 return temporary_buffer(buf, view.size(), make_free_deleter(buf));
230 }
231
236 bool operator==(const temporary_buffer& o) const noexcept {
237 return size() == o.size() && std::equal(begin(), end(), o.begin());
238 }
239
244 bool operator!=(const temporary_buffer& o) const noexcept {
245 return !(*this == o);
246 }
247};
248
250
251}
Definition: deleter.hh:52
Definition: temporary_buffer.hh:67
temporary_buffer(CharType *buf, size_t size, deleter d) noexcept
Definition: temporary_buffer.hh:103
temporary_buffer clone() const
Definition: temporary_buffer.hh:179
temporary_buffer(temporary_buffer &&x) noexcept
Moves a temporary_buffer.
Definition: temporary_buffer.hh:92
void trim(size_t pos) noexcept
Definition: temporary_buffer.hh:194
deleter release() noexcept
Definition: temporary_buffer.hh:203
const CharType * begin() const noexcept
Gets a pointer to the beginning of the buffer.
Definition: temporary_buffer.hh:132
bool empty() const noexcept
Checks whether the buffer is empty.
Definition: temporary_buffer.hh:152
const CharType * end() const noexcept
Gets a pointer to the end of the buffer.
Definition: temporary_buffer.hh:134
bool operator==(const temporary_buffer &o) const noexcept
Definition: temporary_buffer.hh:236
temporary_buffer share(size_t pos, size_t len)
Definition: temporary_buffer.hh:170
temporary_buffer & operator=(temporary_buffer &&x) noexcept
Moves a temporary_buffer.
Definition: temporary_buffer.hh:114
temporary_buffer share()
Definition: temporary_buffer.hh:160
temporary_buffer() noexcept
Creates an empty temporary_buffer that does not point at anything.
Definition: temporary_buffer.hh:86
temporary_buffer(size_t size)
Definition: temporary_buffer.hh:77
void trim_front(size_t pos) noexcept
Definition: temporary_buffer.hh:186
size_t size() const noexcept
Gets the buffer size.
Definition: temporary_buffer.hh:130
const CharType * get() const noexcept
Gets a pointer to the beginning of the buffer.
Definition: temporary_buffer.hh:125
CharType * get_write() noexcept
Definition: temporary_buffer.hh:128
CharType operator[](size_t pos) const noexcept
Definition: temporary_buffer.hh:148
static temporary_buffer aligned(size_t alignment, size_t size)
Definition: temporary_buffer.hh:212
temporary_buffer(const CharType *src, size_t size)
Definition: temporary_buffer.hh:109
bool operator!=(const temporary_buffer &o) const noexcept
Definition: temporary_buffer.hh:244
temporary_buffer prefix(size_t size) &&noexcept
Definition: temporary_buffer.hh:140
deleter share()
Definition: deleter.hh:205
Seastar API namespace.
Definition: abort_on_ebadf.hh:26