lib/shaRes.c

Go to the documentation of this file.
00001 /* ShaRes.c - implementation of shared resources 
00002  *
00003  * This file is copyright 2002 Jim Kent, but license is hereby
00004  * granted for all use - public, private or commercial. */
00005 
00006 #include "common.h"
00007 #include "shaRes.h"
00008 
00009 static char const rcsid[] = "$Id: shaRes.c,v 1.4 2003/05/06 07:33:44 kate Exp $";
00010 
00011 static void shaFreeNode(struct shaResNode *node)
00012 /* Free a node.  (Don't look at link count, just do it.) */
00013 {
00014 struct shaResNode *next = node->next;
00015 struct shaResNode *prev = node->prev;
00016 struct shaResList *list = node->list;
00017 
00018 if (next != NULL)
00019     next->prev = prev;
00020 if (prev != NULL)
00021     prev->next = next;
00022 else
00023     list->head = next;
00024 list->freeData(node->data);
00025 freeMem(node->name);
00026 freeMem(node);
00027 }        
00028 
00029 void shaUnlink(struct shaResNode *node)
00030 /* Decrement link count and free if down to zero. */
00031 {
00032 if (--node->links <= 0)
00033     shaFreeNode(node);
00034 }
00035 
00036 void shaLink(struct shaResNode *node)
00037 /* Increment link count. */
00038 {
00039 ++node->links;
00040 }
00041 
00042 struct shaResNode *shaNewNode(struct shaResList *list, char *name, void *data)
00043 /* Create a new node with link count of one. */
00044 {
00045 struct shaResNode *nn = needMem(sizeof(*nn));
00046 struct shaResNode *head = list->head;
00047 
00048 /* Store the goods and what list we're on, and start with one link. */
00049 nn->list = list;
00050 nn->data = data;
00051 nn->links = 1;
00052 nn->name = cloneString(name);
00053 
00054 /* Put us at the front of the list. */
00055 nn->next = head;
00056 nn->prev = NULL;
00057 if (head != NULL)
00058     head->prev = nn;
00059 list->head = nn;
00060 return nn;
00061 }
00062 
00063 void shaCleanup(struct shaResList *list)
00064 /* Free every node on list. */
00065 {
00066 struct shaResNode *node, *next;
00067 
00068 for (node = list->head;node != NULL;node = next)
00069     {
00070     next = node->next;
00071     shaFreeNode(node);
00072     }
00073 list->head = NULL;
00074 }
00075 
00076 void shaInit(struct shaResList *list, void (*freeData)(void *pData))
00077 /* Start up resource list. */
00078 {
00079 list->head = NULL;
00080 list->freeData = freeData;
00081 }

Generated on Tue Dec 25 18:39:31 2007 for blat by  doxygen 1.5.2