The Gaudi Framework  master (1304469f)
Loading...
Searching...
No Matches
Arena.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 2019-20 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12#include <cstddef>
13#include <functional>
14#include <type_traits>
28 template <typename Resource, typename T, typename DefaultResource = void>
29 struct Arena {
30 using value_type = T;
31 using propagate_on_container_swap = std::true_type;
34
37 constexpr Arena( Resource* resource ) noexcept : m_resource{ resource } {}
38
43 template <typename D = void>
44 requires( std::is_invocable_r_v<Resource*, DefaultResource> )
45 Arena() : Arena( std::invoke( DefaultResource{} ) ) {}
46
49 template <typename U>
50 constexpr Arena( Arena<Resource, U, DefaultResource> const& other ) noexcept : m_resource{ other.m_resource } {}
51
54 [[nodiscard]] T* allocate( std::size_t n ) {
55 return reinterpret_cast<T*>( m_resource->template allocate<alignof( T )>( n * sizeof( T ) ) );
56 }
57
60 void deallocate( T* p, std::size_t n ) noexcept {
61 m_resource->deallocate( reinterpret_cast<std::byte*>( p ), n * sizeof( T ) );
62 }
63
66 [[nodiscard]] Resource* resource() const noexcept { return m_resource; }
67
68 template <typename U>
69 friend constexpr bool operator==( Arena const& lhs, Arena<Resource, U, DefaultResource> const& rhs ) {
70 return lhs.m_resource == rhs.m_resource;
71 }
72
73 template <typename U>
77
78 private:
79 // Required for the Arena<Resource, U, DefaultResource> converting copy constructor
80 template <typename, typename, typename>
81 friend struct Arena;
82
83 Resource* m_resource{ nullptr };
84 };
85
86 template <typename Resource, typename T, typename U, typename DefaultResource>
87 inline constexpr bool operator!=( Arena<Resource, T, DefaultResource> const& lhs,
89 return !( lhs == rhs );
90 }
91} // namespace Gaudi::Allocator
constexpr bool operator!=(Arena< Resource, T, DefaultResource > const &lhs, Arena< Resource, U, DefaultResource > const &rhs)
Definition Arena.h:87
Arena< Resource, U, DefaultResource > other
Definition Arena.h:75
Custom allocator holding a pointer to a generic memory resource.
Definition Arena.h:29
friend constexpr bool operator==(Arena const &lhs, Arena< Resource, U, DefaultResource > const &rhs)
Definition Arena.h:69
friend struct Arena
Definition Arena.h:81
std::true_type propagate_on_container_swap
Definition Arena.h:31
constexpr Arena(Resource *resource) noexcept
Construct an allocator using the given memory resource, which must be valid.
Definition Arena.h:37
constexpr Arena(Arena< Resource, U, DefaultResource > const &other) noexcept
Converting copy constructor, rebinding U -> T.
Definition Arena.h:50
Arena()
Construct an allocator using the resource provided by DefaultResource.
Definition Arena.h:45
T * allocate(std::size_t n)
Allocate storage for n objects.
Definition Arena.h:54
std::true_type propagate_on_container_copy_assignment
Definition Arena.h:32
void deallocate(T *p, std::size_t n) noexcept
Deallocate storage for n objects.
Definition Arena.h:60
std::true_type propagate_on_container_move_assignment
Definition Arena.h:33