Remove usages of generic allocator in the layer
authorNormunds Rieksts <normunds.rieksts@arm.com>
Mon, 19 Jul 2021 16:34:54 +0000 (17:34 +0100)
committerNormunds Rieksts <normunds.rieksts@arm.com>
Thu, 22 Jul 2021 18:05:26 +0000 (19:05 +0100)
Remove all the leftover instances in layer that still use the
generic allocator rather than the VkAllocationCallbacks

Add additional memory utility that allows to release memory
allocated by custom allocators using RAII

Change-Id: I43594ddd3c506048ca0f02e31b64597a7abc481b
Signed-off-by: Normunds Rieksts <normunds.rieksts@arm.com>
layer/private_data.cpp
util/custom_allocator.hpp
wsi/surface_properties.hpp
wsi/wayland/surface_properties.cpp
wsi/wayland/surface_properties.hpp
wsi/wsi_factory.cpp

index ae4a23e8f79bbcd22475fffde2427ffed1d414a8..c4e9a3d9d07cecb371855e96b50de92985505e9f 100644 (file)
@@ -98,8 +98,8 @@ VkResult instance_private_data::associate(VkInstance instance, instance_dispatch
                                           PFN_vkSetInstanceLoaderData set_loader_data,
                                           util::wsi_platform_set enabled_layer_platforms, const util::allocator &allocator)
 {
-   auto *instance_data =
-      allocator.create<instance_private_data>(1, table, set_loader_data, enabled_layer_platforms, allocator);
+   auto instance_data =
+      allocator.make_unique<instance_private_data>(table, set_loader_data, enabled_layer_platforms, allocator);
 
    if (instance_data == nullptr)
    {
@@ -120,9 +120,10 @@ VkResult instance_private_data::associate(VkInstance instance, instance_dispatch
       g_instance_data.erase(it);
    }
 
-   auto result = g_instance_data.try_insert(std::make_pair(key, instance_data));
+   auto result = g_instance_data.try_insert(std::make_pair(key, instance_data.get()));
    if (result.has_value())
    {
+      instance_data.release();
       return VK_SUCCESS;
    }
    else
@@ -130,7 +131,6 @@ VkResult instance_private_data::associate(VkInstance instance, instance_dispatch
       WSI_LOG_WARNING("Failed to insert instance_private_data for instance (%p) as host is out of memory",
                       reinterpret_cast<void *>(instance));
 
-      destroy(instance_data);
       return VK_ERROR_OUT_OF_HOST_MEMORY;
    }
 }
@@ -231,8 +231,8 @@ VkResult device_private_data::associate(VkDevice dev, instance_private_data &ins
                                         const device_dispatch_table &table, PFN_vkSetDeviceLoaderData set_loader_data,
                                         const util::allocator &allocator)
 {
-   auto *device_data =
-      allocator.create<device_private_data>(1, inst_data, phys_dev, dev, table, set_loader_data, allocator);
+   auto device_data =
+      allocator.make_unique<device_private_data>(inst_data, phys_dev, dev, table, set_loader_data, allocator);
 
    if (device_data == nullptr)
    {
@@ -252,9 +252,10 @@ VkResult device_private_data::associate(VkDevice dev, instance_private_data &ins
       g_device_data.erase(it);
    }
 
-   auto result = g_device_data.try_insert(std::make_pair(key, device_data));
+   auto result = g_device_data.try_insert(std::make_pair(key, device_data.get()));
    if (result.has_value())
    {
+      device_data.release();
       return VK_SUCCESS;
    }
    else
@@ -262,7 +263,6 @@ VkResult device_private_data::associate(VkDevice dev, instance_private_data &ins
       WSI_LOG_WARNING("Failed to insert device_private_data for device (%p) as host is out of memory",
                       reinterpret_cast<void *>(dev));
 
-      destroy(device_data);
       return VK_ERROR_OUT_OF_HOST_MEMORY;
    }
 }
index 90dd34cbb7acad24ebd96a5ffbd66fbf29ef5236..9eacd913f33acd46bb9faf4c11e683bf19075138 100644 (file)
@@ -26,6 +26,7 @@
 #include <vector>
 #include <string>
 #include <cassert>
+#include <memory>
 
 #include <vulkan/vulkan.h>
 
 namespace util
 {
 
+template <typename T>
+class deleter;
+
+template <typename T>
+using unique_ptr = std::unique_ptr<T, deleter<T>>;
+
 /**
  * @brief Minimalistic wrapper of VkAllocationCallbacks.
  */
@@ -89,6 +96,9 @@ public:
    template <typename T>
    void destroy(size_t num_objects, T *obj) const noexcept;
 
+   template <typename T, typename... Args>
+   util::unique_ptr<T> make_unique(Args &&...args) const noexcept;
+
    VkAllocationCallbacks m_callbacks;
    VkSystemAllocationScope m_scope;
 };
@@ -223,9 +233,27 @@ void allocator::destroy(size_t num_objects, T *objects) const noexcept
 }
 
 template <typename T>
-void destroy_custom(T *obj)
+class deleter
+{
+public:
+   deleter(allocator allocator)
+      : m_allocator(std::move(allocator))
+   {}
+
+   void operator()(T *object)
+   {
+      m_allocator.destroy<T>(1, object);
+   }
+
+private:
+   allocator m_allocator;
+};
+
+template <typename T, typename... Args>
+util::unique_ptr<T> allocator::make_unique(Args &&...args) const noexcept
 {
-   T::destroy(obj);
+   T *object = create<T>(1, std::forward<Args>(args)...);
+   return util::unique_ptr<T>(object, *this);
 }
 
 /**
index b496134f8fd4ada935c7fe9238f9f834397c87f5..477fb18c809e5b615ac790a7f6ec2fb5df2d0fcc 100644 (file)
@@ -63,10 +63,10 @@ public:
    /**
     * @brief Return the device extensions that this surface_properties implementation needs.
     */
-   virtual const util::extension_list &get_required_device_extensions()
+   virtual VkResult get_required_device_extensions(util::extension_list &extension_list)
    {
-      static const util::extension_list empty{util::allocator::get_generic()};
-      return empty;
+      /* Requires no additional extensions */
+      return VK_SUCCESS;
    }
 
    /**
index 1160bcbe5f0520fc39dc62e2db8dfd146f71b444..45ea1248ab534638a7b48d1f9c052db7c8e825de 100644 (file)
@@ -269,18 +269,9 @@ static const char *required_device_extensions[] = {
    VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
 };
 
-static std::unique_ptr<util::extension_list> populate_device_extensions()
+VkResult surface_properties::get_required_device_extensions(util::extension_list &extension_list)
 {
-   std::unique_ptr<util::extension_list> ret(new util::extension_list(util::allocator::get_generic()));
-   ret->add(required_device_extensions, NELEMS(required_device_extensions));
-
-   return ret;
-}
-
-const util::extension_list &surface_properties::get_required_device_extensions()
-{
-   static std::unique_ptr<util::extension_list> device_extensions = populate_device_extensions();
-   return *device_extensions;
+   return extension_list.add(required_device_extensions, NELEMS(required_device_extensions));
 }
 
 /* TODO: Check for zwp_linux_dmabuf_v1 protocol in display */
index 86a6101588b7bba16c5fd7f6939d0a00177bdd49..755c9c4048fbf5f49026309ba182cfa44d83d86d 100644 (file)
@@ -43,7 +43,7 @@ public:
    VkResult get_surface_present_modes(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
                                       uint32_t *pPresentModeCount, VkPresentModeKHR *pPresentModes) override;
 
-   const util::extension_list &get_required_device_extensions() override;
+   VkResult get_required_device_extensions(util::extension_list &extension_list) override;
 
    PFN_vkVoidFunction get_proc_addr(const char *name) override;
 };
index 37ad3be0e786a733e21296b3faaea3e97e37dd24..a34f1fb79a557e14a3202df094ec0866135a755e 100644 (file)
@@ -162,8 +162,14 @@ VkResult add_extensions_required_by_layer(VkPhysicalDevice phys_dev, const util:
          continue;
       }
 
+      util::extension_list extensions_required_by_layer{allocator};
       surface_properties *props = get_surface_properties(wsi_ext.platform);
-      const auto &extensions_required_by_layer = props->get_required_device_extensions();
+      res = props->get_required_device_extensions(extensions_required_by_layer);
+      if (res != VK_SUCCESS)
+      {
+         return res;
+      }
+
       bool supported = device_extensions.contains(extensions_required_by_layer);
       if (!supported)
       {