diff options
author | Jason Ekstrand <[email protected]> | 2020-04-21 15:51:01 -0500 |
---|---|---|
committer | Marge Bot <[email protected]> | 2020-05-04 14:06:27 +0000 |
commit | 32f20783a512129fc2d7dd8a11ffa8670cef0068 (patch) | |
tree | cefa1d84336ad9835127e59da069782e1fd2e983 | |
parent | 7628585dd709f3ffb18c7ca134fff1f375ad7b6a (diff) |
vulkan: Add run-time object type asserts in handle casts
Reviewed-by: Lionel Landwerlin <[email protected]>
Acked-by: Kristian H. Kristensen <[email protected]>
Acked-by: Bas Nieuwenhuizen <[email protected]>
Reviewed-by: Samuel Pitoiset <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4690>
-rw-r--r-- | src/vulkan/util/vk_object.c | 3 | ||||
-rw-r--r-- | src/vulkan/util/vk_object.h | 21 |
2 files changed, 22 insertions, 2 deletions
diff --git a/src/vulkan/util/vk_object.c b/src/vulkan/util/vk_object.c index d6965158914..f84a68034b0 100644 --- a/src/vulkan/util/vk_object.c +++ b/src/vulkan/util/vk_object.c @@ -29,6 +29,9 @@ vk_object_base_init(UNUSED struct vk_device *device, UNUSED VkObjectType obj_type) { base->_loader_data.loaderMagic = ICD_LOADER_MAGIC; +#ifndef NDEBUG + base->type = obj_type; +#endif } void diff --git a/src/vulkan/util/vk_object.h b/src/vulkan/util/vk_object.h index f91cc1640a0..bd32f1ad681 100644 --- a/src/vulkan/util/vk_object.h +++ b/src/vulkan/util/vk_object.h @@ -36,6 +36,9 @@ struct vk_device; struct vk_object_base { VK_LOADER_DATA _loader_data; +#ifndef NDEBUG + VkObjectType type; +#endif }; void vk_object_base_init(UNUSED struct vk_device *device, @@ -43,6 +46,13 @@ void vk_object_base_init(UNUSED struct vk_device *device, UNUSED VkObjectType obj_type); void vk_object_base_finish(UNUSED struct vk_object_base *base); +static inline void +vk_object_base_assert_valid(ASSERTED struct vk_object_base *base, + ASSERTED VkObjectType obj_type) +{ + assert(base == NULL || base->type == obj_type); +} + struct vk_device { struct vk_object_base base; @@ -59,13 +69,16 @@ void vk_device_finish(struct vk_device *device); static inline struct __driver_type * \ __driver_type ## _from_handle(__VkType _handle) \ { \ + struct vk_object_base *base = (struct vk_object_base *)_handle; \ + vk_object_base_assert_valid(base, __VK_TYPE); \ STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0); \ - return (struct __driver_type *) _handle; \ + return (struct __driver_type *) base; \ } \ \ static inline __VkType \ __driver_type ## _to_handle(struct __driver_type *_obj) \ { \ + vk_object_base_assert_valid(&_obj->__base, __VK_TYPE); \ return (__VkType) _obj; \ } @@ -73,13 +86,17 @@ void vk_device_finish(struct vk_device *device); static inline struct __driver_type * \ __driver_type ## _from_handle(__VkType _handle) \ { \ + struct vk_object_base *base = \ + (struct vk_object_base *)(uintptr_t)_handle; \ + vk_object_base_assert_valid(base, __VK_TYPE); \ STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0); \ - return (struct __driver_type *)(uintptr_t) _handle; \ + return (struct __driver_type *)base; \ } \ \ static inline __VkType \ __driver_type ## _to_handle(struct __driver_type *_obj) \ { \ + vk_object_base_assert_valid(&_obj->__base, __VK_TYPE); \ return (__VkType)(uintptr_t) _obj; \ } |