[PATCH] Define/use X_ALIGN2() instead of open coding it
Matt Turner
mattst88 at gmail.com
Sat Jun 19 15:14:31 PDT 2010
Signed-off-by: Matt Turner <mattst88 at gmail.com>
---
Notes:
Page size is always a power of two, so those conversions are safe.
This patch requires "[PATCH] Guard x* macros with _HAVE_XALLOC_DECLS
as well to avoid clashes with Xlib" because it includes misc.h in
XF86dri.c, causing a clash between Xlib's Xcalloc with two arguments
and the xserver's with one.
I named the macro X_ALIGN2 because
- ALIGN is apparently something different on BSD, so add X_
- add 2 to the end to denote alignment to power of two,
allows us to add an arbitrary alignment function, X_ALIGN
if we feel so inclined
I can't compile test the changes in hw/xfree86/os-support/, so please
test these changes if you're able.
Xext/xf86bigfont.c | 2 +-
dix/privates.c | 4 ++--
exa/exa_priv.h | 2 +-
hw/dmx/dmxpict.c | 2 +-
hw/kdrive/ephyr/XF86dri.c | 9 ++++++---
hw/xfree86/dixmods/extmod/xf86vmode.c | 6 +++---
hw/xfree86/int10/generic.c | 16 ++++++----------
hw/xfree86/os-support/bsd/alpha_video.c | 2 +-
hw/xfree86/os-support/bsd/arm_video.c | 2 +-
hw/xfree86/os-support/bsd/i386_video.c | 2 +-
hw/xfree86/os-support/bus/Sbus.c | 12 ++++++------
hw/xfree86/os-support/shared/bios_mmap.c | 4 ++--
hw/xfree86/os-support/solaris/sun_vid.c | 2 +-
hw/xquartz/xpr/xprCursor.c | 2 +-
include/misc.h | 14 +++++++++++++-
miext/rootless/rootlessWindow.c | 2 +-
render/render.c | 2 +-
17 files changed, 48 insertions(+), 37 deletions(-)
diff --git a/Xext/xf86bigfont.c b/Xext/xf86bigfont.c
index 8f0ddd9..58d851f 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 = X_ALIGN2(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/dix/privates.c b/dix/privates.c
index ec818d4..c1bc41b 100644
--- a/dix/privates.c
+++ b/dix/privates.c
@@ -184,7 +184,7 @@ dixRegisterPrivateKey(DevPrivateKey key, DevPrivateType type, unsigned size)
bytes = sizeof (void *);
/* align to void * size */
- bytes = (bytes + sizeof (void *) - 1) & ~(sizeof (void *) - 1);
+ bytes = X_ALIGN2(bytes, sizeof(void *));
/* Update offsets for all affected keys */
if (type == PRIVATE_XSELINUX) {
@@ -303,7 +303,7 @@ _dixAllocateObjectWithPrivates(unsigned baseSize, unsigned clear, unsigned offse
assert (type > PRIVATE_SCREEN && type < PRIVATE_LAST);
/* round up so that void * is aligned */
- baseSize = (baseSize + sizeof (void *) - 1) & ~(sizeof (void *) - 1);
+ baseSize = X_ALIGN2(baseSize, sizeof(void *));
totalSize = baseSize + keys[type].offset;
object = malloc(totalSize);
if (!object)
diff --git a/exa/exa_priv.h b/exa/exa_priv.h
index 58d04b0..0d2c179 100644
--- a/exa/exa_priv.h
+++ b/exa/exa_priv.h
@@ -273,7 +273,7 @@ extern DevPrivateKeyRec exaGCPrivateKeyRec;
#define EXA_ALIGN(offset, align) (((offset) + (align) - 1) - \
(((offset) + (align) - 1) % (align)))
/** Align an offset to a power-of-two alignment */
-#define EXA_ALIGN2(offset, align) (((offset) + (align) - 1) & ~((align) - 1))
+#define EXA_ALIGN2(offset, align) X_ALIGN2(offset, align)
#define EXA_PIXMAP_SCORE_MOVE_IN 10
#define EXA_PIXMAP_SCORE_MAX 20
diff --git a/hw/dmx/dmxpict.c b/hw/dmx/dmxpict.c
index 915e767..b48b75d 100644
--- a/hw/dmx/dmxpict.c
+++ b/hw/dmx/dmxpict.c
@@ -672,7 +672,7 @@ static int dmxProcRenderSetPictureFilter(ClientPtr client)
if (pPictPriv->pict) {
filter = (char *)(stuff + 1);
- params = (XFixed *)(filter + ((stuff->nbytes + 3) & ~3));
+ params = (XFixed *)(filter + X_ALIGN2(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 e7c88b3..77283f7 100644
--- a/hw/kdrive/ephyr/XF86dri.c
+++ b/hw/kdrive/ephyr/XF86dri.c
@@ -65,6 +65,9 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "xf86dri.h"
#include <X11/dri/xf86driproto.h>
+#define _HAVE_XALLOC_DECLS
+#include "misc.h"
+
static XExtensionInfo _xf86dri_info_data;
static XExtensionInfo *xf86dri_info = &_xf86dri_info_data;
static char xf86dri_extension_name[] = XF86DRINAME;
@@ -222,7 +225,7 @@ XF86DRIOpenConnection (Display *dpy, int screen,
if (rep.length) {
if (!(*busIdString = (char *)calloc(rep.busIdStringLength + 1, 1))) {
- _XEatData(dpy, ((rep.busIdStringLength+3) & ~3));
+ _XEatData(dpy, X_ALIGN2(rep.busIdStringLength, 4));
UnlockDisplay(dpy);
SyncHandle();
TRACE("OpenConnection... return False");
@@ -317,7 +320,7 @@ Bool XF86DRIGetClientDriverName(Display *dpy, int screen,
if (rep.length) {
if (!(*clientDriverName = (char *)calloc(rep.clientDriverNameLength + 1, 1))) {
- _XEatData(dpy, ((rep.clientDriverNameLength+3) & ~3));
+ _XEatData(dpy, X_ALIGN2(rep.clientDriverNameLength, 4));
UnlockDisplay(dpy);
SyncHandle();
TRACE("GetClientDriverName... return False");
@@ -588,7 +591,7 @@ XF86DRIGetDeviceInfo (Display *dpy, int screen, drm_handle_t *hFrameBuffer,
if (rep.length) {
if (!(*pDevPrivate = (void *)calloc(rep.devPrivateSize, 1))) {
- _XEatData(dpy, ((rep.devPrivateSize+3) & ~3));
+ _XEatData(dpy, X_ALIGN2(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 0001c9e..c44bed0 100644
--- a/hw/xfree86/dixmods/extmod/xf86vmode.c
+++ b/hw/xfree86/dixmods/extmod/xf86vmode.c
@@ -1520,7 +1520,7 @@ ProcXF86VidModeSetGammaRamp(ClientPtr client)
if(stuff->size != VidModeGetGammaRampSize(stuff->screen))
return BadValue;
- length = (stuff->size + 1) & ~1;
+ length = X_ALIGN2(stuff->size, 2);
REQUEST_FIXED_SIZE(xXF86VidModeSetGammaRampReq, length * 6);
@@ -1551,7 +1551,7 @@ ProcXF86VidModeGetGammaRamp(ClientPtr client)
REQUEST_SIZE_MATCH(xXF86VidModeGetGammaRampReq);
- length = (stuff->size + 1) & ~1;
+ length = X_ALIGN2(stuff->size, 2);
if(stuff->size) {
ramplen = length * 3 * sizeof(CARD16);
@@ -2065,7 +2065,7 @@ SProcXF86VidModeSetGammaRamp(ClientPtr client)
REQUEST_AT_LEAST_SIZE(xXF86VidModeSetGammaRampReq);
swaps(&stuff->size, n);
swaps(&stuff->screen, n);
- length = ((stuff->size + 1) & ~1) * 6;
+ length = X_ALIGN2(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 fe8bb69..f09d246 100644
--- a/hw/xfree86/int10/generic.c
+++ b/hw/xfree86/int10/generic.c
@@ -55,10 +55,6 @@ 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())
-#endif
static void *sysMem = NULL;
@@ -81,9 +77,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 = X_ALIGN2(Base + Len, pagesize) - offset;
unsigned char *ptr, *src;
int len;
@@ -218,7 +214,7 @@ xf86ExtendedInitInt10(int entityIndex, int Flags)
strerror(err));
goto error1;
}
- INTPriv(pInt)->highMemory = GET_HIGH_BASE(rom_device->rom_size);
+ INTPriv(pInt)->highMemory = X_ALIGN2(V_BIOS + rom_device->rom_size, getpagesize());
break;
}
default:
@@ -304,7 +300,7 @@ static void
MapVRam(xf86Int10InfoPtr pInt)
{
int pagesize = getpagesize();
- int size = ((VRAM_SIZE + pagesize - 1) / pagesize) * pagesize;
+ int size = X_ALIGN2(VRAM_SIZE, pagesize);
INTPriv(pInt)->vRam = xf86MapDomainMemory(pInt->scrnIndex, VIDMEM_MMIO,
pInt->dev, V_RAM, size);
@@ -317,7 +313,7 @@ UnmapVRam(xf86Int10InfoPtr pInt)
{
int screen = pInt->scrnIndex;
int pagesize = getpagesize();
- int size = ((VRAM_SIZE + pagesize - 1)/pagesize) * pagesize;
+ int size = X_ALIGN2(VRAM_SIZE, pagesize);
xf86UnMapVidMem(screen, INTPriv(pInt)->vRam, size);
}
diff --git a/hw/xfree86/os-support/bsd/alpha_video.c b/hw/xfree86/os-support/bsd/alpha_video.c
index bb3a5cb..95e8881 100644
--- a/hw/xfree86/os-support/bsd/alpha_video.c
+++ b/hw/xfree86/os-support/bsd/alpha_video.c
@@ -376,7 +376,7 @@ xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf,
psize = getpagesize();
Offset += Base & (psize - 1);
Base &= ~(psize - 1);
- mlen = (Offset + Len + psize - 1) & ~(psize - 1);
+ mlen = X_ALIGN2(Offset + Len, psize);
ptr = (unsigned char *)mmap((caddr_t)0, mlen, PROT_READ,
MAP_SHARED, devMemFd, (off_t)Base+BUS_BASE);
if ((long)ptr == -1)
diff --git a/hw/xfree86/os-support/bsd/arm_video.c b/hw/xfree86/os-support/bsd/arm_video.c
index 482d53f..13c8488 100644
--- a/hw/xfree86/os-support/bsd/arm_video.c
+++ b/hw/xfree86/os-support/bsd/arm_video.c
@@ -249,7 +249,7 @@ xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf,
psize = getpagesize();
Offset += Base & (psize - 1);
Base &= ~(psize - 1);
- mlen = (Offset + Len + psize - 1) & ~(psize - 1);
+ mlen = X_ALIGN2(Offset + Len, psize);
ptr = (unsigned char *)mmap((caddr_t)0, mlen, PROT_READ,
MAP_SHARED, devMemFd, (off_t)Base+BUS_BASE);
if ((long)ptr == -1)
diff --git a/hw/xfree86/os-support/bsd/i386_video.c b/hw/xfree86/os-support/bsd/i386_video.c
index 7aef079..b5c5350 100644
--- a/hw/xfree86/os-support/bsd/i386_video.c
+++ b/hw/xfree86/os-support/bsd/i386_video.c
@@ -295,7 +295,7 @@ xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf,
psize = getpagesize();
Offset += Base & (psize - 1);
Base &= ~(psize - 1);
- mlen = (Offset + Len + psize - 1) & ~(psize - 1);
+ mlen = X_ALIGN2(Offset + Len, psize);
ptr = (unsigned char *)mmap((caddr_t)0, mlen, PROT_READ,
MAP_SHARED, devMemFd, (off_t)Base);
if ((long)ptr == -1)
diff --git a/hw/xfree86/os-support/bus/Sbus.c b/hw/xfree86/os-support/bus/Sbus.c
index 7829d80..5eb0352 100644
--- a/hw/xfree86/os-support/bus/Sbus.c
+++ b/hw/xfree86/os-support/bus/Sbus.c
@@ -611,9 +611,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 = X_ALIGN2(offset + size, pagesize) - off;
if (psdp->fd == -1) {
psdp->fd = open(psdp->device, O_RDWR);
@@ -637,9 +637,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/xfree86/os-support/shared/bios_mmap.c b/hw/xfree86/os-support/shared/bios_mmap.c
index 33a8df0..d26d32f 100644
--- a/hw/xfree86/os-support/shared/bios_mmap.c
+++ b/hw/xfree86/os-support/shared/bios_mmap.c
@@ -58,7 +58,7 @@ xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf,
psize = getpagesize();
Offset += Base & (psize - 1);
Base &= ~(psize - 1);
- mlen = (Offset + Len + psize - 1) & ~(psize - 1);
+ mlen = X_ALIGN2(Offset + Len, psize);
ptr = (unsigned char *)mmap((caddr_t)0, mlen, PROT_READ,
MAP_SHARED, fd, (off_t)Base);
if (ptr == MAP_FAILED)
@@ -120,7 +120,7 @@ xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf,
psize = getpagesize();
Offset += Base & (psize - 1);
Base &= ~(psize - 1);
- mlen = (Offset + Len + psize - 1) & ~(psize - 1);
+ mlen = X_ALIGN2(Offset + Len, psize);
base = mmap((caddr_t)0, mlen, PROT_READ,
MAP_SHARED, fd, (off_t)(Base + BUS_BASE));
diff --git a/hw/xfree86/os-support/solaris/sun_vid.c b/hw/xfree86/os-support/solaris/sun_vid.c
index 5089ae7..d108583 100644
--- a/hw/xfree86/os-support/solaris/sun_vid.c
+++ b/hw/xfree86/os-support/solaris/sun_vid.c
@@ -191,7 +191,7 @@ xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf,
psize = getpagesize();
Offset += Base & (psize - 1);
Base &= ~(psize - 1);
- mlen = (Offset + Len + psize - 1) & ~(psize - 1);
+ mlen = X_ALIGN2(Offset + Len, psize);
if (solOpenAperture() == FALSE)
{
diff --git a/hw/xquartz/xpr/xprCursor.c b/hw/xquartz/xpr/xprCursor.c
index 67fcbe7..8468b56 100644
--- a/hw/xquartz/xpr/xprCursor.c
+++ b/hw/xquartz/xpr/xprCursor.c
@@ -119,7 +119,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 = X_ALIGN2((src->bits->width * 4), 32);
data = malloc(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..df2608f 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -201,13 +201,25 @@ bytes_to_int32(const int bytes) {
}
/**
+ * 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
+X_ALIGN2(const int bytes, const int alignment) {
+ return (bytes + alignment - 1) & ~(alignment - 1);
+}
+
+/**
* Calculate the number of bytes (in multiples of 4) needed to hold bytes.
* @param bytes The minimum number of bytes needed.
* @return The closest multiple of 4 that is equal or higher than bytes.
*/
static inline int
pad_to_int32(const int bytes) {
- return (((bytes) + 3) & ~3);
+ return X_ALIGN2(bytes, 4);
}
extern char**
diff --git a/miext/rootless/rootlessWindow.c b/miext/rootless/rootlessWindow.c
index 7fe5e1d..ab42384 100644
--- a/miext/rootless/rootlessWindow.c
+++ b/miext/rootless/rootlessWindow.c
@@ -972,7 +972,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 = X_ALIGN2((copy_rect_width * Bpp), 32);
gResizeDeathBits = malloc(copy_rowbytes
* copy_rect_height);
diff --git a/render/render.c b/render/render.c
index ef233e4..751b740 100644
--- a/render/render.c
+++ b/render/render.c
@@ -1747,7 +1747,7 @@ ProcRenderQueryFilters (ClientPtr client)
if (!reply)
return BadAlloc;
aliases = (INT16 *) (reply + 1);
- names = (char *) (aliases + ((nnames + 1) & ~1));
+ names = (char *) (aliases + X_ALIGN2(nnames, 2));
reply->type = X_Reply;
reply->sequenceNumber = client->sequence;
--
1.7.1
More information about the xorg-devel
mailing list