[PATCH 3/5] Replace some input devPrivates with regular struct fields

Jamey Sharp jamey at minilop.net
Thu Apr 29 12:24:49 PDT 2010


From: Keith Packard <keithp at keithp.com>

In the process, fixes a memory leak in CloseDevice, and an unchecked
memory allocation in InitializePredictableAccelerationProperties.

Signed-off-by: Keith Packard <keithp at keithp.com>
Reviewed-by: Jamey Sharp <jamey at minilop.net>
---
 Xi/exevents.c      |   63 +++++++++++++++++++--------------------------------
 dix/devices.c      |   18 +++-----------
 dix/events.c       |   19 +++------------
 dix/ptrveloc.c     |   31 +++++++------------------
 include/inputstr.h |    3 +-
 include/ptrveloc.h |    5 +++-
 6 files changed, 47 insertions(+), 92 deletions(-)

diff --git a/Xi/exevents.c b/Xi/exevents.c
index e680f6f..949efe7 100644
--- a/Xi/exevents.c
+++ b/Xi/exevents.c
@@ -94,9 +94,6 @@ Bool ShouldFreeInputMasks(WindowPtr /* pWin */ ,
 static Bool MakeInputMasks(WindowPtr	/* pWin */
     );
 
-/* Used to sture classes currently not in use by an MD */
-extern DevPrivateKey UnusedClassesPrivateKey;
-
 /*
  * Only let the given client know of core events which will affect its
  * interpretation of input events, if the client's ClientPointer (or the
@@ -228,8 +225,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
 
         if (!to->intfeed)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->intfeed = classes->intfeed;
         }
 
@@ -253,7 +249,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->intfeed && !from->intfeed)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->intfeed = to->intfeed;
         to->intfeed      = NULL;
     }
@@ -264,8 +260,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
 
         if (!to->stringfeed)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->stringfeed = classes->stringfeed;
         }
 
@@ -289,7 +284,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->stringfeed && !from->stringfeed)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->stringfeed = to->stringfeed;
         to->stringfeed      = NULL;
     }
@@ -300,8 +295,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
 
         if (!to->bell)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->bell = classes->bell;
         }
 
@@ -326,7 +320,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->bell && !from->bell)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->bell = to->bell;
         to->bell      = NULL;
     }
@@ -337,8 +331,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
 
         if (!to->leds)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->leds = classes->leds;
         }
 
@@ -365,7 +358,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->leds && !from->leds)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->leds = to->leds;
         to->leds      = NULL;
     }
@@ -385,8 +378,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
 
         if (!to->kbdfeed)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
 
             to->kbdfeed = classes->kbdfeed;
             if (!to->kbdfeed)
@@ -417,7 +409,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->kbdfeed && !from->kbdfeed)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->kbdfeed = to->kbdfeed;
         to->kbdfeed      = NULL;
     }
@@ -426,8 +418,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
     {
         if (!to->key)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                    UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->key = classes->key;
             if (!to->key)
                 InitKeyboardDeviceStruct(to, NULL, NULL, NULL);
@@ -439,7 +430,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->key && !from->key)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->key = to->key;
         to->key      = NULL;
     }
@@ -476,8 +467,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
         {
             WindowPtr *oldTrace;
 
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->focus = classes->focus;
             if (!to->focus)
             {
@@ -500,7 +490,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->focus)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->focus = to->focus;
         to->focus      = NULL;
     }
@@ -518,8 +508,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
         PtrFeedbackPtr *p, it;
         if (!to->ptrfeed)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->ptrfeed = classes->ptrfeed;
         }
 
@@ -543,7 +532,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->ptrfeed && !from->ptrfeed)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->ptrfeed = to->ptrfeed;
         to->ptrfeed      = NULL;
     }
@@ -553,8 +542,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
         ValuatorClassPtr v;
         if (!to->valuator)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->valuator = classes->valuator;
             if (to->valuator)
                 classes->valuator = NULL;
@@ -577,7 +565,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->valuator && !from->valuator)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->valuator = to->valuator;
         to->valuator      = NULL;
     }
@@ -586,8 +574,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
     {
         if (!to->button)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->button = classes->button;
             if (!to->button)
             {
@@ -617,7 +604,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->button && !from->button)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->button = to->button;
         to->button      = NULL;
     }
@@ -626,8 +613,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
     {
         if (!to->proximity)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->proximity = classes->proximity;
             if (!to->proximity)
             {
@@ -642,7 +628,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->proximity)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->proximity = to->proximity;
         to->proximity      = NULL;
     }
@@ -651,8 +637,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
     {
         if (!to->absolute)
         {
-            classes = dixLookupPrivate(&to->devPrivates,
-                                       UnusedClassesPrivateKey);
+            classes = to->unused_classes;
             to->absolute = classes->absolute;
             if (!to->absolute)
             {
@@ -667,7 +652,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
     } else if (to->absolute)
     {
         ClassesPtr classes;
-        classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey);
+        classes = to->unused_classes;
         classes->absolute = to->absolute;
         to->absolute      = NULL;
     }
diff --git a/dix/devices.c b/dix/devices.c
index 6119dce..1386491 100644
--- a/dix/devices.c
+++ b/dix/devices.c
@@ -89,13 +89,6 @@ SOFTWARE.
  * This file handles input device-related stuff.
  */
 
-static int CoreDevicePrivateKeyIndex;
-DevPrivateKey CoreDevicePrivateKey = &CoreDevicePrivateKeyIndex;
-/* Used to store classes currently not in use by an MD */
-static int UnusedClassesPrivateKeyIndex;
-DevPrivateKey UnusedClassesPrivateKey = &UnusedClassesPrivateKeyIndex;
-
-
 static void RecalculateMasterButtons(DeviceIntPtr slave);
 
 /**
@@ -852,8 +845,9 @@ CloseDevice(DeviceIntPtr dev)
 
     if (IsMaster(dev))
     {
-        classes = dixLookupPrivate(&dev->devPrivates, UnusedClassesPrivateKey);
+        classes = dev->unused_classes;
         FreeAllDeviceClasses(classes);
+	xfree(classes);
     }
 
     if (DevHasCursor(dev) && dev->spriteInfo->sprite) {
@@ -2465,7 +2459,6 @@ AllocDevicePair (ClientPtr client, char* name,
 {
     DeviceIntPtr pointer;
     DeviceIntPtr keyboard;
-    ClassesPtr classes;
     *ptr = *keybd = NULL;
 
     pointer = AddInputDevice(client, ptr_proc, TRUE);
@@ -2511,12 +2504,9 @@ AllocDevicePair (ClientPtr client, char* name,
     keyboard->last.slave = NULL;
     keyboard->type = (master) ? MASTER_KEYBOARD : SLAVE;
 
-
     /* The ClassesRec stores the device classes currently not used. */
-    classes = xcalloc(1, sizeof(ClassesRec));
-    dixSetPrivate(&pointer->devPrivates, UnusedClassesPrivateKey, classes);
-    classes = xcalloc(1, sizeof(ClassesRec));
-    dixSetPrivate(&keyboard->devPrivates, UnusedClassesPrivateKey, classes);
+    pointer->unused_classes = xcalloc(1, sizeof(ClassesRec));
+    keyboard->unused_classes = xcalloc(1, sizeof(ClassesRec));
 
     *ptr = pointer;
     *keybd = keyboard;
diff --git a/dix/events.c b/dix/events.c
index b26e28a..60dd162 100644
--- a/dix/events.c
+++ b/dix/events.c
@@ -1408,12 +1408,8 @@ CheckGrabForSyncs(DeviceIntPtr thisDev, Bool thisMode, Bool otherMode)
     ComputeFreezes();
 }
 
-/* Only ever used if a grab is called on an attached slave device. */
-static int GrabPrivateKeyIndex;
-static DevPrivateKey GrabPrivateKey = &GrabPrivateKeyIndex;
-
 /**
- * Save the device's master device in the devPrivates. This needs to be done
+ * Save the device's master device id. This needs to be done
  * if a client directly grabs a slave device that is attached to a master. For
  * the duration of the grab, the device is detached, ungrabbing re-attaches it
  * though.
@@ -1424,35 +1420,28 @@ static DevPrivateKey GrabPrivateKey = &GrabPrivateKeyIndex;
 static void
 DetachFromMaster(DeviceIntPtr dev)
 {
-    intptr_t id;
     if (!dev->u.master)
         return;
 
-    id = dev->u.master->id;
+    dev->saved_master_id = dev->u.master->id;
 
-    dixSetPrivate(&dev->devPrivates, GrabPrivateKey, (void *)id);
     AttachDevice(NULL, dev, NULL);
 }
 
 static void
 ReattachToOldMaster(DeviceIntPtr dev)
 {
-    int id;
-    void *p;
     DeviceIntPtr master = NULL;
 
     if (IsMaster(dev))
         return;
 
-
-    p = dixLookupPrivate(&dev->devPrivates, GrabPrivateKey);
-    id = (intptr_t) p; /* silence gcc warnings */
-    dixLookupDevice(&master, id, serverClient, DixUseAccess);
+    dixLookupDevice(&master, dev->saved_master_id, serverClient, DixUseAccess);
 
     if (master)
     {
         AttachDevice(serverClient, dev, master);
-        dixSetPrivate(&dev->devPrivates, GrabPrivateKey, NULL);
+	dev->saved_master_id = 0;
     }
 }
 
diff --git a/dix/ptrveloc.c b/dix/ptrveloc.c
index c2f4378..100d6f8 100644
--- a/dix/ptrveloc.c
+++ b/dix/ptrveloc.c
@@ -83,8 +83,6 @@ GetAccelerationProfile(DeviceVelocityPtr vel, int profile_num);
 /* some int which is not a profile number */
 #define PROFILE_UNINITIALIZE (-100)
 
-/* number of properties for predictable acceleration */
-#define NPROPS_PREDICTABLE_ACCEL 4
 
 /**
  * Init struct so it should match the average case
@@ -323,26 +321,18 @@ AccelInitScaleProperty(DeviceIntPtr dev, DeviceVelocityPtr vel)
     return XIRegisterPropertyHandler(dev, AccelSetScaleProperty, NULL, NULL);
 }
 
-static int AccelPropHandlerPrivateKeyIndex;
-DevPrivateKey AccelPropHandlerPrivateKey = &AccelPropHandlerPrivateKeyIndex;
-
 BOOL
 InitializePredictableAccelerationProperties(DeviceIntPtr dev)
 {
     DeviceVelocityPtr  vel = GetDevicePredictableAccelData(dev);
-    long *prop_handlers;
 
     if(!vel)
 	return FALSE;
-    prop_handlers = xalloc(NPROPS_PREDICTABLE_ACCEL * sizeof(long));
-
-    prop_handlers[0] = AccelInitProfileProperty(dev, vel);
-    prop_handlers[1] = AccelInitDecelProperty(dev, vel);
-    prop_handlers[2] = AccelInitAdaptDecelProperty(dev, vel);
-    prop_handlers[3] = AccelInitScaleProperty(dev, vel);
 
-    dixSetPrivate(&dev->devPrivates, AccelPropHandlerPrivateKey,
-                  prop_handlers);
+    vel->prop_handlers[0] = AccelInitProfileProperty(dev, vel);
+    vel->prop_handlers[1] = AccelInitDecelProperty(dev, vel);
+    vel->prop_handlers[2] = AccelInitAdaptDecelProperty(dev, vel);
+    vel->prop_handlers[3] = AccelInitScaleProperty(dev, vel);
 
     return TRUE;
 }
@@ -350,8 +340,8 @@ InitializePredictableAccelerationProperties(DeviceIntPtr dev)
 BOOL
 DeletePredictableAccelerationProperties(DeviceIntPtr dev)
 {
+    DeviceVelocityPtr  vel;
     Atom prop;
-    long *prop_handlers;
     int i;
 
     prop = XIGetKnownProperty(ACCEL_PROP_VELOCITY_SCALING);
@@ -363,13 +353,10 @@ DeletePredictableAccelerationProperties(DeviceIntPtr dev)
     prop = XIGetKnownProperty(ACCEL_PROP_PROFILE_NUMBER);
     XIDeleteDeviceProperty(dev, prop, FALSE);
 
-    prop_handlers = dixLookupPrivate(&dev->devPrivates,
-                                     AccelPropHandlerPrivateKey);
-    dixSetPrivate(&dev->devPrivates, AccelPropHandlerPrivateKey, NULL);
-
-    for (i = 0; prop_handlers && i < NPROPS_PREDICTABLE_ACCEL; i++)
-        XIUnregisterPropertyHandler(dev, prop_handlers[i]);
-    xfree(prop_handlers);
+    vel = GetDevicePredictableAccelData(dev);
+    for (i = 0; vel && i < NPROPS_PREDICTABLE_ACCEL; i++)
+	if (vel->prop_handlers[i])
+	    XIUnregisterPropertyHandler(dev, vel->prop_handlers[i]);
 
     return TRUE;
 }
diff --git a/include/inputstr.h b/include/inputstr.h
index de4026c..6da3f38 100644
--- a/include/inputstr.h
+++ b/include/inputstr.h
@@ -514,8 +514,9 @@ typedef struct _DeviceIntRec {
     LedFeedbackPtr	leds;
     struct _XkbInterest *xkb_interest;
     char                *config_info; /* used by the hotplug layer */
+    ClassesPtr		unused_classes; /* for master devices */
+    int			saved_master_id;	/* for slaves while grabbed */
     PrivateRec		*devPrivates;
-    int			nPrivates;
     DeviceUnwrapProc    unwrapProc;
     SpriteInfoPtr       spriteInfo;
     union {
diff --git a/include/ptrveloc.h b/include/ptrveloc.h
index 676c464..6f999a8 100644
--- a/include/ptrveloc.h
+++ b/include/ptrveloc.h
@@ -62,6 +62,9 @@ typedef struct _MotionTracker {
     int dir;        /* initial direction bitfield */
 } MotionTracker, *MotionTrackerPtr;
 
+/* number of properties for predictable acceleration */
+#define NPROPS_PREDICTABLE_ACCEL 4
+
 /**
  * Contains all data needed to implement mouse ballistics
  */
@@ -88,9 +91,9 @@ typedef struct _DeviceVelocityRec {
     struct {   /* to be able to query this information */
         int     profile_number;
     } statistics;
+    long    prop_handlers[NPROPS_PREDICTABLE_ACCEL];
 } DeviceVelocityRec, *DeviceVelocityPtr;
 
-
 extern _X_EXPORT void
 InitVelocityData(DeviceVelocityPtr vel);
 
-- 
1.7.0



More information about the xorg-devel mailing list