[PATCH] Define/use ALIGN() instead of open coding it

Matt Turner mattst88 at gmail.com
Thu Apr 8 16:10:52 PDT 2010


Signed-off-by: Matt Turner <mattst88 at gmail.com>
---
 Xext/xf86bigfont.c                    |    2 +-
 hw/dmx/dmxpict.c                      |    2 +-
 hw/kdrive/ephyr/XF86dri.c             |    6 +++---
 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 ++++++++++++++--
 miext/rootless/rootlessWindow.c       |    2 +-
 os/utils.c                            |    6 +++---
 render/render.c                       |    2 +-
 11 files changed, 40 insertions(+), 29 deletions(-)

diff --git a/Xext/xf86bigfont.c b/Xext/xf86bigfont.c
index a4eb3f6..59ba031 100644
--- a/Xext/xf86bigfont.c
+++ b/Xext/xf86bigfont.c
@@ -234,7 +234,7 @@ shmalloc(
     if (!pDesc)
 	return (ShmDescPtr) NULL;
 
-    size = (size + pagesize-1) & -pagesize;
+    size = ALIGN(size, pagesize);
     shmid = shmget(IPC_PRIVATE, size, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
     if (shmid == -1) {
 	ErrorF(XF86BIGFONTNAME " extension: shmget() failed, size = %u, %s\n",
diff --git a/hw/dmx/dmxpict.c b/hw/dmx/dmxpict.c
index 072e3a6..818c65d 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 + ALIGN(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..d832d2c 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, ALIGN(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, ALIGN(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, ALIGN(rep.devPrivateSize, 4));
             UnlockDisplay(dpy);
             SyncHandle();
             TRACE("GetDeviceInfo... return False");
diff --git a/hw/xfree86/dixmods/extmod/xf86vmode.c b/hw/xfree86/dixmods/extmod/xf86vmode.c
index a304a42..b885166 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 = ALIGN(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 = ALIGN(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 = ALIGN(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..6ee2f97 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) (ALIGN(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 & ~(pagesize - 1);
+    const unsigned long size = ALIGN(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 = ALIGN(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 = ALIGN(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..804de0a 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 = ALIGN(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 = ALIGN((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..78417d3 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 = ALIGN(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..f599dea 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, must be a power of two.
+ * @return The next multiple of "alignment" greater or equal to "bytes".
+ */
+static inline int
+ALIGN(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 ALIGN(bytes, 4);
 }
 
 extern char**
diff --git a/miext/rootless/rootlessWindow.c b/miext/rootless/rootlessWindow.c
index e78e2c8..dd05ec8 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 = ALIGN(copy_rect_width * Bpp, 32);
             gResizeDeathBits = xalloc(copy_rowbytes
                                       * copy_rect_height);
 
diff --git a/os/utils.c b/os/utils.c
index 13d3b3f..e71c0bd 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 = ALIGN(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 = ALIGN(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 = ALIGN(amount, sizeof(long));
     if (ptr)
         ptr = realloc(ptr, amount);
     else
diff --git a/render/render.c b/render/render.c
index 2d9e47a..90bcb8e 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 + ALIGN(nnames, 2));
     
     reply->type = X_Reply;
     reply->sequenceNumber = client->sequence;
-- 
1.6.4.4



More information about the xorg-devel mailing list