[vlc-devel] [PATCH 1/3] vlc_list: helpers for doubly linked lists

Romain Vimont rom1v at videolabs.io
Tue Jun 12 22:06:05 CEST 2018


On Tue, Jun 12, 2018 at 10:41:50PM +0300, Rémi Denis-Courmont wrote:
> Le tiistaina 12. kesäkuuta 2018, 14.35.02 EEST Romain Vimont a écrit :
> > Anyway, my remark about the safe/unsafe version was not about
> > performance, but more about passing an additional argument that could be
> > avoided in most cases (a foreach with 5 arguments is quite complex to
> > use).
> 
> Third and fifth arguments define what is actually iterated, and seem 
> intrinsically necessary. The first argument defines a variable name and is 
> necessary to avoid shadowing.

Yes.

> I cannot find a way to avoid the second (iterator) argument without both 
> relying on UB and losing safety against element removal.

I agree. But I think that it is very common to iterate over a list
without removing, so providing an additional macro with one parameter
less (and one variable declaration less "struct vlc_list_it it;" for
each caller) could be worth it.

> The forth argument can be avoided with typeof, but I cannot see a well-defined 
> and standard-compliant alternative. Specifically
>     (((char *)(&pos->member)) - ((char *)pos))
> is constant but undefined if pos is not initialized or not a valid pointer. As 
> far as I recall, ubsan is prone to tripping on it.

I cannot see a well-defined+standard-compliant solution either.

However, it seems that every modern compiler supports typeof() or
__typeof() or __typeof__():

<https://stackoverflow.com/a/14878370/1987178>

Maybe we could #define some vlc_typeof() to use the right name?


More information about the vlc-devel mailing list