Use ARRAY_SIZE all over the tree

Roundhouse kick replacing the various (sizeof(foo)/sizeof(foo[0])) with
the ARRAY_SIZE macro from dix.h when possible. A semantic patch for
coccinelle has been used first. Additionally, a few macros have been
inlined as they had only one or two users.

Signed-off-by: Daniel Martin <consume.noise@gmail.com>
Reviewed-by: Adam Jackson <ajax@redhat.com>
This commit is contained in:
Daniel Martin 2017-10-27 16:11:56 +02:00 committed by Adam Jackson
parent 15a32ee5d1
commit d5379b350f
44 changed files with 66 additions and 105 deletions

View File

@ -47,7 +47,6 @@ static const int version_requests[] = {
/* Forward declarations */ /* Forward declarations */
static void SGEGenericEvent(xEvent *from, xEvent *to); static void SGEGenericEvent(xEvent *from, xEvent *to);
#define NUM_VERSION_REQUESTS (sizeof (version_requests) / sizeof (version_requests[0]))
#define EXT_MASK(ext) ((ext) & 0x7F) #define EXT_MASK(ext) ((ext) & 0x7F)
/************************************************************/ /************************************************************/
@ -127,7 +126,7 @@ ProcGEDispatch(ClientPtr client)
REQUEST(xGEReq); REQUEST(xGEReq);
if (pGEClient->major_version >= NUM_VERSION_REQUESTS) if (pGEClient->major_version >= ARRAY_SIZE(version_requests))
return BadRequest; return BadRequest;
if (stuff->ReqType > version_requests[pGEClient->major_version]) if (stuff->ReqType > version_requests[pGEClient->major_version])
return BadRequest; return BadRequest;

View File

@ -1268,14 +1268,12 @@ ProcScreenSaverQueryVersion,
ProcScreenSaverSetAttributes, ProcScreenSaverSetAttributes,
ProcScreenSaverUnsetAttributes, ProcScreenSaverSuspend,}; ProcScreenSaverUnsetAttributes, ProcScreenSaverSuspend,};
#define NUM_REQUESTS ((sizeof NormalVector) / (sizeof NormalVector[0]))
static int static int
ProcScreenSaverDispatch(ClientPtr client) ProcScreenSaverDispatch(ClientPtr client)
{ {
REQUEST(xReq); REQUEST(xReq);
if (stuff->data < NUM_REQUESTS) if (stuff->data < ARRAY_SIZE(NormalVector))
return (*NormalVector[stuff->data]) (client); return (*NormalVector[stuff->data]) (client);
return BadRequest; return BadRequest;
} }
@ -1360,7 +1358,7 @@ SProcScreenSaverDispatch(ClientPtr client)
{ {
REQUEST(xReq); REQUEST(xReq);
if (stuff->data < NUM_REQUESTS) if (stuff->data < ARRAY_SIZE(NormalVector))
return (*SwappedVector[stuff->data]) (client); return (*SwappedVector[stuff->data]) (client);
return BadRequest; return BadRequest;
} }

View File

@ -372,8 +372,7 @@ XIGetKnownProperty(const char *name)
if (!name) if (!name)
return None; return None;
for (i = 0; i < (sizeof(dev_properties) / sizeof(struct dev_properties)); for (i = 0; i < ARRAY_SIZE(dev_properties); i++) {
i++) {
if (strcmp(name, dev_properties[i].name) == 0) { if (strcmp(name, dev_properties[i].name) == 0) {
if (dev_properties[i].type == None) { if (dev_properties[i].type == None) {
dev_properties[i].type = dev_properties[i].type =
@ -393,8 +392,7 @@ XIResetProperties(void)
{ {
int i; int i;
for (i = 0; i < (sizeof(dev_properties) / sizeof(struct dev_properties)); for (i = 0; i < ARRAY_SIZE(dev_properties); i++)
i++)
dev_properties[i].type = None; dev_properties[i].type = None;
} }

View File

@ -284,9 +284,6 @@ static CompAlternateVisual altVisuals[] = {
{32, PICT_a8r8g8b8}, {32, PICT_a8r8g8b8},
}; };
static const int NUM_COMP_ALTERNATE_VISUALS = sizeof(altVisuals) /
sizeof(CompAlternateVisual);
static Bool static Bool
compAddAlternateVisual(ScreenPtr pScreen, CompScreenPtr cs, compAddAlternateVisual(ScreenPtr pScreen, CompScreenPtr cs,
CompAlternateVisual * alt) CompAlternateVisual * alt)
@ -362,7 +359,7 @@ compAddAlternateVisuals(ScreenPtr pScreen, CompScreenPtr cs)
{ {
int alt, ret = 0; int alt, ret = 0;
for (alt = 0; alt < NUM_COMP_ALTERNATE_VISUALS; alt++) for (alt = 0; alt < ARRAY_SIZE(altVisuals); alt++)
ret |= compAddAlternateVisual(pScreen, cs, altVisuals + alt); ret |= compAddAlternateVisual(pScreen, cs, altVisuals + alt);
return ! !ret; return ! !ret;

View File

@ -464,8 +464,6 @@ static const int version_requests[] = {
X_DamageAdd, /* Version 1 */ X_DamageAdd, /* Version 1 */
}; };
#define NUM_VERSION_REQUESTS (sizeof (version_requests) / sizeof (version_requests[0]))
static int (*ProcDamageVector[XDamageNumberRequests]) (ClientPtr) = { static int (*ProcDamageVector[XDamageNumberRequests]) (ClientPtr) = {
/*************** Version 1 ******************/ /*************** Version 1 ******************/
ProcDamageQueryVersion, ProcDamageQueryVersion,
@ -482,7 +480,7 @@ ProcDamageDispatch(ClientPtr client)
REQUEST(xDamageReq); REQUEST(xDamageReq);
DamageClientPtr pDamageClient = GetDamageClient(client); DamageClientPtr pDamageClient = GetDamageClient(client);
if (pDamageClient->major_version >= NUM_VERSION_REQUESTS) if (pDamageClient->major_version >= ARRAY_SIZE(version_requests))
return BadRequest; return BadRequest;
if (stuff->damageReqType > version_requests[pDamageClient->major_version]) if (stuff->damageReqType > version_requests[pDamageClient->major_version])
return BadRequest; return BadRequest;

View File

@ -655,9 +655,7 @@ eventToDeviceEvent(DeviceEvent *ev, xEvent **xi)
btlen = bytes_to_int32(bits_to_bytes(MAX_BUTTONS)); btlen = bytes_to_int32(bits_to_bytes(MAX_BUTTONS));
len += btlen * 4; /* buttonmask len */ len += btlen * 4; /* buttonmask len */
vallen = vallen = count_bits(ev->valuators.mask, ARRAY_SIZE(ev->valuators.mask));
count_bits(ev->valuators.mask,
sizeof(ev->valuators.mask) / sizeof(ev->valuators.mask[0]));
len += vallen * 2 * sizeof(uint32_t); /* axisvalues */ len += vallen * 2 * sizeof(uint32_t); /* axisvalues */
vallen = bytes_to_int32(bits_to_bytes(MAX_VALUATORS)); vallen = bytes_to_int32(bits_to_bytes(MAX_VALUATORS));
len += vallen * 4; /* valuators mask */ len += vallen * 4; /* valuators mask */

View File

@ -439,7 +439,7 @@ ChangeGCXIDs(ClientPtr client, GC * pGC, BITS32 mask, CARD32 *pC32)
} }
for (i = Ones(mask); i--;) for (i = Ones(mask); i--;)
vals[i].val = pC32[i]; vals[i].val = pC32[i];
for (i = 0; i < sizeof(xidfields) / sizeof(*xidfields); ++i) { for (i = 0; i < ARRAY_SIZE(xidfields); ++i) {
int offset, rc; int offset, rc;
if (!(mask & xidfields[i].mask)) if (!(mask & xidfields[i].mask))

View File

@ -571,8 +571,6 @@ static const int offsets[] = {
offsetof(ColormapRec, devPrivates), /* RT_COLORMAP */ offsetof(ColormapRec, devPrivates), /* RT_COLORMAP */
}; };
#define NUM_OFFSETS (sizeof (offsets) / sizeof (offsets[0]))
int int
dixLookupPrivateOffset(RESTYPE type) dixLookupPrivateOffset(RESTYPE type)
{ {
@ -587,7 +585,7 @@ dixLookupPrivateOffset(RESTYPE type)
return offsets[RT_PIXMAP & TypeMask]; return offsets[RT_PIXMAP & TypeMask];
} }
type = type & TypeMask; type = type & TypeMask;
if (type < NUM_OFFSETS) if (type < ARRAY_SIZE(offsets))
return offsets[type]; return offsets[type];
return -1; return -1;
} }

View File

@ -562,10 +562,6 @@
(c)[1] = (float)y; \ (c)[1] = (float)y; \
} while(0) } while(0)
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
#define ALIGN(i,m) (((i) + (m) - 1) & ~((m) - 1)) #define ALIGN(i,m) (((i) + (m) - 1) & ~((m) - 1))
#define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b))

View File

@ -66,8 +66,6 @@ KdDepths kdDepths[] = {
{32, 32} {32, 32}
}; };
#define NUM_KD_DEPTHS (sizeof (kdDepths) / sizeof (kdDepths[0]))
#define KD_DEFAULT_BUTTONS 5 #define KD_DEFAULT_BUTTONS 5
DevPrivateKeyRec kdScreenPrivateKeyRec; DevPrivateKeyRec kdScreenPrivateKeyRec;
@ -886,7 +884,7 @@ KdSetPixmapFormats(ScreenInfo * pScreenInfo)
/* /*
* Fill in additional formats * Fill in additional formats
*/ */
for (i = 0; i < NUM_KD_DEPTHS; i++) for (i = 0; i < ARRAY_SIZE(kdDepths); i++)
if (!depthToBpp[kdDepths[i].depth]) if (!depthToBpp[kdDepths[i].depth])
depthToBpp[kdDepths[i].depth] = kdDepths[i].bpp; depthToBpp[kdDepths[i].depth] = kdDepths[i].bpp;

View File

@ -89,5 +89,5 @@ BEGIN {
END { END {
print "};" print "};"
printf "const int xf86NumDefaultModes = sizeof(xf86DefaultModes) / sizeof(DisplayModeRec);" printf "const int xf86NumDefaultModes = ARRAY_SIZE(xf86DefaultModes);"
} }

View File

@ -206,7 +206,7 @@ configureScreenSection(int screennum)
XNFasprintf(&tmp, "Card%d", screennum); XNFasprintf(&tmp, "Card%d", screennum);
ptr->scrn_device_str = tmp; ptr->scrn_device_str = tmp;
for (i = 0; i < sizeof(depths) / sizeof(depths[0]); i++) { for (i = 0; i < ARRAY_SIZE(depths); i++) {
XF86ConfDisplayPtr conf_display; XF86ConfDisplayPtr conf_display;
conf_display = calloc(1, sizeof(XF86ConfDisplayRec)); conf_display = calloc(1, sizeof(XF86ConfDisplayRec));

View File

@ -477,7 +477,7 @@ xf86LookupMode(ScrnInfoPtr scrp, DisplayModePtr modep,
M_T_DRIVER, M_T_DRIVER,
0 0
}; };
const int ntypes = sizeof(types) / sizeof(int); const int ntypes = ARRAY_SIZE(types);
strategy &= ~(LOOKUP_CLKDIV2 | LOOKUP_OPTIONAL_TOLERANCES); strategy &= ~(LOOKUP_CLKDIV2 | LOOKUP_OPTIONAL_TOLERANCES);
@ -1853,7 +1853,7 @@ xf86ValidateModes(ScrnInfoPtr scrp, DisplayModePtr availModes,
M_T_DRIVER, M_T_DRIVER,
0 0
}; };
const int ntypes = sizeof(types) / sizeof(int); const int ntypes = ARRAY_SIZE(types);
int n; int n;
/* /*

View File

@ -1633,7 +1633,7 @@ DRI2ScreenInit(ScreenPtr pScreen, DRI2InfoPtr info)
pScreen->SetWindowPixmap = DRI2SetWindowPixmap; pScreen->SetWindowPixmap = DRI2SetWindowPixmap;
xf86DrvMsg(pScreen->myNum, X_INFO, "[DRI2] Setup complete\n"); xf86DrvMsg(pScreen->myNum, X_INFO, "[DRI2] Setup complete\n");
for (i = 0; i < sizeof(driverTypeNames) / sizeof(driverTypeNames[0]); i++) { for (i = 0; i < ARRAY_SIZE(driverTypeNames); i++) {
if (i < ds->numDrivers && ds->driverNames[i]) { if (i < ds->numDrivers && ds->driverNames[i]) {
xf86DrvMsg(pScreen->myNum, X_INFO, "[DRI2] %s driver: %s\n", xf86DrvMsg(pScreen->myNum, X_INFO, "[DRI2] %s driver: %s\n",
driverTypeNames[i], ds->driverNames[i]); driverTypeNames[i], ds->driverNames[i]);

View File

@ -1688,7 +1688,7 @@ drmmode_create_name(ScrnInfoPtr pScrn, drmModeConnectorPtr koutput, char *name,
return; return;
fallback: fallback:
if (koutput->connector_type >= MS_ARRAY_SIZE(output_names)) if (koutput->connector_type >= ARRAY_SIZE(output_names))
snprintf(name, 32, "Unknown%d-%d", koutput->connector_type, koutput->connector_type_id); snprintf(name, 32, "Unknown%d-%d", koutput->connector_type, koutput->connector_type_id);
else if (pScrn->is_gpu) else if (pScrn->is_gpu)
snprintf(name, 32, "%s-%d-%d", output_names[koutput->connector_type], pScrn->scrnIndex - GPU_SCREEN_OFFSET + 1, koutput->connector_type_id); snprintf(name, 32, "%s-%d-%d", output_names[koutput->connector_type], pScrn->scrnIndex - GPU_SCREEN_OFFSET + 1, koutput->connector_type_id);

View File

@ -203,6 +203,4 @@ void drmmode_copy_fb(ScrnInfoPtr pScrn, drmmode_ptr drmmode);
#define DRM_CAP_DUMB_PREFER_SHADOW 4 #define DRM_CAP_DUMB_PREFER_SHADOW 4
#endif #endif
#define MS_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#endif #endif

View File

@ -50,6 +50,7 @@
#include <xorg-config.h> #include <xorg-config.h>
#endif #endif
#include "dix.h"
#include "os.h" #include "os.h"
#include "loaderProcs.h" #include "loaderProcs.h"
#include "xf86Module.h" #include "xf86Module.h"
@ -225,7 +226,7 @@ InitPatterns(const char **patternlist)
if (patternlist) { if (patternlist) {
for (i = 0, s = patternlist; *s; i++, s++) for (i = 0, s = patternlist; *s; i++, s++)
if (*s == DEFAULT_LIST) if (*s == DEFAULT_LIST)
i += sizeof(stdPatterns) / sizeof(stdPatterns[0]) - 1 - 1; i += ARRAY_SIZE(stdPatterns) - 1 - 1;
patterns = xallocarray(i + 1, sizeof(PatternRec)); patterns = xallocarray(i + 1, sizeof(PatternRec));
if (!patterns) { if (!patterns) {
return NULL; return NULL;

View File

@ -430,7 +430,7 @@ FindDMTMode(int hsize, int vsize, int refresh, Bool rb)
int i; int i;
const DisplayModeRec *ret; const DisplayModeRec *ret;
for (i = 0; i < sizeof(DMTModes) / sizeof(DisplayModeRec); i++) { for (i = 0; i < ARRAY_SIZE(DMTModes); i++) {
ret = &DMTModes[i]; ret = &DMTModes[i];
if (!rb && xf86ModeIsReduced(ret)) if (!rb && xf86ModeIsReduced(ret))
@ -527,11 +527,9 @@ DDCModeDoInterlaceQuirks(DisplayModePtr mode)
{1440, 576}, {1440, 576},
{2880, 576}, {2880, 576},
}; };
static const int n_modes =
sizeof(cea_interlaced) / sizeof(cea_interlaced[0]);
int i; int i;
for (i = 0; i < n_modes; i++) { for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
if ((mode->HDisplay == cea_interlaced[i].w) && if ((mode->HDisplay == cea_interlaced[i].w) &&
(mode->VDisplay == cea_interlaced[i].h / 2)) { (mode->VDisplay == cea_interlaced[i].h / 2)) {
mode->VDisplay *= 2; mode->VDisplay *= 2;

View File

@ -37,14 +37,12 @@ static struct {
#endif #endif
}; };
#define numApmEvents (sizeof(bsdToXF86Array) / sizeof(bsdToXF86Array[0]))
static pmEvent static pmEvent
bsdToXF86(int type) bsdToXF86(int type)
{ {
int i; int i;
for (i = 0; i < numApmEvents; i++) { for (i = 0; i < ARRAY_SIZE(bsdToXF86Array); i++) {
if (type == bsdToXF86Array[i].apmBsd) { if (type == bsdToXF86Array[i].apmBsd) {
return bsdToXF86Array[i].xf86; return bsdToXF86Array[i].xf86;
} }

View File

@ -192,7 +192,7 @@ xf86OpenConsole()
/* Check that a supported console driver was found */ /* Check that a supported console driver was found */
if (fd < 0) { if (fd < 0) {
char cons_drivers[80] = { 0, }; char cons_drivers[80] = { 0, };
for (i = 0; i < sizeof(supported_drivers) / sizeof(char *); i++) { for (i = 0; i < ARRAY_SIZE(supported_drivers); i++) {
if (i) { if (i) {
strcat(cons_drivers, ", "); strcat(cons_drivers, ", ");
} }

View File

@ -70,14 +70,12 @@ static struct {
#endif #endif
}; };
#define numApmEvents (sizeof(bsdToXF86Array) / sizeof(bsdToXF86Array[0]))
static pmEvent static pmEvent
bsdToXF86(int type) bsdToXF86(int type)
{ {
int i; int i;
for (i = 0; i < numApmEvents; i++) { for (i = 0; i < ARRAY_SIZE(bsdToXF86Array); i++) {
if (type == bsdToXF86Array[i].apmBsd) { if (type == bsdToXF86Array[i].apmBsd) {
return bsdToXF86Array[i].xf86; return bsdToXF86Array[i].xf86;
} }

View File

@ -62,8 +62,6 @@ static struct {
#endif #endif
}; };
#define numApmEvents (sizeof(LinuxToXF86) / sizeof(LinuxToXF86[0]))
/* /*
* APM is still under construction. * APM is still under construction.
* I'm not sure if the places where I initialize/deinitialize * I'm not sure if the places where I initialize/deinitialize
@ -84,12 +82,12 @@ lnxPMGetEventFromOs(int fd, pmEvent * events, int num)
if (n > num) if (n > num)
n = num; n = num;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
for (j = 0; j < numApmEvents; j++) for (j = 0; j < ARRAY_SIZE(LinuxToXF86); j++)
if (LinuxToXF86[j].apmLinux == linuxEvents[i]) { if (LinuxToXF86[j].apmLinux == linuxEvents[i]) {
events[i] = LinuxToXF86[j].xf86; events[i] = LinuxToXF86[j].xf86;
break; break;
} }
if (j == numApmEvents) if (j == ARRAY_SIZE(LinuxToXF86))
events[i] = XF86_APM_UNKNOWN; events[i] = XF86_APM_UNKNOWN;
} }
return n; return n;

View File

@ -485,7 +485,7 @@ static struct states {
#endif #endif
}; };
static int numStates = sizeof(modemStates) / sizeof(modemStates[0]); static int numStates = ARRAY_SIZE(modemStates);
static int static int
xf2osState(int state) xf2osState(int state)

View File

@ -113,14 +113,12 @@ static struct {
#endif #endif
}; };
#define numApmEvents (sizeof(sunToXF86Array) / sizeof(sunToXF86Array[0]))
static pmEvent static pmEvent
sunToXF86(int type) sunToXF86(int type)
{ {
int i; int i;
for (i = 0; i < numApmEvents; i++) { for (i = 0; i < ARRAY_SIZE(sunToXF86Array); i++) {
if (type == sunToXF86Array[i].apmBsd) { if (type == sunToXF86Array[i].apmBsd) {
return sunToXF86Array[i].xf86; return sunToXF86Array[i].xf86;
} }

View File

@ -162,7 +162,6 @@ static PixmapFormatRec formats[] = {
{ 24, 32, BITMAP_SCANLINE_PAD }, { 24, 32, BITMAP_SCANLINE_PAD },
{ 32, 32, BITMAP_SCANLINE_PAD } { 32, 32, BITMAP_SCANLINE_PAD }
}; };
const int NUMFORMATS = sizeof(formats) / sizeof(formats[0]);
void void
DarwinPrintBanner(void) DarwinPrintBanner(void)
@ -659,8 +658,8 @@ InitOutput(ScreenInfo *pScreenInfo, int argc, char **argv)
pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER; pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
// List how we want common pixmap formats to be padded // List how we want common pixmap formats to be padded
pScreenInfo->numPixmapFormats = NUMFORMATS; pScreenInfo->numPixmapFormats = ARRAY_SIZE(formats);
for (i = 0; i < NUMFORMATS; i++) for (i = 0; i < ARRAY_SIZE(formats); i++)
pScreenInfo->formats[i] = formats[i]; pScreenInfo->formats[i] = formats[i];
// Discover screens and do mode specific initialization // Discover screens and do mode specific initialization

View File

@ -832,7 +832,7 @@ long
keysym2ucs(int keysym) keysym2ucs(int keysym)
{ {
int min = 0; int min = 0;
int max = sizeof(keysymtab) / sizeof(struct codepair) - 1; int max = ARRAY_SIZE(keysymtab) - 1;
int mid; int mid;
/* first check for Latin-1 characters (1:1 mapping) */ /* first check for Latin-1 characters (1:1 mapping) */
@ -875,7 +875,7 @@ ucs2keysym(long ucs)
static struct codepair *reverse_keysymtab; static struct codepair *reverse_keysymtab;
int min = 0; int min = 0;
int max = sizeof(keysymtab) / sizeof(struct codepair) - 1; int max = ARRAY_SIZE(keysymtab) - 1;
int mid; int mid;
if (reverse_keysymtab == NULL) { if (reverse_keysymtab == NULL) {
@ -883,7 +883,7 @@ ucs2keysym(long ucs)
memcpy(reverse_keysymtab, keysymtab, sizeof(keysymtab)); memcpy(reverse_keysymtab, keysymtab, sizeof(keysymtab));
qsort(reverse_keysymtab, qsort(reverse_keysymtab,
sizeof(keysymtab) / sizeof(struct codepair), ARRAY_SIZE(keysymtab),
sizeof(struct codepair), sizeof(struct codepair),
reverse_compare); reverse_compare);
} }

View File

@ -30,6 +30,8 @@
#ifndef KEYSYM2UCS_H #ifndef KEYSYM2UCS_H
#define KEYSYM2UCS_H 1 #define KEYSYM2UCS_H 1
#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
extern long extern long
keysym2ucs(int keysym); keysym2ucs(int keysym);
extern int extern int

View File

@ -790,7 +790,7 @@ make_dead_key(KeySym in)
{ {
int i; int i;
for (i = 0; i < sizeof(dead_keys) / sizeof(dead_keys[0]); i++) for (i = 0; i < ARRAY_SIZE(dead_keys); i++)
if (dead_keys[i].normal == in) return dead_keys[i].dead; if (dead_keys[i].normal == in) return dead_keys[i].dead;
return in; return in;
@ -975,7 +975,7 @@ QuartzReadSystemKeymap(darwinKeyboardInfo *info)
#if HACK_MISSING #if HACK_MISSING
/* Fix up some things that are normally missing.. */ /* Fix up some things that are normally missing.. */
for (i = 0; i < sizeof(known_keys) / sizeof(known_keys[0]); i++) { for (i = 0; i < ARRAY_SIZE(known_keys); i++) {
k = info->keyMap + known_keys[i].keycode * GLYPHS_PER_KEY; k = info->keyMap + known_keys[i].keycode * GLYPHS_PER_KEY;
if (k[0] == NoSymbol && k[1] == NoSymbol if (k[0] == NoSymbol && k[1] == NoSymbol
@ -987,8 +987,7 @@ QuartzReadSystemKeymap(darwinKeyboardInfo *info)
#if HACK_KEYPAD #if HACK_KEYPAD
/* And some more things. We find the right symbols for the numeric /* And some more things. We find the right symbols for the numeric
keypad, but not the KP_ keysyms. So try to convert known keycodes. */ keypad, but not the KP_ keysyms. So try to convert known keycodes. */
for (i = 0; i < sizeof(known_numeric_keys) / sizeof(known_numeric_keys[0]); for (i = 0; i < ARRAY_SIZE(known_numeric_keys); i++) {
i++) {
k = info->keyMap + known_numeric_keys[i].keycode * GLYPHS_PER_KEY; k = info->keyMap + known_numeric_keys[i].keycode * GLYPHS_PER_KEY;
if (k[0] == known_numeric_keys[i].normal) if (k[0] == known_numeric_keys[i].normal)
@ -997,8 +996,7 @@ QuartzReadSystemKeymap(darwinKeyboardInfo *info)
#endif #endif
#if HACK_BLACKLIST #if HACK_BLACKLIST
for (i = 0; i < sizeof(keycode_blacklist) / sizeof(keycode_blacklist[0]); for (i = 0; i < ARRAY_SIZE(keycode_blacklist); i++) {
i++) {
k = info->keyMap + keycode_blacklist[i] * GLYPHS_PER_KEY; k = info->keyMap + keycode_blacklist[i] * GLYPHS_PER_KEY;
k[0] = k[1] = k[2] = k[3] = NoSymbol; k[0] = k[1] = k[2] = k[3] = NoSymbol;
} }

View File

@ -37,6 +37,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
struct x_hash_table_struct { struct x_hash_table_struct {
unsigned int bucket_index; unsigned int bucket_index;
unsigned int total_keys; unsigned int total_keys;
@ -65,8 +67,6 @@ static const unsigned int bucket_sizes[] = {
1610612741 1610612741
}; };
#define N_BUCKET_SIZES (sizeof(bucket_sizes) / sizeof(bucket_sizes[0]))
static inline unsigned int static inline unsigned int
hash_table_total_buckets(x_hash_table *h) hash_table_total_buckets(x_hash_table *h)
{ {
@ -110,7 +110,7 @@ hash_table_split(x_hash_table *h)
size_t b; size_t b;
int i; int i;
if (h->bucket_index == N_BUCKET_SIZES - 1) if (h->bucket_index == ARRAY_SIZE(bucket_sizes) - 1)
return; return;
old_size = hash_table_total_buckets(h); old_size = hash_table_total_buckets(h);

View File

@ -111,8 +111,6 @@ static PixmapFormatRec g_PixmapFormats[] = {
{32, 32, BITMAP_SCANLINE_PAD} {32, 32, BITMAP_SCANLINE_PAD}
}; };
const int NUMFORMATS = sizeof(g_PixmapFormats) / sizeof(g_PixmapFormats[0]);
static const ExtensionModule xwinExtensions[] = { static const ExtensionModule xwinExtensions[] = {
#ifdef GLXEXT #ifdef GLXEXT
{ GlxExtensionInit, "GLX", &noGlxExtension }, { GlxExtensionInit, "GLX", &noGlxExtension },
@ -932,10 +930,10 @@ InitOutput(ScreenInfo * pScreenInfo, int argc, char *argv[])
pScreenInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD; pScreenInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
pScreenInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT; pScreenInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER; pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
pScreenInfo->numPixmapFormats = NUMFORMATS; pScreenInfo->numPixmapFormats = ARRAY_SIZE(g_PixmapFormats);
/* Describe how we want common pixmap formats padded */ /* Describe how we want common pixmap formats padded */
for (i = 0; i < NUMFORMATS; i++) { for (i = 0; i < ARRAY_SIZE(g_PixmapFormats); i++) {
pScreenInfo->formats[i] = g_PixmapFormats[i]; pScreenInfo->formats[i] = g_PixmapFormats[i];
} }

View File

@ -88,8 +88,6 @@
#include <winglobals.h> #include <winglobals.h>
#include <indirect.h> #include <indirect.h>
#define NUM_ELEMENTS(x) (sizeof(x)/ sizeof(x[1]))
/* Not yet in w32api */ /* Not yet in w32api */
#ifndef PFD_SUPPORT_DIRECTDRAW #ifndef PFD_SUPPORT_DIRECTDRAW
#define PFD_SUPPORT_DIRECTDRAW 0x00002000 #define PFD_SUPPORT_DIRECTDRAW 0x00002000
@ -1639,7 +1637,7 @@ fbConfigToPixelFormat(__GLXconfig * mode, PIXELFORMATDESCRIPTOR * pfdret,
return 0; return 0;
} }
#define SET_ATTR_VALUE(attr, value) { attribList[i++] = attr; attribList[i++] = value; assert(i < NUM_ELEMENTS(attribList)); } #define SET_ATTR_VALUE(attr, value) { attribList[i++] = attr; attribList[i++] = value; assert(i < ARRAY_SIZE(attribList)); }
static int static int
fbConfigToPixelFormatIndex(HDC hdc, __GLXconfig * mode, fbConfigToPixelFormatIndex(HDC hdc, __GLXconfig * mode,
@ -2018,7 +2016,7 @@ glxWinCreateConfigsExt(HDC hdc, glxWinScreen * screen)
memset(result, 0, sizeof(GLXWinConfig) * numConfigs); memset(result, 0, sizeof(GLXWinConfig) * numConfigs);
n = 0; n = 0;
#define ADD_ATTR(a) { attrs[num_attrs++] = a; assert(num_attrs < NUM_ELEMENTS(attrs)); } #define ADD_ATTR(a) { attrs[num_attrs++] = a; assert(num_attrs < ARRAY_SIZE(attrs)); }
ADD_ATTR(WGL_DRAW_TO_WINDOW_ARB); ADD_ATTR(WGL_DRAW_TO_WINDOW_ARB);
ADD_ATTR(WGL_DRAW_TO_BITMAP_ARB); ADD_ATTR(WGL_DRAW_TO_BITMAP_ARB);

View File

@ -46,6 +46,8 @@
#define WM_WM_REINIT (WM_USER + 1) #define WM_WM_REINIT (WM_USER + 1)
#define WM_WM_QUIT (WM_USER + 2) #define WM_WM_QUIT (WM_USER + 2)
#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
/* /*
* References to external symbols * References to external symbols
*/ */

View File

@ -529,7 +529,7 @@ winClipboardWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
int i,j; int i,j;
for (i = 0 ; data.targetList[i] != 0; i++) for (i = 0 ; data.targetList[i] != 0; i++)
{ {
for (j = 0; j < sizeof(target_priority_table)/sizeof(struct target_priority); j ++) for (j = 0; j < ARRAY_SIZE(target_priority_table); j ++)
{ {
if ((data.targetList[i] == target_priority_table[j].target) && if ((data.targetList[i] == target_priority_table[j].target) &&
(target_priority_table[j].priority < best_priority)) (target_priority_table[j].priority < best_priority))

View File

@ -267,8 +267,7 @@ winClipboardFlushXEvents(HWND hwnd,
32, 32,
PropModeReplace, PropModeReplace,
(unsigned char *) atomTargetArr, (unsigned char *) atomTargetArr,
(sizeof(atomTargetArr) ARRAY_SIZE(atomTargetArr));
/ sizeof(atomTargetArr[0])));
if (iReturn == BadAlloc if (iReturn == BadAlloc
|| iReturn == BadAtom || iReturn == BadAtom
|| iReturn == BadMatch || iReturn == BadMatch

View File

@ -1517,7 +1517,7 @@ winInitMultiWindowWM(WMInfoPtr pWMInfo, WMProcArgPtr pProcArg)
}; };
xcb_ewmh_set_supported(&pWMInfo->ewmh, pProcArg->dwScreen, xcb_ewmh_set_supported(&pWMInfo->ewmh, pProcArg->dwScreen,
sizeof(supported)/sizeof(xcb_atom_t), supported); ARRAY_SIZE(supported), supported);
} }
else { else {
ErrorF("winInitMultiWindowWM - xcb_ewmh_init_atoms() failed\n"); ErrorF("winInitMultiWindowWM - xcb_ewmh_init_atoms() failed\n");

View File

@ -90,8 +90,7 @@ static struct protocol protocols[] = {
#endif #endif
}; };
#define NUM_AUTHORIZATION (sizeof (protocols) /\ #define NUM_AUTHORIZATION ARRAY_SIZE(protocols)
sizeof (struct protocol))
/* /*
* Initialize all classes of authorization by reading the * Initialize all classes of authorization by reading the

View File

@ -49,6 +49,7 @@ SOFTWARE.
#endif #endif
#include <X11/keysym.h> #include <X11/keysym.h>
#include "dix.h"
#include "os.h" #include "os.h"
typedef struct _builtinColor { typedef struct _builtinColor {
@ -1628,8 +1629,6 @@ static const BuiltinColor BuiltinColors[] = {
{154, 205, 50, 7602}, /* YellowGreen */ {154, 205, 50, 7602}, /* YellowGreen */
}; };
#define NUM_BUILTIN_COLORS (sizeof (BuiltinColors) / sizeof (BuiltinColors[0]))
Bool Bool
OsLookupColor(int screen, OsLookupColor(int screen,
char *name, char *name,
@ -1642,7 +1641,7 @@ OsLookupColor(int screen,
int r; int r;
low = 0; low = 0;
high = NUM_BUILTIN_COLORS - 1; high = ARRAY_SIZE(BuiltinColors) - 1;
while (high >= low) { while (high >= low) {
mid = (low + high) / 2; mid = (low + high) / 2;
c = &BuiltinColors[mid]; c = &BuiltinColors[mid];

View File

@ -78,7 +78,7 @@ static GlyphHashSetRec glyphHashSets[] = {
{536870912, 590559793, 590559791} {536870912, 590559793, 590559791}
}; };
#define NGLYPHHASHSETS (sizeof(glyphHashSets)/sizeof(glyphHashSets[0])) #define NGLYPHHASHSETS ARRAY_SIZE(glyphHashSets)
static GlyphHashRec globalGlyphs[GlyphFormatNum]; static GlyphHashRec globalGlyphs[GlyphFormatNum];

View File

@ -1427,7 +1427,7 @@ include_bit_test_macros(void)
uint8_t mask[9] = { 0 }; uint8_t mask[9] = { 0 };
int i; int i;
for (i = 0; i < sizeof(mask) / sizeof(mask[0]); i++) { for (i = 0; i < ARRAY_SIZE(mask); i++) {
assert(BitIsOn(mask, i) == 0); assert(BitIsOn(mask, i) == 0);
SetBit(mask, i); SetBit(mask, i);
assert(BitIsOn(mask, i) == 1); assert(BitIsOn(mask, i) == 1);

View File

@ -145,13 +145,13 @@ number_formatting(void)
-0x7FFFFFFFFFFFFFFF, /* Maximum 64-bit signed number */ -0x7FFFFFFFFFFFFFFF, /* Maximum 64-bit signed number */
} ; } ;
for (i = 0; i < sizeof(unsigned_tests) / sizeof(unsigned_tests[0]); i++) for (i = 0; i < ARRAY_SIZE(unsigned_tests); i++)
assert(check_number_format_test(unsigned_tests[i])); assert(check_number_format_test(unsigned_tests[i]));
for (i = 0; i < sizeof(signed_tests) / sizeof(signed_tests[0]); i++) for (i = 0; i < ARRAY_SIZE(signed_tests); i++)
assert(check_signed_number_format_test(signed_tests[i])); assert(check_signed_number_format_test(signed_tests[i]));
for (i = 0; i < sizeof(float_tests) / sizeof(float_tests[0]); i++) for (i = 0; i < ARRAY_SIZE(float_tests); i++)
assert(check_float_format_test(float_tests[i])); assert(check_float_format_test(float_tests[i]));
} }
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
@ -366,7 +366,7 @@ static void logging_format(void)
} while(ptr); } while(ptr);
for (i = 0; i < sizeof(float_tests)/sizeof(float_tests[0]); i++) { for (i = 0; i < ARRAY_SIZE(float_tests); i++) {
double d = float_tests[i]; double d = float_tests[i];
char expected[30]; char expected[30];
sprintf(expected, "(EE) %.2f\n", d); sprintf(expected, "(EE) %.2f\n", d);

View File

@ -3,6 +3,8 @@
#include "tests.h" #include "tests.h"
#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
#define run_test(func) run_test_in_child(func, #func) #define run_test(func) run_test_in_child(func, #func)
void run_test_in_child(int (*func)(void), const char *funcname); void run_test_in_child(int (*func)(void), const char *funcname);

View File

@ -581,8 +581,6 @@ static const RESTYPE CursorRestypes[] = {
RT_WINDOW, RT_PASSIVEGRAB, RT_CURSOR RT_WINDOW, RT_PASSIVEGRAB, RT_CURSOR
}; };
#define NUM_CURSOR_RESTYPES (sizeof (CursorRestypes) / sizeof (CursorRestypes[0]))
static Bool static Bool
ReplaceCursorLookup(void *value, XID id, void *closure) ReplaceCursorLookup(void *value, XID id, void *closure)
{ {
@ -645,7 +643,7 @@ ReplaceCursor(CursorPtr pCursor, TestCursorFunc testCursor, void *closure)
for (clientIndex = 0; clientIndex < currentMaxClients; clientIndex++) { for (clientIndex = 0; clientIndex < currentMaxClients; clientIndex++) {
if (!clients[clientIndex]) if (!clients[clientIndex])
continue; continue;
for (resIndex = 0; resIndex < NUM_CURSOR_RESTYPES; resIndex++) { for (resIndex = 0; resIndex < ARRAY_SIZE(CursorRestypes); resIndex++) {
rcl.type = CursorRestypes[resIndex]; rcl.type = CursorRestypes[resIndex];
/* /*
* This function walks the entire client resource database * This function walks the entire client resource database

View File

@ -105,8 +105,6 @@ static const int version_requests[] = {
X_XFixesDestroyPointerBarrier, /* Version 5 */ X_XFixesDestroyPointerBarrier, /* Version 5 */
}; };
#define NUM_VERSION_REQUESTS (sizeof (version_requests) / sizeof (version_requests[0]))
int (*ProcXFixesVector[XFixesNumberRequests]) (ClientPtr) = { int (*ProcXFixesVector[XFixesNumberRequests]) (ClientPtr) = {
/*************** Version 1 ******************/ /*************** Version 1 ******************/
ProcXFixesQueryVersion, ProcXFixesQueryVersion,
@ -149,7 +147,7 @@ ProcXFixesDispatch(ClientPtr client)
REQUEST(xXFixesReq); REQUEST(xXFixesReq);
XFixesClientPtr pXFixesClient = GetXFixesClient(client); XFixesClientPtr pXFixesClient = GetXFixesClient(client);
if (pXFixesClient->major_version >= NUM_VERSION_REQUESTS) if (pXFixesClient->major_version >= ARRAY_SIZE(version_requests))
return BadRequest; return BadRequest;
if (stuff->xfixesReqType > version_requests[pXFixesClient->major_version]) if (stuff->xfixesReqType > version_requests[pXFixesClient->major_version])
return BadRequest; return BadRequest;

View File

@ -131,7 +131,7 @@ static XkbKeyTypeRec dflt_types[] = {
None, lnames_SHIFT_ALT} None, lnames_SHIFT_ALT}
}; };
#define num_dflt_types (sizeof(dflt_types)/sizeof(XkbKeyTypeRec)) #define num_dflt_types ARRAY_SIZE(dflt_types)
static void static void
initTypeNames(DPYTYPE dpy) initTypeNames(DPYTYPE dpy)
@ -445,7 +445,7 @@ static XkbSymInterpretRec dfltSI[69] = {
{XkbSA_SetMods, {0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}} {XkbSA_SetMods, {0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}}
}; };
#define num_dfltSI (sizeof(dfltSI)/sizeof(XkbSymInterpretRec)) #define num_dfltSI ARRAY_SIZE(dfltSI)
static XkbCompatMapRec compatMap = { static XkbCompatMapRec compatMap = {
dfltSI, dfltSI,