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

Romain Vimont rom1v at videolabs.io
Thu Aug 30 14:24:47 CEST 2018


On Thu, Aug 30, 2018 at 12:22:15PM +0200, Romain Vimont wrote:
> 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
> unchanged).

In fact, it is not difficult. I submitted a v2.

> 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).
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel


More information about the vlc-devel mailing list