[vlc-devel] [PATCH] use ARRAY_SIZE when applicable
Thomas Guillem
thomas at gllm.fr
Mon Feb 24 10:07:06 CET 2020
+1
On Sun, Feb 23, 2020, at 18:02, Romain Vimont wrote:
> LGTM
>
> On 2/23/20 4:19 PM, Alexandre Janniaux wrote:
> > It's easier to read than sizeof(x)/sizeof(*x) and provides more
> > information than using a define on this previous value.
> > ---
> > src/misc/actions.c | 15 +++++++--------
> > src/misc/fourcc.c | 15 ++++++---------
> > src/misc/keystore.c | 3 +--
> > src/misc/threads.c | 4 ++--
> > src/misc/update.c | 2 +-
> > src/network/httpd.c | 4 ++--
> > src/posix/thread.c | 2 +-
> > src/test/dictionary.c | 2 +-
> > src/test/extensions.c | 2 +-
> > src/test/url.c | 2 +-
> > src/video_output/vout_subpictures.c | 2 +-
> > test/libvlc/slaves.c | 22 ++++++++++------------
> > test/modules/demux/dashuri.cpp | 3 ++-
> > test/modules/keystore/test.c | 2 +-
> > test/src/input/stream.c | 2 +-
> > test/src/input/thumbnail.c | 2 +-
> > test/src/misc/keystore.c | 2 +-
> > 17 files changed, 40 insertions(+), 46 deletions(-)
> >
> > diff --git a/src/misc/actions.c b/src/misc/actions.c
> > index 54996e6ce8..b10b07936e 100644
> > --- a/src/misc/actions.c
> > +++ b/src/misc/actions.c
> > @@ -117,7 +117,6 @@ static const struct key_descriptor
> > { N_("Zoom In"), KEY_ZOOM_IN },
> > { N_("Zoom Out"), KEY_ZOOM_OUT },
> > };
> > -#define KEYS_COUNT (sizeof(s_keys)/sizeof(s_keys[0]))
> >
> > static int keystrcmp (const void *key, const void *elem)
> > {
> > @@ -197,7 +196,7 @@ uint_fast32_t vlc_str2keycode (const char *name)
> > name += len + 1;
> > }
> >
> > - struct key_descriptor *d = bsearch (name, s_keys, KEYS_COUNT,
> > + struct key_descriptor *d = bsearch (name, s_keys, ARRAY_SIZE(s_keys),
> > sizeof (s_keys[0]), keystrcmp);
> > if (d != NULL)
> > code = d->i_code;
> > @@ -230,7 +229,7 @@ char *vlc_keycode2str (uint_fast32_t code, bool locale)
> > char *str, buf[5];
> > uintptr_t key = code & ~KEY_MODIFIER;
> >
> > - for (size_t i = 0; i < KEYS_COUNT; i++)
> > + for (size_t i = 0; i < ARRAY_SIZE(s_keys); i++)
> > if (s_keys[i].i_code == key)
> > {
> > name = s_keys[i].psz;
> > @@ -377,7 +376,6 @@ static const struct name2action
> > { "zoom-original", ACTIONID_ZOOM_ORIGINAL, },
> > { "zoom-quarter", ACTIONID_ZOOM_QUARTER, },
> > };
> > -#define ACTIONS_COUNT (sizeof (s_names2actions) / sizeof (s_names2actions[0]))
> >
> > struct mapping
> > {
> > @@ -505,7 +503,7 @@ int libvlc_InternalActionsInit (libvlc_int_t *libvlc)
> > assert(libvlc != NULL);
> >
> > vlc_object_t *obj = VLC_OBJECT(libvlc);
> > - vlc_actions_t *as = malloc (sizeof (*as) + (1 + ACTIONS_COUNT)
> > + vlc_actions_t *as = malloc (sizeof (*as) + (1 + ARRAY_SIZE(s_names2actions))
> > * sizeof (*as->ppsz_keys));
> >
> > if (unlikely(as == NULL))
> > @@ -518,7 +516,7 @@ int libvlc_InternalActionsInit (libvlc_int_t *libvlc)
> > var_Create (obj, "key-action", VLC_VAR_INTEGER);
> >
> > /* Initialize from configuration */
> > - for (size_t i = 0; i < ACTIONS_COUNT; i++)
> > + for (size_t i = 0; i < ARRAY_SIZE(s_names2actions); i++)
> > {
> > #ifndef NDEBUG
> > if (i > 0
> > @@ -537,7 +535,7 @@ int libvlc_InternalActionsInit (libvlc_int_t *libvlc)
> > init_action (obj, &as->map, name + 7, s_names2actions[i].id);
> > init_action (obj, &as->global_map, name, s_names2actions[i].id);
> > }
> > - as->ppsz_keys[ACTIONS_COUNT] = NULL;
> > + as->ppsz_keys[ARRAY_SIZE(s_names2actions)] = NULL;
> >
> > /* Initialize mouse wheel events */
> > add_wheel_mapping (&as->map, KEY_MOUSEWHEELRIGHT, KEY_MOUSEWHEELLEFT,
> > @@ -593,7 +591,8 @@ vlc_actions_get_id (const char *name)
> > return ACTIONID_NONE;
> > name += 4;
> >
> > - act = bsearch(name, s_names2actions, ACTIONS_COUNT, sizeof(*act), actcmp);
> > + act = bsearch(name, s_names2actions, ARRAY_SIZE(s_names2actions),
> > + sizeof(*act), actcmp);
> > return (act != NULL) ? act->id : ACTIONID_NONE;
> > }
> >
> > diff --git a/src/misc/fourcc.c b/src/misc/fourcc.c
> > index 7211b7d745..32234dfb1a 100644
> > --- a/src/misc/fourcc.c
> > +++ b/src/misc/fourcc.c
> > @@ -72,23 +72,20 @@ static vlc_fourcc_t Lookup(vlc_fourcc_t fourcc, const char **restrict dsc,
> >
> > static vlc_fourcc_t LookupVideo(vlc_fourcc_t fourcc, const char **restrict dsc)
> > {
> > - return Lookup(fourcc, dsc, mapping_video,
> > - sizeof (mapping_video) / sizeof (mapping_video[0]),
> > - desc_video, sizeof (desc_video) / sizeof (desc_video[0]));
> > + return Lookup(fourcc, dsc, mapping_video, ARRAY_SIZE(mapping_video),
> > + desc_video, ARRAY_SIZE(desc_video));
> > }
> >
> > static vlc_fourcc_t LookupAudio(vlc_fourcc_t fourcc, const char **restrict dsc)
> > {
> > - return Lookup(fourcc, dsc, mapping_audio,
> > - sizeof (mapping_audio) / sizeof (mapping_audio[0]),
> > - desc_audio, sizeof (desc_audio) / sizeof (desc_audio[0]));
> > + return Lookup(fourcc, dsc, mapping_audio, ARRAY_SIZE(mapping_audio),
> > + desc_audio, ARRAY_SIZE(desc_audio));
> > }
> >
> > static vlc_fourcc_t LookupSpu(vlc_fourcc_t fourcc, const char **restrict dsc)
> > {
> > - return Lookup(fourcc, dsc, mapping_spu,
> > - sizeof (mapping_spu) / sizeof (mapping_spu[0]),
> > - desc_spu, sizeof (desc_spu) / sizeof (desc_spu[0]));
> > + return Lookup(fourcc, dsc, mapping_spu, ARRAY_SIZE(mapping_spu),
> > + desc_spu, ARRAY_SIZE(desc_spu));
> > }
> >
> > static vlc_fourcc_t LookupCat(vlc_fourcc_t fourcc, const char **restrict dsc,
> > diff --git a/src/misc/keystore.c b/src/misc/keystore.c
> > index 55b094350d..b5b4cc7e50 100644
> > --- a/src/misc/keystore.c
> > +++ b/src/misc/keystore.c
> > @@ -234,8 +234,7 @@ protocol_set_port(const vlc_url_t *p_url, char *psz_port)
> > i_port = p_url->i_port;
> > else
> > {
> > - for (unsigned int i = 0; i < sizeof(protocol_default_ports)
> > - / sizeof(*protocol_default_ports); ++i)
> > + for (unsigned int i = 0; i < ARRAY_SIZE(protocol_default_ports); ++i)
> > {
> > if (strcasecmp(p_url->psz_protocol,
> > protocol_default_ports[i].psz_protocol) == 0)
> > diff --git a/src/misc/threads.c b/src/misc/threads.c
> > index 7855ae11b9..81855ed7fe 100644
> > --- a/src/misc/threads.c
> > +++ b/src/misc/threads.c
> > @@ -41,9 +41,9 @@ void vlc_global_mutex (unsigned n, bool acquire)
> > VLC_STATIC_MUTEX, // For MTA holder
> > #endif
> > };
> > - static_assert (VLC_MAX_MUTEX == (sizeof (locks) / sizeof (locks[0])),
> > + static_assert (VLC_MAX_MUTEX == ARRAY_SIZE(locks),
> > "Wrong number of global mutexes");
> > - assert (n < (sizeof (locks) / sizeof (locks[0])));
> > + assert (n < ARRAY_SIZE(locks));
> >
> > vlc_mutex_t *lock = locks + n;
> > if (acquire)
> > diff --git a/src/misc/update.c b/src/misc/update.c
> > index f5d6dc4ebe..864e525bd3 100644
> > --- a/src/misc/update.c
> > +++ b/src/misc/update.c
> > @@ -449,7 +449,7 @@ bool update_NeedUpgrade( update_t *p_update )
> > p_update->release.i_extra
> > };
> >
> > - for (unsigned i = 0; i < sizeof latest / sizeof *latest; i++) {
> > + for (unsigned i = 0; i < ARRAY_SIZE( latest ); i++) {
> > /* there is a new version available */
> > if (latest[i] > current[i])
> > return true;
> > diff --git a/src/network/httpd.c b/src/network/httpd.c
> > index 2b56c178a1..6853611574 100644
> > --- a/src/network/httpd.c
> > +++ b/src/network/httpd.c
> > @@ -1740,7 +1740,7 @@ static void httpdLoop(httpd_host_t *host)
> > }
> >
> > struct pollfd *pufd = ufd + nfd;
> > - assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
> > + assert (pufd < ufd + ARRAY_SIZE (ufd));
> >
> > pufd->events = pufd->revents = 0;
> >
> > @@ -1990,7 +1990,7 @@ static void httpdLoop(httpd_host_t *host)
> > vlc_list_foreach(cl, &host->clients, node) {
> > const struct pollfd *pufd = &ufd[nfd];
> >
> > - assert(pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])]);
> > + assert(pufd < &ufd[ARRAY_SIZE(ufd)]);
> >
> > if (vlc_tls_GetFD(cl->sock) != pufd->fd)
> > continue; // we were not waiting for this client
> > diff --git a/src/posix/thread.c b/src/posix/thread.c
> > index dfe0ea7c9d..8accd05187 100644
> > --- a/src/posix/thread.c
> > +++ b/src/posix/thread.c
> > @@ -71,7 +71,7 @@ void vlc_trace (const char *fn, const char *file, unsigned line)
> > fflush (stderr); /* needed before switch to low-level I/O */
> > #ifdef HAVE_BACKTRACE
> > void *stack[20];
> > - int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
> > + int len = backtrace (stack, ARRAY_SIZE (stack) );
> > backtrace_symbols_fd (stack, len, 2);
> > #endif
> > fsync (2);
> > diff --git a/src/test/dictionary.c b/src/test/dictionary.c
> > index 92b1d82328..3c00a50dff 100644
> > --- a/src/test/dictionary.c
> > +++ b/src/test/dictionary.c
> > @@ -65,7 +65,7 @@ int main (void)
> > static const char * our_keys[] = {
> > "Hello", "Hella", "flowmeter", "Frostnipped", "frostnipped", "remiform", "quadrifoliolate", "singularity", "unafflicted"
> > };
> > - const int size = sizeof(our_keys)/sizeof(our_keys[0]);
> > + const int size = ARRAY_SIZE(our_keys);
> > char ** keys;
> > intptr_t i = 0;
> >
> > diff --git a/src/test/extensions.c b/src/test/extensions.c
> > index 378570f996..ab71111cad 100644
> > --- a/src/test/extensions.c
> > +++ b/src/test/extensions.c
> > @@ -39,7 +39,7 @@ static void check_extensions( const char* const* extensions, size_t nb_exts )
> > do \
> > { \
> > const char* const exts[] = { ext_list }; \
> > - check_extensions( exts, sizeof( exts ) / sizeof( exts[0] ) ); \
> > + check_extensions( exts, ARRAY_SIZE( exts ) ); \
> > } while(0);
> >
> > int main(void)
> > diff --git a/src/test/url.c b/src/test/url.c
> > index a3a7c77847..d2b4361169 100644
> > --- a/src/test/url.c
> > +++ b/src/test/url.c
> > @@ -221,7 +221,7 @@ int main (void)
> > }
> >
> > char buf[256];
> > - char *tmpdir = getcwd(buf, sizeof (buf) / sizeof (*buf));
> > + char *tmpdir = getcwd(buf, ARRAY_SIZE (buf));
> > if (tmpdir == NULL)
> > {
> > perror("getcwd");
> > diff --git a/src/video_output/vout_subpictures.c b/src/video_output/vout_subpictures.c
> > index 3efd76ff1d..67db53aeb0 100644
> > --- a/src/video_output/vout_subpictures.c
> > +++ b/src/video_output/vout_subpictures.c
> > @@ -1161,7 +1161,7 @@ static subpicture_t *SpuRenderSubpictures(spu_t *spu,
> > size_t subtitle_area_count = 0;
> >
> > subtitle_area = subtitle_area_buffer;
> > - if (subtitle_region_count > sizeof(subtitle_area_buffer)/sizeof(*subtitle_area_buffer))
> > + if (subtitle_region_count > ARRAY_SIZE(subtitle_area_buffer))
> > subtitle_area = calloc(subtitle_region_count, sizeof(*subtitle_area));
> >
> > /* Process all subpictures and regions (in the right order) */
> > diff --git a/test/libvlc/slaves.c b/test/libvlc/slaves.c
> > index 7b2c24fa43..03510e3e5f 100644
> > --- a/test/libvlc/slaves.c
> > +++ b/test/libvlc/slaves.c
> > @@ -172,8 +172,7 @@ main (void)
> > { NULL, libvlc_media_slave_type_subtitle, 0 /* none */ },
> > };
> >
> > - #define EXPECTED_SLAVES_COUNT (sizeof(p_expected_slaves) / sizeof(*p_expected_slaves))
> > - static_assert((sizeof(pp_slave_paths) / sizeof(*pp_slave_paths)) == EXPECTED_SLAVES_COUNT,
> > + static_assert(ARRAY_SIZE(pp_slave_paths) == ARRAY_SIZE(p_expected_slaves),
> > "pp_slave_paths and p_expected_slaves mismatch");
> >
> > const char *pp_args[] = {
> > @@ -182,13 +181,12 @@ main (void)
> > "--codec", "none", /* to ensure we don't depend on codec modules */
> > NULL /* "sub-autodetect-file" place holder */
> > };
> > - #define ARGC (sizeof(pp_args) / sizeof(*pp_args))
> >
> > - libvlc_instance_t *p_vlc = libvlc_new(ARGC - 1, pp_args);
> > + libvlc_instance_t *p_vlc = libvlc_new(ARRAY_SIZE(pp_args) - 1, pp_args);
> > assert(p_vlc != NULL);
> >
> > /* Fill p_expected_slaves with correct VLC mrls */
> > - for (unsigned int i = 0; i < EXPECTED_SLAVES_COUNT; ++i)
> > + for (unsigned int i = 0; i < ARRAY_SIZE(p_expected_slaves); ++i)
> > {
> > p_expected_slaves[i].psz_uri = path_to_mrl(p_vlc, pp_slave_paths[i]);
> > assert(p_expected_slaves[i].psz_uri != NULL);
> > @@ -197,12 +195,12 @@ main (void)
> > printf("== Testing --sub-autodetect-fuzzy 1 (everything) ==\n");
> > test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/test.mp4",
> > p_expected_slaves,
> > - EXPECTED_SLAVES_COUNT);
> > + ARRAY_SIZE(p_expected_slaves));
> > libvlc_release(p_vlc);
> >
> > printf("== Testing --sub-autodetect-fuzzy 2 (full, left, and right match) ==\n");
> > pp_args[2] = "2";
> > - p_vlc = libvlc_new(ARGC - 1, pp_args);
> > + p_vlc = libvlc_new(ARRAY_SIZE(pp_args) - 1, pp_args);
> > assert(p_vlc != NULL);
> > test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/test.mp4",
> > p_expected_slaves, 3);
> > @@ -214,7 +212,7 @@ main (void)
> >
> > printf("== Testing --sub-autodetect-fuzzy 3 (full and left match) ==\n");
> > pp_args[2] = "3";
> > - p_vlc = libvlc_new(ARGC - 1, pp_args);
> > + p_vlc = libvlc_new(ARRAY_SIZE(pp_args) - 1, pp_args);
> > assert(p_vlc != NULL);
> > test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/test.mp4",
> > p_expected_slaves, 2);
> > @@ -222,20 +220,20 @@ main (void)
> >
> > printf("== Testing --sub-autodetect-fuzzy 4 (full match) ==\n");
> > pp_args[2] = "4";
> > - p_vlc = libvlc_new(ARGC - 1, pp_args);
> > + p_vlc = libvlc_new(ARRAY_SIZE(pp_args) - 1, pp_args);
> > assert(p_vlc != NULL);
> > test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/test.mp4",
> > p_expected_slaves, 1);
> > libvlc_release(p_vlc);
> >
> > printf("== Testing --no-sub-autodetect-file (no match) ==\n");
> > - pp_args[ARGC - 1] = "--no-sub-autodetect-file";
> > - p_vlc = libvlc_new(ARGC, pp_args);
> > + pp_args[ARRAY_SIZE(pp_args) - 1] = "--no-sub-autodetect-file";
> > + p_vlc = libvlc_new(ARRAY_SIZE(pp_args), pp_args);
> > assert(p_vlc != NULL);
> > test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/test.mp4", NULL, 0);
> > libvlc_release(p_vlc);
> >
> > - for (unsigned int i = 0; i < EXPECTED_SLAVES_COUNT; ++i)
> > + for (unsigned int i = 0; i < ARRAY_SIZE(p_expected_slaves); ++i)
> > free(p_expected_slaves[i].psz_uri);
> >
> > return 0;
> > diff --git a/test/modules/demux/dashuri.cpp b/test/modules/demux/dashuri.cpp
> > index fb2be90e4a..925665002f 100644
> > --- a/test/modules/demux/dashuri.cpp
> > +++ b/test/modules/demux/dashuri.cpp
> > @@ -25,6 +25,7 @@
> >
> > #include <iostream>
> > #include <cstring>
> > +#include <vlc_common.h>
> >
> > using namespace dash::mpd;
> >
> > @@ -105,7 +106,7 @@ static const struct
> >
> > int main(int, char **)
> > {
> > - for(size_t i=0; i<sizeof(dataset)/sizeof(dataset[0]); i++)
> > + for(size_t i=0; i<ARRAY_SIZE(dataset); i++)
> > {
> > std::string str = std::string(dataset[i].src);
> >
> > diff --git a/test/modules/keystore/test.c b/test/modules/keystore/test.c
> > index 0ddb82758a..38e9ad9aab 100644
> > --- a/test/modules/keystore/test.c
> > +++ b/test/modules/keystore/test.c
> > @@ -309,7 +309,7 @@ main(int i_argc, char *ppsz_argv[])
> > libvlc_instance_t *p_libvlc = libvlc_new(0, NULL);
> > assert(p_libvlc != NULL);
> >
> > - for (unsigned int i = 0; i < sizeof(keystore_args)/sizeof(*keystore_args); ++i)
> > + for (unsigned int i = 0; i < ARRAY_SIZE(keystore_args); ++i)
> > {
> > const char *psz_module = keystore_args[i].psz_module;
> >
> > diff --git a/test/src/input/stream.c b/test/src/input/stream.c
> > index 86adbbae21..d5538e8a4b 100644
> > --- a/test/src/input/stream.c
> > +++ b/test/src/input/stream.c
> > @@ -199,7 +199,7 @@ stream_open( const char *psz_url )
> > p_reader = calloc( 1, sizeof(struct reader) );
> > assert( p_reader );
> >
> > - p_vlc = libvlc_new( sizeof(argv) / sizeof(argv[0]), argv );
> > + p_vlc = libvlc_new( ARRAY_SIZE(argv), argv );
> > assert( p_vlc != NULL );
> >
> > p_reader->u.s = vlc_stream_NewURL( p_vlc->p_libvlc_int, psz_url );
> > diff --git a/test/src/input/thumbnail.c b/test/src/input/thumbnail.c
> > index b7dc26f978..3a3e61a28e 100644
> > --- a/test/src/input/thumbnail.c
> > +++ b/test/src/input/thumbnail.c
> > @@ -115,7 +115,7 @@ static void test_thumbnails( libvlc_instance_t* p_vlc )
> > vlc_cond_init( &ctx.cond );
> > vlc_mutex_init( &ctx.lock );
> >
> > - for ( size_t i = 0; i < sizeof(test_params) / sizeof(test_params[0]); ++i)
> > + for ( size_t i = 0; i < ARRAY_SIZE(test_params); ++i)
> > {
> > char* psz_mrl;
> >
> > diff --git a/test/src/misc/keystore.c b/test/src/misc/keystore.c
> > index 36d07169e7..d510b5d1cf 100644
> > --- a/test/src/misc/keystore.c
> > +++ b/test/src/misc/keystore.c
> > @@ -353,7 +353,7 @@ main(void)
> >
> > libvlc_instance_t *p_libvlc = create_libvlc(i_vlc_argc, ppsz_vlc_argv);
> >
> > - for (unsigned int i = 0; i < sizeof(testcases)/sizeof(*testcases); ++i)
> > + for (unsigned int i = 0; i < ARRAY_SIZE(testcases); ++i)
> > {
> > if (testcases[i].psz_url == NULL)
> > {
> >
> _______________________________________________
> 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