diff --git a/src/venus/vkr_common.h b/src/venus/vkr_common.h index 9137245..2e0736b 100644 --- a/src/venus/vkr_common.h +++ b/src/venus/vkr_common.h @@ -334,4 +334,21 @@ vkr_is_recognized_object_type(VkObjectType type) } } +static inline void * +vkr_object_alloc(size_t size, VkObjectType type, vkr_object_id id) +{ + assert(size >= sizeof(struct vkr_object)); + assert(vkr_is_recognized_object_type(type)); + + struct vkr_object *obj = calloc(1, size); + if (!obj) + return NULL; + + /* obj is only half-initialized */ + obj->type = type; + obj->id = id; + + return obj; +} + #endif /* VKR_COMMON_H */ diff --git a/src/venus/vkr_physical_device.c b/src/venus/vkr_physical_device.c index a9afb3a..a7fb96f 100644 --- a/src/venus/vkr_physical_device.c +++ b/src/venus/vkr_physical_device.c @@ -197,14 +197,13 @@ vkr_dispatch_vkEnumeratePhysicalDevices(struct vn_dispatch_context *dispatch, continue; } - physical_dev = calloc(1, sizeof(*physical_dev)); + physical_dev = + vkr_object_alloc(sizeof(*physical_dev), VK_OBJECT_TYPE_PHYSICAL_DEVICE, id); if (!physical_dev) { args->ret = VK_ERROR_OUT_OF_HOST_MEMORY; break; } - physical_dev->base.type = VK_OBJECT_TYPE_PHYSICAL_DEVICE; - physical_dev->base.id = id; physical_dev->base.handle.physical_device = instance->physical_device_handles[i]; vkr_physical_device_init_properties(physical_dev); diff --git a/src/venus/vkr_queue.c b/src/venus/vkr_queue.c index 4d7f011..6704d84 100644 --- a/src/venus/vkr_queue.c +++ b/src/venus/vkr_queue.c @@ -240,12 +240,11 @@ vkr_queue_create(struct vkr_context *ctx, struct vkr_queue *queue; int ret; - queue = calloc(1, sizeof(*queue)); + /* id is set to 0 until vkr_queue_assign_object_id */ + queue = vkr_object_alloc(sizeof(*queue), VK_OBJECT_TYPE_QUEUE, 0); if (!queue) return NULL; - queue->base.type = VK_OBJECT_TYPE_QUEUE; - /* queue->base.id is not assigned until vkr_queue_assign_object_id */ queue->base.handle.queue = handle; queue->context = ctx;