[PATCH] input: properly align doubles in InitValuatorClassDeviceStruct

Keith Packard keithp at keithp.com
Fri Feb 25 21:02:20 PST 2011


On Sat, 26 Feb 2011 00:00:39 +0100, Julien Cristau <jcristau at debian.org> wrote:

> Some architectures (hi, sparc!) are unhappy with unaligned memory
> accesses.  So make sure the axisVal member of ValuatorClassRec has
> sizeof(double) alignment to avoid crashes and test failures.

The 'standard' way to do this is to use a union to ensure correct
alignment. Seems like simply moving the doubles to after the
ValuatorClassRec and using a union between a double and the
ValuatorClassRec would fix this without a lot of ugly pointer computations.

This is completely untested, but I imagine something the following would work:

diff --git a/dix/devices.c b/dix/devices.c
index 6c0dc42..89294aa 100644
--- a/dix/devices.c
+++ b/dix/devices.c
@@ -1225,6 +1225,7 @@ InitValuatorClassDeviceStruct(DeviceIntPtr dev, int numAxes, Atom *labels,
 {
     int i;
     ValuatorClassPtr valc;
+    union align_u { ValuatorClassRec valc; double d; } *align;
 
     if (!dev)
         return FALSE;
@@ -1237,12 +1238,13 @@ InitValuatorClassDeviceStruct(DeviceIntPtr dev, int numAxes, Atom *labels,
         numAxes = MAX_VALUATORS;
     }
 
-    valc = (ValuatorClassPtr)calloc(1, sizeof(ValuatorClassRec) +
-				    numAxes * sizeof(AxisInfo) +
-				    numAxes * sizeof(double));
-    if (!valc)
+    align = (union align_u *) calloc(1, sizeof(union align_u) +
+				     numAxes * sizeof(double) +
+				     numAxes * sizeof(AxisInfo));
+    if (!align)
 	return FALSE;
 
+    valc = &align->valc;
     valc->sourceid = dev->id;
     valc->motion = NULL;
     valc->first_motion = 0;
@@ -1251,8 +1253,8 @@ InitValuatorClassDeviceStruct(DeviceIntPtr dev, int numAxes, Atom *labels,
     valc->numMotionEvents = numMotionEvents;
     valc->motionHintWindow = NullWindow;
     valc->numAxes = numAxes;
-    valc->axes = (AxisInfoPtr)(valc + 1);
-    valc->axisVal = (double *)(valc->axes + numAxes);
+    valc->axisVal = (double *)(align + 1);
+    valc->axes = (AxisInfoPtr)(valc->axisVal + numAxes);
 
     if (mode & OutOfProximity)
         InitProximityClassDeviceStruct(dev);


-- 
keith.packard at intel.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.x.org/archives/xorg-devel/attachments/20110225/c46d00c0/attachment.pgp>


More information about the xorg-devel mailing list