Refactor DeviceMemory buffer allocation/freeing

This change completes replacing "deallocate" with "free" to match the
Vulkan nomenclature.

DeviceMemory::size was renamed to allocationSize to match the
VkMemoryAllocateInfo field and avoid potential confusion with other
sizes. The virtual DeviceMemory::allocate() method was only ever called
with the allocation size and a pointer to the (previously private)
buffer member as parameters. So it was renamed to allocateBuffer() and
doesn't take any parameters so each implementation sets the member
variable directly. Likewise deallocate() previously took the same
parameters and has been replaced by freeBuffer().

Bug: b/134584057
Change-Id: I401b4dad2632593dd7e155480475f0f7f20788f6
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/57969
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Vulkan/VkDeviceMemory.cpp b/src/Vulkan/VkDeviceMemory.cpp
index 871fec7..aa6d7e2 100644
--- a/src/Vulkan/VkDeviceMemory.cpp
+++ b/src/Vulkan/VkDeviceMemory.cpp
@@ -61,17 +61,17 @@
 	    , allocateInfo(extendedAllocationInfo)
 	{}
 
-	VkResult allocate(size_t size, void **pBuffer) override
+	VkResult allocateBuffer() override
 	{
 		if(allocateInfo.supported)
 		{
-			*pBuffer = allocateInfo.hostPointer;
+			buffer = allocateInfo.hostPointer;
 			return VK_SUCCESS;
 		}
 		return VK_ERROR_INVALID_EXTERNAL_HANDLE;
 	}
 
-	void deallocate(void *buffer, size_t size) override
+	void freeBuffer() override
 	{}
 
 	VkExternalMemoryHandleTypeFlagBits getFlagBit() const override
@@ -194,21 +194,23 @@
 }
 
 DeviceMemory::DeviceMemory(const VkMemoryAllocateInfo *pAllocateInfo, Device *pDevice)
-    : size(pAllocateInfo->allocationSize)
+    : allocationSize(pAllocateInfo->allocationSize)
     , memoryTypeIndex(pAllocateInfo->memoryTypeIndex)
     , device(pDevice)
 {
-	ASSERT(size);
+	ASSERT(allocationSize);
 }
 
 void DeviceMemory::destroy(const VkAllocationCallbacks *pAllocator)
 {
 #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
-	device->emitDeviceMemoryReport(isImport() ? VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_UNIMPORT_EXT : VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_FREE_EXT, getMemoryObjectId(), 0 /* size */, VK_OBJECT_TYPE_DEVICE_MEMORY, (uint64_t)(void *)VkDeviceMemory(*this));
+	VkDeviceMemoryReportEventTypeEXT eventType = isImport() ? VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_UNIMPORT_EXT : VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_FREE_EXT;
+	device->emitDeviceMemoryReport(eventType, getMemoryObjectId(), 0 /* size */, VK_OBJECT_TYPE_DEVICE_MEMORY, (uint64_t)(void *)VkDeviceMemory(*this));
 #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
+
 	if(buffer)
 	{
-		deallocate(buffer, size);
+		freeBuffer();
 		buffer = nullptr;
 	}
 }
@@ -305,29 +307,33 @@
 
 VkResult DeviceMemory::allocate()
 {
-	if(size > MAX_MEMORY_ALLOCATION_SIZE)
+	if(allocationSize > MAX_MEMORY_ALLOCATION_SIZE)
 	{
 #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
-		device->emitDeviceMemoryReport(VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATION_FAILED_EXT, 0 /* memoryObjectId */, size, VK_OBJECT_TYPE_DEVICE_MEMORY, 0 /* objectHandle */);
+		device->emitDeviceMemoryReport(VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATION_FAILED_EXT, 0 /* memoryObjectId */, allocationSize, VK_OBJECT_TYPE_DEVICE_MEMORY, 0 /* objectHandle */);
 #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
+
 		return VK_ERROR_OUT_OF_DEVICE_MEMORY;
 	}
 
 	VkResult result = VK_SUCCESS;
 	if(!buffer)
 	{
-		result = allocate(size, &buffer);
+		result = allocateBuffer();
 	}
+
 #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
 	if(result == VK_SUCCESS)
 	{
-		device->emitDeviceMemoryReport(isImport() ? VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_IMPORT_EXT : VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATE_EXT, getMemoryObjectId(), size, VK_OBJECT_TYPE_DEVICE_MEMORY, (uint64_t)(void *)VkDeviceMemory(*this));
+		VkDeviceMemoryReportEventTypeEXT eventType = isImport() ? VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_IMPORT_EXT : VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATE_EXT;
+		device->emitDeviceMemoryReport(eventType, getMemoryObjectId(), allocationSize, VK_OBJECT_TYPE_DEVICE_MEMORY, (uint64_t)(void *)VkDeviceMemory(*this));
 	}
 	else
 	{
-		device->emitDeviceMemoryReport(VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATION_FAILED_EXT, 0 /* memoryObjectId */, size, VK_OBJECT_TYPE_DEVICE_MEMORY, 0 /* objectHandle */);
+		device->emitDeviceMemoryReport(VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATION_FAILED_EXT, 0 /* memoryObjectId */, allocationSize, VK_OBJECT_TYPE_DEVICE_MEMORY, 0 /* objectHandle */);
 	}
 #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
+
 	return result;
 }
 
@@ -340,7 +346,7 @@
 
 VkDeviceSize DeviceMemory::getCommittedMemoryInBytes() const
 {
-	return size;
+	return allocationSize;
 }
 
 void *DeviceMemory::getOffsetPointer(VkDeviceSize pOffset) const
@@ -371,22 +377,21 @@
 	return (supportedHandleTypes & handle_type_bit) != 0;
 }
 
-// Allocate the memory according to |size|. On success return VK_SUCCESS
-// and sets |*pBuffer|.
-VkResult DeviceMemory::allocate(size_t size, void **pBuffer)
+// Allocate the memory according to `allocationSize`. On success return VK_SUCCESS
+// and sets `buffer`.
+VkResult DeviceMemory::allocateBuffer()
 {
-	buffer = vk::allocateDeviceMemory(size, REQUIRED_MEMORY_ALIGNMENT);
+	buffer = vk::allocateDeviceMemory(allocationSize, REQUIRED_MEMORY_ALIGNMENT);
 	if(!buffer)
 	{
 		return VK_ERROR_OUT_OF_DEVICE_MEMORY;
 	}
 
-	*pBuffer = buffer;
 	return VK_SUCCESS;
 }
 
-// Deallocate previously allocated memory at |buffer|.
-void DeviceMemory::deallocate(void *buffer, size_t size)
+// Free previously allocated memory at `buffer`.
+void DeviceMemory::freeBuffer()
 {
 	vk::freeDeviceMemory(buffer);
 	buffer = nullptr;
diff --git a/src/Vulkan/VkDeviceMemory.hpp b/src/Vulkan/VkDeviceMemory.hpp
index 77bf23a..1d89a72 100644
--- a/src/Vulkan/VkDeviceMemory.hpp
+++ b/src/Vulkan/VkDeviceMemory.hpp
@@ -95,19 +95,16 @@
 	virtual VkDeviceSize externalImageMemoryOffset(VkImageAspectFlagBits aspect) const { return 0; }
 
 protected:
-	// Allocate the memory according to |size|. On success return VK_SUCCESS
-	// and sets |*pBuffer|.
-	virtual VkResult allocate(size_t size, void **pBuffer);
+	// Allocate the memory according to `allocationSize`. On success return VK_SUCCESS and sets `buffer`.
+	virtual VkResult allocateBuffer();
 
-	// Deallocate previously allocated memory at |buffer|.
-	virtual void deallocate(void *buffer, size_t size);
+	// Free previously allocated memory at `buffer`.
+	virtual void freeBuffer();
 
 	// Return the handle type flag bit supported by this implementation.
 	// A value of 0 corresponds to non-external memory.
 	virtual VkExternalMemoryHandleTypeFlagBits getFlagBit() const;
 
-	virtual void setDevicePtr(Device *pDevice) {}
-
 #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
 	virtual bool isImport() const
 	{
@@ -120,15 +117,15 @@
 	}
 #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
 
+	void *buffer = nullptr;
+	const VkDeviceSize allocationSize;
+	const uint32_t memoryTypeIndex;
+	Device *const device;
+
 private:
 	static VkResult ParseAllocationInfo(const VkMemoryAllocateInfo *pAllocateInfo, DeviceMemory::ExtendedAllocationInfo *extendedAllocationInfo);
 	static VkResult Allocate(const VkAllocationCallbacks *pAllocator, const VkMemoryAllocateInfo *pAllocateInfo, VkDeviceMemory *pMemory,
 	                         const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, Device *device);
-
-	void *buffer = nullptr;
-	VkDeviceSize size = 0;
-	uint32_t memoryTypeIndex = 0;
-	Device *device;
 };
 
 // This class represents a DeviceMemory object with no external memory
diff --git a/src/Vulkan/VkDeviceMemoryExternalAndroid.cpp b/src/Vulkan/VkDeviceMemoryExternalAndroid.cpp
index 0b9b2fb..3ec7a0e 100644
--- a/src/Vulkan/VkDeviceMemoryExternalAndroid.cpp
+++ b/src/Vulkan/VkDeviceMemoryExternalAndroid.cpp
@@ -199,25 +199,24 @@
 
 AHardwareBufferExternalMemory::~AHardwareBufferExternalMemory()
 {
-	// correct deallocation of AHB does not require a pointer or size
-	deallocate(nullptr, 0);
+	freeBuffer();
 }
 
-// VkAllocateMemory
-VkResult AHardwareBufferExternalMemory::allocate(size_t size, void **pBuffer)
+// vkAllocateMemory
+VkResult AHardwareBufferExternalMemory::allocateBuffer()
 {
 	if(allocateInfo.importAhb)
 	{
-		return importAndroidHardwareBuffer(allocateInfo.ahb, pBuffer);
+		return importAndroidHardwareBuffer(allocateInfo.ahb, &buffer);
 	}
 	else
 	{
 		ASSERT(allocateInfo.exportAhb);
-		return allocateAndroidHardwareBuffer(size, pBuffer);
+		return allocateAndroidHardwareBuffer(allocationSize, &buffer);
 	}
 }
 
-void AHardwareBufferExternalMemory::deallocate(void *buffer, size_t size)
+void AHardwareBufferExternalMemory::freeBuffer()
 {
 	if(ahb != nullptr)
 	{
diff --git a/src/Vulkan/VkDeviceMemoryExternalAndroid.hpp b/src/Vulkan/VkDeviceMemoryExternalAndroid.hpp
index b8e5844..e89bf47 100644
--- a/src/Vulkan/VkDeviceMemoryExternalAndroid.hpp
+++ b/src/Vulkan/VkDeviceMemoryExternalAndroid.hpp
@@ -53,15 +53,13 @@
 	explicit AHardwareBufferExternalMemory(const VkMemoryAllocateInfo *pCreateInfo, void *mem, const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, vk::Device *pDevice);
 	~AHardwareBufferExternalMemory();
 
-	VkResult allocate(size_t size, void **pBuffer) override;
-	void deallocate(void *buffer, size_t size) override;
+	VkResult allocateBuffer() override;
+	void freeBuffer() override;
 
 	VkExternalMemoryHandleTypeFlagBits getFlagBit() const override { return typeFlagBit; }
 
 	virtual VkResult exportAndroidHardwareBuffer(AHardwareBuffer **pAhb) const override final;
 
-	void setDevicePtr(vk::Device *pDevice) override { device = pDevice; }
-
 	static VkFormat GetVkFormatFromAHBFormat(uint32_t ahbFormat);
 	static VkResult GetAndroidHardwareBufferFormatProperties(const AHardwareBuffer_Desc &ahbDesc, VkAndroidHardwareBufferFormatPropertiesANDROID *pFormat);
 	static VkResult GetAndroidHardwareBufferProperties(VkDevice &device, const AHardwareBuffer *buffer, VkAndroidHardwareBufferPropertiesANDROID *pProperties);
diff --git a/src/Vulkan/VkDeviceMemoryExternalFuchsia.hpp b/src/Vulkan/VkDeviceMemoryExternalFuchsia.hpp
index b7a4cef..f47f985 100644
--- a/src/Vulkan/VkDeviceMemoryExternalFuchsia.hpp
+++ b/src/Vulkan/VkDeviceMemoryExternalFuchsia.hpp
@@ -79,7 +79,7 @@
 		closeVmo();
 	}
 
-	VkResult allocate(size_t size, void **pBuffer) override
+	VkResult allocateBuffer() override
 	{
 		if(allocateInfo.importHandle)
 		{
@@ -89,7 +89,7 @@
 		else
 		{
 			ASSERT(allocateInfo.exportHandle);
-			zx_status_t status = zx_vmo_create(size, 0, &vmoHandle);
+			zx_status_t status = zx_vmo_create(allocationSize, 0, &vmoHandle);
 			if(status != ZX_OK)
 			{
 				TRACE("zx_vmo_create() returned %d", status);
@@ -104,22 +104,22 @@
 		                                 0,  // vmar_offset
 		                                 vmoHandle,
 		                                 0,  // vmo_offset
-		                                 size,
+		                                 allocationSize,
 		                                 &addr);
 		if(status != ZX_OK)
 		{
 			TRACE("zx_vmar_map() failed with %d", status);
 			return VK_ERROR_MEMORY_MAP_FAILED;
 		}
-		*pBuffer = reinterpret_cast<void *>(addr);
+		buffer = reinterpret_cast<void *>(addr);
 		return VK_SUCCESS;
 	}
 
-	void deallocate(void *buffer, size_t size) override
+	void freeBuffer() override
 	{
 		zx_status_t status = zx_vmar_unmap(zx_vmar_root_self(),
 		                                   reinterpret_cast<zx_vaddr_t>(buffer),
-		                                   size);
+		                                   allocationSize);
 		if(status != ZX_OK)
 		{
 			TRACE("zx_vmar_unmap() failed with %d", status);
diff --git a/src/Vulkan/VkDeviceMemoryExternalLinux.hpp b/src/Vulkan/VkDeviceMemoryExternalLinux.hpp
index f4d7f66..d68eb45 100644
--- a/src/Vulkan/VkDeviceMemoryExternalLinux.hpp
+++ b/src/Vulkan/VkDeviceMemoryExternalLinux.hpp
@@ -43,7 +43,7 @@
 		memfd.close();
 	}
 
-	VkResult allocate(size_t size, void **pBuffer) override
+	VkResult allocateBuffer() override
 	{
 		if(allocateInfo.importFd)
 		{
@@ -59,24 +59,24 @@
 			static int counter = 0;
 			char name[40];
 			snprintf(name, sizeof(name), "SwiftShader.Memory.%d", ++counter);
-			if(!memfd.allocate(name, size))
+			if(!memfd.allocate(name, allocationSize))
 			{
 				TRACE("memfd.allocate() returned %s", strerror(errno));
 				return VK_ERROR_OUT_OF_DEVICE_MEMORY;
 			}
 		}
-		void *addr = memfd.mapReadWrite(0, size);
+		void *addr = memfd.mapReadWrite(0, allocationSize);
 		if(!addr)
 		{
 			return VK_ERROR_MEMORY_MAP_FAILED;
 		}
-		*pBuffer = addr;
+		buffer = addr;
 		return VK_SUCCESS;
 	}
 
-	void deallocate(void *buffer, size_t size) override
+	void freeBuffer() override
 	{
-		memfd.unmap(buffer, size);
+		memfd.unmap(buffer, allocationSize);
 	}
 
 	VkExternalMemoryHandleTypeFlagBits getFlagBit() const override
diff --git a/src/Vulkan/VkDeviceMemoryExternalMac.hpp b/src/Vulkan/VkDeviceMemoryExternalMac.hpp
index b68f618..f23d416 100644
--- a/src/Vulkan/VkDeviceMemoryExternalMac.hpp
+++ b/src/Vulkan/VkDeviceMemoryExternalMac.hpp
@@ -83,7 +83,7 @@
 		}
 	}
 
-	VkResult allocate(size_t size, void **pBuffer) override
+	VkResult allocateBuffer() override
 	{
 		if(allocateInfo.importFd)
 		{
@@ -138,9 +138,9 @@
 			}
 
 			// Ensure there is enough space.
-			if(fd >= 0 && size > 0)
+			if(fd >= 0 && allocationSize > 0)
 			{
-				if(::ftruncate(fd, size) < 0)
+				if(::ftruncate(fd, allocationSize) < 0)
 				{
 					TRACE("ftruncate() failed with: %s", strerror(errno));
 					close(fd);
@@ -157,20 +157,20 @@
 			shm_fd_ = fd;
 		}
 
-		void *addr = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED,
+		void *addr = ::mmap(nullptr, allocationSize, PROT_READ | PROT_WRITE, MAP_SHARED,
 		                    shm_fd_, 0);
 
 		if(addr == MAP_FAILED)
 		{
 			return VK_ERROR_MEMORY_MAP_FAILED;
 		}
-		*pBuffer = addr;
+		buffer = addr;
 		return VK_SUCCESS;
 	}
 
-	void deallocate(void *buffer, size_t size) override
+	void freeBuffer() override
 	{
-		::munmap(buffer, size);
+		::munmap(buffer, allocationSize);
 	}
 
 	VkExternalMemoryHandleTypeFlagBits getFlagBit() const override