dispatch: Make all API functions non-static.
[mesa.git] / src / mesa / main / hash.c
index dd2c309d52dd46b80f67682c39bc3b65ab5d19c1..61c369a80cdada0d2fc67562875942a5fd92e877 100644 (file)
@@ -37,7 +37,7 @@
 
 #include "glheader.h"
 #include "imports.h"
-#include "glthread.h"
+#include "glapi/glthread.h"
 #include "hash.h"
 
 
@@ -48,8 +48,6 @@
 
 /**
  * An entry in the hash table.  
- *
- * This struct is private to this file.
  */
 struct HashEntry {
    GLuint Key;             /**< the entry's key */
@@ -57,19 +55,20 @@ struct HashEntry {
    struct HashEntry *Next; /**< pointer to next entry */
 };
 
+
 /**
  * The hash table data structure.  
- *
- * This is an opaque types (it's not defined in hash.h file).
  */
 struct _mesa_HashTable {
    struct HashEntry *Table[TABLE_SIZE];  /**< the lookup table */
    GLuint MaxKey;                        /**< highest key inserted so far */
    _glthread_Mutex Mutex;                /**< mutual exclusion lock */
+   _glthread_Mutex WalkMutex;            /**< for _mesa_HashWalk() */
    GLboolean InDeleteAll;                /**< Debug check */
 };
 
 
+
 /**
  * Create a new hash table.
  * 
@@ -81,6 +80,7 @@ _mesa_NewHashTable(void)
    struct _mesa_HashTable *table = CALLOC_STRUCT(_mesa_HashTable);
    if (table) {
       _glthread_INIT_MUTEX(table->Mutex);
+      _glthread_INIT_MUTEX(table->WalkMutex);
    }
    return table;
 }
@@ -98,32 +98,33 @@ _mesa_NewHashTable(void)
 void
 _mesa_DeleteHashTable(struct _mesa_HashTable *table)
 {
-   GLuint i;
+   GLuint pos;
    assert(table);
-   for (i = 0; i < TABLE_SIZE; i++) {
-      struct HashEntry *entry = table->Table[i];
+   for (pos = 0; pos < TABLE_SIZE; pos++) {
+      struct HashEntry *entry = table->Table[pos];
       while (entry) {
         struct HashEntry *next = entry->Next;
-        FREE(entry);
+         if (entry->Data) {
+            _mesa_problem(NULL,
+                          "In _mesa_DeleteHashTable, found non-freed data");
+         }
+        free(entry);
         entry = next;
       }
    }
    _glthread_DESTROY_MUTEX(table->Mutex);
-   FREE(table);
+   _glthread_DESTROY_MUTEX(table->WalkMutex);
+   free(table);
 }
 
 
 
 /**
- * Lookup an entry in the hash table.
- * 
- * \param table the hash table.
- * \param key the key.
- * 
- * \return pointer to user's data or NULL if key not in table
+ * Lookup an entry in the hash table, without locking.
+ * \sa _mesa_HashLookup
  */
-void *
-_mesa_HashLookup(const struct _mesa_HashTable *table, GLuint key)
+static inline void *
+_mesa_HashLookup_unlocked(struct _mesa_HashTable *table, GLuint key)
 {
    GLuint pos;
    const struct HashEntry *entry;
@@ -135,7 +136,7 @@ _mesa_HashLookup(const struct _mesa_HashTable *table, GLuint key)
    entry = table->Table[pos];
    while (entry) {
       if (entry->Key == key) {
-        return entry->Data;
+         return entry->Data;
       }
       entry = entry->Next;
    }
@@ -143,6 +144,25 @@ _mesa_HashLookup(const struct _mesa_HashTable *table, GLuint key)
 }
 
 
+/**
+ * Lookup an entry in the hash table.
+ * 
+ * \param table the hash table.
+ * \param key the key.
+ * 
+ * \return pointer to user's data or NULL if key not in table
+ */
+void *
+_mesa_HashLookup(struct _mesa_HashTable *table, GLuint key)
+{
+   void *res;
+   assert(table);
+   _glthread_LOCK_MUTEX(table->Mutex);
+   res = _mesa_HashLookup_unlocked(table, key);
+   _glthread_UNLOCK_MUTEX(table->Mutex);
+   return res;
+}
+
 
 /**
  * Insert a key/pointer pair into the hash table.  
@@ -168,23 +188,30 @@ _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data)
       table->MaxKey = key;
 
    pos = HASH_FUNC(key);
-   entry = table->Table[pos];
-   while (entry) {
+
+   /* check if replacing an existing entry with same key */
+   for (entry = table->Table[pos]; entry; entry = entry->Next) {
       if (entry->Key == key) {
          /* replace entry's data */
+#if 0 /* not sure this check is always valid */
+         if (entry->Data) {
+            _mesa_problem(NULL, "Memory leak detected in _mesa_HashInsert");
+         }
+#endif
         entry->Data = data;
          _glthread_UNLOCK_MUTEX(table->Mutex);
         return;
       }
-      entry = entry->Next;
    }
 
    /* alloc and insert new table entry */
    entry = MALLOC_STRUCT(HashEntry);
-   entry->Key = key;
-   entry->Data = data;
-   entry->Next = table->Table[pos];
-   table->Table[pos] = entry;
+   if (entry) {
+      entry->Key = key;
+      entry->Data = data;
+      entry->Next = table->Table[pos];
+      table->Table[pos] = entry;
+   }
 
    _glthread_UNLOCK_MUTEX(table->Mutex);
 }
@@ -230,7 +257,7 @@ _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key)
          else {
             table->Table[pos] = entry->Next;
          }
-         FREE(entry);
+         free(entry);
          _glthread_UNLOCK_MUTEX(table->Mutex);
         return;
       }
@@ -250,7 +277,7 @@ _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key)
  * \param table  the hash table to delete
  * \param callback  the callback function
  * \param userData  arbitrary pointer to pass along to the callback
- *                  (this is typically a GLcontext pointer)
+ *                  (this is typically a struct gl_context pointer)
  */
 void
 _mesa_HashDeleteAll(struct _mesa_HashTable *table,
@@ -267,7 +294,7 @@ _mesa_HashDeleteAll(struct _mesa_HashTable *table,
       for (entry = table->Table[pos]; entry; entry = next) {
          callback(entry->Key, entry->Data, userData);
          next = entry->Next;
-         _mesa_free(entry);
+         free(entry);
       }
       table->Table[pos] = NULL;
    }
@@ -278,10 +305,15 @@ _mesa_HashDeleteAll(struct _mesa_HashTable *table,
 
 /**
  * Walk over all entries in a hash table, calling callback function for each.
+ * Note: we use a separate mutex in this function to avoid a recursive
+ * locking deadlock (in case the callback calls _mesa_HashRemove()) and to
+ * prevent multiple threads/contexts from getting tangled up.
+ * A lock-less version of this function could be used when the table will
+ * not be modified.
  * \param table  the hash table to walk
  * \param callback  the callback function
  * \param userData  arbitrary pointer to pass along to the callback
- *                  (this is typically a GLcontext pointer)
+ *                  (this is typically a struct gl_context pointer)
  */
 void
 _mesa_HashWalk(const struct _mesa_HashTable *table,
@@ -293,14 +325,16 @@ _mesa_HashWalk(const struct _mesa_HashTable *table,
    GLuint pos;
    ASSERT(table);
    ASSERT(callback);
-   _glthread_UNLOCK_MUTEX(table2->Mutex);
+   _glthread_LOCK_MUTEX(table2->WalkMutex);
    for (pos = 0; pos < TABLE_SIZE; pos++) {
-      struct HashEntry *entry;
-      for (entry = table->Table[pos]; entry; entry = entry->Next) {
+      struct HashEntry *entry, *next;
+      for (entry = table->Table[pos]; entry; entry = next) {
+         /* save 'next' pointer now in case the callback deletes the entry */
+         next = entry->Next;
          callback(entry->Key, entry->Data, userData);
       }
    }
-   _glthread_UNLOCK_MUTEX(table2->Mutex);
+   _glthread_UNLOCK_MUTEX(table2->WalkMutex);
 }
 
 
@@ -318,7 +352,7 @@ _mesa_HashFirstEntry(struct _mesa_HashTable *table)
    GLuint pos;
    assert(table);
    _glthread_LOCK_MUTEX(table->Mutex);
-   for (pos=0; pos < TABLE_SIZE; pos++) {
+   for (pos = 0; pos < TABLE_SIZE; pos++) {
       if (table->Table[pos]) {
          _glthread_UNLOCK_MUTEX(table->Mutex);
          return table->Table[pos]->Key;
@@ -346,16 +380,14 @@ _mesa_HashNextEntry(const struct _mesa_HashTable *table, GLuint key)
 
    /* Find the entry with given key */
    pos = HASH_FUNC(key);
-   entry = table->Table[pos];
-   while (entry) {
+   for (entry = table->Table[pos]; entry ; entry = entry->Next) {
       if (entry->Key == key) {
          break;
       }
-      entry = entry->Next;
    }
 
    if (!entry) {
-      /* the key was not found, we can't find next entry */
+      /* the given key was not found, so we can't find the next entry */
       return 0;
    }
 
@@ -385,10 +417,10 @@ _mesa_HashNextEntry(const struct _mesa_HashTable *table, GLuint key)
 void
 _mesa_HashPrint(const struct _mesa_HashTable *table)
 {
-   GLuint i;
+   GLuint pos;
    assert(table);
-   for (i=0;i<TABLE_SIZE;i++) {
-      const struct HashEntry *entry = table->Table[i];
+   for (pos = 0; pos < TABLE_SIZE; pos++) {
+      const struct HashEntry *entry = table->Table[pos];
       while (entry) {
         _mesa_debug(NULL, "%u %p\n", entry->Key, entry->Data);
         entry = entry->Next;
@@ -414,7 +446,7 @@ _mesa_HashPrint(const struct _mesa_HashTable *table)
 GLuint
 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys)
 {
-   GLuint maxKey = ~((GLuint) 0);
+   const GLuint maxKey = ~((GLuint) 0);
    _glthread_LOCK_MUTEX(table->Mutex);
    if (maxKey - numKeys > table->MaxKey) {
       /* the quick solution */
@@ -426,8 +458,8 @@ _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys)
       GLuint freeCount = 0;
       GLuint freeStart = 1;
       GLuint key;
-      for (key=1; key!=maxKey; key++) {
-        if (_mesa_HashLookup(table, key)) {
+      for (key = 1; key != maxKey; key++) {
+        if (_mesa_HashLookup_unlocked(table, key)) {
            /* darn, this key is already in use */
            freeCount = 0;
            freeStart = key+1;
@@ -448,6 +480,26 @@ _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys)
 }
 
 
+/**
+ * Return the number of entries in the hash table.
+ */
+GLuint
+_mesa_HashNumEntries(const struct _mesa_HashTable *table)
+{
+   GLuint pos, count = 0;
+
+   for (pos = 0; pos < TABLE_SIZE; pos++) {
+      const struct HashEntry *entry;
+      for (entry = table->Table[pos]; entry; entry = entry->Next) {
+         count++;
+      }
+   }
+
+   return count;
+}
+
+
+
 #if 0 /* debug only */
 
 /**