[vlc-devel] [RFC 1/1] vlc_vector: add helpers for vectors

Romain Vimont rom1v at videolabs.io
Thu Aug 30 12:22:15 CEST 2018

On Wed, Aug 29, 2018 at 10:28:20PM +0200, Romain Vimont wrote:
> +#define vlc_vector_realloc_(pv, newsize) \
> +    (((pv)->cap = (newsize), true) && \
> +     ((pv)->size = vlc_vector_min_((pv)->size, newsize), true) && \
> +     ((pv)->data = vlc_vector_reallocarray_or_free_( \
> +                            (pv)->data, newsize, sizeof(*(pv)->data))))

This is wrong: e.g. for vectors of pointers, this will leak all the
pointers on allocation error.

I see no way to properly fix this without using an additional parameter
to report allocation errors (we need a way to test the result of realloc
but keep the original pointer in case it fails, to keep the vector

So unless someone has a better solution, I will report errors using an
additional output parameter (for vlc_vector_push(), vlc_vector_insert(),
etc.) (even if that makes me sad ^^).

> +#define vlc_vector_foreach(item, pv) \
> +    for (size_t vlc_vector_idx_##item = 0; \
> +         vlc_vector_idx_##item < (pv)->size && \
> +             ((item) = (pv)->data[vlc_vector_idx_##item], true); \
> +         ++vlc_vector_idx_##item)

As reported by tguillem, it's ok only for vectors of primitive types or
pointers. If the vector contains raw structs, they will be copied.

There are no "references", so either we always iterate using a pointer
to items, or we expose two foreach macros... or we just get rid of this
foreach macro (iteration is simple enough for an array not to add
complexity with a macro-ish foreach loop).

More information about the vlc-devel mailing list