[FFmpeg-devel] [PATCH 1/3] lavu: add a template for refcounted objects.

Anton Khirnov anton at khirnov.net
Wed Jul 1 19:11:25 EEST 2020


Quoting Nicolas George (2020-07-01 17:13:22)
> Anton Khirnov (12020-07-01):
> > Why a template? It seems simpler to add a struct like
> > typedef struct AVRefcount {
> >     atomic_uint refcount;
> >     void       *opaque;
> >     void      (*free)(void *opaque);
> > } AVRefcount;
> > and then embed it in everything that wants to be refcounted. All just
> > normal structs and functions, no layers of macros.
> 
> Because what you propose requires an extra layer of pointers, which is
> both annoying when reading and writing the code

instead of
#define AVRC_TYPE AVBuffer
#define AVRC_PREFIX prefix
#define AVRC_FIELD refcount
you'd have
av_refcount_init(&buf->refcount, buf, buffer_free);
Does not look more annoying to me, to the contrary macro templates
require more effort to understand or debug.

> and inefficient when running it.

Why? I do not see how an extra pointer dereference could possibly matter here.

> 
> Also because what you propose has void pointers, and therefore will not
> benefit from the compiler checking the types at compilation time.

In my experience, providing the wrong opaque pointer has never been a
common source of bugs (and is typically easily noticeable if it does
happen), so I see that as an acceptable tradeoff.

-- 
Anton Khirnov


More information about the ffmpeg-devel mailing list