Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (f31105fd)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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>
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>
74  struct rebind {
76  };
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
std::true_type
Gaudi::Allocator::operator!=
constexpr bool operator!=(Arena< Resource, T, DefaultResource > const &lhs, Arena< Resource, U, DefaultResource > const &rhs)
Definition: Arena.h:87
Gaudi::Allocator::Arena::deallocate
void deallocate(T *p, std::size_t n) noexcept
Deallocate storage for n objects.
Definition: Arena.h:60
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::requires
requires(std::is_invocable_r_v< Resource *, DefaultResource >) Arena()
Construct an allocator using the resource provided by DefaultResource.
Definition: Arena.h:44
Gaudi::Allocator::Arena::rebind
Definition: Arena.h:74
Gaudi::Allocator::Arena::Arena
friend struct Arena
Definition: Arena.h:81
Gaudi::Allocator::Arena::Arena
constexpr Arena(Arena< Resource, U, DefaultResource > const &other) noexcept
Converting copy constructor, rebinding U -> T.
Definition: Arena.h:50
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:66
Gaudi::Allocator::Arena::m_resource
Resource * m_resource
Definition: Arena.h:83
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:69
Gaudi::Allocator::Arena::allocate
T * allocate(std::size_t n)
Allocate storage for n objects.
Definition: Arena.h:54
std::size_t
Gaudi::Allocator::Arena::value_type
T value_type
Definition: Arena.h:30
Gaudi::Allocator
Definition: Arena.h:15