render/vulkan: use wl_array for wlr_vk_shared_buffer.allocs

Avoids the need to open-code the realloc() logic.
This commit is contained in:
Simon Ser 2022-11-07 19:47:22 +01:00 committed by Kirill Primak
parent 388de59df3
commit 6c31f3b078
2 changed files with 25 additions and 33 deletions

View file

@ -275,10 +275,7 @@ struct wlr_vk_shared_buffer {
VkBuffer buffer; VkBuffer buffer;
VkDeviceMemory memory; VkDeviceMemory memory;
VkDeviceSize buf_size; VkDeviceSize buf_size;
struct wl_array allocs; // struct wlr_vk_allocation
size_t allocs_size;
size_t allocs_capacity;
struct wlr_vk_allocation *allocs;
}; };
// Suballocated range on a buffer. // Suballocated range on a buffer.

View file

@ -170,12 +170,12 @@ static void shared_buffer_destroy(struct wlr_vk_renderer *r,
return; return;
} }
if (buffer->allocs_size > 0) { if (buffer->allocs.size > 0) {
wlr_log(WLR_ERROR, "shared_buffer_finish: %d allocations left", wlr_log(WLR_ERROR, "shared_buffer_finish: %zu allocations left",
(unsigned) buffer->allocs_size); buffer->allocs.size / sizeof(struct wlr_vk_allocation));
} }
free(buffer->allocs); wl_array_release(&buffer->allocs);
if (buffer->buffer) { if (buffer->buffer) {
vkDestroyBuffer(r->dev->dev, buffer->buffer, NULL); vkDestroyBuffer(r->dev->dev, buffer->buffer, NULL);
} }
@ -190,7 +190,7 @@ static void shared_buffer_destroy(struct wlr_vk_renderer *r,
static void release_stage_allocations(struct wlr_vk_renderer *renderer) { static void release_stage_allocations(struct wlr_vk_renderer *renderer) {
struct wlr_vk_shared_buffer *buf; struct wlr_vk_shared_buffer *buf;
wl_list_for_each(buf, &renderer->stage.buffers, link) { wl_list_for_each(buf, &renderer->stage.buffers, link) {
buf->allocs_size = 0u; buf->allocs.size = 0;
} }
} }
@ -202,8 +202,10 @@ struct wlr_vk_buffer_span vulkan_get_stage_span(struct wlr_vk_renderer *r,
struct wlr_vk_shared_buffer *buf; struct wlr_vk_shared_buffer *buf;
wl_list_for_each_reverse(buf, &r->stage.buffers, link) { wl_list_for_each_reverse(buf, &r->stage.buffers, link) {
VkDeviceSize start = 0u; VkDeviceSize start = 0u;
if (buf->allocs_size > 0) { if (buf->allocs.size > 0) {
struct wlr_vk_allocation *last = &buf->allocs[buf->allocs_size - 1]; const struct wlr_vk_allocation *allocs = buf->allocs.data;
size_t allocs_len = buf->allocs.size / sizeof(struct wlr_vk_allocation);
const struct wlr_vk_allocation *last = &allocs[allocs_len - 1];
start = last->start + last->size; start = last->start + last->size;
} }
@ -212,22 +214,16 @@ struct wlr_vk_buffer_span vulkan_get_stage_span(struct wlr_vk_renderer *r,
continue; continue;
} }
++buf->allocs_size; struct wlr_vk_allocation *a = wl_array_add(&buf->allocs, sizeof(*a));
if (buf->allocs_size > buf->allocs_capacity) { if (a == NULL) {
buf->allocs_capacity = buf->allocs_size * 2; wlr_log_errno(WLR_ERROR, "Allocation failed");
void *allocs = realloc(buf->allocs, goto error_alloc;
buf->allocs_capacity * sizeof(*buf->allocs));
if (!allocs) {
wlr_log_errno(WLR_ERROR, "Allocation failed");
goto error_alloc;
}
buf->allocs = allocs;
} }
struct wlr_vk_allocation *a = &buf->allocs[buf->allocs_size - 1]; *a = (struct wlr_vk_allocation){
a->start = start; .start = start,
a->size = size; .size = size,
};
return (struct wlr_vk_buffer_span) { return (struct wlr_vk_buffer_span) {
.buffer = buf, .buffer = buf,
.alloc = *a, .alloc = *a,
@ -300,9 +296,8 @@ struct wlr_vk_buffer_span vulkan_get_stage_span(struct wlr_vk_renderer *r,
goto error; goto error;
} }
size_t start_count = 8u; struct wlr_vk_allocation *a = wl_array_add(&buf->allocs, sizeof(*a));
buf->allocs = calloc(start_count, sizeof(*buf->allocs)); if (a == NULL) {
if (!buf->allocs) {
wlr_log_errno(WLR_ERROR, "Allocation failed"); wlr_log_errno(WLR_ERROR, "Allocation failed");
goto error; goto error;
} }
@ -311,13 +306,13 @@ struct wlr_vk_buffer_span vulkan_get_stage_span(struct wlr_vk_renderer *r,
buf->buf_size = bsize; buf->buf_size = bsize;
wl_list_insert(&r->stage.buffers, &buf->link); wl_list_insert(&r->stage.buffers, &buf->link);
buf->allocs_capacity = start_count; *a = (struct wlr_vk_allocation){
buf->allocs_size = 1u; .start = 0,
buf->allocs[0].start = 0u; .size = size,
buf->allocs[0].size = size; };
return (struct wlr_vk_buffer_span) { return (struct wlr_vk_buffer_span) {
.buffer = buf, .buffer = buf,
.alloc = buf->allocs[0], .alloc = *a,
}; };
error: error: