Skip to content

Commit 2e5354d

Browse files
committed
net: extend input_buffer_factory with passing back of unused buffers.
Signed-off-by: Radoslaw Zarzynski <rzarzyns@redhat.com>
1 parent c7aef8c commit 2e5354d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

include/seastar/net/api.hh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,18 @@ public:
131131

132132
class input_buffer_factory {
133133
public:
134+
using buffer_t = temporary_buffer<char>;
135+
134136
virtual ~input_buffer_factory() = default;
135137
/// Provide a rx buffer. Implementation is responsible for determining its size
136138
/// and memory. This is useful when a network stack implementation does not put
137139
/// extra requirements on these factors. The POSIX stack is the example here.
138140
/// \param allocator Memory allocator \c connected_socket implementation prefers.
139141
/// Maybe nullptr.
140-
virtual temporary_buffer<char> create(compat::polymorphic_allocator<char>* allocator) = 0;
142+
virtual buffer_t create(compat::polymorphic_allocator<char>* allocator) = 0;
143+
144+
// Give back to the factory unused part of a buffer obtained from it
145+
virtual void return_unused(buffer_t&& buf) = 0;
141146
};
142147

143148
} /* namespace net */

src/net/posix-stack.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ class posix_connected_socket_impl final : public connected_socket_impl, posix_co
119119
virtual data_source source(net::input_buffer_factory* ibf) override {
120120
if (!ibf) {
121121
static struct final : input_buffer_factory {
122-
temporary_buffer<char> create(compat::polymorphic_allocator<char>* const allocator) override {
122+
buffer_t create(compat::polymorphic_allocator<char>* const allocator) override {
123123
return make_temporary_buffer<char>(allocator, 8192);
124124
}
125+
void return_unused(buffer_t&&) override {
126+
}
125127
} default_posix_inbuf_factory{};
126128
ibf = &default_posix_inbuf_factory;
127129
}
@@ -327,9 +329,11 @@ future<temporary_buffer<char>>
327329
posix_data_source_impl::get() {
328330
_buf = _buffer_factory->create(_buffer_allocator);
329331
return _fd->read_some(_buf.get_write(), _buf.size()).then([this] (size_t size) {
330-
_buf.trim(size);
331-
auto ret = std::move(_buf);
332-
return make_ready_future<temporary_buffer<char>>(std::move(ret));
332+
if (size < _buf.size()) {
333+
_buffer_factory->return_unused(_buf.share(size, _buf.size() - size));
334+
_buf.trim(size);
335+
}
336+
return make_ready_future<temporary_buffer<char>>(std::move(_buf));
333337
});
334338
}
335339

0 commit comments

Comments
 (0)