memory_resource.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2024 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_DETAIL_MEMORY_RESOURCE_HPP_INCLUDED
5 #define LEXY_DETAIL_MEMORY_RESOURCE_HPP_INCLUDED
6 
7 #include <cstring>
10 #include <new>
11 
12 #if 0
13 // Subset of the interface of std::pmr::memory_resource.
14 class MemoryResource
15 {
16 public:
17  void* allocate(std::size_t bytes, std::size_t alignment);
18  void deallocate(void* ptr, std::size_t bytes, std::size_t alignment);
19 
20  friend bool operator==(const MemoryResource& lhs, const MemoryResource& rhs);
21 };
22 #endif
23 
24 namespace lexy::_detail
25 {
27 {
28 public:
29  static void* allocate(std::size_t bytes, std::size_t alignment)
30  {
31  if (alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
32  return ::operator new(bytes, std::align_val_t{alignment});
33  else
34  return ::operator new(bytes);
35  }
36 
37  static void deallocate(void* ptr, std::size_t bytes, std::size_t alignment) noexcept
38  {
39 #if LEXY_ENABLE_ASSERT
40  // In debug mode, we fill freed memory with 0xFF to detect dangling lexemes.
41  // For default, ASCII, bytes, this is just a noticable value.
42  // For UTF-8, this is the EOF integer value as its an invalid code unit.
43  // For UTF-16, this is the code point 0xFFFF, which is the replacement character.
44  // For UTF-32, this is an out of range code point.
45  std::memset(ptr, 0xFF, bytes);
46 #endif
47 
48 #ifdef __cpp_sized_deallocation
49  if (alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
50  ::operator delete(ptr, bytes, std::align_val_t{alignment});
51  else
52  ::operator delete(ptr, bytes);
53 #else
54  (void)bytes;
55 
56  if (alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
57  ::operator delete(ptr, std::align_val_t{alignment});
58  else
59  ::operator delete(ptr);
60 #endif
61  }
62 
64  {
65  return true;
66  }
67 };
68 } // namespace lexy::_detail
69 
70 namespace lexy::_detail
71 {
72 template <typename MemoryResource>
74 {
75 public:
76  constexpr explicit _memory_resource_ptr_empty(MemoryResource*) noexcept {}
77  constexpr explicit _memory_resource_ptr_empty(void*) noexcept {}
78 
79  constexpr auto operator*() const noexcept
80  {
81  return MemoryResource{};
82  }
83 
84  constexpr auto operator->() const noexcept
85  {
86  struct proxy
87  {
88  MemoryResource _resource;
89 
90  constexpr MemoryResource* operator->() noexcept
91  {
92  return &_resource;
93  }
94  };
95 
96  return proxy{};
97  }
98 
99  constexpr MemoryResource* get() const noexcept
100  {
101  return nullptr;
102  }
103 };
104 
105 template <typename MemoryResource>
107 {
108 public:
109  constexpr explicit _memory_resource_ptr(MemoryResource* resource) noexcept : _resource(resource)
110  {
111  LEXY_PRECONDITION(resource);
112  }
113 
114  constexpr MemoryResource& operator*() const noexcept
115  {
116  return *_resource;
117  }
118 
119  constexpr MemoryResource* operator->() const noexcept
120  {
121  return _resource;
122  }
123 
124  constexpr MemoryResource* get() const noexcept
125  {
126  return _resource;
127  }
128 
129 private:
130  MemoryResource* _resource;
131 };
132 
133 // clang-format off
134 template <typename MemoryResource>
136  = std::conditional_t<std::is_void_v<MemoryResource>,
138  std::conditional_t<std::is_empty_v<MemoryResource>,
141 // clang-format on
142 
143 template <typename MemoryResource, typename = std::enable_if_t<std::is_void_v<MemoryResource>
144  || std::is_empty_v<MemoryResource>>>
145 constexpr MemoryResource* get_memory_resource()
146 {
147  return nullptr;
148 }
149 } // namespace lexy::_detail
150 
151 #endif // LEXY_DETAIL_MEMORY_RESOURCE_HPP_INCLUDED
152 
lexyd::bytes
constexpr auto bytes
Matches N arbitrary bytes.
Definition: byte.hpp:137
lexy::_detail::default_memory_resource
Definition: memory_resource.hpp:26
zmq::proxy
void proxy(void *frontend, void *backend, void *capture)
Definition: 3rdparty/cppzmq/zmq.hpp:2267
lexy::_detail::_memory_resource_ptr::_memory_resource_ptr
constexpr _memory_resource_ptr(MemoryResource *resource) noexcept
Definition: memory_resource.hpp:109
lexy::_detail::memory_resource_ptr
std::conditional_t< std::is_void_v< MemoryResource >, _memory_resource_ptr_empty< default_memory_resource >, std::conditional_t< std::is_empty_v< MemoryResource >, _memory_resource_ptr_empty< MemoryResource >, _memory_resource_ptr< MemoryResource > >> memory_resource_ptr
Definition: memory_resource.hpp:140
config.hpp
lexy::_detail::_memory_resource_ptr_empty::operator->
constexpr auto operator->() const noexcept
Definition: memory_resource.hpp:84
lexy::_detail::get_memory_resource
constexpr MemoryResource * get_memory_resource()
Definition: memory_resource.hpp:145
lexy::_detail::_memory_resource_ptr_empty::operator*
constexpr auto operator*() const noexcept
Definition: memory_resource.hpp:79
lexy::_detail::default_memory_resource::deallocate
static void deallocate(void *ptr, std::size_t bytes, std::size_t alignment) noexcept
Definition: memory_resource.hpp:37
zmq::operator==
bool operator==(const detail::socket_base &a, const detail::socket_base &b) ZMQ_NOTHROW
Definition: 3rdparty/cppzmq/zmq.hpp:2139
LEXY_PRECONDITION
#define LEXY_PRECONDITION(Expr)
Definition: assert.hpp:36
detail::void
j template void())
Definition: json.hpp:4893
lexy::_detail::default_memory_resource::allocate
static void * allocate(std::size_t bytes, std::size_t alignment)
Definition: memory_resource.hpp:29
lexy::_detail::_memory_resource_ptr::_resource
MemoryResource * _resource
Definition: memory_resource.hpp:130
lexy::_detail::_memory_resource_ptr::get
constexpr MemoryResource * get() const noexcept
Definition: memory_resource.hpp:124
assert.hpp
lexy::_detail::_memory_resource_ptr_empty
Definition: memory_resource.hpp:73
lexy::_detail::_memory_resource_ptr::operator->
constexpr MemoryResource * operator->() const noexcept
Definition: memory_resource.hpp:119
lexy::_detail::default_memory_resource::operator==
constexpr friend bool operator==(default_memory_resource, default_memory_resource) noexcept
Definition: memory_resource.hpp:63
lexy::_detail::_memory_resource_ptr::operator*
constexpr MemoryResource & operator*() const noexcept
Definition: memory_resource.hpp:114
lexy::_detail
Definition: any_ref.hpp:12
lexy::_detail::_memory_resource_ptr_empty::get
constexpr MemoryResource * get() const noexcept
Definition: memory_resource.hpp:99
lexy::_detail::_memory_resource_ptr_empty::_memory_resource_ptr_empty
constexpr _memory_resource_ptr_empty(MemoryResource *) noexcept
Definition: memory_resource.hpp:76
lexy::_detail::_memory_resource_ptr
Definition: memory_resource.hpp:106
lexy::_detail::_memory_resource_ptr_empty::_memory_resource_ptr_empty
constexpr _memory_resource_ptr_empty(void *) noexcept
Definition: memory_resource.hpp:77


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17