From c853ffb5b2f75f5a889ed2e3ef89b818a736e87a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:50:49 +0200 Subject: Adding upstream version 1.3+ds. Signed-off-by: Daniel Baumann --- src/util/pool.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/util/pool.cpp (limited to 'src/util/pool.cpp') diff --git a/src/util/pool.cpp b/src/util/pool.cpp new file mode 100644 index 0000000..c49b8ea --- /dev/null +++ b/src/util/pool.cpp @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include "pool.h" +#include +#include + +namespace Inkscape { +namespace Util { + +// Round up x to the next multiple of m. +static std::byte *roundup(std::byte *x, std::size_t m) +{ + auto y = reinterpret_cast(x); + y = ((y - 1) / m + 1) * m; + return reinterpret_cast(y); +} + +std::byte *Pool::allocate(std::size_t size, std::size_t alignment) +{ + auto a = roundup(cur, alignment); + auto b = a + size; + + if (b <= end) { + cur = b; + return a; + } + + cursize = std::max(nextsize, size + alignment - 1); + buffers.emplace_back(std::make_unique(cursize)); + // buffers.emplace_back(std::make_unique_for_overwrite(cursize)); // Todo: C++20. + resetblock(); + nextsize = cursize * 3 / 2; + + a = roundup(cur, alignment); + b = a + size; + + assert(b <= end); + cur = b; + return a; +}; + +void Pool::free_all() noexcept +{ + if (buffers.empty()) return; + if (buffers.size() > 1) { + buffers.front() = std::move(buffers.back()); + buffers.resize(1); + } + resetblock(); +} + +void Pool::movefrom(Pool &other) noexcept +{ + buffers = std::move(other.buffers); + cur = other.cur; + end = other.end; + cursize = other.cursize; + nextsize = other.nextsize; + + other.buffers.clear(); + other.cur = nullptr; + other.end = nullptr; + other.cursize = 0; + other.nextsize = 2; +} + +void Pool::resetblock() noexcept +{ + assert(!buffers.empty()); + cur = buffers.back().get(); + end = cur + cursize; +} + +} // namespace Util +} // namespace Inkscape -- cgit v1.2.3