OGLplus  (0.59.0) a C++ wrapper for rendering APIs

buffer_pool.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_MEMORY_BUFFER_POOL_HPP
10 #define EAGINE_MEMORY_BUFFER_POOL_HPP
11 
12 #include "buffer.hpp"
13 #include <algorithm>
14 #include <vector>
15 
16 namespace eagine::memory {
17 //------------------------------------------------------------------------------
21 class buffer_pool {
22 public:
26  auto get(span_size_t req_size = 0) -> memory::buffer {
27  auto pos = std::lower_bound(
28  _pool.begin(), _pool.end(), req_size, [](auto& buf, auto req) {
29  return buf.capacity() < req;
30  });
31  if(pos != _pool.end()) {
32  memory::buffer result{std::move(*pos)};
33  _pool.erase(pos);
34  result.resize(req_size);
35  return result;
36  }
37  memory::buffer result{};
38  result.resize(req_size);
39  return result;
40  }
41 
44  void eat(memory::buffer used) {
45  auto pos = std::lower_bound(
46  _pool.begin(),
47  _pool.end(),
48  used.capacity(),
49  [](auto& buf, auto capacity) { return buf.capacity() < capacity; });
50  _pool.emplace(pos, std::move(used));
51  }
52 
53 private:
54  std::vector<memory::buffer> _pool;
55 };
56 //------------------------------------------------------------------------------
57 } // namespace eagine::memory
58 
59 #endif // EAGINE_MEMORY_BUFFER_POOL_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
Class storing multiple reusable memory buffer instances.
Definition: buffer_pool.hpp:21
auto get(span_size_t req_size=0) -> memory::buffer
Gets a buffer with the specified required size.
Definition: buffer_pool.hpp:26
Reallocatable owning byte buffer.
Definition: buffer.hpp:22
void eat(memory::buffer used)
Returns the specified buffer back to the pool for further reuse.
Definition: buffer_pool.hpp:44
auto capacity() const noexcept -> span_size_t
Returns the capacity of this buffer.
Definition: buffer.hpp:95
auto resize(span_size_t new_size) -> auto &
Resizes the buffer to the specified number of bytes.
Definition: buffer.hpp:114

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).