Line data Source code
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 : 25 : namespace 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 : */ 41 : template<typename T, typename Allocator> 42 : class AllocatorAdapter 43 : { 44 : public: 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). 49 : using propagate_on_container_copy_assignment = std::true_type; 50 : using propagate_on_container_move_assignment = std::true_type; 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 : { 57 : using other = AllocatorAdapter<OtherT, Allocator>; 58 : }; 59 : 60 4 : constexpr AllocatorAdapter(Allocator& allocator) noexcept : m_Allocator(std::addressof(allocator)) {} 61 : template<typename OtherT> 62 1 : constexpr AllocatorAdapter(const AllocatorAdapter<OtherT, Allocator>& allocatorAdapter) noexcept : m_Allocator(allocatorAdapter.m_Allocator) {} 63 : 64 4 : [[nodiscard]] constexpr T* allocate(std::size_t n) 65 : { 66 4 : return static_cast<T*>(m_Allocator->allocate(n * sizeof(T), alignof(T))); 67 : } 68 : 69 4 : constexpr void deallocate(T* ptr, std::size_t n) 70 : { 71 4 : m_Allocator->deallocate(static_cast<void*>(ptr), n * sizeof(T)); 72 : } 73 : 74 : template<typename OtherT> 75 2 : constexpr bool operator==(const AllocatorAdapter<OtherT, Allocator>& other) const noexcept 76 : { 77 0 : return m_Allocator == other.m_Allocator; 78 : } 79 : 80 : private: 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