util: Port nir_array functionality to u_dynarray

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
This commit is contained in:
Thomas Helland 2017-06-01 23:15:43 +02:00 committed by Marek Olšák
parent 07653f159f
commit 9cb42ae997
7 changed files with 45 additions and 18 deletions

View 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 *

View 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);

View 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)) {

View 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;

View 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;

View 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;

View 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.
@ -37,37 +38,51 @@
*/
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 */