[PATCH] Define/use pad_to_pow_two() instead of open coding it
Matt Turner
mattst88 at gmail.com
Wed Mar 24 10:57:15 PDT 2010
à la the Linux Kernel's ALIGN macro.
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 | 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 +-
10 files changed, 39 insertions(+), 28 deletions(-)
diff --git a/hw/dmx/dmxpict.c b/hw/dmx/dmxpict.c
index 072e3a6..51616bb 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_to_pow_two(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..aef9828 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_to_pow_two(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_to_pow_two(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_to_pow_two(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..d3f1a44 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_to_pow_two(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_to_pow_two(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_to_pow_two(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..399250e 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_to_pow_two(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 = pad_to_pow_two(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_to_pow_two(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_to_pow_two(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..2724101 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_to_pow_two(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_to_pow_two((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..d1a36eb 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_to_pow_two(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..eeb1f87 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
+pad_to_pow_two(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_to_pow_two(bytes, 4);
}
extern char**
diff --git a/miext/rootless/rootlessWindow.c b/miext/rootless/rootlessWindow.c
index e78e2c8..d0956a8 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_to_pow_two(copy_rect_width * Bpp, 32);
gResizeDeathBits = xalloc(copy_rowbytes
* copy_rect_height);
diff --git a/os/utils.c b/os/utils.c
index 5a5a203..ed16809 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_to_pow_two(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_to_pow_two(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_to_pow_two(amount, sizeof(long));
if (ptr)
ptr = realloc(ptr, amount);
else
diff --git a/render/render.c b/render/render.c
index 2d9e47a..7e6a1cb 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_to_pow_two(nnames, 2));
reply->type = X_Reply;
reply->sequenceNumber = client->sequence;
--
1.6.4.4
More information about the xorg-devel
mailing list