util: Port nir_array functionality to u_dynarray
authorThomas Helland <thomashelland90@gmail.com>
Thu, 1 Jun 2017 21:15:43 +0000 (23:15 +0200)
committerMarek Olšák <marek.olsak@amd.com>
Wed, 7 Jun 2017 19:07:24 +0000 (21:07 +0200)
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
src/gallium/drivers/freedreno/freedreno_batch.c
src/gallium/drivers/nouveau/nv30/nvfx_fragprog.c
src/gallium/drivers/nouveau/nv30/nvfx_vertprog.c
src/gallium/drivers/nouveau/nv50/nv50_context.c
src/gallium/drivers/nouveau/nvc0/nvc0_context.c
src/gallium/state_trackers/va/surface.c
src/util/u_dynarray.h

index 7c7a45b..c2142b5 100644 (file)
@@ -76,14 +76,14 @@ batch_init(struct fd_batch *batch)
        batch->max_scissor.minx = batch->max_scissor.miny = ~0;
        batch->max_scissor.maxx = batch->max_scissor.maxy = 0;
 
-       util_dynarray_init(&batch->draw_patches);
+       util_dynarray_init(&batch->draw_patches, NULL);
 
        if (is_a3xx(ctx->screen))
-               util_dynarray_init(&batch->rbrc_patches);
+               util_dynarray_init(&batch->rbrc_patches, NULL);
 
        assert(batch->resources->entries == 0);
 
-       util_dynarray_init(&batch->samples);
+       util_dynarray_init(&batch->samples, NULL);
 }
 
 struct fd_batch *
index 61a5701..278a8a4 100644 (file)
@@ -1119,7 +1119,7 @@ _nvfx_fragprog_translate(uint16_t oclass, struct nv30_fragprog *fp)
       goto out_err;
 
    tgsi_parse_init(&parse, fp->pipe.tokens);
-   util_dynarray_init(&insns);
+   util_dynarray_init(&insns, NULL);
 
    while (!tgsi_parse_end_of_tokens(&parse)) {
       tgsi_parse_token(&parse);
index 03d711a..bec9975 100644 (file)
@@ -998,7 +998,7 @@ _nvfx_vertprog_translate(uint16_t oclass, struct nv30_vertprog *vp)
       vpc->cvtx_idx = vpc->hpos_idx;
    }
 
-   util_dynarray_init(&insns);
+   util_dynarray_init(&insns, NULL);
 
    tgsi_parse_init(&parse, vp->pipe.tokens);
    while (!tgsi_parse_end_of_tokens(&parse)) {
index 1ca6a0a..6124343 100644 (file)
@@ -377,7 +377,7 @@ nv50_create(struct pipe_screen *pscreen, void *priv, unsigned ctxflags)
 
    nv50->base.scratch.bo_size = 2 << 20;
 
-   util_dynarray_init(&nv50->global_residents);
+   util_dynarray_init(&nv50->global_residents, NULL);
 
    return pipe;
 
index 59edd3d..d5ef585 100644 (file)
@@ -461,7 +461,7 @@ nvc0_create(struct pipe_screen *pscreen, void *priv, unsigned ctxflags)
 
    memset(nvc0->tex_handles, ~0, sizeof(nvc0->tex_handles));
 
-   util_dynarray_init(&nvc0->global_residents);
+   util_dynarray_init(&nvc0->global_residents, NULL);
 
    return pipe;
 
index c7d6ef7..f968e9e 100644 (file)
@@ -749,7 +749,7 @@ vlVaCreateSurfaces2(VADriverContextP ctx, unsigned int format,
          assert(0);
       }
 
-      util_dynarray_init(&surf->subpics);
+      util_dynarray_init(&surf->subpics, NULL);
       surfaces[i] = handle_table_add(drv->htab, surf);
       if (!surfaces[i]) {
          vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
index 9143c5a..ad3889a 100644 (file)
@@ -28,6 +28,7 @@
 #define U_DYNARRAY_H
 
 #include <stdlib.h>
+#include "ralloc.h"
 
 /* A zero-initialized version of this is guaranteed to represent an
  * empty array.
  */
 struct util_dynarray
 {
+   void *mem_ctx;
    void *data;
    unsigned size;
    unsigned capacity;
 };
 
 static inline void
-util_dynarray_init(struct util_dynarray *buf)
+util_dynarray_init(struct util_dynarray *buf, void *mem_ctx)
 {
    memset(buf, 0, sizeof(*buf));
+   buf->mem_ctx = mem_ctx;
 }
 
 static inline void
 util_dynarray_fini(struct util_dynarray *buf)
 {
    if (buf->data) {
-      free(buf->data);
-      util_dynarray_init(buf);
+      if (buf->mem_ctx) {
+         ralloc_free(buf->data);
+      } else {
+         free(buf->data);
+      }
+      util_dynarray_init(buf, buf->mem_ctx);
    }
 }
 
+#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)
 {
    void *p;
    if (newsize > buf->capacity) {
-      unsigned newcap = buf->capacity << 1;
-      if (newsize > newcap)
-             newcap = newsize;
-      buf->data = realloc(buf->data, newcap);
-      buf->capacity = newcap;
+      if (buf->capacity == 0)
+         buf->capacity = DYN_ARRAY_INITIAL_SIZE;
+
+      while (newsize > buf->capacity)
+         buf->capacity *= 2;
+
+      if (buf->mem_ctx) {
+         buf->data = reralloc_size(buf->mem_ctx, buf->data, buf->capacity);
+      } else {
+         buf->data = realloc(buf->data, buf->capacity);
+      }
    }
 
    p = (void *)((char *)buf->data + buf->size);
@@ -87,10 +102,18 @@ util_dynarray_trim(struct util_dynarray *buf)
 {
    if (buf->size != buf->capacity) {
       if (buf->size) {
-         buf->data = realloc(buf->data, buf->size);
+         if (buf->mem_ctx) {
+            reralloc_size(buf->mem_ctx, buf->data, buf->size);
+         } else {
+            buf->data = realloc(buf->data, buf->size);
+         }
          buf->capacity = buf->size;
       } else {
-         free(buf->data);
+         if (buf->mem_ctx) {
+            ralloc_free(buf->data);
+         } else {
+            free(buf->data);
+         }
          buf->data = 0;
          buf->capacity = 0;
       }
@@ -107,5 +130,9 @@ util_dynarray_trim(struct util_dynarray *buf)
 #define util_dynarray_begin(buf) ((buf)->data)
 #define util_dynarray_end(buf) ((void*)util_dynarray_element((buf), char, (buf)->size))
 
+#define util_dynarray_foreach(buf, type, elem) \
+   for (type *elem = (type *)(buf)->data; \
+        elem < (type *)((char *)(buf)->data + (buf)->size); elem++)
+
 #endif /* U_DYNARRAY_H */