2010-04-13 05:05:12 +01:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright 2010 Luca Barbieri
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial
|
|
|
|
* portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#ifndef U_DYNARRAY_H
|
|
|
|
#define U_DYNARRAY_H
|
|
|
|
|
2017-06-01 21:38:59 +01:00
|
|
|
#include <stdlib.h>
|
2018-01-17 04:49:09 +00:00
|
|
|
#include <string.h>
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
#include <limits.h>
|
2017-06-01 22:15:43 +01:00
|
|
|
#include "ralloc.h"
|
2010-04-13 05:05:12 +01:00
|
|
|
|
2017-06-03 18:59:07 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2010-04-13 05:05:12 +01:00
|
|
|
/* A zero-initialized version of this is guaranteed to represent an
|
|
|
|
* empty array.
|
|
|
|
*
|
|
|
|
* Also, size <= capacity and data != 0 if and only if capacity != 0
|
|
|
|
* capacity will always be the allocation size of data
|
|
|
|
*/
|
|
|
|
struct util_dynarray
|
|
|
|
{
|
2017-06-01 22:15:43 +01:00
|
|
|
void *mem_ctx;
|
2010-04-13 05:05:12 +01:00
|
|
|
void *data;
|
|
|
|
unsigned size;
|
|
|
|
unsigned capacity;
|
|
|
|
};
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline void
|
2017-06-01 22:15:43 +01:00
|
|
|
util_dynarray_init(struct util_dynarray *buf, void *mem_ctx)
|
2010-04-13 05:05:12 +01:00
|
|
|
{
|
|
|
|
memset(buf, 0, sizeof(*buf));
|
2017-06-01 22:15:43 +01:00
|
|
|
buf->mem_ctx = mem_ctx;
|
2010-04-13 05:05:12 +01:00
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline void
|
2010-04-13 05:05:12 +01:00
|
|
|
util_dynarray_fini(struct util_dynarray *buf)
|
|
|
|
{
|
2017-06-01 21:38:59 +01:00
|
|
|
if (buf->data) {
|
2017-06-01 22:15:43 +01:00
|
|
|
if (buf->mem_ctx) {
|
|
|
|
ralloc_free(buf->data);
|
|
|
|
} else {
|
|
|
|
free(buf->data);
|
|
|
|
}
|
|
|
|
util_dynarray_init(buf, buf->mem_ctx);
|
2010-04-13 05:05:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 12:55:09 +01:00
|
|
|
static inline void
|
|
|
|
util_dynarray_clear(struct util_dynarray *buf)
|
|
|
|
{
|
|
|
|
buf->size = 0;
|
|
|
|
}
|
|
|
|
|
2017-06-01 22:15:43 +01:00
|
|
|
#define DYN_ARRAY_INITIAL_SIZE 64
|
|
|
|
|
2019-05-13 15:58:07 +01:00
|
|
|
MUST_CHECK static inline void *
|
2018-02-19 13:44:44 +00:00
|
|
|
util_dynarray_ensure_cap(struct util_dynarray *buf, unsigned newcap)
|
2010-04-13 05:05:12 +01:00
|
|
|
{
|
2018-02-19 13:44:44 +00:00
|
|
|
if (newcap > buf->capacity) {
|
2019-05-13 15:58:07 +01:00
|
|
|
unsigned capacity = MAX3(DYN_ARRAY_INITIAL_SIZE, buf->capacity * 2, newcap);
|
|
|
|
void *data;
|
2017-06-01 22:15:43 +01:00
|
|
|
|
|
|
|
if (buf->mem_ctx) {
|
2019-05-13 15:58:07 +01:00
|
|
|
data = reralloc_size(buf->mem_ctx, buf->data, capacity);
|
2017-06-01 22:15:43 +01:00
|
|
|
} else {
|
2019-05-13 15:58:07 +01:00
|
|
|
data = realloc(buf->data, capacity);
|
2017-06-01 22:15:43 +01:00
|
|
|
}
|
2019-05-13 15:58:07 +01:00
|
|
|
if (!data)
|
2021-08-02 22:39:27 +01:00
|
|
|
return NULL;
|
2019-05-13 15:58:07 +01:00
|
|
|
|
|
|
|
buf->data = data;
|
|
|
|
buf->capacity = capacity;
|
2010-04-13 05:05:12 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 13:44:44 +00:00
|
|
|
return (void *)((char *)buf->data + buf->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use util_dynarray_trim to reduce the allocated storage */
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
MUST_CHECK static inline void *
|
|
|
|
util_dynarray_resize_bytes(struct util_dynarray *buf, unsigned nelts, size_t eltsize)
|
2018-02-19 13:44:44 +00:00
|
|
|
{
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
if (unlikely(nelts > UINT_MAX / eltsize))
|
2021-08-02 22:39:27 +01:00
|
|
|
return NULL;
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
|
|
|
|
unsigned newsize = nelts * eltsize;
|
2018-02-19 13:44:44 +00:00
|
|
|
void *p = util_dynarray_ensure_cap(buf, newsize);
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
if (!p)
|
2021-08-02 22:39:27 +01:00
|
|
|
return NULL;
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
|
2010-04-13 05:05:12 +01:00
|
|
|
buf->size = newsize;
|
2017-06-01 21:38:59 +01:00
|
|
|
|
2010-04-13 05:05:12 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:41:19 +01:00
|
|
|
static inline void
|
|
|
|
util_dynarray_clone(struct util_dynarray *buf, void *mem_ctx,
|
|
|
|
struct util_dynarray *from_buf)
|
|
|
|
{
|
|
|
|
util_dynarray_init(buf, mem_ctx);
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
if (util_dynarray_resize_bytes(buf, from_buf->size, 1))
|
|
|
|
memcpy(buf->data, from_buf->data, from_buf->size);
|
2018-08-13 22:41:19 +01:00
|
|
|
}
|
|
|
|
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
MUST_CHECK static inline void *
|
|
|
|
util_dynarray_grow_bytes(struct util_dynarray *buf, unsigned ngrow, size_t eltsize)
|
2010-04-13 07:23:29 +01:00
|
|
|
{
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
unsigned growbytes = ngrow * eltsize;
|
|
|
|
|
|
|
|
if (unlikely(ngrow > (UINT_MAX / eltsize) ||
|
|
|
|
growbytes > UINT_MAX - buf->size))
|
2021-08-02 22:39:27 +01:00
|
|
|
return NULL;
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
|
|
|
|
unsigned newsize = buf->size + growbytes;
|
|
|
|
void *p = util_dynarray_ensure_cap(buf, newsize);
|
|
|
|
if (!p)
|
2021-08-02 22:39:27 +01:00
|
|
|
return NULL;
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
|
|
|
|
buf->size = newsize;
|
|
|
|
|
|
|
|
return p;
|
2010-04-13 07:23:29 +01:00
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline void
|
2010-04-13 05:05:12 +01:00
|
|
|
util_dynarray_trim(struct util_dynarray *buf)
|
|
|
|
{
|
2010-04-13 08:59:49 +01:00
|
|
|
if (buf->size != buf->capacity) {
|
|
|
|
if (buf->size) {
|
2017-06-01 22:15:43 +01:00
|
|
|
if (buf->mem_ctx) {
|
2017-06-12 20:50:18 +01:00
|
|
|
buf->data = reralloc_size(buf->mem_ctx, buf->data, buf->size);
|
2017-06-01 22:15:43 +01:00
|
|
|
} else {
|
|
|
|
buf->data = realloc(buf->data, buf->size);
|
|
|
|
}
|
2010-04-13 08:59:49 +01:00
|
|
|
buf->capacity = buf->size;
|
2017-06-01 21:38:59 +01:00
|
|
|
} else {
|
2017-06-01 22:15:43 +01:00
|
|
|
if (buf->mem_ctx) {
|
|
|
|
ralloc_free(buf->data);
|
|
|
|
} else {
|
|
|
|
free(buf->data);
|
|
|
|
}
|
2018-10-28 18:04:01 +00:00
|
|
|
buf->data = NULL;
|
2010-04-13 08:59:49 +01:00
|
|
|
buf->capacity = 0;
|
|
|
|
}
|
2010-04-13 07:18:59 +01:00
|
|
|
}
|
2010-04-13 05:05:12 +01:00
|
|
|
}
|
|
|
|
|
u_dynarray: turn util_dynarray_{grow, resize} into element-oriented macros
The main motivation for this change is API ergonomics: most operations
on dynarrays are really on elements, not on bytes, so it's weird to have
grow and resize as the odd operations out.
The secondary motivation is memory safety. Users of the old byte-oriented
functions would often multiply a number of elements with the element size,
which could overflow, and checking for overflow is tedious.
With this change, we only need to implement the overflow checks once.
The checks are cheap: since eltsize is a compile-time constant and the
functions should be inlined, they only add a single comparison and an
unlikely branch.
v2:
- ensure operations are no-op when allocation fails
- in util_dynarray_clone, call resize_bytes with a compile-time constant element size
v3:
- fix iris, lima, panfrost
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
2019-05-13 15:58:08 +01:00
|
|
|
#define util_dynarray_append(buf, type, v) do {type __v = (v); memcpy(util_dynarray_grow_bytes((buf), 1, sizeof(type)), &__v, sizeof(type));} while(0)
|
|
|
|
/* Returns a pointer to the space of the first new element (in case of growth) or NULL on failure. */
|
|
|
|
#define util_dynarray_resize(buf, type, nelts) util_dynarray_resize_bytes(buf, (nelts), sizeof(type))
|
|
|
|
#define util_dynarray_grow(buf, type, ngrow) util_dynarray_grow_bytes(buf, (ngrow), sizeof(type))
|
2010-04-13 05:05:12 +01:00
|
|
|
#define util_dynarray_top_ptr(buf, type) (type*)((char*)(buf)->data + (buf)->size - sizeof(type))
|
|
|
|
#define util_dynarray_top(buf, type) *util_dynarray_top_ptr(buf, type)
|
|
|
|
#define util_dynarray_pop_ptr(buf, type) (type*)((char*)(buf)->data + ((buf)->size -= sizeof(type)))
|
|
|
|
#define util_dynarray_pop(buf, type) *util_dynarray_pop_ptr(buf, type)
|
|
|
|
#define util_dynarray_contains(buf, type) ((buf)->size >= sizeof(type))
|
2010-08-22 23:31:08 +01:00
|
|
|
#define util_dynarray_element(buf, type, idx) ((type*)(buf)->data + (idx))
|
|
|
|
#define util_dynarray_begin(buf) ((buf)->data)
|
|
|
|
#define util_dynarray_end(buf) ((void*)util_dynarray_element((buf), char, (buf)->size))
|
2018-09-12 23:21:20 +01:00
|
|
|
#define util_dynarray_num_elements(buf, type) ((buf)->size / sizeof(type))
|
2010-04-13 05:05:12 +01:00
|
|
|
|
2017-06-01 22:15:43 +01:00
|
|
|
#define util_dynarray_foreach(buf, type, elem) \
|
|
|
|
for (type *elem = (type *)(buf)->data; \
|
|
|
|
elem < (type *)((char *)(buf)->data + (buf)->size); elem++)
|
|
|
|
|
2018-09-12 22:57:35 +01:00
|
|
|
#define util_dynarray_foreach_reverse(buf, type, elem) \
|
|
|
|
if ((buf)->size > 0) \
|
|
|
|
for (type *elem = util_dynarray_top_ptr(buf, type); \
|
|
|
|
elem; \
|
|
|
|
elem = elem > (type *)(buf)->data ? elem - 1 : NULL)
|
|
|
|
|
2017-05-29 16:26:30 +01:00
|
|
|
#define util_dynarray_delete_unordered(buf, type, v) \
|
|
|
|
do { \
|
|
|
|
unsigned num_elements = (buf)->size / sizeof(type); \
|
|
|
|
unsigned i; \
|
|
|
|
for (i = 0; i < num_elements; i++) { \
|
|
|
|
type __v = *util_dynarray_element((buf), type, (i)); \
|
|
|
|
if (v == __v) { \
|
|
|
|
memcpy(util_dynarray_element((buf), type, (i)), \
|
|
|
|
util_dynarray_pop_ptr((buf), type), sizeof(type)); \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2017-06-03 18:59:07 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-04-13 05:05:12 +01:00
|
|
|
#endif /* U_DYNARRAY_H */
|
|
|
|
|