24dde35ac49b1494578f8b99d148d99511c0b244
[ircu2.10.12-pk.git] / libs / dbprim / ll_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 "dbprim.h"
22 #include "dbprim_int.h"
23
24 RCSTAG("@(#)$Id$");
25
26 /** \ingroup dbprim_link
27  * \brief Flush a linked list.
28  *
29  * This function flushes a linked list--that is, it removes each
30  * element from the list.  If a \p flush_func is specified, it will be
31  * called on the entry after it has been removed from the list, and
32  * may safely call <CODE>free()</CODE>.
33  *
34  * \param list  A pointer to a #link_head_t.
35  * \param flush_func
36  *              A pointer to a callback function used to perform
37  *              user-specified actions on an element after removing it
38  *              from the list.  May be \c NULL.  See the documentation
39  *              for #link_iter_t for more information.
40  * \param extra A \c void pointer that will be passed to \p
41  *              flush_func.
42  *
43  * \retval DB_ERR_BADARGS       An argument was invalid.
44  */
45 unsigned long
46 ll_flush(link_head_t *list, link_iter_t flush_func, void *extra)
47 {
48   link_elem_t *elem;
49   unsigned long retval;
50
51   initialize_dbpr_error_table(); /* initialize error table */
52
53   if (!ll_verify(list)) /* Verify arguments */
54     return DB_ERR_BADARGS;
55
56   while ((elem = list->lh_first)) { /* Walk through the list... */
57     ll_remove(list, elem); /* remove the element */
58     /* call flush function, erroring out if it fails */
59     if (flush_func && (retval = (*flush_func)(list, elem, extra)))
60       return retval;
61   }
62
63   list->lh_count = 0; /* clear the list head */
64   list->lh_first = 0;
65   list->lh_last = 0;
66
67   return 0;
68 }