From b1325404c5931e0ec744b696eb078774f813505d Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Thu, 17 Dec 2015 11:00:38 -0800 Subject: [PATCH] anv/device: Handle zero-sized memory allocations --- src/vulkan/anv_device.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/vulkan/anv_device.c b/src/vulkan/anv_device.c index fe44d1cb036..17056e35cb5 100644 --- a/src/vulkan/anv_device.c +++ b/src/vulkan/anv_device.c @@ -1002,6 +1002,12 @@ VkResult anv_AllocateMemory( assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO); + if (pAllocateInfo->allocationSize == 0) { + /* Apparently, this is allowed */ + *pMem = VK_NULL_HANDLE; + return VK_SUCCESS; + } + /* We support exactly one memory heap. */ assert(pAllocateInfo->memoryTypeIndex == 0 || (!device->info.has_llc && pAllocateInfo->memoryTypeIndex < 2)); @@ -1037,6 +1043,9 @@ void anv_FreeMemory( ANV_FROM_HANDLE(anv_device, device, _device); ANV_FROM_HANDLE(anv_device_memory, mem, _mem); + if (mem == NULL) + return; + if (mem->bo.map) anv_gem_munmap(mem->bo.map, mem->bo.size); @@ -1057,6 +1066,11 @@ VkResult anv_MapMemory( ANV_FROM_HANDLE(anv_device, device, _device); ANV_FROM_HANDLE(anv_device_memory, mem, _memory); + if (mem == NULL) { + *ppData = NULL; + return VK_SUCCESS; + } + /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only * takes a VkDeviceMemory pointer, it seems like only one map of the memory * at a time is valid. We could just mmap up front and return an offset @@ -1081,6 +1095,9 @@ void anv_UnmapMemory( { ANV_FROM_HANDLE(anv_device_memory, mem, _memory); + if (mem == NULL) + return; + anv_gem_munmap(mem->map, mem->map_size); } @@ -1207,8 +1224,13 @@ VkResult anv_BindBufferMemory( ANV_FROM_HANDLE(anv_device_memory, mem, _memory); ANV_FROM_HANDLE(anv_buffer, buffer, _buffer); - buffer->bo = &mem->bo; - buffer->offset = memoryOffset; + if (mem) { + buffer->bo = &mem->bo; + buffer->offset = memoryOffset; + } else { + buffer->bo = NULL; + buffer->offset = 0; + } return VK_SUCCESS; } @@ -1222,8 +1244,13 @@ VkResult anv_BindImageMemory( ANV_FROM_HANDLE(anv_device_memory, mem, _memory); ANV_FROM_HANDLE(anv_image, image, _image); - image->bo = &mem->bo; - image->offset = memoryOffset; + if (mem) { + image->bo = &mem->bo; + image->offset = memoryOffset; + } else { + image->bo = NULL; + image->offset = 0; + } return VK_SUCCESS; } -- 2.30.2