Zserio C++ runtime library  1.3.0
Built for Zserio 2.18.0
MemoryResource.h
Go to the documentation of this file.
1 #ifndef ZSERIO_PMR_I_MEMORY_RESOURCE_H_INC
2 #define ZSERIO_PMR_I_MEMORY_RESOURCE_H_INC
3 
4 #ifdef ZSERIO_MEMORY_RESOURCE_TRACING
5  #include <iostream>
6 #endif
7 
8 #include "zserio/Types.h"
9 
10 namespace zserio
11 {
12 namespace pmr
13 {
14 
19 {
20 public:
21  // this empty constructor is necessary for gcc 9.3.0 bug which causes test coverage failure
25  MemoryResource() = default;
26 
30  virtual ~MemoryResource() = default;
31 
36  MemoryResource(const MemoryResource& other) = delete;
37  MemoryResource(MemoryResource&& other) = delete;
38 
39  MemoryResource& operator=(const MemoryResource& other) = delete;
51  void* allocate(size_t bytes, size_t alignment = alignof(max_align_t))
52  {
53 #ifdef ZSERIO_MEMORY_RESOURCE_TRACING
54  void* const ptr = doAllocate(bytes, alignment);
55  std::cout << "MemoryResource::allocate bytes=" << bytes << " alignment=" << alignment << " -> "
56  << std::hex << ptr << std::dec << std::endl;
57  return ptr;
58 #else
59  return doAllocate(bytes, alignment);
60 #endif
61  }
62 
72  void deallocate(void* storage, size_t bytes, size_t alignment = alignof(max_align_t))
73  {
74 #ifdef ZSERIO_MEMORY_RESOURCE_TRACING
75  std::cout << "MemoryResource::deallocate p=" << std::hex << storage << std::dec << " bytes=" << bytes
76  << " alignment=" << alignment << std::endl;
77 #endif
78  doDeallocate(storage, bytes, alignment);
79  }
80 
91  bool isEqual(const MemoryResource& other) const noexcept
92  {
93 #ifdef ZSERIO_MEMORY_RESOURCE_TRACING
94  std::cout << "MemoryResource::isEqual other=" << std::hex << &other << std::dec << std::endl;
95 #endif
96  return doIsEqual(other);
97  }
98 
99 private:
108  virtual void* doAllocate(size_t bytes, size_t alignment) = 0;
109 
119  virtual void doDeallocate(void* storage, size_t bytes, size_t alignment) = 0;
120 
131  virtual bool doIsEqual(const MemoryResource& other) const noexcept = 0;
132 };
133 
142 inline bool operator==(const MemoryResource& lhs, const MemoryResource& rhs)
143 {
144  return &lhs == &rhs || lhs.isEqual(rhs);
145 }
146 
155 inline bool operator!=(const MemoryResource& lhs, const MemoryResource& rhs)
156 {
157  return !(lhs == rhs);
158 }
159 
169 MemoryResource* getDefaultResource() noexcept;
170 
183 MemoryResource* setDefaultResource(MemoryResource* resource) noexcept;
184 
185 } // namespace pmr
186 } // namespace zserio
187 
188 #endif // ZSERIO_PMR_I_MEMORY_RESOURCE_H_INC
MemoryResource(const MemoryResource &other)=delete
MemoryResource(MemoryResource &&other)=delete
void * allocate(size_t bytes, size_t alignment=alignof(max_align_t))
bool isEqual(const MemoryResource &other) const noexcept
MemoryResource & operator=(const MemoryResource &other)=delete
virtual ~MemoryResource()=default
MemoryResource & operator=(MemoryResource &&other)=delete
void deallocate(void *storage, size_t bytes, size_t alignment=alignof(max_align_t))
bool operator==(const MemoryResource &lhs, const MemoryResource &rhs)
MemoryResource * setDefaultResource(MemoryResource *resource) noexcept
MemoryResource * getDefaultResource() noexcept
bool operator!=(const MemoryResource &lhs, const MemoryResource &rhs)