39801fad8b2f5afb17fb1c880553df401afc0871
[ircu2.10.12-pk.git] / libs / dbprim / smat_freelist.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 static link_head_t _smat_freelist = LINK_HEAD_INIT(0);
29
30 smat_entry_t *
31 _smat_alloc(void)
32 {
33   link_elem_t *le;
34   smat_entry_t *se;
35
36   if (!ll_count(&_smat_freelist) || !(le = ll_first(&_smat_freelist)) ||
37       ll_remove(&_smat_freelist, le)) {
38     /* Must allocate a new element */
39     if (!(se = (smat_entry_t *)malloc(sizeof(smat_entry_t))))
40       return 0; /* couldn't allocate an entry... */
41   } else
42     se = le_object(le); /* get smat entry object */
43
44   /* initialize a smat entry */
45   if (he_init(&se->se_hash, se) || le_init(&se->se_link[SMAT_LOC_FIRST], se) ||
46       le_init(&se->se_link[SMAT_LOC_SECOND], se)) {
47     free(se); /* initialization failed... */
48     return 0;
49   }
50
51   se->se_table = 0; /* initialize the rest of the structure */
52   se->se_object[SMAT_LOC_FIRST] = 0;
53   se->se_object[SMAT_LOC_SECOND] = 0;
54
55   se->se_magic = SMAT_ENTRY_MAGIC; /* set up the magic number */
56
57   return se; /* return the object */
58 }
59
60 void
61 _smat_free(smat_entry_t *entry)
62 {
63   entry->se_magic = 0; /* clear magic number to prevent use */
64
65   /* Add the entry to the free list */
66   if (ll_add(&_smat_freelist, _se_link(entry), LINK_LOC_HEAD, 0))
67     free(entry); /* addition failed, so free the entry */
68 }
69
70 /** \ingroup dbprim_smat
71  * \brief Clean up the smat free list.
72  *
73  * This function frees all smat_entry_t objects on the internal free
74  * list.  It is always successful and returns 0.
75  */
76 unsigned long
77 smat_cleanup(void)
78 {
79   link_elem_t *entry;
80
81   initialize_dbpr_error_table(); /* set up error tables */
82
83   /* walk the free list */
84   while ((entry = ll_first(&_smat_freelist))) {
85     ll_remove(&_smat_freelist, entry); /* remove entry */
86     free(le_object(entry)); /* free the element */
87   }
88
89   return 0;
90 }
91
92 /** \ingroup dbprim_smat
93  * \brief Report how much memory is used by the free list.
94  *
95  * This function returns the amount of memory being used by the
96  * internal free list of smat_entry_t objects.
97  *
98  * \return      A number indicating the size, in bytes, of the memory
99  *              allocated for smat_entry_t objects on the free list.
100  */
101 unsigned long
102 smat_freemem(void)
103 {
104   initialize_dbpr_error_table(); /* set up error tables */
105
106   /* tell caller how much memory we're using */
107   return ll_count(&_smat_freelist) * sizeof(smat_entry_t);
108 }