Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
q_pmr_emulation_p.h
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef Q_PMR_EMULATION_P_H
5#define Q_PMR_EMULATION_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qtconfigmacros.h>
19
20#if defined(__APPLE__)
21// minimum requirement: iOS 17.0, macOS 14.0, watchOS 10.0 and tvOS 17.0.
22# include <Availability.h>
23# if !_LIBCPP_AVAILABILITY_HAS_PMR
24# define QT_MM_PMR_EMULATION
25# endif
26#endif
27
28#if defined(Q_OS_VXWORKS)
29// std::pmr::deque<variant> doesn't seem to compile on VxWorks, so we use our emulation
30# define QT_MM_PMR_EMULATION
31#endif
32
33#if !__has_include(<memory_resource>)
34# define QT_MM_PMR_EMULATION
35#endif
36
37#ifdef QT_MM_PMR_EMULATION
38
39# include <QtCore/qassert.h>
40
41# include <cstddef>
42# include <new>
43# include <typeinfo>
44
45#else
46
47# include <memory_resource>
48
49#endif
50
51#include <map>
52#include <vector>
53
54QT_BEGIN_NAMESPACE
55
57
58#ifndef QT_MM_PMR_EMULATION
59
60using memory_resource = std::pmr::memory_resource;
61template <typename T = std::byte>
62using polymorphic_allocator = std::pmr::polymorphic_allocator<T>;
63
64inline auto get_default_resource() noexcept
65{
66 return std::pmr::get_default_resource();
67}
68
69inline auto new_delete_resource() noexcept
70{
71 return std::pmr::new_delete_resource();
72}
73
74#else
75
76class memory_resource;
79
81{
82public:
83 memory_resource() = default;
86 virtual ~memory_resource() noexcept = default;
87
88 [[nodiscard]]
90 {
92 }
93
95 {
97 }
98
99 bool is_equal(const memory_resource &other) const noexcept { return do_is_equal(other); }
100
101protected:
103 virtual void do_deallocate(void *p, size_t bytes, size_t alignment) = 0;
104 virtual bool do_is_equal(const memory_resource &other) const noexcept { return this == &other; }
105
106private:
107 template <class T>
109};
110
111inline bool operator==(const memory_resource &a, const memory_resource &b) noexcept
112{
113 return &a == &b || a.is_equal(b);
114}
115
116inline bool operator!=(const memory_resource &a, const memory_resource &b) noexcept
117{
118 return !(a == b);
119}
120
121template <class T>
123{
124public:
125 using value_type = T;
126
127 polymorphic_allocator() noexcept = default;
130
131 template <class U>
136
138
139 [[nodiscard]]
141 {
142 void *ptr = m_resource->allocate(n * sizeof(T), alignof(T));
143 return static_cast<T *>(ptr);
144 }
145
146 void deallocate(T *p, size_t n) { m_resource->deallocate(p, n * sizeof(T), alignof(T)); }
147
148 [[nodiscard]]
149 memory_resource *resource() const noexcept
150 {
151 return m_resource;
152 }
153
154 [[nodiscard]]
159
160private:
162};
163
164template <class T1, class T2>
165bool operator==(const polymorphic_allocator<T1> &a, const polymorphic_allocator<T2> &b) noexcept
166{
167 return *a.resource() == *b.resource();
168}
169
170template <class T1, class T2>
171bool operator!=(const polymorphic_allocator<T1> &a, const polymorphic_allocator<T2> &b) noexcept
172{
173 return !(a == b);
174}
175
176namespace detail {
177
179{
180public:
182
183protected:
185 {
186 return ::operator new(bytes, static_cast<std::align_val_t>(alignment));
187 }
188
190 {
191# ifdef __cpp_sized_deallocation
192 ::operator delete(p, bytes, static_cast<std::align_val_t>(alignment));
193# else
194 ::operator delete(p, static_cast<std::align_val_t>(alignment));
195# endif
196 }
197
198 bool do_is_equal(const memory_resource &other) const noexcept override
199 {
200 return &other == this || typeid(other) == typeid(new_delete_resource_impl);
201 }
202};
203
204} // namespace detail
205
207{
209 return &instance;
210}
211
213{
214 return new_delete_resource();
215}
216
217#endif
218
219template <typename T>
221
222template <typename K, typename V, typename C = std::less<K>>
223using map = std::map<K, V, C, polymorphic_allocator<std::pair<const K, V>>>;
224
225} // namespace QtMultimediaPrivate::pmr
226
227QT_END_NAMESPACE
228
229#ifdef QT_MM_PMR_EMULATION
230# undef QT_MM_PMR_EMULATION
231#endif // QT_MM_PMR_EMULATION
232
233#endif // Q_PMR_EMULATION_P_H
#define __has_include(x)