[Mesa-dev] [PATCH 3/5] nir/array: add a few more helpers

Connor Abbott cwabbott0 at gmail.com
Sat Nov 14 18:59:42 PST 2015


Signed-off-by: Connor Abbott <cwabbott0 at gmail.com>
---
 src/glsl/nir/nir_array.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/glsl/nir/nir_array.h b/src/glsl/nir/nir_array.h
index 1db4e8c..d704119 100644
--- a/src/glsl/nir/nir_array.h
+++ b/src/glsl/nir/nir_array.h
@@ -84,13 +84,34 @@ nir_array_grow(nir_array *arr, size_t additional)
    return ptr;
 }
 
+static inline void *
+nir_array_shrink(nir_array *arr, size_t less)
+{
+   arr->size -= less;
+   assert(arr->size >= 0);
+   return (void *)((char *)arr->data + arr->size);
+}
+
 #define nir_array_add(arr, type, elem) \
    *(type *)nir_array_grow(arr, sizeof(type)) = (elem)
 
+#define nir_array_pop(arr, type) \
+   (*(type *)(nir_array_shrink(arr, sizeof(type))))
+
+#define nir_array_first(arr, type) \
+   ((type *)(arr)->data)
+
+#define nir_array_size(arr, type) \
+   ((arr)->size / sizeof(type))
+
 #define nir_array_foreach(arr, type, elem) \
    for (type *elem = (type *)(arr)->data; \
         elem < (type *)((char *)(arr)->data + (arr)->size); elem++)
 
+#define nir_array_foreach_reverse(arr, type, elem) \
+   for (type *elem = (type *)((char*)(arr)->data + (arr)->size) - 1; \
+        (arr)->data && elem >= (type *)((arr)->data); elem--)
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
-- 
2.4.3



More information about the mesa-dev mailing list