[PATCH xserver 4/4] Use ARRAY_SIZE all over the tree

Daniel Martin consume.noise at gmail.com
Fri Oct 27 14:11:56 UTC 2017


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, few
macros have been inlined as they had only one or two users.

Signed-off-by: Daniel Martin <consume.noise at gmail.com>
---
Just wanted to replace MS_ARRAY_SIZE in modesetting, but couldn't resist
to touch the whole tree and at least found a bug in a test. So, the
effort is justified - for me.
Couldn't (compile) test xquartz and xwin. Though, the changes in
general should be okay, except if the include to dix.h is missing.

 Xext/geext.c                                     |  3 +--
 Xext/saver.c                                     |  6 ++----
 Xi/xiproperty.c                                  |  6 ++----
 composite/compinit.c                             |  5 +----
 damageext/damageext.c                            |  4 +---
 dix/eventconvert.c                               |  4 +---
 dix/gc.c                                         |  2 +-
 dix/privates.c                                   |  4 +---
 glamor/glamor_utils.h                            |  4 ----
 hw/kdrive/src/kdrive.c                           |  4 +---
 hw/xfree86/common/modeline2c.awk                 |  2 +-
 hw/xfree86/common/xf86Configure.c                |  2 +-
 hw/xfree86/common/xf86Mode.c                     |  4 ++--
 hw/xfree86/dri2/dri2.c                           |  2 +-
 hw/xfree86/drivers/modesetting/drmmode_display.c |  2 +-
 hw/xfree86/drivers/modesetting/drmmode_display.h |  2 --
 hw/xfree86/loader/loadmod.c                      |  3 ++-
 hw/xfree86/modes/xf86EdidModes.c                 |  6 ++----
 hw/xfree86/os-support/bsd/bsd_apm.c              |  4 +---
 hw/xfree86/os-support/bsd/bsd_init.c             |  2 +-
 hw/xfree86/os-support/bsd/bsd_kqueue_apm.c       |  4 +---
 hw/xfree86/os-support/linux/lnx_apm.c            |  6 ++----
 hw/xfree86/os-support/shared/posix_tty.c         |  2 +-
 hw/xfree86/os-support/solaris/sun_apm.c          |  4 +---
 hw/xquartz/darwin.c                              |  5 ++---
 hw/xquartz/keysym2ucs.c                          |  6 +++---
 hw/xquartz/quartzKeyboard.c                      | 10 ++++------
 hw/xquartz/xpr/x-hash.c                          |  4 +---
 hw/xwin/InitOutput.c                             |  6 ++----
 hw/xwin/glx/indirect.c                           |  6 ++----
 hw/xwin/winclipboard/wndproc.c                   |  2 +-
 hw/xwin/winclipboard/xevents.c                   |  3 +--
 hw/xwin/winmultiwindowwm.c                       |  2 +-
 os/auth.c                                        |  3 +--
 os/oscolor.c                                     |  5 ++---
 render/glyph.c                                   |  2 +-
 test/input.c                                     |  2 +-
 test/signal-logging.c                            |  8 ++++----
 test/tests-common.h                              |  2 ++
 xfixes/cursor.c                                  |  4 +---
 xfixes/xfixes.c                                  |  4 +---
 xkb/xkbDflts.h                                   |  4 ++--
 42 files changed, 60 insertions(+), 105 deletions(-)

diff --git a/Xext/geext.c b/Xext/geext.c
index 08dff280b..5009c081a 100644
--- a/Xext/geext.c
+++ b/Xext/geext.c
@@ -47,7 +47,6 @@ static const int version_requests[] = {
 /* Forward declarations */
 static void SGEGenericEvent(xEvent *from, xEvent *to);
 
-#define NUM_VERSION_REQUESTS	(sizeof (version_requests) / sizeof (version_requests[0]))
 #define EXT_MASK(ext) ((ext) & 0x7F)
 
 /************************************************************/
@@ -127,7 +126,7 @@ ProcGEDispatch(ClientPtr client)
 
     REQUEST(xGEReq);
 
-    if (pGEClient->major_version >= NUM_VERSION_REQUESTS)
+    if (pGEClient->major_version >= ARRAY_SIZE(version_requests))
         return BadRequest;
     if (stuff->ReqType > version_requests[pGEClient->major_version])
         return BadRequest;
diff --git a/Xext/saver.c b/Xext/saver.c
index f6090d8da..4d9e6b974 100644
--- a/Xext/saver.c
+++ b/Xext/saver.c
@@ -1268,14 +1268,12 @@ ProcScreenSaverQueryVersion,
         ProcScreenSaverSetAttributes,
         ProcScreenSaverUnsetAttributes, ProcScreenSaverSuspend,};
 
-#define NUM_REQUESTS	((sizeof NormalVector) / (sizeof NormalVector[0]))
-
 static int
 ProcScreenSaverDispatch(ClientPtr client)
 {
     REQUEST(xReq);
 
-    if (stuff->data < NUM_REQUESTS)
+    if (stuff->data < ARRAY_SIZE(NormalVector))
         return (*NormalVector[stuff->data]) (client);
     return BadRequest;
 }
@@ -1360,7 +1358,7 @@ SProcScreenSaverDispatch(ClientPtr client)
 {
     REQUEST(xReq);
 
-    if (stuff->data < NUM_REQUESTS)
+    if (stuff->data < ARRAY_SIZE(NormalVector))
         return (*SwappedVector[stuff->data]) (client);
     return BadRequest;
 }
diff --git a/Xi/xiproperty.c b/Xi/xiproperty.c
index 9acb2ebe3..6ec419e87 100644
--- a/Xi/xiproperty.c
+++ b/Xi/xiproperty.c
@@ -372,8 +372,7 @@ XIGetKnownProperty(const char *name)
     if (!name)
         return None;
 
-    for (i = 0; i < (sizeof(dev_properties) / sizeof(struct dev_properties));
-         i++) {
+    for (i = 0; i < ARRAY_SIZE(dev_properties); i++) {
         if (strcmp(name, dev_properties[i].name) == 0) {
             if (dev_properties[i].type == None) {
                 dev_properties[i].type =
@@ -393,8 +392,7 @@ XIResetProperties(void)
 {
     int i;
 
-    for (i = 0; i < (sizeof(dev_properties) / sizeof(struct dev_properties));
-         i++)
+    for (i = 0; i < ARRAY_SIZE(dev_properties); i++)
         dev_properties[i].type = None;
 }
 
diff --git a/composite/compinit.c b/composite/compinit.c
index 791fec922..2590aa46d 100644
--- a/composite/compinit.c
+++ b/composite/compinit.c
@@ -284,9 +284,6 @@ static CompAlternateVisual altVisuals[] = {
     {32, PICT_a8r8g8b8},
 };
 
-static const int NUM_COMP_ALTERNATE_VISUALS = sizeof(altVisuals) /
-    sizeof(CompAlternateVisual);
-
 static Bool
 compAddAlternateVisual(ScreenPtr pScreen, CompScreenPtr cs,
                        CompAlternateVisual * alt)
@@ -362,7 +359,7 @@ compAddAlternateVisuals(ScreenPtr pScreen, CompScreenPtr cs)
 {
     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);
 
     return ! !ret;
diff --git a/damageext/damageext.c b/damageext/damageext.c
index b186d2c40..ce490cbf2 100644
--- a/damageext/damageext.c
+++ b/damageext/damageext.c
@@ -464,8 +464,6 @@ static const int version_requests[] = {
     X_DamageAdd,                /* Version 1 */
 };
 
-#define NUM_VERSION_REQUESTS	(sizeof (version_requests) / sizeof (version_requests[0]))
-
 static int (*ProcDamageVector[XDamageNumberRequests]) (ClientPtr) = {
     /*************** Version 1 ******************/
     ProcDamageQueryVersion,
@@ -482,7 +480,7 @@ ProcDamageDispatch(ClientPtr client)
     REQUEST(xDamageReq);
     DamageClientPtr pDamageClient = GetDamageClient(client);
 
-    if (pDamageClient->major_version >= NUM_VERSION_REQUESTS)
+    if (pDamageClient->major_version >= ARRAY_SIZE(version_requests))
         return BadRequest;
     if (stuff->damageReqType > version_requests[pDamageClient->major_version])
         return BadRequest;
diff --git a/dix/eventconvert.c b/dix/eventconvert.c
index f7ecdba77..4a07b6b6f 100644
--- a/dix/eventconvert.c
+++ b/dix/eventconvert.c
@@ -655,9 +655,7 @@ eventToDeviceEvent(DeviceEvent *ev, xEvent **xi)
     btlen = bytes_to_int32(bits_to_bytes(MAX_BUTTONS));
     len += btlen * 4;           /* buttonmask len */
 
-    vallen =
-        count_bits(ev->valuators.mask,
-                   sizeof(ev->valuators.mask) / sizeof(ev->valuators.mask[0]));
+    vallen = count_bits(ev->valuators.mask, ARRAY_SIZE(ev->valuators.mask));
     len += vallen * 2 * sizeof(uint32_t);       /* axisvalues */
     vallen = bytes_to_int32(bits_to_bytes(MAX_VALUATORS));
     len += vallen * 4;          /* valuators mask */
diff --git a/dix/gc.c b/dix/gc.c
index cb8db8535..4ccbd3b54 100644
--- a/dix/gc.c
+++ b/dix/gc.c
@@ -439,7 +439,7 @@ ChangeGCXIDs(ClientPtr client, GC * pGC, BITS32 mask, CARD32 *pC32)
     }
     for (i = Ones(mask); 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;
 
         if (!(mask & xidfields[i].mask))
diff --git a/dix/privates.c b/dix/privates.c
index 9a4e61138..9ca80f0b6 100644
--- a/dix/privates.c
+++ b/dix/privates.c
@@ -571,8 +571,6 @@ static const int offsets[] = {
     offsetof(ColormapRec, devPrivates), /* RT_COLORMAP */
 };
 
-#define NUM_OFFSETS	(sizeof (offsets) / sizeof (offsets[0]))
-
 int
 dixLookupPrivateOffset(RESTYPE type)
 {
@@ -587,7 +585,7 @@ dixLookupPrivateOffset(RESTYPE type)
             return offsets[RT_PIXMAP & TypeMask];
     }
     type = type & TypeMask;
-    if (type < NUM_OFFSETS)
+    if (type < ARRAY_SIZE(offsets))
         return offsets[type];
     return -1;
 }
diff --git a/glamor/glamor_utils.h b/glamor/glamor_utils.h
index 6e9d88674..090b0e18a 100644
--- a/glamor/glamor_utils.h
+++ b/glamor/glamor_utils.h
@@ -562,10 +562,6 @@
         (c)[1] = (float)y;				\
     } 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 MIN(a,b)	((a) < (b) ? (a) : (b))
 #define MAX(a,b)	((a) > (b) ? (a) : (b))
diff --git a/hw/kdrive/src/kdrive.c b/hw/kdrive/src/kdrive.c
index 30e0ced70..82dcf4ae7 100644
--- a/hw/kdrive/src/kdrive.c
+++ b/hw/kdrive/src/kdrive.c
@@ -66,8 +66,6 @@ KdDepths kdDepths[] = {
     {32, 32}
 };
 
-#define NUM_KD_DEPTHS (sizeof (kdDepths) / sizeof (kdDepths[0]))
-
 #define KD_DEFAULT_BUTTONS 5
 
 DevPrivateKeyRec kdScreenPrivateKeyRec;
@@ -886,7 +884,7 @@ KdSetPixmapFormats(ScreenInfo * pScreenInfo)
     /*
      * 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])
             depthToBpp[kdDepths[i].depth] = kdDepths[i].bpp;
 
diff --git a/hw/xfree86/common/modeline2c.awk b/hw/xfree86/common/modeline2c.awk
index 038e7e935..3c9f602d1 100644
--- a/hw/xfree86/common/modeline2c.awk
+++ b/hw/xfree86/common/modeline2c.awk
@@ -89,5 +89,5 @@ BEGIN {
 
 END {
 	print "};"
-	printf "const int xf86NumDefaultModes = sizeof(xf86DefaultModes) / sizeof(DisplayModeRec);"
+	printf "const int xf86NumDefaultModes = ARRAY_SIZE(xf86DefaultModes);"
 }
diff --git a/hw/xfree86/common/xf86Configure.c b/hw/xfree86/common/xf86Configure.c
index 07d3c6319..672cfd6e8 100644
--- a/hw/xfree86/common/xf86Configure.c
+++ b/hw/xfree86/common/xf86Configure.c
@@ -206,7 +206,7 @@ configureScreenSection(int screennum)
     XNFasprintf(&tmp, "Card%d", screennum);
     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;
 
         conf_display = calloc(1, sizeof(XF86ConfDisplayRec));
diff --git a/hw/xfree86/common/xf86Mode.c b/hw/xfree86/common/xf86Mode.c
index 3053e53f1..ceba87f78 100644
--- a/hw/xfree86/common/xf86Mode.c
+++ b/hw/xfree86/common/xf86Mode.c
@@ -477,7 +477,7 @@ xf86LookupMode(ScrnInfoPtr scrp, DisplayModePtr modep,
         M_T_DRIVER,
         0
     };
-    const int ntypes = sizeof(types) / sizeof(int);
+    const int ntypes = ARRAY_SIZE(types);
 
     strategy &= ~(LOOKUP_CLKDIV2 | LOOKUP_OPTIONAL_TOLERANCES);
 
@@ -1853,7 +1853,7 @@ xf86ValidateModes(ScrnInfoPtr scrp, DisplayModePtr availModes,
                 M_T_DRIVER,
                 0
             };
-            const int ntypes = sizeof(types) / sizeof(int);
+            const int ntypes = ARRAY_SIZE(types);
             int n;
 
             /*
diff --git a/hw/xfree86/dri2/dri2.c b/hw/xfree86/dri2/dri2.c
index 75abc18ac..6619e3aa7 100644
--- a/hw/xfree86/dri2/dri2.c
+++ b/hw/xfree86/dri2/dri2.c
@@ -1633,7 +1633,7 @@ DRI2ScreenInit(ScreenPtr pScreen, DRI2InfoPtr info)
     pScreen->SetWindowPixmap = DRI2SetWindowPixmap;
 
     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]) {
             xf86DrvMsg(pScreen->myNum, X_INFO, "[DRI2]   %s driver: %s\n",
                        driverTypeNames[i], ds->driverNames[i]);
diff --git a/hw/xfree86/drivers/modesetting/drmmode_display.c b/hw/xfree86/drivers/modesetting/drmmode_display.c
index e14833dee..f7298b1d8 100644
--- a/hw/xfree86/drivers/modesetting/drmmode_display.c
+++ b/hw/xfree86/drivers/modesetting/drmmode_display.c
@@ -1688,7 +1688,7 @@ drmmode_create_name(ScrnInfoPtr pScrn, drmModeConnectorPtr koutput, char *name,
     return;
 
  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);
     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);
diff --git a/hw/xfree86/drivers/modesetting/drmmode_display.h b/hw/xfree86/drivers/modesetting/drmmode_display.h
index c86e13f56..2630f9347 100644
--- a/hw/xfree86/drivers/modesetting/drmmode_display.h
+++ b/hw/xfree86/drivers/modesetting/drmmode_display.h
@@ -203,6 +203,4 @@ void drmmode_copy_fb(ScrnInfoPtr pScrn, drmmode_ptr drmmode);
 #define DRM_CAP_DUMB_PREFER_SHADOW 4
 #endif
 
-#define MS_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
-
 #endif
diff --git a/hw/xfree86/loader/loadmod.c b/hw/xfree86/loader/loadmod.c
index 506350085..a6356bd8f 100644
--- a/hw/xfree86/loader/loadmod.c
+++ b/hw/xfree86/loader/loadmod.c
@@ -50,6 +50,7 @@
 #include <xorg-config.h>
 #endif
 
+#include "dix.h"
 #include "os.h"
 #include "loaderProcs.h"
 #include "xf86Module.h"
@@ -225,7 +226,7 @@ InitPatterns(const char **patternlist)
     if (patternlist) {
         for (i = 0, s = patternlist; *s; i++, s++)
             if (*s == DEFAULT_LIST)
-                i += sizeof(stdPatterns) / sizeof(stdPatterns[0]) - 1 - 1;
+                i += ARRAY_SIZE(stdPatterns) - 1 - 1;
         patterns = xallocarray(i + 1, sizeof(PatternRec));
         if (!patterns) {
             return NULL;
diff --git a/hw/xfree86/modes/xf86EdidModes.c b/hw/xfree86/modes/xf86EdidModes.c
index a2e960d42..908c5a4cf 100644
--- a/hw/xfree86/modes/xf86EdidModes.c
+++ b/hw/xfree86/modes/xf86EdidModes.c
@@ -430,7 +430,7 @@ FindDMTMode(int hsize, int vsize, int refresh, Bool rb)
     int i;
     const DisplayModeRec *ret;
 
-    for (i = 0; i < sizeof(DMTModes) / sizeof(DisplayModeRec); i++) {
+    for (i = 0; i < ARRAY_SIZE(DMTModes); i++) {
         ret = &DMTModes[i];
 
         if (!rb && xf86ModeIsReduced(ret))
@@ -527,11 +527,9 @@ DDCModeDoInterlaceQuirks(DisplayModePtr mode)
         {1440, 576},
         {2880, 576},
     };
-    static const int n_modes =
-        sizeof(cea_interlaced) / sizeof(cea_interlaced[0]);
     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) &&
             (mode->VDisplay == cea_interlaced[i].h / 2)) {
             mode->VDisplay *= 2;
diff --git a/hw/xfree86/os-support/bsd/bsd_apm.c b/hw/xfree86/os-support/bsd/bsd_apm.c
index 60a19d79a..dab0a9a7d 100644
--- a/hw/xfree86/os-support/bsd/bsd_apm.c
+++ b/hw/xfree86/os-support/bsd/bsd_apm.c
@@ -37,14 +37,12 @@ static struct {
 #endif
 };
 
-#define numApmEvents (sizeof(bsdToXF86Array) / sizeof(bsdToXF86Array[0]))
-
 static pmEvent
 bsdToXF86(int type)
 {
     int i;
 
-    for (i = 0; i < numApmEvents; i++) {
+    for (i = 0; i < ARRAY_SIZE(bsdToXF86Array); i++) {
         if (type == bsdToXF86Array[i].apmBsd) {
             return bsdToXF86Array[i].xf86;
         }
diff --git a/hw/xfree86/os-support/bsd/bsd_init.c b/hw/xfree86/os-support/bsd/bsd_init.c
index 0d5d3c87c..75a719fc2 100644
--- a/hw/xfree86/os-support/bsd/bsd_init.c
+++ b/hw/xfree86/os-support/bsd/bsd_init.c
@@ -192,7 +192,7 @@ xf86OpenConsole()
         /* Check that a supported console driver was found */
         if (fd < 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) {
                     strcat(cons_drivers, ", ");
                 }
diff --git a/hw/xfree86/os-support/bsd/bsd_kqueue_apm.c b/hw/xfree86/os-support/bsd/bsd_kqueue_apm.c
index 1756612b4..b046a1aa8 100644
--- a/hw/xfree86/os-support/bsd/bsd_kqueue_apm.c
+++ b/hw/xfree86/os-support/bsd/bsd_kqueue_apm.c
@@ -70,14 +70,12 @@ static struct {
 #endif
 };
 
-#define numApmEvents (sizeof(bsdToXF86Array) / sizeof(bsdToXF86Array[0]))
-
 static pmEvent
 bsdToXF86(int type)
 {
     int i;
 
-    for (i = 0; i < numApmEvents; i++) {
+    for (i = 0; i < ARRAY_SIZE(bsdToXF86Array); i++) {
         if (type == bsdToXF86Array[i].apmBsd) {
             return bsdToXF86Array[i].xf86;
         }
diff --git a/hw/xfree86/os-support/linux/lnx_apm.c b/hw/xfree86/os-support/linux/lnx_apm.c
index 621691997..29c363360 100644
--- a/hw/xfree86/os-support/linux/lnx_apm.c
+++ b/hw/xfree86/os-support/linux/lnx_apm.c
@@ -62,8 +62,6 @@ static struct {
 #endif
 };
 
-#define numApmEvents (sizeof(LinuxToXF86) / sizeof(LinuxToXF86[0]))
-
 /*
  * APM is still under construction.
  * 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)
         n = num;
     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]) {
                 events[i] = LinuxToXF86[j].xf86;
                 break;
             }
-        if (j == numApmEvents)
+        if (j == ARRAY_SIZE(LinuxToXF86))
             events[i] = XF86_APM_UNKNOWN;
     }
     return n;
diff --git a/hw/xfree86/os-support/shared/posix_tty.c b/hw/xfree86/os-support/shared/posix_tty.c
index a795ae19d..6768d290b 100644
--- a/hw/xfree86/os-support/shared/posix_tty.c
+++ b/hw/xfree86/os-support/shared/posix_tty.c
@@ -485,7 +485,7 @@ static struct states {
 #endif
 };
 
-static int numStates = sizeof(modemStates) / sizeof(modemStates[0]);
+static int numStates = ARRAY_SIZE(modemStates);
 
 static int
 xf2osState(int state)
diff --git a/hw/xfree86/os-support/solaris/sun_apm.c b/hw/xfree86/os-support/solaris/sun_apm.c
index 4d4dbc105..dbdc4b677 100644
--- a/hw/xfree86/os-support/solaris/sun_apm.c
+++ b/hw/xfree86/os-support/solaris/sun_apm.c
@@ -113,14 +113,12 @@ static struct {
 #endif
 };
 
-#define numApmEvents (sizeof(sunToXF86Array) / sizeof(sunToXF86Array[0]))
-
 static pmEvent
 sunToXF86(int type)
 {
     int i;
 
-    for (i = 0; i < numApmEvents; i++) {
+    for (i = 0; i < ARRAY_SIZE(sunToXF86Array); i++) {
         if (type == sunToXF86Array[i].apmBsd) {
             return sunToXF86Array[i].xf86;
         }
diff --git a/hw/xquartz/darwin.c b/hw/xquartz/darwin.c
index 36c149cba..71a65f3ce 100644
--- a/hw/xquartz/darwin.c
+++ b/hw/xquartz/darwin.c
@@ -162,7 +162,6 @@ static PixmapFormatRec formats[] = {
     { 24, 32, BITMAP_SCANLINE_PAD    },
     { 32, 32, BITMAP_SCANLINE_PAD    }
 };
-const int NUMFORMATS = sizeof(formats) / sizeof(formats[0]);
 
 void
 DarwinPrintBanner(void)
@@ -659,8 +658,8 @@ InitOutput(ScreenInfo *pScreenInfo, int argc, char **argv)
     pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
 
     // List how we want common pixmap formats to be padded
-    pScreenInfo->numPixmapFormats = NUMFORMATS;
-    for (i = 0; i < NUMFORMATS; i++)
+    pScreenInfo->numPixmapFormats = ARRAY_SIZE(formats);
+    for (i = 0; i < ARRAY_SIZE(formats); i++)
         pScreenInfo->formats[i] = formats[i];
 
     // Discover screens and do mode specific initialization
diff --git a/hw/xquartz/keysym2ucs.c b/hw/xquartz/keysym2ucs.c
index 934b57756..12da19d67 100644
--- a/hw/xquartz/keysym2ucs.c
+++ b/hw/xquartz/keysym2ucs.c
@@ -832,7 +832,7 @@ long
 keysym2ucs(int keysym)
 {
     int min = 0;
-    int max = sizeof(keysymtab) / sizeof(struct codepair) - 1;
+    int max = ARRAY_SIZE(keysymtab) - 1;
     int mid;
 
     /* first check for Latin-1 characters (1:1 mapping) */
@@ -875,7 +875,7 @@ ucs2keysym(long ucs)
     static struct codepair *reverse_keysymtab;
 
     int min = 0;
-    int max = sizeof(keysymtab) / sizeof(struct codepair) - 1;
+    int max = ARRAY_SIZE(keysymtab) - 1;
     int mid;
 
     if (reverse_keysymtab == NULL) {
@@ -883,7 +883,7 @@ ucs2keysym(long ucs)
         memcpy(reverse_keysymtab, keysymtab, sizeof(keysymtab));
 
         qsort(reverse_keysymtab,
-              sizeof(keysymtab) / sizeof(struct codepair),
+              ARRAY_SIZE(keysymtab),
               sizeof(struct codepair),
               reverse_compare);
     }
diff --git a/hw/xquartz/quartzKeyboard.c b/hw/xquartz/quartzKeyboard.c
index 2fed59306..755fe0990 100644
--- a/hw/xquartz/quartzKeyboard.c
+++ b/hw/xquartz/quartzKeyboard.c
@@ -790,7 +790,7 @@ make_dead_key(KeySym in)
 {
     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;
 
     return in;
@@ -975,7 +975,7 @@ QuartzReadSystemKeymap(darwinKeyboardInfo *info)
 #if HACK_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;
 
         if (k[0] == NoSymbol && k[1] == NoSymbol
@@ -987,8 +987,7 @@ QuartzReadSystemKeymap(darwinKeyboardInfo *info)
 #if HACK_KEYPAD
     /* And some more things. We find the right symbols for the numeric
        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]);
-         i++) {
+    for (i = 0; i < ARRAY_SIZE(known_numeric_keys); i++) {
         k = info->keyMap + known_numeric_keys[i].keycode * GLYPHS_PER_KEY;
 
         if (k[0] == known_numeric_keys[i].normal)
@@ -997,8 +996,7 @@ QuartzReadSystemKeymap(darwinKeyboardInfo *info)
 #endif
 
 #if HACK_BLACKLIST
-    for (i = 0; i < sizeof(keycode_blacklist) / sizeof(keycode_blacklist[0]);
-         i++) {
+    for (i = 0; i < ARRAY_SIZE(keycode_blacklist); i++) {
         k = info->keyMap + keycode_blacklist[i] * GLYPHS_PER_KEY;
         k[0] = k[1] = k[2] = k[3] = NoSymbol;
     }
diff --git a/hw/xquartz/xpr/x-hash.c b/hw/xquartz/xpr/x-hash.c
index 26e079ff0..6d6f28b88 100644
--- a/hw/xquartz/xpr/x-hash.c
+++ b/hw/xquartz/xpr/x-hash.c
@@ -65,8 +65,6 @@ static const unsigned int bucket_sizes[] = {
     1610612741
 };
 
-#define N_BUCKET_SIZES (sizeof(bucket_sizes) / sizeof(bucket_sizes[0]))
-
 static inline unsigned int
 hash_table_total_buckets(x_hash_table *h)
 {
@@ -110,7 +108,7 @@ hash_table_split(x_hash_table *h)
     size_t b;
     int i;
 
-    if (h->bucket_index == N_BUCKET_SIZES - 1)
+    if (h->bucket_index == ARRAY_SIZE(bucket_sizes) - 1)
         return;
 
     old_size = hash_table_total_buckets(h);
diff --git a/hw/xwin/InitOutput.c b/hw/xwin/InitOutput.c
index 168c47fc4..95fc68bea 100644
--- a/hw/xwin/InitOutput.c
+++ b/hw/xwin/InitOutput.c
@@ -111,8 +111,6 @@ static PixmapFormatRec g_PixmapFormats[] = {
     {32, 32, BITMAP_SCANLINE_PAD}
 };
 
-const int NUMFORMATS = sizeof(g_PixmapFormats) / sizeof(g_PixmapFormats[0]);
-
 static const ExtensionModule xwinExtensions[] = {
 #ifdef GLXEXT
   { GlxExtensionInit, "GLX", &noGlxExtension },
@@ -932,10 +930,10 @@ InitOutput(ScreenInfo * pScreenInfo, int argc, char *argv[])
     pScreenInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
     pScreenInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
     pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
-    pScreenInfo->numPixmapFormats = NUMFORMATS;
+    pScreenInfo->numPixmapFormats = ARRAY_SIZE(g_PixmapFormats);
 
     /* 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];
     }
 
diff --git a/hw/xwin/glx/indirect.c b/hw/xwin/glx/indirect.c
index d6640700e..fb46e1c6a 100644
--- a/hw/xwin/glx/indirect.c
+++ b/hw/xwin/glx/indirect.c
@@ -88,8 +88,6 @@
 #include <winglobals.h>
 #include <indirect.h>
 
-#define NUM_ELEMENTS(x) (sizeof(x)/ sizeof(x[1]))
-
 /* Not yet in w32api */
 #ifndef PFD_SUPPORT_DIRECTDRAW
 #define PFD_SUPPORT_DIRECTDRAW   0x00002000
@@ -1639,7 +1637,7 @@ fbConfigToPixelFormat(__GLXconfig * mode, PIXELFORMATDESCRIPTOR * pfdret,
     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
 fbConfigToPixelFormatIndex(HDC hdc, __GLXconfig * mode,
@@ -2018,7 +2016,7 @@ glxWinCreateConfigsExt(HDC hdc, glxWinScreen * screen)
     memset(result, 0, sizeof(GLXWinConfig) * numConfigs);
     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_BITMAP_ARB);
diff --git a/hw/xwin/winclipboard/wndproc.c b/hw/xwin/winclipboard/wndproc.c
index d289f7755..c8d0064bd 100644
--- a/hw/xwin/winclipboard/wndproc.c
+++ b/hw/xwin/winclipboard/wndproc.c
@@ -529,7 +529,7 @@ winClipboardWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
             int i,j;
             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) &&
                                 (target_priority_table[j].priority < best_priority))
diff --git a/hw/xwin/winclipboard/xevents.c b/hw/xwin/winclipboard/xevents.c
index aee6c86a2..d4ea97fc3 100644
--- a/hw/xwin/winclipboard/xevents.c
+++ b/hw/xwin/winclipboard/xevents.c
@@ -267,8 +267,7 @@ winClipboardFlushXEvents(HWND hwnd,
                                           32,
                                           PropModeReplace,
                                           (unsigned char *) atomTargetArr,
-                                          (sizeof(atomTargetArr)
-                                           / sizeof(atomTargetArr[0])));
+                                          ARRAY_SIZE(atomTargetArr));
                 if (iReturn == BadAlloc
                     || iReturn == BadAtom
                     || iReturn == BadMatch
diff --git a/hw/xwin/winmultiwindowwm.c b/hw/xwin/winmultiwindowwm.c
index c68387d81..32bc722e2 100644
--- a/hw/xwin/winmultiwindowwm.c
+++ b/hw/xwin/winmultiwindowwm.c
@@ -1517,7 +1517,7 @@ winInitMultiWindowWM(WMInfoPtr pWMInfo, WMProcArgPtr pProcArg)
                 };
 
             xcb_ewmh_set_supported(&pWMInfo->ewmh, pProcArg->dwScreen,
-                                   sizeof(supported)/sizeof(xcb_atom_t), supported);
+                                   ARRAY_SIZE(supported), supported);
         }
         else {
             ErrorF("winInitMultiWindowWM - xcb_ewmh_init_atoms() failed\n");
diff --git a/os/auth.c b/os/auth.c
index 9d87c1f39..da8b70985 100644
--- a/os/auth.c
+++ b/os/auth.c
@@ -90,8 +90,7 @@ static struct protocol protocols[] = {
 #endif
 };
 
-#define NUM_AUTHORIZATION  (sizeof (protocols) /\
-			     sizeof (struct protocol))
+#define NUM_AUTHORIZATION  ARRAY_SIZE(protocols)
 
 /*
  * Initialize all classes of authorization by reading the
diff --git a/os/oscolor.c b/os/oscolor.c
index 017aab1f2..af83c6ada 100644
--- a/os/oscolor.c
+++ b/os/oscolor.c
@@ -49,6 +49,7 @@ SOFTWARE.
 #endif
 
 #include <X11/keysym.h>
+#include "dix.h"
 #include "os.h"
 
 typedef struct _builtinColor {
@@ -1628,8 +1629,6 @@ static const BuiltinColor BuiltinColors[] = {
     {154, 205, 50, 7602},       /* YellowGreen */
 };
 
-#define NUM_BUILTIN_COLORS  (sizeof (BuiltinColors) / sizeof (BuiltinColors[0]))
-
 Bool
 OsLookupColor(int screen,
               char *name,
@@ -1642,7 +1641,7 @@ OsLookupColor(int screen,
     int r;
 
     low = 0;
-    high = NUM_BUILTIN_COLORS - 1;
+    high = ARRAY_SIZE(BuiltinColors) - 1;
     while (high >= low) {
         mid = (low + high) / 2;
         c = &BuiltinColors[mid];
diff --git a/render/glyph.c b/render/glyph.c
index ea865af58..f3ed9cf4c 100644
--- a/render/glyph.c
+++ b/render/glyph.c
@@ -78,7 +78,7 @@ static GlyphHashSetRec glyphHashSets[] = {
     {536870912, 590559793, 590559791}
 };
 
-#define NGLYPHHASHSETS	(sizeof(glyphHashSets)/sizeof(glyphHashSets[0]))
+#define NGLYPHHASHSETS	ARRAY_SIZE(glyphHashSets)
 
 static GlyphHashRec globalGlyphs[GlyphFormatNum];
 
diff --git a/test/input.c b/test/input.c
index 8638f1443..9ff1a2fab 100644
--- a/test/input.c
+++ b/test/input.c
@@ -1427,7 +1427,7 @@ include_bit_test_macros(void)
     uint8_t mask[9] = { 0 };
     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);
         SetBit(mask, i);
         assert(BitIsOn(mask, i) == 1);
diff --git a/test/signal-logging.c b/test/signal-logging.c
index ca327e9aa..afbdeb241 100644
--- a/test/signal-logging.c
+++ b/test/signal-logging.c
@@ -145,13 +145,13 @@ number_formatting(void)
                                 -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]));
 
-    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]));
 
-    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]));
 }
 #pragma GCC diagnostic pop
@@ -366,7 +366,7 @@ static void logging_format(void)
     } 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];
         char expected[30];
         sprintf(expected, "(EE) %.2f\n", d);
diff --git a/test/tests-common.h b/test/tests-common.h
index d5286caf2..ea0642247 100644
--- a/test/tests-common.h
+++ b/test/tests-common.h
@@ -3,6 +3,8 @@
 
 #include "tests.h"
 
+#define ARRAY_SIZE(a)  (sizeof((a)) / sizeof((a)[0]))
+
 #define run_test(func) run_test_in_child(func, #func)
 
 void run_test_in_child(int (*func)(void), const char *funcname);
diff --git a/xfixes/cursor.c b/xfixes/cursor.c
index dc447ed6f..09cf2fa69 100644
--- a/xfixes/cursor.c
+++ b/xfixes/cursor.c
@@ -581,8 +581,6 @@ static const RESTYPE CursorRestypes[] = {
     RT_WINDOW, RT_PASSIVEGRAB, RT_CURSOR
 };
 
-#define NUM_CURSOR_RESTYPES (sizeof (CursorRestypes) / sizeof (CursorRestypes[0]))
-
 static Bool
 ReplaceCursorLookup(void *value, XID id, void *closure)
 {
@@ -645,7 +643,7 @@ ReplaceCursor(CursorPtr pCursor, TestCursorFunc testCursor, void *closure)
     for (clientIndex = 0; clientIndex < currentMaxClients; clientIndex++) {
         if (!clients[clientIndex])
             continue;
-        for (resIndex = 0; resIndex < NUM_CURSOR_RESTYPES; resIndex++) {
+        for (resIndex = 0; resIndex < ARRAY_SIZE(CursorRestypes); resIndex++) {
             rcl.type = CursorRestypes[resIndex];
             /*
              * This function walks the entire client resource database
diff --git a/xfixes/xfixes.c b/xfixes/xfixes.c
index 248bf0287..53a6bd62f 100644
--- a/xfixes/xfixes.c
+++ b/xfixes/xfixes.c
@@ -105,8 +105,6 @@ static const int version_requests[] = {
     X_XFixesDestroyPointerBarrier,      /* Version 5 */
 };
 
-#define NUM_VERSION_REQUESTS	(sizeof (version_requests) / sizeof (version_requests[0]))
-
 int (*ProcXFixesVector[XFixesNumberRequests]) (ClientPtr) = {
 /*************** Version 1 ******************/
     ProcXFixesQueryVersion,
@@ -149,7 +147,7 @@ ProcXFixesDispatch(ClientPtr client)
     REQUEST(xXFixesReq);
     XFixesClientPtr pXFixesClient = GetXFixesClient(client);
 
-    if (pXFixesClient->major_version >= NUM_VERSION_REQUESTS)
+    if (pXFixesClient->major_version >= ARRAY_SIZE(version_requests))
         return BadRequest;
     if (stuff->xfixesReqType > version_requests[pXFixesClient->major_version])
         return BadRequest;
diff --git a/xkb/xkbDflts.h b/xkb/xkbDflts.h
index 994726c0f..b02dff9de 100644
--- a/xkb/xkbDflts.h
+++ b/xkb/xkbDflts.h
@@ -131,7 +131,7 @@ static XkbKeyTypeRec dflt_types[] = {
      None, lnames_SHIFT_ALT}
 };
 
-#define num_dflt_types (sizeof(dflt_types)/sizeof(XkbKeyTypeRec))
+#define num_dflt_types ARRAY_SIZE(dflt_types)
 
 static void
 initTypeNames(DPYTYPE dpy)
@@ -445,7 +445,7 @@ static XkbSymInterpretRec dfltSI[69] = {
      {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 = {
     dfltSI,
-- 
2.13.6



More information about the xorg-devel mailing list