[RFC] [PATCH v2 6/12] [xserver] dix: add hashing functions to resource.h for others to use.

Erkki Seppälä erkki.seppala at vincit.fi
Fri Dec 31 05:32:44 PST 2010


The public hashing function HashResourceID uses the same hashing
hashing algorithm as resource.c uses internally, but uses an interface
that will be directly usable by a generic hash table module
(introduced in a later patch).

Signed-off-by: Erkki Seppälä <erkki.seppala at vincit.fi>
Reviewed-by: Rami Ylimäki <rami.ylimaki at vincit.fi>
---
 dix/resource.c     |   46 ++++++++++++++++++++++++++++++++++------------
 include/resource.h |   16 ++++++++++++++++
 2 files changed, 50 insertions(+), 12 deletions(-)

diff --git a/dix/resource.c b/dix/resource.c
index 7184ead..3270843 100644
--- a/dix/resource.c
+++ b/dix/resource.c
@@ -641,27 +641,49 @@ InitClientResources(ClientPtr client)
     return TRUE;
 }
 
-
-static int
-Hash(int client, XID id)
+unsigned
+HashResourceID(void * cdata, const void * data, int numBits)
 {
-    id &= RESOURCE_ID_MASK;
-    switch (clientTable[client].hashsize)
+    const XID* idPtr = data;
+    XID id = *idPtr & RESOURCE_ID_MASK;
+    switch (numBits)
     {
 	case 6:
-	    return ((int)(0x03F & (id ^ (id>>6) ^ (id>>12))));
+	    return ((unsigned)(0x03F & (id ^ (id>>6) ^ (id>>12))));
 	case 7:
-	    return ((int)(0x07F & (id ^ (id>>7) ^ (id>>13))));
+	    return ((unsigned)(0x07F & (id ^ (id>>7) ^ (id>>13))));
 	case 8:
-	    return ((int)(0x0FF & (id ^ (id>>8) ^ (id>>16))));
+	    return ((unsigned)(0x0FF & (id ^ (id>>8) ^ (id>>16))));
 	case 9:
-	    return ((int)(0x1FF & (id ^ (id>>9))));
+	    return ((unsigned)(0x1FF & (id ^ (id>>9))));
 	case 10:
-	    return ((int)(0x3FF & (id ^ (id>>10))));
+	    return ((unsigned)(0x3FF & (id ^ (id>>10))));
 	case 11:
-	    return ((int)(0x7FF & (id ^ (id>>11))));
+	    return ((unsigned)(0x7FF & (id ^ (id>>11))));
+    }
+    return 0;
+}
+
+static int
+Hash(int client, XID id)
+{
+    int numBits = clientTable[client].hashsize;
+    if (numBits < INITHASHSIZE || numBits > MAXHASHSIZE) {
+        return -1;
+    } else {
+        return HashResourceID(NULL, &id, numBits);
     }
-    return -1;
+}
+
+int
+CompareResourceID(void* cdata, const void* a, const void* b)
+{
+    const XID* xa = a;
+    const XID* xb = b;
+    return
+      *xa < *xb ? -1 :
+      *xa > *xb ? 1 :
+      0;
 }
 
 static XID
diff --git a/include/resource.h b/include/resource.h
index aea617c..ded49ec 100644
--- a/include/resource.h
+++ b/include/resource.h
@@ -290,6 +290,22 @@ extern _X_EXPORT unsigned int GetXIDList(
 extern _X_EXPORT RESTYPE lastResourceType;
 extern _X_EXPORT RESTYPE TypeMask;
 
+/** @brief  A hashing function to be used for hashing resource IDs when
+            used with HashTables. It makes no use of cdata, so that can
+            be NULL. */
+extern _X_EXPORT unsigned HashResourceID(
+    void * cdata,
+    const void * data,
+    int numBits);
+
+/** @brief  A comparison function to be used for hashing resource IDs
+            when used with HashTables. It makes no use of cdata, so 
+            that can be NULL. */
+extern _X_EXPORT int CompareResourceID(
+    void* cdata,
+    const void* a,
+    const void* b);
+
 /*
  * These are deprecated compatibility functions and will be removed soon!
  * Please use the noted replacements instead.
-- 
1.7.0.4



More information about the xorg-devel mailing list