00001 /* 00002 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu> 00003 ** 00004 ** This library is free software; you can redistribute it and/or 00005 ** modify it under the terms of the GNU Library General Public 00006 ** License as published by the Free Software Foundation; either 00007 ** version 2 of the License, or (at your option) any later version. 00008 ** 00009 ** This library is distributed in the hope that it will be useful, 00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 ** Library General Public License for more details. 00013 ** 00014 ** You should have received a copy of the GNU Library General Public 00015 ** License along with this library; if not, write to the Free 00016 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00017 ** MA 02111-1307, USA 00018 ** 00019 ** @(#)$Id: ht_flush.c,v 1.3 2006/07/13 05:36:16 klmitch Exp $ 00020 */ 00028 #include <stdlib.h> 00029 00030 #include "dbprim.h" 00031 #include "dbprim_int.h" 00032 00033 RCSTAG("@(#)$Id: ht_flush.c,v 1.3 2006/07/13 05:36:16 klmitch Exp $"); 00034 00035 unsigned long 00036 ht_flush(hash_table_t *table, hash_iter_t flush_func, void *extra) 00037 { 00038 unsigned long retval = 0; 00039 int i; 00040 link_elem_t *elem; 00041 00042 initialize_dbpr_error_table(); /* initialize error table */ 00043 00044 if (!ht_verify(table)) /* verify arguments */ 00045 return DB_ERR_BADARGS; 00046 00047 if (table->ht_flags & HASH_FLAG_FREEZE) /* don't mess with frozen tables */ 00048 return DB_ERR_FROZEN; 00049 00050 /* walk through all the elements */ 00051 for (i = 0; i < table->ht_modulus; i++) 00052 for (elem = ll_first(&table->ht_table[i]); elem; 00053 elem = ll_first(&table->ht_table[i])) { 00054 ht_remove(table, le_object(elem)); /* remove the entry... */ 00055 00056 table->ht_flags |= HASH_FLAG_FREEZE; /* freeze the table */ 00057 00058 if (flush_func) /* call flush function */ 00059 retval = (*flush_func)(table, le_object(elem), extra); 00060 00061 table->ht_flags &= ~HASH_FLAG_FREEZE; /* unfreeze table... */ 00062 00063 if (retval) /* if flush function failed, error out */ 00064 return retval; 00065 } 00066 00067 table->ht_count = 0; /* clear the entry count */ 00068 00069 if (table->ht_flags & HASH_FLAG_AUTOSHRINK) /* shrink the table... */ 00070 return ht_free(table); /* by calling ht_free() */ 00071 00072 return 0; 00073 }