[RFC] Define/use pad() and pad_mask() instead of open coding it
Matt Turner
mattst88 at gmail.com
Thu Mar 11 16:33:30 PST 2010
à la the Linux Kernel's ALIGN and __ALIGN_MASK macros.
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/dixmods/extmod/xf86vmode.c | 6 +++---
hw/xfree86/int10/generic.c | 2 +-
hw/xfree86/os-support/bus/Sbus.c | 4 ++--
hw/xquartz/xpr/xprCursor.c | 2 +-
include/misc.h | 12 +++++++++++-
miext/rootless/rootlessWindow.c | 2 +-
os/utils.c | 6 +++---
render/render.c | 2 +-
10 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/hw/dmx/dmxpict.c b/hw/dmx/dmxpict.c
index 072e3a6..d0cbae3 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..87cd037 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/dixmods/extmod/xf86vmode.c b/hw/xfree86/dixmods/extmod/xf86vmode.c
index a304a42..2812ced 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..1e64912 100644
--- a/hw/xfree86/int10/generic.c
+++ b/hw/xfree86/int10/generic.c
@@ -83,7 +83,7 @@ read_legacy_video_BIOS(struct pci_device *dev, unsigned char *Buf)
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 unsigned long size = pad_mask(Base + Len, pagemask) - offset;
unsigned char *ptr, *src;
int len;
diff --git a/hw/xfree86/os-support/bus/Sbus.c b/hw/xfree86/os-support/bus/Sbus.c
index 0b6205f..a333b6a 100644
--- a/hw/xfree86/os-support/bus/Sbus.c
+++ b/hw/xfree86/os-support/bus/Sbus.c
@@ -615,7 +615,7 @@ 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 len = pad_mask(offset + size, pagemask) - off;
if (psdp->fd == -1) {
psdp->fd = open(psdp->device, O_RDWR);
@@ -641,7 +641,7 @@ 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 len = pad_mask((unsigned long)addr + size, mask) - 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..98d39bd 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -200,6 +200,16 @@ bytes_to_int32(const int bytes) {
return (((bytes) + 3) >> 2);
}
+static inline int
+pad_mask(const int bytes, const int mask) {
+ return (((bytes) + mask) & ~mask);
+}
+
+static inline int
+pad(const int bytes, const int mask) {
+ return pad_mask(bytes, mask - 1);
+}
+
/**
* Calculate the number of bytes (in multiples of 4) needed to hold bytes.
* @param bytes The minimum number of bytes needed.
@@ -207,7 +217,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/miext/rootless/rootlessWindow.c b/miext/rootless/rootlessWindow.c
index e78e2c8..96ef605 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 21e25e0..7f15ade 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -1043,7 +1043,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;
}
@@ -1061,7 +1061,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");
@@ -1113,7 +1113,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..d6b104a 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