[Mesa-dev] [PATCH] anv: Add a pair of foreach_struct macros

Jason Ekstrand jason at jlekstrand.net
Tue Feb 14 23:16:05 UTC 2017


These make iterating over Vulkan extension structs much cleaner.
---
 src/intel/vulkan/anv_device.c  | 26 ++++++++++++--------------
 src/intel/vulkan/anv_formats.c | 14 ++++++--------
 src/intel/vulkan/anv_private.h |  8 ++++++++
 3 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index 46b83a3..7c2f816 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -511,10 +511,10 @@ void anv_GetPhysicalDeviceFeatures2KHR(
 {
    anv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
 
-   for (struct anv_common *c = pFeatures->pNext; c != NULL; c = c->pNext) {
-      switch (c->sType) {
+   anv_foreach_struct(ext, pFeatures->pNext) {
+      switch (ext->sType) {
       default:
-         anv_debug_ignored_stype(c->sType);
+         anv_debug_ignored_stype(ext->sType);
          break;
       }
    }
@@ -669,10 +669,10 @@ void anv_GetPhysicalDeviceProperties2KHR(
 {
    anv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
 
-   for (struct anv_common *c = pProperties->pNext; c != NULL; c = c->pNext) {
-      switch (c->sType) {
+   anv_foreach_struct(ext, pProperties->pNext) {
+      switch (ext->sType) {
       default:
-         anv_debug_ignored_stype(c->sType);
+         anv_debug_ignored_stype(ext->sType);
          break;
       }
    }
@@ -746,11 +746,10 @@ void anv_GetPhysicalDeviceQueueFamilyProperties2KHR(
    anv_get_queue_family_properties(phys_dev,
          &pQueueFamilyProperties->queueFamilyProperties);
 
-   for (struct anv_common *c = pQueueFamilyProperties->pNext;
-        c != NULL; c = c->pNext) {
-      switch (c->sType) {
+   anv_foreach_struct(ext, pQueueFamilyProperties->pNext) {
+      switch (ext->sType) {
       default:
-         anv_debug_ignored_stype(c->sType);
+         anv_debug_ignored_stype(ext->sType);
          break;
       }
    }
@@ -815,11 +814,10 @@ void anv_GetPhysicalDeviceMemoryProperties2KHR(
    anv_GetPhysicalDeviceMemoryProperties(physicalDevice,
                                          &pMemoryProperties->memoryProperties);
 
-   for (struct anv_common *c = pMemoryProperties->pNext;
-        c != NULL; c = c->pNext) {
-      switch (c->sType) {
+   anv_foreach_struct(ext, pMemoryProperties->pNext) {
+      switch (ext->sType) {
       default:
-         anv_debug_ignored_stype(c->sType);
+         anv_debug_ignored_stype(ext->sType);
          break;
       }
    }
diff --git a/src/intel/vulkan/anv_formats.c b/src/intel/vulkan/anv_formats.c
index 0e84960..735d518 100644
--- a/src/intel/vulkan/anv_formats.c
+++ b/src/intel/vulkan/anv_formats.c
@@ -470,11 +470,10 @@ void anv_GetPhysicalDeviceFormatProperties2KHR(
    anv_GetPhysicalDeviceFormatProperties(physicalDevice, format,
                                          &pFormatProperties->formatProperties);
 
-   for (struct anv_common *c = pFormatProperties->pNext;
-        c != NULL; c = c->pNext) {
-      switch (c->sType) {
+   anv_foreach_struct(ext, pFormatProperties->pNext) {
+      switch (ext->sType) {
       default:
-         anv_debug_ignored_stype(c->sType);
+         anv_debug_ignored_stype(ext->sType);
          break;
       }
    }
@@ -668,11 +667,10 @@ VkResult anv_GetPhysicalDeviceImageFormatProperties2KHR(
    if (result != VK_SUCCESS)
       return result;
 
-   for (struct anv_common *c = pImageFormatProperties->pNext;
-        c != NULL; c = c->pNext) {
-      switch (c->sType) {
+   anv_foreach_struct(ext, pImageFormatProperties->pNext) {
+      switch (ext->sType) {
       default:
-         anv_debug_ignored_stype(c->sType);
+         anv_debug_ignored_stype(ext->sType);
          break;
       }
    }
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index ec791a4..527f20f 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -1881,6 +1881,14 @@ struct anv_common {
     struct anv_common *pNext;
 };
 
+#define anv_foreach_struct(__iter, __start)                                \
+   for (struct anv_common *__iter = (struct anv_common *)(__start);        \
+        __iter; __iter = __iter->pNext)
+
+#define anv_foreach_struct_const(__iter, __start)                          \
+   for (const struct anv_common *__iter = (struct anv_common *)(__start);  \
+        __iter; __iter = __iter->pNext)
+
 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType)                      \
                                                                            \
    static inline struct __anv_type *                                       \
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list