Re: [Mesa-dev] [PATCH 01/11] util: Add foreach_reverse for dynarray

2018-10-04 Thread Jason Ekstrand
On Fri, Sep 14, 2018 at 10:46 PM Caio Marcelo de Oliveira Filho <
caio.olive...@intel.com> wrote:

> Useful to walk the array removing elements by swapping them with the
> last element.
> ---
>  src/util/u_dynarray.h | 6 ++
>  1 file changed, 6 insertions(+)
>
> diff --git a/src/util/u_dynarray.h b/src/util/u_dynarray.h
> index 6bea481d44b..f74bfc7080b 100644
> --- a/src/util/u_dynarray.h
> +++ b/src/util/u_dynarray.h
> @@ -154,6 +154,12 @@ util_dynarray_trim(struct util_dynarray *buf)
> for (type *elem = (type *)(buf)->data; \
>  elem < (type *)((char *)(buf)->data + (buf)->size); elem++)
>
> +#define util_dynarray_foreach_reverse(buf, type, elem)  \
> +   if ((buf)->size > 0) \
> +  for (type *elem = util_dynarray_top_ptr(buf, type);   \
> +   elem >= (type *)(buf)->data; \
>

I think this works...  I suppose it's theoretically possible that elem--
could underflow.  However, the only way that could happen is if the element
size is really large and the data pointer ends up being really low.


> +   elem--)
>

We could do "elem; elem = elem > (type *)(buf)->data ? elem - 1 : NULL"?
Maybe that's more complicated than it's worth.  Meh.

Reviewed-by: Jason Ekstrand 

--Jason
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


[Mesa-dev] [PATCH 01/11] util: Add foreach_reverse for dynarray

2018-09-14 Thread Caio Marcelo de Oliveira Filho
Useful to walk the array removing elements by swapping them with the
last element.
---
 src/util/u_dynarray.h | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/src/util/u_dynarray.h b/src/util/u_dynarray.h
index 6bea481d44b..f74bfc7080b 100644
--- a/src/util/u_dynarray.h
+++ b/src/util/u_dynarray.h
@@ -154,6 +154,12 @@ util_dynarray_trim(struct util_dynarray *buf)
for (type *elem = (type *)(buf)->data; \
 elem < (type *)((char *)(buf)->data + (buf)->size); elem++)
 
+#define util_dynarray_foreach_reverse(buf, type, elem)  \
+   if ((buf)->size > 0) \
+  for (type *elem = util_dynarray_top_ptr(buf, type);   \
+   elem >= (type *)(buf)->data; \
+   elem--)
+
 #define util_dynarray_delete_unordered(buf, type, v)\
do { \
   unsigned num_elements = (buf)->size / sizeof(type);   \
-- 
2.19.0

___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev