The Gaudi Framework  master (37c0b60a)
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>
15 namespace Gaudi::Allocator {
28  template <typename Resource, typename T, typename DefaultResource = void>
29  struct Arena {
30  using value_type = T;
34 
37  constexpr Arena( Resource* resource ) noexcept : m_resource{ resource } {}
38 
43  template <typename D = void, typename = std::enable_if_t<std::is_invocable_r_v<Resource*, DefaultResource>, D>>
44  Arena() : Arena( std::invoke( DefaultResource{} ) ) {}
45 
48  template <typename U>
49  constexpr Arena( Arena<Resource, U, DefaultResource> const& other ) noexcept : m_resource{ other.m_resource } {}
50 
53  [[nodiscard]] T* allocate( std::size_t n ) {
54  return reinterpret_cast<T*>( m_resource->template allocate<alignof( T )>( n * sizeof( T ) ) );
55  }
56 
59  void deallocate( T* p, std::size_t n ) noexcept {
60  m_resource->deallocate( reinterpret_cast<std::byte*>( p ), n * sizeof( T ) );
61  }
62 
65  [[nodiscard]] Resource* resource() const noexcept { return m_resource; }
66 
67  template <typename U>
68  friend constexpr bool operator==( Arena const& lhs, Arena<Resource, U, DefaultResource> const& rhs ) {
69  return lhs.m_resource == rhs.m_resource;
70  }
71 
72  template <typename U>
73  struct rebind {
75  };
76 
77  private:
78  // Required for the Arena<Resource, U, DefaultResource> converting copy constructor
79  template <typename, typename, typename>
80  friend struct Arena;
81 
82  Resource* m_resource{ nullptr };
83  };
84 
85  template <typename Resource, typename T, typename U, typename DefaultResource>
86  inline constexpr bool operator!=( Arena<Resource, T, DefaultResource> const& lhs,
88  return !( lhs == rhs );
89  }
90 } // namespace Gaudi::Allocator
std::true_type
Gaudi::Allocator::operator!=
constexpr bool operator!=(Arena< Resource, T, DefaultResource > const &lhs, Arena< Resource, U, DefaultResource > const &rhs)
Definition: Arena.h:86
Gaudi::Allocator::Arena::deallocate
void deallocate(T *p, std::size_t n) noexcept
Deallocate storage for n objects.
Definition: Arena.h:59
Gaudi::Allocator::Arena::Arena
constexpr Arena(Resource *resource) noexcept
Construct an allocator using the given memory resource, which must be valid.
Definition: Arena.h:37
Gaudi::Allocator::Arena::rebind
Definition: Arena.h:73
Gaudi::Allocator::Arena::Arena
Arena()
Construct an allocator using the resource provided by DefaultResource.
Definition: Arena.h:44
Gaudi::Allocator::Arena::Arena
constexpr Arena(Arena< Resource, U, DefaultResource > const &other) noexcept
Converting copy constructor, rebinding U -> T.
Definition: Arena.h:49
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Gaudi::Allocator::Arena::resource
Resource * resource() const noexcept
Return a pointer to the memory resource.
Definition: Arena.h:65
Gaudi::Allocator::Arena::m_resource
Resource * m_resource
Definition: Arena.h:82
Gaudi::Allocator::Arena
Custom allocator holding a pointer to a generic memory resource.
Definition: Arena.h:29
std
STL namespace.
Gaudi::Allocator::Arena::operator==
constexpr friend bool operator==(Arena const &lhs, Arena< Resource, U, DefaultResource > const &rhs)
Definition: Arena.h:68
Gaudi::Allocator::Arena::allocate
T * allocate(std::size_t n)
Allocate storage for n objects.
Definition: Arena.h:53
std::size_t
Gaudi::Allocator::Arena::value_type
T value_type
Definition: Arena.h:30
Gaudi::Allocator
Definition: Arena.h:15