Warning, file /include/Gaudi/Allocator/Arena.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #pragma once
0012 #include <cstddef>
0013 #include <functional>
0014 #include <type_traits>
0015 namespace Gaudi::Allocator {
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 template <typename Resource, typename T, typename DefaultResource = void>
0029 struct Arena {
0030 using value_type = T;
0031 using propagate_on_container_swap = std::true_type;
0032 using propagate_on_container_copy_assignment = std::true_type;
0033 using propagate_on_container_move_assignment = std::true_type;
0034
0035
0036
0037 constexpr Arena( Resource* resource ) noexcept : m_resource{ resource } {}
0038
0039
0040
0041
0042
0043 template <typename D = void>
0044 requires( std::is_invocable_r_v<Resource*, DefaultResource> )
0045 Arena() : Arena( std::invoke( DefaultResource{} ) ) {}
0046
0047
0048
0049 template <typename U>
0050 constexpr Arena( Arena<Resource, U, DefaultResource> const& other ) noexcept : m_resource{ other.m_resource } {}
0051
0052
0053
0054 [[nodiscard]] T* allocate( std::size_t n ) {
0055 return reinterpret_cast<T*>( m_resource->template allocate<alignof( T )>( n * sizeof( T ) ) );
0056 }
0057
0058
0059
0060 void deallocate( T* p, std::size_t n ) noexcept {
0061 m_resource->deallocate( reinterpret_cast<std::byte*>( p ), n * sizeof( T ) );
0062 }
0063
0064
0065
0066 [[nodiscard]] Resource* resource() const noexcept { return m_resource; }
0067
0068 template <typename U>
0069 friend constexpr bool operator==( Arena const& lhs, Arena<Resource, U, DefaultResource> const& rhs ) {
0070 return lhs.m_resource == rhs.m_resource;
0071 }
0072
0073 template <typename U>
0074 struct rebind {
0075 using other = Arena<Resource, U, DefaultResource>;
0076 };
0077
0078 private:
0079
0080 template <typename, typename, typename>
0081 friend struct Arena;
0082
0083 Resource* m_resource{ nullptr };
0084 };
0085
0086 template <typename Resource, typename T, typename U, typename DefaultResource>
0087 inline constexpr bool operator!=( Arena<Resource, T, DefaultResource> const& lhs,
0088 Arena<Resource, U, DefaultResource> const& rhs ) {
0089 return !( lhs == rhs );
0090 }
0091 }