[RFC] Define/use pad() instead of open coding it

Matt Turner mattst88 at gmail.com
Thu Mar 11 22:01:52 PST 2010


à la the Linux Kernel's ALIGN macro.

Not even compile tested, some files might need to include "misc.h".

CC: Peter Hutterer <peter.hutterer at who-t.net>
Signed-off-by: Matt Turner <mattst88 at gmail.com>
---
 hw/dmx/dmxpict.c                      |    2 +-
 hw/kdrive/ephyr/XF86dri.c             |    6 +++---
 hw/xfree86/common/xf86Mode.c          |    2 +-
 hw/xfree86/common/xf86fbman.c         |   10 +++++-----
 hw/xfree86/dixmods/extmod/xf86vmode.c |    6 +++---
 hw/xfree86/int10/generic.c            |   13 ++++++-------
 hw/xfree86/os-support/bus/Sbus.c      |   12 ++++++------
 hw/xquartz/xpr/xprCursor.c            |    2 +-
 include/misc.h                        |   16 ++++++++++++++--
 mi/mibank.c                           |    6 ++----
 miext/rootless/rootlessWindow.c       |    2 +-
 os/utils.c                            |    6 +++---
 render/render.c                       |    2 +-
 13 files changed, 47 insertions(+), 38 deletions(-)

diff --git a/hw/dmx/dmxpict.c b/hw/dmx/dmxpict.c
index 072e3a6..a2b9484 100644
--- a/hw/dmx/dmxpict.c
+++ b/hw/dmx/dmxpict.c
@@ -674,7 +674,7 @@ static int dmxProcRenderSetPictureFilter(ClientPtr client)
 
     if (pPictPriv->pict) {
 	filter  = (char *)(stuff + 1);
-	params  = (XFixed *)(filter + ((stuff->nbytes + 3) & ~3));
+	params  = (XFixed *)(filter + pad(stuff->nbytes, 4));
 	nparams = ((XFixed *)stuff + client->req_len) - params;
 
 	XRenderSetPictureFilter(dmxScreen->beDisplay,
diff --git a/hw/kdrive/ephyr/XF86dri.c b/hw/kdrive/ephyr/XF86dri.c
index 08123d3..ed8271b 100644
--- a/hw/kdrive/ephyr/XF86dri.c
+++ b/hw/kdrive/ephyr/XF86dri.c
@@ -222,7 +222,7 @@ XF86DRIOpenConnection (Display *dpy, int screen,
 
     if (rep.length) {
         if (!(*busIdString = (char *)Xcalloc(rep.busIdStringLength + 1, 1))) {
-            _XEatData(dpy, ((rep.busIdStringLength+3) & ~3));
+            _XEatData(dpy, pad(rep.busIdStringLength, 4));
             UnlockDisplay(dpy);
             SyncHandle();
             TRACE("OpenConnection... return False");
@@ -317,7 +317,7 @@ Bool XF86DRIGetClientDriverName(Display *dpy, int screen,
 
     if (rep.length) {
         if (!(*clientDriverName = (char *)Xcalloc(rep.clientDriverNameLength + 1, 1))) {
-            _XEatData(dpy, ((rep.clientDriverNameLength+3) & ~3));
+            _XEatData(dpy, pad(rep.clientDriverNameLength, 4));
             UnlockDisplay(dpy);
             SyncHandle();
             TRACE("GetClientDriverName... return False");
@@ -588,7 +588,7 @@ XF86DRIGetDeviceInfo (Display *dpy, int screen, drm_handle_t *hFrameBuffer,
 
     if (rep.length) {
         if (!(*pDevPrivate = (void *)Xcalloc(rep.devPrivateSize, 1))) {
-            _XEatData(dpy, ((rep.devPrivateSize+3) & ~3));
+            _XEatData(dpy, pad(rep.devPrivateSize, 4));
             UnlockDisplay(dpy);
             SyncHandle();
             TRACE("GetDeviceInfo... return False");
diff --git a/hw/xfree86/common/xf86Mode.c b/hw/xfree86/common/xf86Mode.c
index 949d4fc..c3e518a 100644
--- a/hw/xfree86/common/xf86Mode.c
+++ b/hw/xfree86/common/xf86Mode.c
@@ -740,7 +740,7 @@ xf86CheckModeSize(ScrnInfoPtr scrp, int w, int x, int y)
     if ((w < 0) || (x < 0) || (y <= 0))
 	return FALSE;
 
-    lineWidth = (((w * bpp) + pad - 1) / pad) * pad;
+    lineWidth = pad(w * bpp, pad);
     lastWidth = x * bpp;
 
     /*
diff --git a/hw/xfree86/common/xf86fbman.c b/hw/xfree86/common/xf86fbman.c
index 5b88717..049c419 100644
--- a/hw/xfree86/common/xf86fbman.c
+++ b/hw/xfree86/common/xf86fbman.c
@@ -370,7 +370,7 @@ AllocateArea(
    for(i = 0; i < num; i++, boxp++) {
 	x = boxp->x1;
 	if (granularity > 1)
-	    x = ((x + granularity - 1) / granularity) * granularity;
+	    x = pad(x, granularity);
 
 	if(((boxp->y2 - boxp->y1) < h) || ((boxp->x2 - x) < w))
 	   continue;
@@ -398,7 +398,7 @@ AllocateArea(
 	   boxp = &(link->area.box);
 	   x = boxp->x1;
  	   if (granularity > 1)
-		x = ((x + granularity - 1) / granularity) * granularity;
+		x = pad(x, granularity);
 
 	   if(((boxp->y2 - boxp->y1) < h) || ((boxp->x2 - x) < w)) {
 		link = link->next;
@@ -683,7 +683,7 @@ localQueryLargestOffscreenArea(
     while(nbox--) {
 	x = pbox->x1;
 	if (granularity > 1)
-	   x = ((x + granularity - 1) / granularity) * granularity;
+	   x = pad(x, granularity);
 
 	w = pbox->x2 - x;
 	h = pbox->y2 - pbox->y1;
@@ -842,7 +842,7 @@ AllocateLinear(
       if (!linear->area && linear->free) {
 	 offset = linear->linear.offset;
 	 if (granularity > 1)
-	    offset = ((offset + granularity - 1) / granularity) * granularity;
+	    offset = pad(offset, granularity);
 	 end = offset+size;
 	 if (end <= (linear->linear.offset + linear->linear.size))
 	    break;
@@ -964,7 +964,7 @@ localAllocateOffscreenLinear(
 	linear->size = h * w;
 	linear->offset = (pitch * area->box.y1) + area->box.x1;
 	if (gran > 1)
-            linear->offset = ((linear->offset + gran - 1) / gran) * gran;
+            linear->offset = pad(linear->offset, gran);
 	linear->granularity = gran;
 	linear->MoveLinearCallback = moveCB;
 	linear->RemoveLinearCallback = removeCB;
diff --git a/hw/xfree86/dixmods/extmod/xf86vmode.c b/hw/xfree86/dixmods/extmod/xf86vmode.c
index a304a42..54058d3 100644
--- a/hw/xfree86/dixmods/extmod/xf86vmode.c
+++ b/hw/xfree86/dixmods/extmod/xf86vmode.c
@@ -1522,7 +1522,7 @@ ProcXF86VidModeSetGammaRamp(ClientPtr client)
     if(stuff->size != VidModeGetGammaRampSize(stuff->screen))
 	return BadValue;
 
-    length = (stuff->size + 1) & ~1;
+    length = pad(stuff->size, 2);
 
     REQUEST_FIXED_SIZE(xXF86VidModeSetGammaRampReq, length * 6);
 
@@ -1553,7 +1553,7 @@ ProcXF86VidModeGetGammaRamp(ClientPtr client)
 
     REQUEST_SIZE_MATCH(xXF86VidModeGetGammaRampReq);
 
-    length = (stuff->size + 1) & ~1;
+    length = pad(stuff->size, 2);
 
     if(stuff->size) {
 	ramplen = length * 3 * sizeof(CARD16);
@@ -2067,7 +2067,7 @@ SProcXF86VidModeSetGammaRamp(ClientPtr client)
     REQUEST_AT_LEAST_SIZE(xXF86VidModeSetGammaRampReq);
     swaps(&stuff->size, n);
     swaps(&stuff->screen, n);
-    length = ((stuff->size + 1) & ~1) * 6;
+    length = pad(stuff->size, 2) * 6;
     REQUEST_FIXED_SIZE(xXF86VidModeSetGammaRampReq, length);
     SwapRestS(stuff);
     return ProcXF86VidModeSetGammaRamp(client);
diff --git a/hw/xfree86/int10/generic.c b/hw/xfree86/int10/generic.c
index 9d39e99..a2ff0f2 100644
--- a/hw/xfree86/int10/generic.c
+++ b/hw/xfree86/int10/generic.c
@@ -56,8 +56,7 @@ int10MemRec genericMem = {
 static void MapVRam(xf86Int10InfoPtr pInt);
 static void UnmapVRam(xf86Int10InfoPtr pInt);
 #ifdef _PC
-#define GET_HIGH_BASE(x) (((V_BIOS + (x) + getpagesize() - 1)/getpagesize()) \
-                              * getpagesize())
+#define GET_HIGH_BASE(x) (pad(V_BIOS + (x), getpagesize()))
 #endif
 
 static void *sysMem = NULL;
@@ -81,9 +80,9 @@ read_legacy_video_BIOS(struct pci_device *dev, unsigned char *Buf)
 {
     const ADDRESS Base = 0xC0000;
     const int Len = 0x10000 * 2;
-    const int pagemask = getpagesize() - 1;
-    const ADDRESS offset = Base & ~pagemask;
-    const unsigned long size = ((Base + Len + pagemask) & ~pagemask) - offset;
+    const int pagesize = getpagesize();
+    const ADDRESS offset = Base & ~(pagemask - 1);
+    const unsigned long size = pad(Base + Len, pagesize) - offset;
     unsigned char *ptr, *src;
     int len;
 
@@ -304,7 +303,7 @@ static void
 MapVRam(xf86Int10InfoPtr pInt)
 {
     int pagesize = getpagesize();
-    int size = ((VRAM_SIZE + pagesize - 1) / pagesize) * pagesize;
+    int size = pad(VRAM_SIZE, pagesize);
 
     INTPriv(pInt)->vRam = xf86MapDomainMemory(pInt->scrnIndex, VIDMEM_MMIO,
 					      pInt->dev, V_RAM, size);
@@ -317,7 +316,7 @@ UnmapVRam(xf86Int10InfoPtr pInt)
 {
     int screen = pInt->scrnIndex;
     int pagesize = getpagesize();
-    int size = ((VRAM_SIZE + pagesize - 1)/pagesize) * pagesize;
+    int size = pad(VRAM_SIZE, pagesize);
 
     xf86UnMapVidMem(screen, INTPriv(pInt)->vRam, size);
 }
diff --git a/hw/xfree86/os-support/bus/Sbus.c b/hw/xfree86/os-support/bus/Sbus.c
index 0b6205f..850686b 100644
--- a/hw/xfree86/os-support/bus/Sbus.c
+++ b/hw/xfree86/os-support/bus/Sbus.c
@@ -613,9 +613,9 @@ pointer
 xf86MapSbusMem(sbusDevicePtr psdp, unsigned long offset, unsigned long size)
 {
     pointer ret;
-    unsigned long pagemask = getpagesize() - 1;
-    unsigned long off = offset & ~pagemask;
-    unsigned long len = ((offset + size + pagemask) & ~pagemask) - off;
+    unsigned long pagesize = getpagesize();
+    unsigned long off = offset & ~(pagesize - 1);
+    unsigned long len = pad(offset + size, pagesize) - off;
 
     if (psdp->fd == -1) {
 	psdp->fd = open(psdp->device, O_RDWR);
@@ -639,9 +639,9 @@ xf86MapSbusMem(sbusDevicePtr psdp, unsigned long offset, unsigned long size)
 void
 xf86UnmapSbusMem(sbusDevicePtr psdp, pointer addr, unsigned long size)
 {
-    unsigned long mask = getpagesize() - 1;
-    unsigned long base = (unsigned long)addr & ~mask;
-    unsigned long len = (((unsigned long)addr + size + mask) & ~mask) - base;
+    unsigned long pagesize = getpagesize();
+    unsigned long base = (unsigned long)addr & ~(pagesize - 1);
+    unsigned long len = pad((unsigned long)addr + size, pagesize) - base;
 
     munmap ((pointer)base, len);
 }
diff --git a/hw/xquartz/xpr/xprCursor.c b/hw/xquartz/xpr/xprCursor.c
index fbaf825..8b60cb4 100644
--- a/hw/xquartz/xpr/xprCursor.c
+++ b/hw/xquartz/xpr/xprCursor.c
@@ -120,7 +120,7 @@ load_cursor(CursorPtr src, int screen)
         bg_color = htonl(bg_color);
 
         /* round up to 8 pixel boundary so we can convert whole bytes */
-        rowbytes = ((src->bits->width * 4) + 31) & ~31;
+        rowbytes = pad(src->bits->width * 4, 32);
         data = xalloc(rowbytes * src->bits->height);
         if(!data) {
             FatalError("Failed to allocate memory in %s\n", __func__);
diff --git a/include/misc.h b/include/misc.h
index 62d813e..74f6eb5 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -197,7 +197,19 @@ bits_to_bytes(const int bits) {
  */
 static inline int
 bytes_to_int32(const int bytes) {
-    return (((bytes) + 3) >> 2);
+    return ((bytes + 3) >> 2);
+}
+
+/**
+ * Calculate the number of bytes needed to align "bytes" to a multiple of
+ * "alignment".
+ * @param bytes The minimum number of bytes needed.
+ * @param alignment The number to align to.
+ * @return The next multiple of "alignment" greater or equal to "bytes".
+ */
+static inline int
+pad(const int bytes, const int alignment) {
+	return (bytes + alignment - 1) & ~(alignment - 1);
 }
 
 /**
@@ -207,7 +219,7 @@ bytes_to_int32(const int bytes) {
  */
 static inline int
 pad_to_int32(const int bytes) {
-    return (((bytes) + 3) & ~3);
+    return pad(bytes, 4);
 }
 
 extern char**
diff --git a/mi/mibank.c b/mi/mibank.c
index 9e4d631..1803da5 100644
--- a/mi/mibank.c
+++ b/mi/mibank.c
@@ -2235,8 +2235,7 @@ miScanLineWidth(
         width = xsize;
     nBitsPerScanlinePadUnit = miLCM(pBankFormat->scanlinePad, nWidthUnit);
     nBitsPerScanline =
-        (((width * pBankFormat->bitsPerPixel) + nBitsPerScanlinePadUnit - 1) /
-         nBitsPerScanlinePadUnit) * nBitsPerScanlinePadUnit;
+        pad((width * pBankFormat->bitsPerPixel), nBitsPerScanlinePadUnit);
     width = nBitsPerScanline / pBankFormat->bitsPerPixel;
 
     if (!xsize || !(nBitsPerBank % pBankFormat->bitsPerPixel))
@@ -2269,8 +2268,7 @@ miScanLineWidth(
     {
         unsigned long BankBase, BankUnit;
 
-        BankUnit = ((nBitsPerBank + nBitsPerScanline - 1) / nBitsPerBank) *
-            nBitsPerBank;
+        BankUnit = pad(nBitsPerScanline, nBitsPerBank);
         if (!(BankUnit % nBitsPerScanline))
             return (int)width;
 
diff --git a/miext/rootless/rootlessWindow.c b/miext/rootless/rootlessWindow.c
index e78e2c8..a593414 100644
--- a/miext/rootless/rootlessWindow.c
+++ b/miext/rootless/rootlessWindow.c
@@ -983,7 +983,7 @@ StartFrameResize(WindowPtr pWin, Bool gravity,
             Bpp = winRec->win->drawable.bitsPerPixel / 8;
             copy_rect_width = copy_rect.x2 - copy_rect.x1;
             copy_rect_height = copy_rect.y2 - copy_rect.y1;
-            copy_rowbytes = ((copy_rect_width * Bpp) + 31) & ~31;
+            copy_rowbytes = pad(copy_rect_width * Bpp, 32);
             gResizeDeathBits = xalloc(copy_rowbytes
                                       * copy_rect_height);
 
diff --git a/os/utils.c b/os/utils.c
index 5a5a203..e8a5313 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -1041,7 +1041,7 @@ Xalloc(unsigned long amount)
 	return NULL;
     }
     /* aligned extra on long word boundary */
-    amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
+    amount = pad(amount, sizeof(long));
     ptr = malloc(amount);
     return ptr;
 }
@@ -1059,7 +1059,7 @@ XNFalloc(unsigned long amount)
     if ((long)amount <= 0)
         return NULL;
     /* aligned extra on long word boundary */
-    amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
+    amount = pad(amount, sizeof(long));
     ptr = malloc(amount);
     if (!ptr)
         FatalError("Out of memory");
@@ -1111,7 +1111,7 @@ Xrealloc(pointer ptr, unsigned long amount)
 	    free(ptr);
 	return NULL;
     }
-    amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
+    amount = pad(amount, sizeof(long));
     if (ptr)
         ptr = realloc(ptr, amount);
     else
diff --git a/render/render.c b/render/render.c
index 2d9e47a..728af5d 100644
--- a/render/render.c
+++ b/render/render.c
@@ -1758,7 +1758,7 @@ ProcRenderQueryFilters (ClientPtr client)
     if (!reply)
 	return BadAlloc;
     aliases = (INT16 *) (reply + 1);
-    names = (char *) (aliases + ((nnames + 1) & ~1));
+    names = (char *) (aliases + pad(nnames, 2));
     
     reply->type = X_Reply;
     reply->sequenceNumber = client->sequence;
-- 
1.6.4.4



More information about the xorg-devel mailing list