Allocation failure fix

Intentional allocation failure tests were crashing on Linux,
because the object's constructor was called even when the
allocator returned nullptr. Separated the allocation from
the construction to fix the issue.

Bug b/116336664

Change-Id: I7a5d4e957ec27f37a96b795a7f17aacebb240fe9
Tests: dEQP-VK.api.device_init.create_instance_device_intentional_alloc_fail
Tests: dEQP-VK.api.object_management.alloc_callback_fail.*
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/28948
Tested-by: Alexis Hétu <sugoi@google.com>
Presubmit-Ready: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Vulkan/VkCommandPool.cpp b/src/Vulkan/VkCommandPool.cpp
index 85c5738..0a7dc1b 100644
--- a/src/Vulkan/VkCommandPool.cpp
+++ b/src/Vulkan/VkCommandPool.cpp
@@ -16,6 +16,7 @@
 #include "VkCommandBuffer.hpp"
 #include "VkDestroy.h"
 #include <algorithm>
+#include <new>
 
 namespace vk
 {
@@ -23,7 +24,10 @@
 CommandPool::CommandPool(const VkCommandPoolCreateInfo* pCreateInfo, void* mem)
 {
 	// FIXME (b/119409619): use an allocator here so we can control all memory allocations
-	commandBuffers = new std::set<VkCommandBuffer>();
+	void* deviceMemory = vk::allocate(sizeof(std::set<VkCommandBuffer>), REQUIRED_MEMORY_ALIGNMENT,
+	                                  DEVICE_MEMORY, GetAllocationScope());
+	ASSERT(deviceMemory);
+	commandBuffers = new (deviceMemory) std::set<VkCommandBuffer>();
 }
 
 void CommandPool::destroy(const VkAllocationCallbacks* pAllocator)
@@ -35,7 +39,7 @@
 	}
 
 	// FIXME (b/119409619): use an allocator here so we can control all memory allocations
-	delete commandBuffers;
+	vk::deallocate(commandBuffers, DEVICE_MEMORY);
 }
 
 size_t CommandPool::ComputeRequiredAllocationSize(const VkCommandPoolCreateInfo* pCreateInfo)
@@ -47,7 +51,11 @@
 {
 	for(uint32_t i = 0; i < commandBufferCount; i++)
 	{
-		DispatchableCommandBuffer* commandBuffer = new (DEVICE_MEMORY) DispatchableCommandBuffer(level);
+		// FIXME (b/119409619): use an allocator here so we can control all memory allocations
+		void* deviceMemory = vk::allocate(sizeof(DispatchableCommandBuffer), REQUIRED_MEMORY_ALIGNMENT,
+		                                  DEVICE_MEMORY, DispatchableCommandBuffer::GetAllocationScope());
+		ASSERT(deviceMemory);
+		DispatchableCommandBuffer* commandBuffer = new (deviceMemory) DispatchableCommandBuffer(level);
 		if(commandBuffer)
 		{
 			pCommandBuffers[i] = *commandBuffer;