[Lima] [Mesa-dev] [PATCH 3/9] u_dynarray: add util_dynarray_enlarge

Caio Marcelo de Oliveira Filho caio.oliveira at intel.com
Sat Mar 16 03:26:47 UTC 2019


On Sat, Mar 16, 2019 at 09:28:48AM +0800, Qiang Yu wrote:
> This is for the case that user only know a max size
> it wants to append to the array and enlarge the array
> capacity before writing into it.
> 
> Signed-off-by: Qiang Yu <yuq825 at gmail.com>
> ---
>  src/util/u_dynarray.h | 19 +++++++++++++++----
>  1 file changed, 15 insertions(+), 4 deletions(-)
> 
> diff --git a/src/util/u_dynarray.h b/src/util/u_dynarray.h
> index 9bed2b9c25c..c5217082b7f 100644
> --- a/src/util/u_dynarray.h
> +++ b/src/util/u_dynarray.h
> @@ -77,11 +77,9 @@ util_dynarray_clear(struct util_dynarray *buf)
>  
>  #define DYN_ARRAY_INITIAL_SIZE 64
>  
> -/* use util_dynarray_trim to reduce the allocated storage */
>  static inline void *
> -util_dynarray_resize(struct util_dynarray *buf, unsigned newsize)
> +util_dynarray_ensure_cap(struct util_dynarray *buf, unsigned newsize)

Rename the parameter to newcap to avoid confusion in the body.

>  {
> -   void *p;
>     if (newsize > buf->capacity) {
>        if (buf->capacity == 0)
>           buf->capacity = DYN_ARRAY_INITIAL_SIZE;
> @@ -96,7 +94,20 @@ util_dynarray_resize(struct util_dynarray *buf, unsigned newsize)
>        }
>     }
>  
> -   p = (void *)((char *)buf->data + buf->size);
> +   return (void *)((char *)buf->data + buf->size);
> +}
> +
> +static inline void *
> +util_dynarray_enlarge(struct util_dynarray *buf, int diff)
> +{
> +   return util_dynarray_ensure_cap(buf, buf->size + diff);
> +}

We already have util_dynarray_grow for size, so enlarge can be
confusing.  What do you think about calling this one
util_dynarray_grow_cap?


> +
> +/* use util_dynarray_trim to reduce the allocated storage */
> +static inline void *
> +util_dynarray_resize(struct util_dynarray *buf, unsigned newsize)
> +{
> +   void *p = util_dynarray_ensure_cap(buf, newsize);
>     buf->size = newsize;
>  
>     return p;
> -- 
> 2.17.1
> 
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev


	Caio


More information about the lima mailing list