Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
AllocatorAdapter.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_PS_ALLOCATORADAPTER
19#define INCLUDED_PS_ALLOCATORADAPTER
20
21#include <cstddef>
22#include <memory>
23#include <type_traits>
24
25namespace PS::Memory
26{
27
28/**
29 * Provides a way to use untyped allocators like LinearAllocator for
30 * containers. This allocator adapter must not outlive the target
31 * allocator. A client must not mix adapters with different
32 * allocators.
33 *
34 * It provides a basic STL allocator interface. It allows to use a stateful
35 * allocator:
36 *
37 * std::vector<Type, PS::Memory::AllocatorAdapter<Type, PS::Memory::ScopedLinearAllocator>>
38 *
39 * @see ScopedLinearAllocator
40 */
41template<typename T, typename Allocator>
43{
44public:
45 using value_type = T;
46
47 // We don't need runtime checks and per-element copies for containers
48 // when we have a unique allocator (means no mixing different allocators).
51 using propagate_on_container_swap = std::true_type;
52 using is_always_equal = std::false_type;
53
54 template<typename OtherT>
55 struct rebind
56 {
58 };
59
60 constexpr AllocatorAdapter(Allocator& allocator) noexcept : m_Allocator(std::addressof(allocator)) {}
61 template<typename OtherT>
62 constexpr AllocatorAdapter(const AllocatorAdapter<OtherT, Allocator>& allocatorAdapter) noexcept : m_Allocator(allocatorAdapter.m_Allocator) {}
63
64 [[nodiscard]] constexpr T* allocate(std::size_t n)
65 {
66 return static_cast<T*>(m_Allocator->allocate(n * sizeof(T), alignof(T)));
67 }
68
69 constexpr void deallocate(T* ptr, std::size_t n)
70 {
71 m_Allocator->deallocate(static_cast<void*>(ptr), n * sizeof(T));
72 }
73
74 template<typename OtherT>
75 constexpr bool operator==(const AllocatorAdapter<OtherT, Allocator>& other) const noexcept
76 {
77 return m_Allocator == other.m_Allocator;
78 }
79
80private:
81 template<typename OtherT, typename OtherAllocator> friend class AllocatorAdapter;
82
83 // A pointer to make it moveable.
84 Allocator* m_Allocator{nullptr};
85};
86
87} // namespace PS::Memory
88
89#endif // INCLUDED_PS_ALLOCATORADAPTER
Provides a way to use untyped allocators like LinearAllocator for containers.
Definition: AllocatorAdapter.h:43
Allocator * m_Allocator
Definition: AllocatorAdapter.h:84
constexpr AllocatorAdapter(const AllocatorAdapter< OtherT, Allocator > &allocatorAdapter) noexcept
Definition: AllocatorAdapter.h:62
constexpr void deallocate(T *ptr, std::size_t n)
Definition: AllocatorAdapter.h:69
std::true_type propagate_on_container_copy_assignment
Definition: AllocatorAdapter.h:49
constexpr AllocatorAdapter(Allocator &allocator) noexcept
Definition: AllocatorAdapter.h:60
std::true_type propagate_on_container_swap
Definition: AllocatorAdapter.h:51
std::false_type is_always_equal
Definition: AllocatorAdapter.h:52
constexpr bool operator==(const AllocatorAdapter< OtherT, Allocator > &other) const noexcept
Definition: AllocatorAdapter.h:75
T value_type
Definition: AllocatorAdapter.h:45
std::true_type propagate_on_container_move_assignment
Definition: AllocatorAdapter.h:50
constexpr T * allocate(std::size_t n)
Definition: AllocatorAdapter.h:64
Definition: AllocatorAdapter.h:26
#define T(string_literal)
Definition: secure_crt.cpp:81
Definition: AllocatorAdapter.h:56