<p dir="ltr">The beginning of the header file should describe what it is.</p>
<p dir="ltr">Marek</p>
<div class="gmail_extra"><br><div class="gmail_quote">On Oct 14, 2016 6:45 AM, "Edward O'Callaghan" <<a href="mailto:funfunctor@folklore1984.net">funfunctor@folklore1984.net</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Too easy, one trivial comment below but either way:<br>
Reviewed-by: Edward O'Callaghan <<a href="mailto:funfunctor@folkore1984.net">funfunctor@folkore1984.net</a>><br>
<br>
P.S. thanks for getting on top of this kind of maintainability stuff so<br>
fast !<br>
<br>
On 10/14/2016 02:16 PM, Dave Airlie wrote:<br>
> From: Dave Airlie <<a href="mailto:airlied@redhat.com">airlied@redhat.com</a>><br>
><br>
> This is ported from anv, both anv and radv can share this.<br>
><br>
> Signed-off-by: Dave Airlie <<a href="mailto:airlied@redhat.com">airlied@redhat.com</a>><br>
> ---<br>
> src/util/Makefile.sources | 4 +-<br>
> src/util/u_vector.c | 98 ++++++++++++++++++++++++++++++<wbr>+++++++++++++++++<br>
> src/util/u_vector.h | 85 ++++++++++++++++++++++++++++++<wbr>++++++++++<br>
> 3 files changed, 186 insertions(+), 1 deletion(-)<br>
> create mode 100644 src/util/u_vector.c<br>
> create mode 100644 src/util/u_vector.h<br>
><br>
> diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources<br>
> index 8b17bcf..b7b1e91 100644<br>
> --- a/src/util/Makefile.sources<br>
> +++ b/src/util/Makefile.sources<br>
> @@ -35,7 +35,9 @@ MESA_UTIL_FILES := \<br>
> strtod.h \<br>
> texcompress_rgtc_tmp.h \<br>
> u_atomic.h \<br>
> - u_endian.h<br>
> + u_endian.h \<br>
> + u_vector.c \<br>
> + u_vector.h<br>
><br>
> MESA_UTIL_GENERATED_FILES = \<br>
> format_srgb.c<br>
> diff --git a/src/util/u_vector.c b/src/util/u_vector.c<br>
> new file mode 100644<br>
> index 0000000..37c4245<br>
> --- /dev/null<br>
> +++ b/src/util/u_vector.c<br>
> @@ -0,0 +1,98 @@<br>
> +/*<br>
> + * Copyright © 2015 Intel Corporation<br>
> + *<br>
> + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> + * copy of this software and associated documentation files (the "Software"),<br>
> + * to deal in the Software without restriction, including without limitation<br>
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> + * and/or sell copies of the Software, and to permit persons to whom the<br>
> + * Software is furnished to do so, subject to the following conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice (including the next<br>
> + * paragraph) shall be included in all copies or substantial portions of the<br>
> + * Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> + * IN THE SOFTWARE.<br>
> + */<br>
> +#include "util/u_vector.h"<br>
> +<br>
> +int<br>
> +u_vector_init(struct u_vector *vector, uint32_t element_size, uint32_t size)<br>
> +{<br>
> + assert(util_is_power_of_two(<wbr>size));<br>
> + assert(element_size < size && util_is_power_of_two(element_<wbr>size));<br>
> +<br>
> + vector->head = 0;<br>
> + vector->tail = 0;<br>
> + vector->element_size = element_size;<br>
> + vector->size = size;<br>
> + vector->data = malloc(size);<br>
> +<br>
> + return vector->data != NULL;<br>
> +}<br>
> +<br>
> +void *<br>
> +u_vector_add(struct u_vector *vector)<br>
> +{<br>
> + uint32_t offset, size, split, src_tail, dst_tail;<br>
> + void *data;<br>
> +<br>
> + if (vector->head - vector->tail == vector->size) {<br>
> + size = vector->size * 2;<br>
> + data = malloc(size);<br>
> + if (data == NULL)<br>
<br>
simplify to (!data)<br>
<br>
> + return NULL;<br>
> + src_tail = vector->tail & (vector->size - 1);<br>
> + dst_tail = vector->tail & (size - 1);<br>
> + if (src_tail == 0) {<br>
> + /* Since we know that the vector is full, this means that it's<br>
> + * linear from start to end so we can do one copy.<br>
> + */<br>
> + memcpy((char *)data + dst_tail, vector->data, vector->size);<br>
> + } else {<br>
> + /* In this case, the vector is split into two pieces and we have<br>
> + * to do two copies. We have to be careful to make sure each<br>
> + * piece goes to the right locations. Thanks to the change in<br>
> + * size, it may or may not still wrap around.<br>
> + */<br>
> + split = u_align_u32(vector->tail, vector->size);<br>
> + assert(vector->tail <= split && split < vector->head);<br>
> + memcpy((char *)data + dst_tail, (char *)vector->data + src_tail,<br>
> + split - vector->tail);<br>
> + memcpy((char *)data + (split & (size - 1)), vector->data,<br>
> + vector->head - split);<br>
> + }<br>
> + free(vector->data);<br>
> + vector->data = data;<br>
> + vector->size = size;<br>
> + }<br>
> +<br>
> + assert(vector->head - vector->tail < vector->size);<br>
> +<br>
> + offset = vector->head & (vector->size - 1);<br>
> + vector->head += vector->element_size;<br>
> +<br>
> + return (char *)vector->data + offset;<br>
> +}<br>
> +<br>
> +void *<br>
> +u_vector_remove(struct u_vector *vector)<br>
> +{<br>
> + uint32_t offset;<br>
> +<br>
> + if (vector->head == vector->tail)<br>
> + return NULL;<br>
> +<br>
> + assert(vector->head - vector->tail <= vector->size);<br>
> +<br>
> + offset = vector->tail & (vector->size - 1);<br>
> + vector->tail += vector->element_size;<br>
> +<br>
> + return (char *)vector->data + offset;<br>
> +}<br>
> diff --git a/src/util/u_vector.h b/src/util/u_vector.h<br>
> new file mode 100644<br>
> index 0000000..ea52837<br>
> --- /dev/null<br>
> +++ b/src/util/u_vector.h<br>
> @@ -0,0 +1,85 @@<br>
> +/*<br>
> + * Copyright © 2015 Intel Corporation<br>
> + *<br>
> + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> + * copy of this software and associated documentation files (the "Software"),<br>
> + * to deal in the Software without restriction, including without limitation<br>
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> + * and/or sell copies of the Software, and to permit persons to whom the<br>
> + * Software is furnished to do so, subject to the following conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice (including the next<br>
> + * paragraph) shall be included in all copies or substantial portions of the<br>
> + * Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> + * IN THE SOFTWARE.<br>
> + */<br>
> +#ifndef U_VECTOR_H<br>
> +#define U_VECTOR_H<br>
> +<br>
> +#include <stdint.h><br>
> +#include <stdlib.h><br>
> +#include "util/u_math.h"<br>
> +#include "util/macros.h"<br>
> +<br>
> +static inline uint32_t<br>
> +u_align_u32(uint32_t v, uint32_t a)<br>
> +{<br>
> + assert(a != 0 && a == (a & -a));<br>
> + return (v + a - 1) & ~(a - 1);<br>
> +}<br>
> +<br>
> +struct u_vector {<br>
> + uint32_t head;<br>
> + uint32_t tail;<br>
> + uint32_t element_size;<br>
> + uint32_t size;<br>
> + void *data;<br>
> +};<br>
> +<br>
> +int u_vector_init(struct u_vector *queue, uint32_t element_size, uint32_t size);<br>
> +void *u_vector_add(struct u_vector *queue);<br>
> +void *u_vector_remove(struct u_vector *queue);<br>
> +<br>
> +static inline int<br>
> +u_vector_length(struct u_vector *queue)<br>
> +{<br>
> + return (queue->head - queue->tail) / queue->element_size;<br>
> +}<br>
> +<br>
> +static inline void *<br>
> +u_vector_head(struct u_vector *vector)<br>
> +{<br>
> + assert(vector->tail < vector->head);<br>
> + return (void *)((char *)vector->data +<br>
> + ((vector->head - vector->element_size) &<br>
> + (vector->size - 1)));<br>
> +}<br>
> +<br>
> +static inline void *<br>
> +u_vector_tail(struct u_vector *vector)<br>
> +{<br>
> + return (void *)((char *)vector->data + (vector->tail & (vector->size - 1)));<br>
> +}<br>
> +<br>
> +static inline void<br>
> +u_vector_finish(struct u_vector *queue)<br>
> +{<br>
> + free(queue->data);<br>
> +}<br>
> +<br>
> +#define u_vector_foreach(elem, queue) \<br>
> + static_assert(__builtin_types_<wbr>compatible_p(__typeof__(queue)<wbr>, struct u_vector *), ""); \<br>
> + for (uint32_t __u_vector_offset = (queue)->tail; \<br>
> + elem = (queue)->data + (__u_vector_offset & ((queue)->size - 1)), __u_vector_offset < (queue)->head; \<br>
> + __u_vector_offset += (queue)->element_size)<br>
> +<br>
> +<br>
> +#endif<br>
> +<br>
><br>
<br>
<br>______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
<br></blockquote></div></div>