Skip to content

Commit

Permalink
Always check allocate_memory_* return values
Browse files Browse the repository at this point in the history
  • Loading branch information
Upliner committed Oct 21, 2024
1 parent a6d3ef5 commit 0765937
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,12 @@ VkBufferMemory* VkBlobAllocator::fastMalloc(size_t size)
}

block->memory = allocate_memory(memoryRequirements.size, buffer_memory_type_index);
if (!block->memory)
{
vkDestroyBuffer(vkdev->vkdevice(), block->buffer, 0);
delete block;
return 0;
}

// ignore memoryRequirements.alignment as we always bind at zero offset
vkBindBufferMemory(vkdev->vkdevice(), block->buffer, block->memory, 0);
Expand Down Expand Up @@ -1032,6 +1038,12 @@ VkImageMemory* VkBlobAllocator::fastMalloc(int w, int h, int c, size_t elemsize,

// bind at memory offset
ptr->memory = allocate_memory(new_block_size, image_memory_type_index);
if (!ptr->memory)
{
vkDestroyImage(vkdev->vkdevice(), ptr->image, 0);
delete ptr;
return 0;
}
ptr->bind_offset = 0;
ptr->bind_capacity = aligned_size;

Expand Down Expand Up @@ -1341,6 +1353,12 @@ VkBufferMemory* VkWeightAllocator::fastMalloc(size_t size)
}

block->memory = allocate_dedicated_memory(memoryRequirements2.memoryRequirements.size, buffer_memory_type_index, 0, block->buffer);
if (!block->memory)
{
vkDestroyBuffer(vkdev->vkdevice(), block->buffer, 0);
delete block;
return 0;
}

// ignore memoryRequirements2.memoryRequirements.alignment as we always bind at zero offset
vkBindBufferMemory(vkdev->vkdevice(), block->buffer, block->memory, 0);
Expand Down Expand Up @@ -1400,6 +1418,12 @@ VkBufferMemory* VkWeightAllocator::fastMalloc(size_t size)
}

block->memory = allocate_memory(memoryRequirements.size, buffer_memory_type_index);
if (!block->memory)
{
vkDestroyBuffer(vkdev->vkdevice(), block->buffer, 0);
delete block;
return 0;
}

// ignore memoryRequirements.alignment as we always bind at zero offset
vkBindBufferMemory(vkdev->vkdevice(), block->buffer, block->memory, 0);
Expand Down Expand Up @@ -1547,6 +1571,12 @@ VkImageMemory* VkWeightAllocator::fastMalloc(int w, int h, int c, size_t elemsiz

// bind memory
ptr->memory = allocate_dedicated_memory(memoryRequirements2.memoryRequirements.size, image_memory_type_index, ptr->image, 0);
if (!ptr->memory)
{
vkDestroyImage(vkdev->vkdevice(), ptr->image, 0);
delete ptr;
return 0;
}
ptr->bind_offset = 0;
ptr->bind_capacity = memoryRequirements2.memoryRequirements.size;

Expand Down Expand Up @@ -1654,6 +1684,12 @@ VkImageMemory* VkWeightAllocator::fastMalloc(int w, int h, int c, size_t elemsiz

// bind at memory offset
ptr->memory = allocate_memory(new_block_size, image_memory_type_index);
if (!ptr->memory)
{
vkDestroyImage(vkdev->vkdevice(), ptr->image, 0);
delete ptr;
return 0;
}
ptr->bind_offset = 0;
ptr->bind_capacity = aligned_size;

Expand Down Expand Up @@ -1788,6 +1824,12 @@ VkBufferMemory* VkStagingAllocator::fastMalloc(size_t size)
}

ptr->memory = allocate_memory(memoryRequirements.size, buffer_memory_type_index);
if (!ptr->memory)
{
vkDestroyBuffer(vkdev->vkdevice(), ptr->buffer, 0);
delete ptr;
return 0;
}

// ignore memoryRequirements.alignment as we always bind at zero offset
vkBindBufferMemory(vkdev->vkdevice(), ptr->buffer, ptr->memory, 0);
Expand Down Expand Up @@ -1897,6 +1939,12 @@ VkBufferMemory* VkWeightStagingAllocator::fastMalloc(size_t size)
}

ptr->memory = allocate_memory(memoryRequirements.size, buffer_memory_type_index);
if (!ptr->memory)
{
vkDestroyBuffer(vkdev->vkdevice(), ptr->buffer, 0);
delete ptr;
return 0;
}

// ignore memoryRequirements.alignment as we always bind at zero offset
vkBindBufferMemory(vkdev->vkdevice(), ptr->buffer, ptr->memory, 0);
Expand Down

0 comments on commit 0765937

Please sign in to comment.