2010-04-13 06:05:12 +02: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 22:38:59 +02:00
|
|
|
#include <stdlib.h>
|
2018-01-16 21:49:09 -07: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 16:58:08 +02:00
|
|
|
#include <limits.h>
|
2017-06-01 23:15:43 +02:00
|
|
|
#include "ralloc.h"
|
2010-04-13 06:05:12 +02:00
|
|
|
|
2017-06-03 19:59:07 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-04-05 11:55:51 +02:00
|
|
|
extern unsigned util_dynarray_is_data_stack_allocated;
|
2023-02-10 15:36:56 +00:00
|
|
|
|
2025-10-31 13:46:42 -04:00
|
|
|
/* Size <= capacity and data != 0 if and only if capacity != 0 capacity will
|
|
|
|
|
* always be the allocation size of data.
|
2010-04-13 06:05:12 +02:00
|
|
|
*/
|
|
|
|
|
struct util_dynarray
|
|
|
|
|
{
|
2017-06-01 23:15:43 +02:00
|
|
|
void *mem_ctx;
|
2010-04-13 06:05:12 +02:00
|
|
|
void *data;
|
|
|
|
|
unsigned size;
|
|
|
|
|
unsigned capacity;
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-31 13:46:42 -04:00
|
|
|
/* A zero-initialized util_dynarray represents an empty array. */
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
#define UTIL_DYNARRAY_INIT { 0 }
|
|
|
|
|
#else
|
|
|
|
|
#define UTIL_DYNARRAY_INIT (struct util_dynarray){ .capacity = 0 }
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-07-20 19:58:43 -04:00
|
|
|
static inline void
|
2017-06-01 23:15:43 +02:00
|
|
|
util_dynarray_init(struct util_dynarray *buf, void *mem_ctx)
|
2010-04-13 06:05:12 +02:00
|
|
|
{
|
|
|
|
|
memset(buf, 0, sizeof(*buf));
|
2017-06-01 23:15:43 +02:00
|
|
|
buf->mem_ctx = mem_ctx;
|
2010-04-13 06:05:12 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-10 15:36:56 +00:00
|
|
|
static inline void
|
|
|
|
|
util_dynarray_init_from_stack(struct util_dynarray *buf, void *data, unsigned capacity)
|
|
|
|
|
{
|
|
|
|
|
memset(buf, 0, sizeof(*buf));
|
|
|
|
|
buf->mem_ctx = &util_dynarray_is_data_stack_allocated;
|
|
|
|
|
buf->data = data;
|
|
|
|
|
buf->capacity = capacity;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 19:58:43 -04:00
|
|
|
static inline void
|
2010-04-13 06:05:12 +02:00
|
|
|
util_dynarray_fini(struct util_dynarray *buf)
|
|
|
|
|
{
|
2017-06-01 22:38:59 +02:00
|
|
|
if (buf->data) {
|
2023-02-10 15:36:56 +00:00
|
|
|
if (buf->mem_ctx == &util_dynarray_is_data_stack_allocated) {
|
|
|
|
|
} else if (buf->mem_ctx) {
|
2017-06-01 23:15:43 +02:00
|
|
|
ralloc_free(buf->data);
|
|
|
|
|
} else {
|
|
|
|
|
free(buf->data);
|
|
|
|
|
}
|
|
|
|
|
util_dynarray_init(buf, buf->mem_ctx);
|
2010-04-13 06:05:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-14 13:55:09 +02:00
|
|
|
static inline void
|
|
|
|
|
util_dynarray_clear(struct util_dynarray *buf)
|
|
|
|
|
{
|
|
|
|
|
buf->size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 23:15:43 +02:00
|
|
|
#define DYN_ARRAY_INITIAL_SIZE 64
|
|
|
|
|
|
2019-05-13 16:58:07 +02:00
|
|
|
MUST_CHECK static inline void *
|
2018-02-19 21:44:44 +08:00
|
|
|
util_dynarray_ensure_cap(struct util_dynarray *buf, unsigned newcap)
|
2010-04-13 06:05:12 +02:00
|
|
|
{
|
2018-02-19 21:44:44 +08:00
|
|
|
if (newcap > buf->capacity) {
|
2019-05-13 16:58:07 +02:00
|
|
|
unsigned capacity = MAX3(DYN_ARRAY_INITIAL_SIZE, buf->capacity * 2, newcap);
|
|
|
|
|
void *data;
|
2017-06-01 23:15:43 +02:00
|
|
|
|
2023-02-10 15:36:56 +00:00
|
|
|
if (buf->mem_ctx == &util_dynarray_is_data_stack_allocated) {
|
|
|
|
|
data = malloc(capacity);
|
|
|
|
|
if (data) {
|
|
|
|
|
memcpy(data, buf->data, buf->size);
|
|
|
|
|
buf->mem_ctx = NULL;
|
|
|
|
|
}
|
|
|
|
|
} else if (buf->mem_ctx) {
|
2019-05-13 16:58:07 +02:00
|
|
|
data = reralloc_size(buf->mem_ctx, buf->data, capacity);
|
2017-06-01 23:15:43 +02:00
|
|
|
} else {
|
2019-05-13 16:58:07 +02:00
|
|
|
data = realloc(buf->data, capacity);
|
2017-06-01 23:15:43 +02:00
|
|
|
}
|
2019-05-13 16:58:07 +02:00
|
|
|
if (!data)
|
2021-08-02 23:39:27 +02:00
|
|
|
return NULL;
|
2019-05-13 16:58:07 +02:00
|
|
|
|
|
|
|
|
buf->data = data;
|
|
|
|
|
buf->capacity = capacity;
|
2010-04-13 06:05:12 +02:00
|
|
|
}
|
|
|
|
|
|
2018-02-19 21:44:44 +08: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 16:58:08 +02:00
|
|
|
MUST_CHECK static inline void *
|
|
|
|
|
util_dynarray_resize_bytes(struct util_dynarray *buf, unsigned nelts, size_t eltsize)
|
2018-02-19 21:44:44 +08: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 16:58:08 +02:00
|
|
|
if (unlikely(nelts > UINT_MAX / eltsize))
|
2021-08-02 23:39:27 +02: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 16:58:08 +02:00
|
|
|
|
2025-01-09 06:59:11 -05:00
|
|
|
unsigned newsize = nelts * (unsigned)eltsize;
|
2018-02-19 21:44:44 +08: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 16:58:08 +02:00
|
|
|
if (!p)
|
2021-08-02 23:39:27 +02: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 16:58:08 +02:00
|
|
|
|
2010-04-13 06:05:12 +02:00
|
|
|
buf->size = newsize;
|
2017-06-01 22:38:59 +02:00
|
|
|
|
2010-04-13 06:05:12 +02:00
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-22 10:40:44 -04:00
|
|
|
MUST_CHECK static inline void *
|
|
|
|
|
util_dynarray_resize_bytes_zero(struct util_dynarray *buf, unsigned nelts, size_t eltsize)
|
|
|
|
|
{
|
|
|
|
|
size_t size = buf->capacity;
|
|
|
|
|
void *ret = util_dynarray_resize_bytes(buf, nelts, eltsize);
|
|
|
|
|
if (ret) {
|
|
|
|
|
uint8_t *data = (uint8_t*)buf->data;
|
|
|
|
|
memset(data + size, 0, buf->capacity - size);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-13 14:41:19 -07: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 16:58:08 +02:00
|
|
|
if (util_dynarray_resize_bytes(buf, from_buf->size, 1))
|
|
|
|
|
memcpy(buf->data, from_buf->data, from_buf->size);
|
2018-08-13 14:41:19 -07: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 16:58:08 +02:00
|
|
|
MUST_CHECK static inline void *
|
|
|
|
|
util_dynarray_grow_bytes(struct util_dynarray *buf, unsigned ngrow, size_t eltsize)
|
2010-04-13 08:23:29 +02:00
|
|
|
{
|
2025-01-09 06:59:11 -05:00
|
|
|
unsigned growbytes = ngrow * (unsigned)eltsize;
|
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 16:58:08 +02:00
|
|
|
|
|
|
|
|
if (unlikely(ngrow > (UINT_MAX / eltsize) ||
|
|
|
|
|
growbytes > UINT_MAX - buf->size))
|
2021-08-02 23:39:27 +02: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 16:58:08 +02:00
|
|
|
|
|
|
|
|
unsigned newsize = buf->size + growbytes;
|
|
|
|
|
void *p = util_dynarray_ensure_cap(buf, newsize);
|
|
|
|
|
if (!p)
|
2021-08-02 23:39:27 +02: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 16:58:08 +02:00
|
|
|
|
|
|
|
|
buf->size = newsize;
|
|
|
|
|
|
|
|
|
|
return p;
|
2010-04-13 08:23:29 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-20 19:58:43 -04:00
|
|
|
static inline void
|
2010-04-13 06:05:12 +02:00
|
|
|
util_dynarray_trim(struct util_dynarray *buf)
|
|
|
|
|
{
|
2023-02-10 15:36:56 +00:00
|
|
|
if (buf->mem_ctx == &util_dynarray_is_data_stack_allocated)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-04-13 08:59:49 +01:00
|
|
|
if (buf->size != buf->capacity) {
|
|
|
|
|
if (buf->size) {
|
2017-06-01 23:15:43 +02:00
|
|
|
if (buf->mem_ctx) {
|
2017-06-13 05:50:18 +10:00
|
|
|
buf->data = reralloc_size(buf->mem_ctx, buf->data, buf->size);
|
2017-06-01 23:15:43 +02:00
|
|
|
} else {
|
|
|
|
|
buf->data = realloc(buf->data, buf->size);
|
|
|
|
|
}
|
2010-04-13 08:59:49 +01:00
|
|
|
buf->capacity = buf->size;
|
2017-06-01 22:38:59 +02:00
|
|
|
} else {
|
2017-06-01 23:15:43 +02: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 08:18:59 +02:00
|
|
|
}
|
2010-04-13 06:05:12 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-15 18:31:06 -05:00
|
|
|
static inline void
|
|
|
|
|
util_dynarray_append_dynarray(struct util_dynarray *buf,
|
|
|
|
|
const struct util_dynarray *other)
|
|
|
|
|
{
|
|
|
|
|
if (other->size > 0) {
|
|
|
|
|
void *p = util_dynarray_grow_bytes(buf, 1, other->size);
|
|
|
|
|
memcpy(p, other->data, other->size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
util/dynarray: infer type in append
Most of the time, we can infer the type to append in
util_dynarray_append using __typeof__, which is standardized in C23 and
support in Jesse's MSMSVCV. This patch drops the type argument most of
the time, making util_dynarray a little more ergonomic to use.
This is done in four steps.
First, rename util_dynarray_append -> util_dynarray_append_typed
bash -c "find . -type f -exec sed -i -e 's/util_dynarray_append(/util_dynarray_append_typed(/g' \{} \;"
Then, add a new append that infers the type. This is much more ergonomic
for what you want most of the time.
Next, use type-inferred append as much as possible, via Coccinelle
patch (plus manual fixup):
@@
expression dynarray, element;
type type;
@@
-util_dynarray_append_typed(dynarray, type, element);
+util_dynarray_append(dynarray, element);
Finally, hand fixup cases that Coccinelle missed or incorrectly
translated, of which there were several because we can't used the
untyped append with a literal (since the sizeof won't do what you want).
All four steps are squashed to produce a single patch changing every
util_dynarray_append call site in tree to either drop a type parameter
(if possible) or insert a _typed suffix (if we can't infer). As such,
the final patch is best reviewed by hand even though it was
tool-assisted.
No Long Linguine Meals were involved in the making of this patch.
Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@intel.com>
Acked-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/38038>
2025-10-23 15:36:13 -04:00
|
|
|
#define util_dynarray_append_typed(buf, type, v) do {type __v = (v); memcpy(util_dynarray_grow_bytes((buf), 1, sizeof(type)), &__v, sizeof(type));} while(0)
|
2024-12-13 15:49:35 -05:00
|
|
|
#define util_dynarray_append_array(buf, type, v, count) do {memcpy(util_dynarray_grow_bytes((buf), count, sizeof(type)), v, sizeof(type) * count);} while(0)
|
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 16:58:08 +02:00
|
|
|
/* 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))
|
2025-04-22 10:40:44 -04:00
|
|
|
#define util_dynarray_resize_zero(buf, type, nelts) util_dynarray_resize_bytes_zero(buf, (nelts), sizeof(type))
|
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 16:58:08 +02:00
|
|
|
#define util_dynarray_grow(buf, type, ngrow) util_dynarray_grow_bytes(buf, (ngrow), sizeof(type))
|
2024-09-24 20:19:49 +02:00
|
|
|
#define util_dynarray_top_ptr(buf, type) ((type*)((char*)(buf)->data + (buf)->size - sizeof(type)))
|
2010-04-13 06:05:12 +02:00
|
|
|
#define util_dynarray_top(buf, type) *util_dynarray_top_ptr(buf, type)
|
2024-09-24 20:19:49 +02:00
|
|
|
#define util_dynarray_pop_ptr(buf, type) ((type*)((char*)(buf)->data + ((buf)->size -= sizeof(type))))
|
2010-04-13 06:05:12 +02:00
|
|
|
#define util_dynarray_pop(buf, type) *util_dynarray_pop_ptr(buf, type)
|
2024-11-18 16:10:26 -05:00
|
|
|
#define util_dynarray_last_ptr(buf, type) ((type*)((char*)(buf)->data + ((buf)->size - sizeof(type))))
|
2010-04-13 06:05:12 +02:00
|
|
|
#define util_dynarray_contains(buf, type) ((buf)->size >= sizeof(type))
|
2010-08-23 00:31:08 +02: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 15:21:20 -07:00
|
|
|
#define util_dynarray_num_elements(buf, type) ((buf)->size / sizeof(type))
|
2010-04-13 06:05:12 +02:00
|
|
|
|
util/dynarray: infer type in append
Most of the time, we can infer the type to append in
util_dynarray_append using __typeof__, which is standardized in C23 and
support in Jesse's MSMSVCV. This patch drops the type argument most of
the time, making util_dynarray a little more ergonomic to use.
This is done in four steps.
First, rename util_dynarray_append -> util_dynarray_append_typed
bash -c "find . -type f -exec sed -i -e 's/util_dynarray_append(/util_dynarray_append_typed(/g' \{} \;"
Then, add a new append that infers the type. This is much more ergonomic
for what you want most of the time.
Next, use type-inferred append as much as possible, via Coccinelle
patch (plus manual fixup):
@@
expression dynarray, element;
type type;
@@
-util_dynarray_append_typed(dynarray, type, element);
+util_dynarray_append(dynarray, element);
Finally, hand fixup cases that Coccinelle missed or incorrectly
translated, of which there were several because we can't used the
untyped append with a literal (since the sizeof won't do what you want).
All four steps are squashed to produce a single patch changing every
util_dynarray_append call site in tree to either drop a type parameter
(if possible) or insert a _typed suffix (if we can't infer). As such,
the final patch is best reviewed by hand even though it was
tool-assisted.
No Long Linguine Meals were involved in the making of this patch.
Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@intel.com>
Acked-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/38038>
2025-10-23 15:36:13 -04:00
|
|
|
/* typeof is standard in C23 but not in C++, and MSVC lacks support when
|
|
|
|
|
* compiling as C++. Conversely, decltype is standard in C++11 but missing in C.
|
|
|
|
|
* For our purposes, they are equivalent - just use the right one depending
|
|
|
|
|
* whether we compile as C or C++ so the macro works everywhere with MSVC.
|
|
|
|
|
*/
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
#define util_dynarray_append(buf, v) util_dynarray_append_typed(buf, decltype(v), v)
|
|
|
|
|
#else
|
|
|
|
|
#define util_dynarray_append(buf, v) util_dynarray_append_typed(buf, __typeof__(v), v)
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-06-01 23:15:43 +02: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 14:57:35 -07: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 17:26:30 +02: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 19:59:07 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-04-13 06:05:12 +02:00
|
|
|
#endif /* U_DYNARRAY_H */
|