[PATCH 2/2] xfixes: Use list.h macros in tracking hide count

Kristian Høgsberg krh at bitplanet.net
Mon Jun 14 06:25:23 PDT 2010


---

This is not a patch to be applied.  Recently, the utility of list.h was
questioned and as I came a across the cursor hide count code today I
figured it would make a good example of how list.h can make the code
significantly simpler.  In particular, it's easy to prepend and iterate
though a singly linked list, but anything else is going to be buggy or slow
or best-case just 20 lines of code to delete an element that you shouldn't
have to write and debug again (cf deleteCursorHideCount() below).

Kristian

 xfixes/cursor.c |   47 +++++++++++++----------------------------------
 1 files changed, 13 insertions(+), 34 deletions(-)

diff --git a/xfixes/cursor.c b/xfixes/cursor.c
index d3a207d..c8d03c7 100644
--- a/xfixes/cursor.c
+++ b/xfixes/cursor.c
@@ -53,6 +53,7 @@
 #include "inputstr.h"
 #include "windowstr.h"
 #include "xace.h"
+#include "list.h"
 
 static RESTYPE		CursorClientType;
 static RESTYPE		CursorHideCountType;
@@ -100,7 +101,7 @@ static CursorEventPtr	    cursorEvents;
 typedef struct _CursorHideCountRec *CursorHideCountPtr;
 
 typedef struct _CursorHideCountRec {
-    CursorHideCountPtr   pNext;  
+    struct list          link;
     ClientPtr            pClient;
     ScreenPtr            pScreen;
     int                  hideCount;
@@ -114,7 +115,8 @@ typedef struct _CursorHideCountRec {
 typedef struct _CursorScreen {
     DisplayCursorProcPtr	DisplayCursor;
     CloseScreenProcPtr		CloseScreen;
-    CursorHideCountPtr          pCursorHideCounts;
+    struct list                 CursorHideCounts;
+
 } CursorScreenRec, *CursorScreenPtr;
 
 #define GetCursorScreen(s) ((CursorScreenPtr)dixLookupPrivate(&(s)->devPrivates, CursorScreenPrivateKey))
@@ -146,7 +148,7 @@ CursorDisplayCursor (DeviceIntPtr pDev,
     if (ConnectionInfo)
 	CursorVisible = EnableCursor;
 
-    if (cs->pCursorHideCounts != NULL || !CursorVisible) {
+    if (!list_is_empty(&cs->CursorHideCounts) || !CursorVisible) {
 	ret = (*pScreen->DisplayCursor) (pDev, pScreen, NullCursor);
     } else {
 	ret = (*pScreen->DisplayCursor) (pDev, pScreen, pCursor);
@@ -779,7 +781,7 @@ findCursorHideCount (ClientPtr pClient, ScreenPtr pScreen)
     CursorScreenPtr    cs = GetCursorScreen(pScreen);
     CursorHideCountPtr pChc;
 
-    for (pChc = cs->pCursorHideCounts; pChc != NULL; pChc = pChc->pNext) {
+    list_for_each_entry(pChc, &cs->CursorHideCounts, link) {
 	if (pChc->pClient == pClient) {
 	    return pChc;
 	}
@@ -802,8 +804,7 @@ createCursorHideCount (ClientPtr pClient, ScreenPtr pScreen)
     pChc->pScreen = pScreen;
     pChc->hideCount = 1;
     pChc->resource = FakeClientID(pClient->index);
-    pChc->pNext = cs->pCursorHideCounts;
-    cs->pCursorHideCounts = pChc;
+    list_add(&pChc->link, &cs->CursorHideCounts);
     
     /* 
      * Create a resource for this element so it can be deleted
@@ -822,27 +823,10 @@ createCursorHideCount (ClientPtr pClient, ScreenPtr pScreen)
  * Delete the given hide-counts list element from its screen list.
  */
 static void
-deleteCursorHideCount (CursorHideCountPtr pChcToDel, ScreenPtr pScreen)
+deleteCursorHideCount (CursorHideCountPtr pChcToDel)
 {
-    CursorScreenPtr    cs = GetCursorScreen(pScreen);
-    CursorHideCountPtr pChc, pNext;
-    CursorHideCountPtr pChcLast = NULL;
-
-    pChc = cs->pCursorHideCounts;
-    while (pChc != NULL) {
-	pNext = pChc->pNext;
-	if (pChc == pChcToDel) {
-	    free(pChc);
-	    if (pChcLast == NULL) {
-		cs->pCursorHideCounts = pNext;
-	    } else {
-		pChcLast->pNext = pNext;
-	    }
-	    return;
-	}
-	pChcLast = pChc;
-	pChc = pNext;
-    }
+    list_del(&pChcToDel->link);
+    free(pChcToDel);
 }
 
 /* 
@@ -854,13 +838,8 @@ deleteCursorHideCountsForScreen (ScreenPtr pScreen)
     CursorScreenPtr    cs = GetCursorScreen(pScreen);
     CursorHideCountPtr pChc, pTmp;
 
-    pChc = cs->pCursorHideCounts;
-    while (pChc != NULL) {
-	pTmp = pChc->pNext;
+    list_for_each_entry_safe(pChc, pTmp, &cs->CursorHideCounts, link)
 	FreeResource(pChc->resource, 0);
-	pChc = pTmp;
-    }
-    cs->pCursorHideCounts = NULL;   
 }
 
 int 
@@ -1002,7 +981,7 @@ CursorFreeHideCount (pointer data, XID id)
     ScreenPtr pScreen = pChc->pScreen;
     DeviceIntPtr dev;
 
-    deleteCursorHideCount(pChc, pChc->pScreen);
+    deleteCursorHideCount(pChc);
     for (dev = inputInfo.devices; dev; dev = dev->next)
     {
         if (IsMaster(dev) && IsPointerDevice(dev))
@@ -1047,7 +1026,7 @@ XFixesCursorInit (void)
 	    return FALSE;
 	Wrap (cs, pScreen, CloseScreen, CursorCloseScreen);
 	Wrap (cs, pScreen, DisplayCursor, CursorDisplayCursor);
-	cs->pCursorHideCounts = NULL;
+	list_init(&cs->CursorHideCounts);
 	SetCursorScreen (pScreen, cs);
     }
     CursorClientType = CreateNewResourceType(CursorFreeClient,
-- 
1.7.1



More information about the xorg-devel mailing list