2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Library General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2 of the License, or (at your option) any later version.
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Library General Public License for more details.
14 ** You should have received a copy of the GNU Library General Public
15 ** License along with this library; if not, write to the Free
16 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 #include "dbprim_int.h"
28 /** \ingroup dbprim_hash
29 * \brief Flush a hash table.
31 * This function flushes a hash table--that is, it removes each entry
32 * from the table. If a \p flush_func is specified, it will be called
33 * on the entry after it has been removed from the table, and may
34 * safely call <CODE>free()</CODE>.
36 * \param table A pointer to a #hash_table_t.
38 * A pointer to a callback function used to perform
39 * user-specified actions on an entry after removing it
40 * from the table. May be \c NULL. See the
41 * documentation for #hash_iter_t for more information.
42 * \param extra A \c void pointer that will be passed to \p
45 * \retval DB_ERR_BADARGS An argument was invalid.
46 * \retval DB_ERR_FROZEN The hash table is frozen.
49 ht_flush(hash_table_t *table, hash_iter_t flush_func, void *extra)
51 unsigned long retval = 0;
55 initialize_dbpr_error_table(); /* initialize error table */
57 if (!ht_verify(table)) /* verify arguments */
58 return DB_ERR_BADARGS;
60 if (table->ht_flags & HASH_FLAG_FREEZE) /* don't mess with frozen tables */
63 /* walk through all the elements */
64 for (i = 0; i < table->ht_modulus; i++)
65 for (elem = ll_first(&table->ht_table[i]); elem;
66 elem = ll_first(&table->ht_table[i])) {
67 ht_remove(table, le_object(elem)); /* remove the entry... */
69 table->ht_flags |= HASH_FLAG_FREEZE; /* freeze the table */
71 if (flush_func) /* call flush function */
72 retval = (*flush_func)(table, le_object(elem), extra);
74 table->ht_flags &= ~HASH_FLAG_FREEZE; /* unfreeze table... */
76 if (retval) /* if flush function failed, error out */
80 table->ht_count = 0; /* clear the entry count */
82 if (table->ht_flags & HASH_FLAG_AUTOSHRINK) /* shrink the table... */
83 return ht_free(table); /* by calling ht_free() */