xref: /aosp_15_r20/external/swiftshader/src/Vulkan/VkDestroy.hpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "VkBuffer.hpp"
16 #include "VkBufferView.hpp"
17 #include "VkCommandBuffer.hpp"
18 #include "VkCommandPool.hpp"
19 #include "VkDebugUtilsMessenger.hpp"
20 #include "VkDevice.hpp"
21 #include "VkDeviceMemory.hpp"
22 #include "VkEvent.hpp"
23 #include "VkFence.hpp"
24 #include "VkFramebuffer.hpp"
25 #include "VkImage.hpp"
26 #include "VkImageView.hpp"
27 #include "VkInstance.hpp"
28 #include "VkPhysicalDevice.hpp"
29 #include "VkPipeline.hpp"
30 #include "VkPipelineCache.hpp"
31 #include "VkPipelineLayout.hpp"
32 #include "VkPrivateData.hpp"
33 #include "VkQueryPool.hpp"
34 #include "VkQueue.hpp"
35 #include "VkRenderPass.hpp"
36 #include "VkSampler.hpp"
37 #include "VkSemaphore.hpp"
38 #include "VkShaderModule.hpp"
39 #include "WSI/VkSurfaceKHR.hpp"
40 #include "WSI/VkSwapchainKHR.hpp"
41 
42 #include <type_traits>
43 
44 namespace vk {
45 
46 // Because Vulkan uses optional allocation callbacks, we use them in a custom
47 // placement new operator in the VkObjectBase class for simplicity.
48 // Unfortunately, since we use a placement new to allocate VkObjectBase derived
49 // classes objects, the corresponding deletion operator is a placement delete,
50 // which does nothing. In order to properly dispose of these objects' memory,
51 // we use this function, which calls the T:destroy() function then the T
52 // destructor prior to releasing the object (by default,
53 // VkObjectBase::destroy does nothing).
54 template<typename VkT>
destroy(VkT vkObject,const VkAllocationCallbacks * pAllocator)55 inline void destroy(VkT vkObject, const VkAllocationCallbacks *pAllocator)
56 {
57 	auto object = Cast(vkObject);
58 	if(object)
59 	{
60 		using T = typename std::remove_pointer<decltype(object)>::type;
61 		object->destroy(pAllocator);
62 		object->~T();
63 		// object may not point to the same pointer as vkObject, for dispatchable objects,
64 		// for example, so make sure to deallocate based on the vkObject pointer, which
65 		// should always point to the beginning of the allocated memory
66 		vk::freeHostMemory(vkObject, pAllocator);
67 	}
68 }
69 
70 template<typename VkT>
release(VkT vkObject,const VkAllocationCallbacks * pAllocator)71 inline void release(VkT vkObject, const VkAllocationCallbacks *pAllocator)
72 {
73 	auto object = Cast(vkObject);
74 	if(object)
75 	{
76 		using T = typename std::remove_pointer<decltype(object)>::type;
77 		if(object->release(pAllocator))
78 		{
79 			object->~T();
80 			// object may not point to the same pointer as vkObject, for dispatchable objects,
81 			// for example, so make sure to deallocate based on the vkObject pointer, which
82 			// should always point to the beginning of the allocated memory
83 			vk::freeHostMemory(vkObject, pAllocator);
84 		}
85 	}
86 }
87 
88 }  // namespace vk
89