https://gcc.gnu.org/g:99835bd68e5360b0b3c8ad9c61ea23f70ad3dce6
commit r15-9504-g99835bd68e5360b0b3c8ad9c61ea23f70ad3dce6
Author: waffl3x
Date: Tue Apr 15 14:34:38 2025 -0600
OpenMP: omp.h omp::allocator C++ Allocator interface
The implementation of each allocator is simplified by inheriting from
__detail::__allocator_templ. At the moment, none of the implementations
diverge in any way, simply passing in the allocator handle to be used when
an allocation is made. In the future, const_mem will need special handling
added to it to support constant memory space.
libgomp/ChangeLog:
* omp.h.in: Add omp::allocator::* and ompx::allocator::* allocators.
(__detail::__allocator_templ):
New struct template.
(null_allocator): New struct template.
(default_mem): Likewise.
(large_cap_mem): Likewise.
(const_mem): Likewise.
(high_bw_mem): Likewise.
(low_lat_mem): Likewise.
(cgroup_mem): Likewise.
(pteam_mem): Likewise.
(thread_mem): Likewise.
(ompx::allocator::gnu_pinned_mem): Likewise.
* testsuite/libgomp.c++/allocator-1.C: New test.
* testsuite/libgomp.c++/allocator-2.C: New test.
Signed-off-by: waffl3x
Diff:
---
libgomp/omp.h.in| 132 +++
libgomp/testsuite/libgomp.c++/allocator-1.C | 158
libgomp/testsuite/libgomp.c++/allocator-2.C | 132 +++
3 files changed, 422 insertions(+)
diff --git a/libgomp/omp.h.in b/libgomp/omp.h.in
index d5e8be46e944..8d17db1da9a7 100644
--- a/libgomp/omp.h.in
+++ b/libgomp/omp.h.in
@@ -432,4 +432,136 @@ extern const char *omp_get_uid_from_device (int)
__GOMP_NOTHROW;
}
#endif
+#if __cplusplus >= 201103L
+
+/* std::__throw_bad_alloc and std::__throw_bad_array_new_length. */
+#include
+
+namespace omp
+{
+namespace allocator
+{
+
+namespace __detail
+{
+
+template
+struct __allocator_templ
+{
+ using value_type = __T;
+ using pointer = __T*;
+ using const_pointer = const __T*;
+ using size_type = __SIZE_TYPE__;
+ using difference_type = __PTRDIFF_TYPE__;
+
+ __T*
+ allocate (size_type __n)
+ {
+if (__SIZE_MAX__ / sizeof(__T) < __n)
+ std::__throw_bad_array_new_length ();
+void *__p = omp_aligned_alloc (alignof(__T), __n * sizeof(__T), __Handle);
+if (!__p)
+ std::__throw_bad_alloc ();
+return static_cast<__T*>(__p);
+ }
+
+ void
+ deallocate (__T *__p, size_type) __GOMP_NOTHROW
+ {
+omp_free (static_cast(__p), __Handle);
+ }
+};
+
+template
+constexpr bool
+operator== (const __allocator_templ<__T, __Handle>&,
+ const __allocator_templ<__U, __Handle>&) __GOMP_NOTHROW
+{
+ return true;
+}
+
+template
+constexpr bool
+operator== (const __allocator_templ<__T, __Handle>&,
+ const __allocator_templ<__U, __UHandle>&) __GOMP_NOTHROW
+{
+ return false;
+}
+
+template
+constexpr bool
+operator!= (const __allocator_templ<__T, __Handle>&,
+ const __allocator_templ<__U, __Handle>&) __GOMP_NOTHROW
+{
+ return false;
+}
+
+template
+constexpr bool
+operator!= (const __allocator_templ<__T, __Handle>&,
+ const __allocator_templ<__U, __UHandle>&) __GOMP_NOTHROW
+{
+ return true;
+}
+
+} /* namespace __detail */
+
+template
+struct null_allocator
+ : __detail::__allocator_templ<__T, omp_null_allocator> {};
+
+template
+struct default_mem
+ : __detail::__allocator_templ<__T, omp_default_mem_alloc> {};
+
+template
+struct large_cap_mem
+ : __detail::__allocator_templ<__T, omp_large_cap_mem_alloc> {};
+
+template
+struct const_mem
+ : __detail::__allocator_templ<__T, omp_const_mem_alloc> {};
+
+template
+struct high_bw_mem
+ : __detail::__allocator_templ<__T, omp_high_bw_mem_alloc> {};
+
+template
+struct low_lat_mem
+ : __detail::__allocator_templ<__T, omp_low_lat_mem_alloc> {};
+
+template
+struct cgroup_mem
+ : __detail::__allocator_templ<__T, omp_cgroup_mem_alloc> {};
+
+template
+struct pteam_mem
+ : __detail::__allocator_templ<__T, omp_pteam_mem_alloc> {};
+
+template
+struct thread_mem
+ : __detail::__allocator_templ<__T, omp_thread_mem_alloc> {};
+
+} /* namespace allocator */
+
+} /* namespace omp */
+
+namespace ompx
+{
+
+namespace allocator
+{
+
+template
+struct gnu_pinned_mem
+ : omp::allocator::__detail::__allocator_templ<__T,
ompx_gnu_pinned_mem_alloc> {};
+
+} /* namespace allocator */
+
+} /* namespace ompx */
+
+#endif /* __cplusplus */
+
#endif /* _OMP_H */
diff --git a/libgomp/testsuite/libgomp.c++/allocator-1.C
b/libgomp/testsuite/libgomp.c++/allocator-1.C
new file mode 100644
index ..f82072284883
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/allocator-1.C
@@ -0,0 +1,158 @@
+// { dg-do run }
+
+#include
+#include
+#include
+
+template class Alloc>
+void test (T const initial_value = T())
+{
+ using Allocator = Alloc;
+ Allocator a;
+