[Mesa-dev] [PATCH v2 27/64] gallium/util: add new util_dynarray_{shrink, delete} helpers

Samuel Pitoiset samuel.pitoiset at gmail.com
Tue May 30 20:35:58 UTC 2017


These helpers will be used for handling dynamic arrays of resident
texture/image handles.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset at gmail.com>
---
 src/gallium/auxiliary/util/u_dynarray.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/gallium/auxiliary/util/u_dynarray.h b/src/gallium/auxiliary/util/u_dynarray.h
index 7b7a093d82..d6f3d5c901 100644
--- a/src/gallium/auxiliary/util/u_dynarray.h
+++ b/src/gallium/auxiliary/util/u_dynarray.h
@@ -109,6 +109,26 @@ util_dynarray_trim(struct util_dynarray *buf)
 #define util_dynarray_element(buf, type, idx) ((type*)(buf)->data + (idx))
 #define util_dynarray_begin(buf) ((buf)->data)
 #define util_dynarray_end(buf) ((void*)util_dynarray_element((buf), char, (buf)->size))
+#define util_dynarray_shrink(buf, type, begin, end)                        \
+   do {                                                                    \
+      void *__begin = util_dynarray_element((buf), type, (begin));         \
+      void *__end = util_dynarray_element((buf), type, (end));             \
+      if ((end) * sizeof(type) < (buf)->size)                              \
+         memmove(__begin, __end, (buf)->size - ((end) * sizeof(type)));    \
+      (buf)->size = (buf)->size - ((end) - (begin)) * sizeof(type);        \
+   } while (0)
+#define util_dynarray_delete(buf, type, v)                           \
+   do {                                                              \
+      unsigned num_elements = (buf)->size / sizeof(type);            \
+      unsigned i;                                                    \
+      for (i = 0; i < num_elements; i++) {                           \
+         type __v = *util_dynarray_element((buf), type, (i));        \
+         if (v == __v) {                                             \
+            util_dynarray_shrink((buf), type, i, i + 1);             \
+            break;                                                   \
+         }                                                           \
+      }                                                              \
+   } while (0)
 
 #endif /* U_DYNARRAY_H */
 
-- 
2.13.0



More information about the mesa-dev mailing list