f735cc5e5fb8996c58db0365ee37b602423ac65a
[ircu2.10.12-pk.git] / libs / dbprim / ht_flush.c
1 /*
2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
3 **
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.
8 **
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.
13 **
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,
17 ** MA 02111-1307, USA
18 **
19 ** @(#)$Id$
20 */
21 #include <stdlib.h>
22
23 #include "dbprim.h"
24 #include "dbprim_int.h"
25
26 RCSTAG("@(#)$Id$");
27
28 /** \ingroup dbprim_hash
29  * \brief Flush a hash table.
30  *
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>.
35  *
36  * \param table A pointer to a #hash_table_t.
37  * \param flush_func
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
43  *              flush_func.
44  *
45  * \retval DB_ERR_BADARGS       An argument was invalid.
46  * \retval DB_ERR_FROZEN        The hash table is frozen.
47  */
48 unsigned long
49 ht_flush(hash_table_t *table, hash_iter_t flush_func, void *extra)
50 {
51   unsigned long retval = 0;
52   int i;
53   link_elem_t *elem;
54
55   initialize_dbpr_error_table(); /* initialize error table */
56
57   if (!ht_verify(table)) /* verify arguments */
58     return DB_ERR_BADARGS;
59
60   if (table->ht_flags & HASH_FLAG_FREEZE) /* don't mess with frozen tables */
61     return DB_ERR_FROZEN;
62
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... */
68
69       table->ht_flags |= HASH_FLAG_FREEZE; /* freeze the table */
70
71       if (flush_func) /* call flush function */
72         retval = (*flush_func)(table, le_object(elem), extra);
73
74       table->ht_flags &= ~HASH_FLAG_FREEZE; /* unfreeze table... */
75
76       if (retval) /* if flush function failed, error out */
77         return retval;
78     }
79
80   table->ht_count = 0; /* clear the entry count */
81
82   if (table->ht_flags & HASH_FLAG_AUTOSHRINK) /* shrink the table... */
83     return ht_free(table); /* by calling ht_free() */
84
85   return 0;
86 }