Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals

smat_freelist.c

Go to the documentation of this file.
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: smat_freelist.c,v 1.5 2006/07/13 20:27:36 klmitch Exp $
00020 */
00030 #include <stdlib.h>
00031 #include <string.h>
00032 
00033 #include "dbprim.h"
00034 #include "dbprim_int.h"
00035 
00036 RCSTAG("@(#)$Id: smat_freelist.c,v 1.5 2006/07/13 20:27:36 klmitch Exp $");
00037 
00045 static link_head_t _smat_freelist = LINK_HEAD_INIT(0);
00046 
00047 smat_entry_t *
00048 _smat_alloc(void)
00049 {
00050   link_elem_t *le;
00051   smat_entry_t *se;
00052 
00053   if (!ll_count(&_smat_freelist) || !(le = ll_first(&_smat_freelist)) ||
00054       ll_remove(&_smat_freelist, le)) {
00055     /* Must allocate a new element */
00056     if (!(se = (smat_entry_t *)malloc(sizeof(smat_entry_t))))
00057       return 0; /* couldn't allocate an entry... */
00058   } else
00059     se = le_object(le); /* get smat entry object */
00060 
00061   /* initialize a smat entry */
00062   if (he_init(&se->se_hash, se) || le_init(&se->se_link[SMAT_LOC_FIRST], se) ||
00063       le_init(&se->se_link[SMAT_LOC_SECOND], se)) {
00064     free(se); /* initialization failed... */
00065     return 0;
00066   }
00067 
00068   se->se_table = 0; /* initialize the rest of the structure */
00069   memset(se->se_object, 0, sizeof(se->se_object)); /* zero object pointers */
00070 
00071   se->se_magic = SMAT_ENTRY_MAGIC; /* set up the magic number */
00072 
00073   return se; /* return the object */
00074 }
00075 
00076 void
00077 _smat_free(smat_entry_t *entry)
00078 {
00079   entry->se_magic = 0; /* clear magic number to prevent use */
00080 
00081   /* Force the link element to point to the *smat* entry */
00082   le_object(_se_link(entry)) = entry;
00083 
00084   /* Add the entry to the free list */
00085   if (ll_add(&_smat_freelist, _se_link(entry), LINK_LOC_HEAD, 0))
00086     free(entry); /* addition failed, so free the entry */
00087 }
00088 
00089 unsigned long
00090 smat_cleanup(void)
00091 {
00092   link_elem_t *entry;
00093 
00094   initialize_dbpr_error_table(); /* set up error tables */
00095 
00096   /* walk the free list */
00097   while ((entry = ll_first(&_smat_freelist))) {
00098     ll_remove(&_smat_freelist, entry); /* remove entry */
00099     free(le_object(entry)); /* free the element */
00100   }
00101 
00102   return 0;
00103 }
00104 
00105 unsigned long
00106 smat_freemem(void)
00107 {
00108   initialize_dbpr_error_table(); /* set up error tables */
00109 
00110   /* tell caller how much memory we're using */
00111   return ll_count(&_smat_freelist) * sizeof(smat_entry_t);
00112 }

Generated on Sat Jul 15 14:10:33 2006 for DatabasePrimitivesLibrary by  doxygen 1.4.4