New macro ARRAY_SIZE()

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2020-12-26 06:15:40 +01:00
parent c506cd7ed2
commit d1b844c6eb
11 changed files with 37 additions and 35 deletions

View file

@ -29,7 +29,7 @@
* *
* Known contributors to this file: * Known contributors to this file:
* Doug Hay, 1998 * Doug Hay, 1998
* Markus Armbruster, 2004-2014 * Markus Armbruster, 2004-2020
*/ */
#ifndef MISC_H #ifndef MISC_H
@ -89,6 +89,11 @@ extern void (*oops_handler)(void);
#define BUILD_ASSERT(cond) \ #define BUILD_ASSERT(cond) \
((void)BUILD_ASSERT_ONE(cond)) ((void)BUILD_ASSERT_ONE(cond))
/*
* Number of elements in ARRAY.
*/
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
void exit_nomem(void) ATTRIBUTE((noreturn)); void exit_nomem(void) ATTRIBUTE((noreturn));
/* return codes from command routines */ /* return codes from command routines */

View file

@ -27,7 +27,7 @@
* cargo.c: Cargo lists * cargo.c: Cargo lists
* *
* Known contributors to this file: * Known contributors to this file:
* Markus Armbruster, 2009 * Markus Armbruster, 2009-2020
*/ */
#include <config.h> #include <config.h>
@ -94,7 +94,7 @@ clink_init(struct clink *cl)
unsigned i; unsigned i;
cl->next = -1; cl->next = -1;
for (i = 0; i < sizeof(cl->head) / sizeof(*cl->head); i++) for (i = 0; i < ARRAY_SIZE(cl->head); i++)
cl->head[i] = -1; cl->head[i] = -1;
} }

View file

@ -27,7 +27,7 @@
* filetable.c: Empire game data file descriptions. * filetable.c: Empire game data file descriptions.
* *
* Known contributors to this file: * Known contributors to this file:
* Markus Armbruster, 2005-2016 * Markus Armbruster, 2005-2020
*/ */
#include <config.h> #include <config.h>
@ -70,9 +70,6 @@ static void nchr_oninit(void *);
static char dummy_cache; static char dummy_cache;
/* Number of elements in ARRAY. */
#define SZ(array) (sizeof(array) / sizeof((array)[0]))
/* Initializers for members flags... */ /* Initializers for members flags... */
/* Unmapped cache */ /* Unmapped cache */
#define UNMAPPED_CACHE(type, nent, flags) \ #define UNMAPPED_CACHE(type, nent, flags) \
@ -84,7 +81,7 @@ static char dummy_cache;
*/ */
#define ARRAY_CACHE(array, flags) \ #define ARRAY_CACHE(array, flags) \
sizeof(*(array)), -1, (flags), (char *)(array), \ sizeof(*(array)), -1, (flags), (char *)(array), \
SZ((array)), 0, 0, 0, -1 ARRAY_SIZE((array)), 0, 0, 0, -1
/* /*
* Mapped cache, array with unknown size. * Mapped cache, array with unknown size.
* Member csize gets a bogus value, needs to be fixed up. * Member csize gets a bogus value, needs to be fixed up.
@ -98,7 +95,7 @@ static char dummy_cache;
*/ */
#define ARRAY_TABLE(array, nent, flags) \ #define ARRAY_TABLE(array, nent, flags) \
sizeof(*(array)), (nent), (flags), (char *)(array), \ sizeof(*(array)), (nent), (flags), (char *)(array), \
SZ((array)), 0, (nent), (nent), -1 ARRAY_SIZE((array)), 0, (nent), (nent), -1
/* Common configuration table flags */ /* Common configuration table flags */
#define EFF_CFG (EFF_PRIVATE | EFF_MEM | EFF_STATIC | EFF_SENTINEL) #define EFF_CFG (EFF_PRIVATE | EFF_MEM | EFF_STATIC | EFF_SENTINEL)

View file

@ -27,7 +27,7 @@
* nsc.c: Empire selection global structures * nsc.c: Empire selection global structures
* *
* Known contributors to this file: * Known contributors to this file:
* Markus Armbruster, 2004-2016 * Markus Armbruster, 2004-2020
*/ */
/* /*
@ -640,7 +640,7 @@ struct castr cou_ca[] = {
#undef CURSTR #undef CURSTR
}; };
struct castr nat_ca[sizeof(cou_ca) / sizeof(*cou_ca)]; struct castr nat_ca[ARRAY_SIZE(cou_ca)];
/* initialized by nsc_init() */ /* initialized by nsc_init() */
struct castr relat_ca[] = { struct castr relat_ca[] = {

View file

@ -30,7 +30,7 @@
* Dave Pare, 1986 * Dave Pare, 1986
* Thomas Ruschak, 1992 * Thomas Ruschak, 1992
* Steve McClure, 1996 * Steve McClure, 1996
* Markus Armbruster, 2006-2014 * Markus Armbruster, 2006-2020
*/ */
#include <config.h> #include <config.h>
@ -716,7 +716,7 @@ ac_flak_dam(int guns, int def, int pl_flags)
0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f, 0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
/* +5 +6 +7 +8 >+8 */ /* +5 +6 +7 +8 >+8 */
0.70f,0.75f, 0.80f, 0.85f, 1.1305f }; 0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 }; enum { FLAK_MAX = ARRAY_SIZE(flaktable) - 1 };
flak = guns - def; flak = guns - def;
if ((pl_flags & P_T) == 0) if ((pl_flags & P_T) == 0)

View file

@ -28,7 +28,7 @@
* *
* Known contributors to this file: * Known contributors to this file:
* Ron Koenderink, 2005 * Ron Koenderink, 2005
* Markus Armbruster, 2005-2016 * Markus Armbruster, 2005-2020
*/ */
#include <config.h> #include <config.h>
@ -63,7 +63,7 @@ ef_init_srv(int force_bad_state)
{ {
unsigned i; unsigned i;
for (i = 0; i < sizeof(fileinit) / sizeof(fileinit[0]); i++) { for (i = 0; i < ARRAY_SIZE(fileinit); i++) {
empfile[fileinit[i].ef_type].postread = fileinit[i].postread; empfile[fileinit[i].ef_type].postread = fileinit[i].postread;
empfile[fileinit[i].ef_type].prewrite = fileinit[i].prewrite; empfile[fileinit[i].ef_type].prewrite = fileinit[i].prewrite;
empfile[fileinit[i].ef_type].onresize = fileinit[i].onresize; empfile[fileinit[i].ef_type].onresize = fileinit[i].onresize;

View file

@ -27,7 +27,7 @@
* paths.c: Routines associated with paths, directions, etc. * paths.c: Routines associated with paths, directions, etc.
* *
* Known contributors to this file: * Known contributors to this file:
* Markus Armbruster, 2005-2011 * Markus Armbruster, 2005-2020
*/ */
#include <config.h> #include <config.h>
@ -73,7 +73,7 @@ diridx(char dir)
{ {
unsigned i = dir - 'a'; unsigned i = dir - 'a';
if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex) if (CANT_HAPPEN(i >= ARRAY_SIZE(dirindex)
|| dirindex[i] < 0)) || dirindex[i] < 0))
return DIR_STOP; return DIR_STOP;
return dirindex[i]; return dirindex[i];

View file

@ -31,7 +31,7 @@
* Jeff Bailey, 1990 * Jeff Bailey, 1990
* Steve McClure, 1996 * Steve McClure, 1996
* Ron Koenderink, 2005-2009 * Ron Koenderink, 2005-2009
* Markus Armbruster, 2006-2017 * Markus Armbruster, 2006-2020
*/ */
#include <config.h> #include <config.h>
@ -187,7 +187,7 @@ show_nuke_stats(int tlev)
void void
show_nuke_build(int tlev) show_nuke_build(int tlev)
{ {
struct chr_index chridx[sizeof(nchr) / sizeof(*nchr)]; struct chr_index chridx[ARRAY_SIZE(nchr)];
int n = make_nchr_index(chridx, tlev); int n = make_nchr_index(chridx, tlev);
int i; int i;
struct nchrstr *np; struct nchrstr *np;
@ -208,7 +208,7 @@ show_nuke_build(int tlev)
void void
show_nuke_capab(int tlev) show_nuke_capab(int tlev)
{ {
struct chr_index chridx[sizeof(nchr) / sizeof(*nchr)]; struct chr_index chridx[ARRAY_SIZE(nchr)];
int n = make_nchr_index(chridx, tlev); int n = make_nchr_index(chridx, tlev);
int i; int i;
struct nchrstr *np; struct nchrstr *np;
@ -230,7 +230,7 @@ show_nuke_capab(int tlev)
void void
show_ship_build(int tlev) show_ship_build(int tlev)
{ {
struct chr_index chridx[sizeof(mchr) / sizeof(*mchr)]; struct chr_index chridx[ARRAY_SIZE(mchr)];
int n = make_mchr_index(chridx, tlev); int n = make_mchr_index(chridx, tlev);
int i; int i;
struct mchrstr *mp; struct mchrstr *mp;
@ -247,7 +247,7 @@ show_ship_build(int tlev)
void void
show_ship_stats(int tlev) show_ship_stats(int tlev)
{ {
struct chr_index chridx[sizeof(mchr) / sizeof(*mchr)]; struct chr_index chridx[ARRAY_SIZE(mchr)];
int n = make_mchr_index(chridx, tlev); int n = make_mchr_index(chridx, tlev);
int i; int i;
struct mchrstr *mp; struct mchrstr *mp;
@ -268,7 +268,7 @@ show_ship_stats(int tlev)
void void
show_ship_capab(int tlev) show_ship_capab(int tlev)
{ {
struct chr_index chridx[sizeof(mchr) / sizeof(*mchr)]; struct chr_index chridx[ARRAY_SIZE(mchr)];
int n = make_mchr_index(chridx, tlev); int n = make_mchr_index(chridx, tlev);
int i; int i;
struct mchrstr *mp; struct mchrstr *mp;
@ -286,7 +286,7 @@ show_ship_capab(int tlev)
void void
show_plane_stats(int tlev) show_plane_stats(int tlev)
{ {
struct chr_index chridx[sizeof(plchr) / sizeof(*plchr)]; struct chr_index chridx[ARRAY_SIZE(plchr)];
int n = make_plchr_index(chridx, tlev); int n = make_plchr_index(chridx, tlev);
int i; int i;
struct plchrstr *pp; struct plchrstr *pp;
@ -304,7 +304,7 @@ show_plane_stats(int tlev)
void void
show_plane_capab(int tlev) show_plane_capab(int tlev)
{ {
struct chr_index chridx[sizeof(plchr) / sizeof(*plchr)]; struct chr_index chridx[ARRAY_SIZE(plchr)];
int n = make_plchr_index(chridx, tlev); int n = make_plchr_index(chridx, tlev);
int i; int i;
struct plchrstr *pp; struct plchrstr *pp;
@ -321,7 +321,7 @@ show_plane_capab(int tlev)
void void
show_plane_build(int tlev) show_plane_build(int tlev)
{ {
struct chr_index chridx[sizeof(plchr) / sizeof(*plchr)]; struct chr_index chridx[ARRAY_SIZE(plchr)];
int n = make_plchr_index(chridx, tlev); int n = make_plchr_index(chridx, tlev);
int i; int i;
struct plchrstr *pp; struct plchrstr *pp;
@ -339,7 +339,7 @@ show_plane_build(int tlev)
void void
show_land_build(int tlev) show_land_build(int tlev)
{ {
struct chr_index chridx[sizeof(lchr) / sizeof(*lchr)]; struct chr_index chridx[ARRAY_SIZE(lchr)];
int n = make_lchr_index(chridx, tlev); int n = make_lchr_index(chridx, tlev);
int i; int i;
struct lchrstr *lp; struct lchrstr *lp;
@ -358,7 +358,7 @@ show_land_build(int tlev)
void void
show_land_capab(int tlev) show_land_capab(int tlev)
{ {
struct chr_index chridx[sizeof(lchr) / sizeof(*lchr)]; struct chr_index chridx[ARRAY_SIZE(lchr)];
int n = make_lchr_index(chridx, tlev); int n = make_lchr_index(chridx, tlev);
int i; int i;
struct lchrstr *lcp; struct lchrstr *lcp;
@ -376,7 +376,7 @@ show_land_capab(int tlev)
void void
show_land_stats(int tlev) show_land_stats(int tlev)
{ {
struct chr_index chridx[sizeof(lchr) / sizeof(*lchr)]; struct chr_index chridx[ARRAY_SIZE(lchr)];
int n = make_lchr_index(chridx, tlev); int n = make_lchr_index(chridx, tlev);
int i; int i;
struct lchrstr *lcp; struct lchrstr *lcp;

View file

@ -28,7 +28,7 @@
* *
* Known contributors to this file: * Known contributors to this file:
* Dave Pare, 1989 * Dave Pare, 1989
* Markus Armbruster, 2009-2011 * Markus Armbruster, 2009-2020
*/ */
#include <config.h> #include <config.h>
@ -241,7 +241,7 @@ snxtitem_use_condarg(struct nstr_item *np)
if (!player->condarg) if (!player->condarg)
return 1; return 1;
n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond), n = nstr_comp(np->cond, ARRAY_SIZE(np->cond),
np->type, player->condarg); np->type, player->condarg);
if (n < 0) if (n < 0)
return 0; return 0;

View file

@ -28,7 +28,7 @@
* *
* Known contributors to this file: * Known contributors to this file:
* Dave Pare, 1989 * Dave Pare, 1989
* Markus Armbruster, 2006-2011 * Markus Armbruster, 2006-2020
*/ */
#include <config.h> #include <config.h>
@ -127,7 +127,7 @@ snxtsct_use_condarg(struct nstr_sect *np)
if (!player->condarg) if (!player->condarg)
return 1; return 1;
n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond), n = nstr_comp(np->cond, ARRAY_SIZE(np->cond),
EF_SECTOR, player->condarg); EF_SECTOR, player->condarg);
if (n < 0) if (n < 0)
return 0; return 0;

View file

@ -30,7 +30,7 @@
* Dave Pare, 1994 * Dave Pare, 1994
* Steve McClure, 1996 * Steve McClure, 1996
* Ron Koenderink, 2005 * Ron Koenderink, 2005
* Markus Armbruster, 2007-2012 * Markus Armbruster, 2007-2020
*/ */
#include <config.h> #include <config.h>
@ -83,7 +83,7 @@ static int
update_get_schedule(void) update_get_schedule(void)
{ {
time_t now = time(NULL); time_t now = time(NULL);
int n = sizeof(update_time) / sizeof(*update_time); int n = ARRAY_SIZE(update_time);
int i; int i;
ef_truncate(EF_UPDATES, 0); ef_truncate(EF_UPDATES, 0);