Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
pool.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
6#ifndef INKSCAPE_UTIL_POOL_H
7#define INKSCAPE_UTIL_POOL_H
8
9#include <algorithm>
10#include <cstddef>
11#include <memory>
12#include <utility>
13#include <vector>
14
15namespace Inkscape::Util {
16
26class Pool final
27{
28public:
29 Pool() = default;
30 Pool(Pool const &) = delete;
31 Pool &operator=(Pool const &) = delete;
32 Pool(Pool &&other) noexcept { movefrom(other); }
33 Pool &operator=(Pool &&other) noexcept { movefrom(other); return *this; }
34
36 void reserve(std::size_t size) { nextsize = std::max(nextsize, size); }
37
39 std::byte *allocate(std::size_t size, std::size_t alignment);
40
42 template<typename T>
43 T *allocate() { return reinterpret_cast<T*>(allocate(sizeof(T), alignof(T))); }
44
46 void free_all() noexcept;
47
48private:
49 std::vector<std::unique_ptr<std::byte[]>> buffers;
50 std::byte *cur = nullptr, *end = nullptr;
51 std::size_t cursize = 0, nextsize = 2;
52
53 void movefrom(Pool &other) noexcept;
54 void resetblock() noexcept;
55};
56
57} // namespace Inkscape::Util
58
59#endif // INKSCAPE_UTIL_POOL_H
60
61/*
62 Local Variables:
63 mode:c++
64 c-file-style:"stroustrup"
65 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
66 indent-tabs-mode:nil
67 fill-column:99
68 End:
69*/
70// vim:filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99:
A Pool is a block allocator with the following characteristics:
Definition pool.h:27
Pool(Pool const &)=delete
T * allocate()
Convenience function: allocate a block of size and aligment for T.
Definition pool.h:43
std::byte * cur
Definition pool.h:50
std::size_t nextsize
Definition pool.h:51
std::byte * end
Definition pool.h:50
Pool & operator=(Pool const &)=delete
std::vector< std::unique_ptr< std::byte[]> > buffers
Definition pool.h:49
std::size_t cursize
Definition pool.h:51
void movefrom(Pool &other) noexcept
Definition pool.cpp:54
void resetblock() noexcept
Definition pool.cpp:69
void free_all() noexcept
Free all previous allocations, retaining the largest existing buffer for re-use.
Definition pool.cpp:44
Pool(Pool &&other) noexcept
Definition pool.h:32
Pool & operator=(Pool &&other) noexcept
Definition pool.h:33
void reserve(std::size_t size)
Ensure that the next buffer requested has at least the specified size.
Definition pool.h:36
Miscellaneous supporting code.
Definition document.h:93
STL namespace.