Skip to content

Commit

Permalink
net: extend input_buffer_factory with passing back of unused buffers.
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslaw Zarzynski <[email protected]>
  • Loading branch information
rzarzynski committed Jun 7, 2019
1 parent 2073fcb commit af064d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 6 additions & 1 deletion include/seastar/net/api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,18 @@ public:

class input_buffer_factory {
public:
using buffer_t = temporary_buffer<char>;

virtual ~input_buffer_factory() = default;
/// Provide a rx buffer. Implementation is responsible for determining its size
/// and memory. This is useful when a network stack implementation does not put
/// extra requirements on these factors. The POSIX stack is the example here.
/// \param allocator Memory allocator \c connected_socket implementation prefers.
/// Maybe nullptr.
virtual temporary_buffer<char> create(compat::polymorphic_allocator<char>* allocator) = 0;
virtual buffer_t create(compat::polymorphic_allocator<char>* allocator) = 0;

// Give back to the factory unused part of a buffer obtained from it
virtual void return_unused(buffer_t&& buf) = 0;
};

} /* namespace net */
Expand Down
12 changes: 8 additions & 4 deletions src/net/posix-stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ class posix_connected_socket_impl final : public connected_socket_impl, posix_co
virtual data_source source(net::input_buffer_factory* ibf) override {
if (!ibf) {
static struct : input_buffer_factory {
temporary_buffer<char> create(compat::polymorphic_allocator<char>* const allocator) override {
buffer_t create(compat::polymorphic_allocator<char>* const allocator) override {
return make_temporary_buffer<char>(allocator, 8192);
}
void return_unused(buffer_t&&) override {
}
} default_posix_inbuf_factory{};
ibf = &default_posix_inbuf_factory;
}
Expand Down Expand Up @@ -327,9 +329,11 @@ future<temporary_buffer<char>>
posix_data_source_impl::get() {
_buf = _buffer_factory->create(_buffer_allocator);
return _fd->read_some(_buf.get_write(), _buf.size()).then([this] (size_t size) {
_buf.trim(size);
auto ret = std::move(_buf);
return make_ready_future<temporary_buffer<char>>(std::move(ret));
if (size < _buf.size()) {
_buffer_factory->return_unused(_buf.share(size, _buf.size() - size));
_buf.trim(size);
}
return make_ready_future<temporary_buffer<char>>(std::move(_buf));
});
}

Expand Down

0 comments on commit af064d8

Please sign in to comment.