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 "VkQueryPool.hpp"
33 #include "VkQueue.hpp"
34 #include "VkRenderPass.hpp"
35 #include "VkSampler.hpp"
36 #include "VkSemaphore.hpp"
37 #include "VkShaderModule.hpp"
38 #include "WSI/VkSurfaceKHR.hpp"
39 #include "WSI/VkSwapchainKHR.hpp"
40 
41 #include <type_traits>
42 
43 namespace vk {
44 
45 // Because Vulkan uses optional allocation callbacks, we use them in a custom
46 // placement new operator in the VkObjectBase class for simplicity.
47 // Unfortunately, since we use a placement new to allocate VkObjectBase derived
48 // classes objects, the corresponding deletion operator is a placement delete,
49 // which does nothing. In order to properly dispose of these objects' memory,
50 // we use this function, which calls the T:destroy() function then the T
51 // destructor prior to releasing the object (by default,
52 // VkObjectBase::destroy does nothing).
53 template<typename VkT>
destroy(VkT vkObject,const VkAllocationCallbacks * pAllocator)54 inline void destroy(VkT vkObject, const VkAllocationCallbacks *pAllocator)
55 {
56 	auto object = Cast(vkObject);
57 	if(object)
58 	{
59 		using T = typename std::remove_pointer<decltype(object)>::type;
60 		object->destroy(pAllocator);
61 		object->~T();
62 		// object may not point to the same pointer as vkObject, for dispatchable objects,
63 		// for example, so make sure to deallocate based on the vkObject pointer, which
64 		// should always point to the beginning of the allocated memory
65 		vk::deallocate(vkObject, pAllocator);
66 	}
67 }
68 
69 template<typename VkT>
release(VkT vkObject,const VkAllocationCallbacks * pAllocator)70 inline void release(VkT vkObject, const VkAllocationCallbacks *pAllocator)
71 {
72 	auto object = Cast(vkObject);
73 	if(object)
74 	{
75 		using T = typename std::remove_pointer<decltype(object)>::type;
76 		if(object->release(pAllocator))
77 		{
78 			object->~T();
79 			// object may not point to the same pointer as vkObject, for dispatchable objects,
80 			// for example, so make sure to deallocate based on the vkObject pointer, which
81 			// should always point to the beginning of the allocated memory
82 			vk::deallocate(vkObject, pAllocator);
83 		}
84 	}
85 }
86 
87 }  // namespace vk
88