Mesa (main): r600/sfn: emulate pmr::monotonic_buffer_resource if needed

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Jul 13 13:42:04 UTC 2022


Module: Mesa
Branch: main
Commit: 643623e1a3549861f1fc229ec5b77cab701ae59b
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=643623e1a3549861f1fc229ec5b77cab701ae59b

Author: Gert Wollny <gert.wollny at collabora.com>
Date:   Mon Jul 11 08:58:48 2022 +0200

r600/sfn: emulate pmr::monotonic_buffer_resource if needed

libc++ does not yet implement the c++17 monotonic_buffer_resource,
so emulate it by doing normal allocations that are cleaned up
when the resource is destroyed.

v2: - Use C include and version without namespace aligned_alloc
    - add include for sstream needed with clang++ (maurossi)

Closes: #6836
Signed-off-by: Gert Wollny <gert.wollny at collabora.com>
Reviewed-by: Filip Gawin <filip at gawin.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17452>

---

 src/gallium/drivers/r600/meson.build               |  9 ++++-
 src/gallium/drivers/r600/sfn/sfn_memorypool.cpp    | 46 +++++++++++++++++++++-
 src/gallium/drivers/r600/sfn/sfn_memorypool.h      |  5 ---
 src/gallium/drivers/r600/sfn/sfn_virtualvalues.cpp |  2 +
 src/gallium/drivers/r600/sfn/tests/meson.build     |  2 +-
 5 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/src/gallium/drivers/r600/meson.build b/src/gallium/drivers/r600/meson.build
index 7059f6d43d9..eb01c79a2b9 100644
--- a/src/gallium/drivers/r600/meson.build
+++ b/src/gallium/drivers/r600/meson.build
@@ -193,11 +193,18 @@ if with_gallium_opencl
   r600_c_args += '-DHAVE_OPENCL'
 endif
 
+r600_cpp_args = ['-std=c++17']
+if cpp.has_type('std::pmr::monotonic_buffer_resource',
+                 args:['-std=c++17'],
+                 prefix : '#include <memory_resource>')
+   r600_cpp_args += '-DHAVE_MEMORY_RESOURCE'
+endif
+
 libr600 = static_library(
   'r600',
   [files_r600, egd_tables_h],
   c_args : [r600_c_args, '-Wstrict-overflow=0'],
-  cpp_args: '-std=c++17',
+  cpp_args: r600_cpp_args,
   gnu_symbol_visibility : 'hidden',
   include_directories : [
     inc_src, inc_mapi, inc_mesa, inc_include, inc_compiler, inc_gallium, inc_gallium_aux, inc_amd_common,
diff --git a/src/gallium/drivers/r600/sfn/sfn_memorypool.cpp b/src/gallium/drivers/r600/sfn/sfn_memorypool.cpp
index 234ccebbc60..8780d58cf76 100644
--- a/src/gallium/drivers/r600/sfn/sfn_memorypool.cpp
+++ b/src/gallium/drivers/r600/sfn/sfn_memorypool.cpp
@@ -29,15 +29,34 @@
 #include <cassert>
 #include <iostream>
 
+#ifdef HAVE_MEMORY_RESOURCE
+#include <memory_resource>
+#else
+#include <list>
+#include <stdlib.h>
+#endif
+
 namespace r600 {
 
+#ifndef HAVE_MEMORY_RESOURCE
+/* Fallback memory resource if the C++17 memory resource is not
+ * avaliable
+*/
+struct MemoryBacking {
+   ~MemoryBacking();
+   void *allocate(size_t size);
+   void *allocate(size_t size, size_t align);
+   std::list<void *> m_data;
+};
+#endif
+
 struct MemoryPoolImpl {
 public:
    MemoryPoolImpl();
    ~MemoryPoolImpl();
-
+#ifdef HAVE_MEMORY_RESOURCE
    using MemoryBacking = ::std::pmr::monotonic_buffer_resource;
-
+#endif
    MemoryBacking *pool;
 };
 
@@ -109,4 +128,27 @@ MemoryPoolImpl::~MemoryPoolImpl()
    delete pool;
 }
 
+#ifndef HAVE_MEMORY_RESOURCE
+MemoryBacking::~MemoryBacking()
+{
+   for (auto p : m_data)
+      free(p);
+}
+
+void *MemoryBacking::allocate(size_t size)
+{
+   void *retval = malloc(size);
+   m_data.push_back(retval);
+   return retval;
+}
+
+void *MemoryBacking::allocate(size_t size, size_t align)
+{
+   void *retval = aligned_alloc(align, size);
+   m_data.push_back(retval);
+   return retval;
+}
+
+#endif
+
 }
diff --git a/src/gallium/drivers/r600/sfn/sfn_memorypool.h b/src/gallium/drivers/r600/sfn/sfn_memorypool.h
index 44bb5dcfe10..44154102295 100644
--- a/src/gallium/drivers/r600/sfn/sfn_memorypool.h
+++ b/src/gallium/drivers/r600/sfn/sfn_memorypool.h
@@ -31,12 +31,7 @@
 #include <memory>
 #include <stack>
 
-#if __cplusplus >= 21703L
-#include <memory_resource>
 #define R600_POINTER_TYPE(X) X *
-#else
-#error Need C++17
-#endif
 
 namespace r600  {
 
diff --git a/src/gallium/drivers/r600/sfn/sfn_virtualvalues.cpp b/src/gallium/drivers/r600/sfn/sfn_virtualvalues.cpp
index 9b96d602957..32af0477c8f 100644
--- a/src/gallium/drivers/r600/sfn/sfn_virtualvalues.cpp
+++ b/src/gallium/drivers/r600/sfn/sfn_virtualvalues.cpp
@@ -35,6 +35,8 @@
 #include <ostream>
 #include <iostream>
 #include <iomanip>
+#include <limits>
+#include <sstream>
 
 namespace r600 {
 
diff --git a/src/gallium/drivers/r600/sfn/tests/meson.build b/src/gallium/drivers/r600/sfn/tests/meson.build
index e256548dda5..ba00dc58c05 100644
--- a/src/gallium/drivers/r600/sfn/tests/meson.build
+++ b/src/gallium/drivers/r600/sfn/tests/meson.build
@@ -1,6 +1,6 @@
 
 r600_test_lib = static_library('r600_test', 'sfn_test_shaders.cpp',
-   cpp_args: '-std=c++17',
+   cpp_args: r600_cpp_args,
    include_directories : [ inc_src, inc_mapi, inc_mesa, inc_include,
                            inc_compiler, inc_gallium, inc_gallium_aux, inc_amd_common,
                            inc_gallium_drivers, ],



More information about the mesa-commit mailing list