util/strndup: move header inclusion as applicable
[mesa.git] / src / util / hash_table.c
index 3ec65afd4bb43b3e55c34564ff6641dc3fb2cc05..9e643af8b239a19c84472d1fb5dbe4cdba89729b 100644 (file)
@@ -163,6 +163,32 @@ _mesa_hash_table_destroy(struct hash_table *ht,
    ralloc_free(ht);
 }
 
+/**
+ * Deletes all entries of the given hash table without deleting the table
+ * itself or changing its structure.
+ *
+ * If delete_function is passed, it gets called on each entry present.
+ */
+void
+_mesa_hash_table_clear(struct hash_table *ht,
+                       void (*delete_function)(struct hash_entry *entry))
+{
+   struct hash_entry *entry;
+
+   for (entry = ht->table; entry != ht->table + ht->size; entry++) {
+      if (entry->key == NULL)
+         continue;
+
+      if (delete_function != NULL && entry->key != ht->deleted_key)
+         delete_function(entry);
+
+      entry->key = NULL;
+   }
+
+   ht->entries = 0;
+   ht->deleted_entries = 0;
+}
+
 /** Sets the value of the key pointer used for deleted entries in the table.
  *
  * The assumption is that usually keys are actual pointers, so we use a
@@ -269,6 +295,8 @@ hash_table_insert(struct hash_table *ht, uint32_t hash,
    uint32_t start_hash_address, hash_address;
    struct hash_entry *available_entry = NULL;
 
+   assert(key != NULL);
+
    if (ht->entries >= ht->max_entries) {
       _mesa_hash_table_rehash(ht, ht->size_index + 1);
    } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {