CookMem 1.6.0
A C++ memory context / allocator
Loading...
Searching...
No Matches
memresource.h
1/*
2 * Copyright (c) 2026 Heng Yuan
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef COOK_MEM_MEMRESOURCE_H
17#define COOK_MEM_MEMRESOURCE_H
18
19// std::pmr::memory_resource needs C++17 and a standard library that has
20// <memory_resource>. COOKMEM_HAS_PMR is 1 when cookmem::MemResource is
21// available. It can be defined to 0 to leave it out.
22#ifndef COOKMEM_HAS_PMR
23# if defined(_MSVC_LANG)
24# define COOKMEM_CPLUSPLUS _MSVC_LANG
25# else
26# define COOKMEM_CPLUSPLUS __cplusplus
27# endif
28# if COOKMEM_CPLUSPLUS >= 201703L && defined(__has_include)
29# if __has_include(<memory_resource>)
30# include <memory_resource>
31# if defined(__cpp_lib_memory_resource) && __cpp_lib_memory_resource >= 201603L
32# define COOKMEM_HAS_PMR 1
33# endif
34# endif
35# endif
36# undef COOKMEM_CPLUSPLUS
37# ifndef COOKMEM_HAS_PMR
38# define COOKMEM_HAS_PMR 0
39# endif
40#endif
41
42#if COOKMEM_HAS_PMR
43
44#include <cstddef>
45#include <memory_resource>
46#include <new>
47
48#include "context.h"
49
50namespace cookmem
51{
52
66template<class Context>
67class MemResource : public std::pmr::memory_resource
68{
69public:
78 explicit MemResource (Context& context) noexcept
79 : m_context (&context)
80 {
81 }
82
88 Context&
89 getContext () const noexcept
90 {
91 return *m_context;
92 }
93
94protected:
105 void*
106 do_allocate (std::size_t bytes, std::size_t alignment) override
107 {
108 void* ptr = m_context->allocateAligned (bytes, alignment);
109 if (ptr == nullptr)
110 {
111 throw std::bad_alloc ();
112 }
113 return ptr;
114 }
115
126 void
127 do_deallocate (void* ptr, std::size_t bytes, std::size_t alignment) override
128 {
129 (void) alignment;
130 m_context->deallocate (ptr, bytes);
131 }
132
142 bool
143 do_is_equal (const std::pmr::memory_resource& other) const noexcept override
144 {
145 if (this == &other)
146 {
147 return true;
148 }
149#if defined(__cpp_rtti) || defined(__GXX_RTTI) || defined(_CPPRTTI)
150 const MemResource* otherResource = dynamic_cast<const MemResource*> (&other);
151 return otherResource != nullptr && otherResource->m_context == m_context;
152#else
153 return false;
154#endif
155 }
156
157private:
158 Context* m_context;
159};
160
161} // namespace cookmem
162
163#endif // COOKMEM_HAS_PMR
164
165#endif // COOK_MEM_MEMRESOURCE_H
void * do_allocate(std::size_t bytes, std::size_t alignment) override
Definition memresource.h:106
void do_deallocate(void *ptr, std::size_t bytes, std::size_t alignment) override
Definition memresource.h:127
bool do_is_equal(const std::pmr::memory_resource &other) const noexcept override
Definition memresource.h:143
MemResource(Context &context) noexcept
Definition memresource.h:78
Context & getContext() const noexcept
Definition memresource.h:89
Definition allocator.h:29