cscvs to tla changeset 20
authorJonathan McDowell <noodles@earth.li>
Mon, 31 May 2004 23:47:03 +0000 (23:47 +0000)
committerJonathan McDowell <noodles@earth.li>
Mon, 31 May 2004 23:47:03 +0000 (23:47 +0000)
Author: noodles
Date: 2002/11/09 13:04:17
Addition of llfree function to cleanup linked lists.

hash.c
ll.c
ll.h

diff --git a/hash.c b/hash.c
index e29017cda8a98e286dbcb3ab1c7e81537276328b..f07017a669d7e65342ce81637d801d0548bb9f59 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -48,20 +48,14 @@ void destroyhash(void)
 {
        int i;
        struct ll *curll = NULL;
-       struct ll *nextll = NULL;
 
        for (i = 0; i < HASHSIZE; i++) {
                curll = hashtable[i];
-               while (curll != NULL) {
-                       nextll = curll->next;
-                       /*
-                        * TODO: The problem is the object has pointers that
-                        * need freed too.
-                        */
-                       free(curll->object);
-                       free(curll);
-                       curll = nextll;
-               }
+               /*
+                * TODO: The problem is the object has pointers that
+                * need freed too.
+                */
+               llfree(curll, free);
                hashtable[i] = NULL;
        }
        elements = 0;
diff --git a/ll.c b/ll.c
index 71103786483d2bfc1fb7eeaf8879b9b894f06ea1..5936bf60d5665f444237561d6b612205a22c7dc0 100644 (file)
--- a/ll.c
+++ b/ll.c
@@ -81,3 +81,29 @@ unsigned long llsize(struct ll *curll)
 
        return count;
 }
+
+/**
+ *     llfree - Frees a linked list.
+ *     @curll: The list to free.
+ *     @objectfree: A pointer to a free function for the object.
+ *
+ *     Walks through a list and free it. If a function is provided for
+ *     objectfree then it's called for each element to free them, if it's NULL
+ *     just the list is freed.
+ */
+struct ll *llfree(struct ll *curll,
+       void (*objectfree) (void *object))
+{
+       struct ll *nextll;
+
+       while (curll != NULL) {
+               nextll = curll->next;
+               if (curll->object != NULL && objectfree != NULL) {
+                       objectfree(curll->object);
+                       curll->object = NULL;
+               }
+               free(curll);
+               curll = nextll;
+       }
+       return NULL;
+}
diff --git a/ll.h b/ll.h
index 3890ca9c9c5f019b2b1e3375c3dce650f03e7c80..15bc64d39c80413b1e54d98001ef75db46e57822 100644 (file)
--- a/ll.h
+++ b/ll.h
@@ -50,13 +50,26 @@ struct ll {
 struct ll *lladd(struct ll *curll, void *object);
 
 /**
+ *     lldel - Remove an item from a linked list.
+ *     @curll: The list to remove the item from.
+ *     @object: The object to remove.
+ *     @objectcmp: A pointer to a comparision function for the object type.
  *
+ *     Trawls through the list looking for the object. If it's found then it
+ *     is removed from the list. Only one occurance is searched for. Returns
+ *     a pointer to the head of the new list.
  */
 struct ll *lldel(struct ll *curll, void *object,
        int (*objectcmp) (const void *object1, const void *object2));
 
 /**
+ *     llfind - Find an item in a linked list.
+ *     @curll: The list to look in.
+ *     @object: The object to look for.
+ *     @objectcmp: A pointer to a comparision function for the object type.
  *
+ *     Searches through a list for an object. Returns a pointer to the object
+ *     if it's found, otherwise NULL.
  */
 struct ll *llfind(struct ll *curll, void *object,
        int (*objectcmp) (const void *object1, const void *object2));
@@ -69,4 +82,16 @@ struct ll *llfind(struct ll *curll, void *object,
  */
 unsigned long llsize(struct ll *curll);
 
+/**
+ *     llfree - Frees a linked list.
+ *     @curll: The list to free.
+ *     @objectfree: A pointer to a free function for the object.
+ *
+ *     Walks through a list and free it. If a function is provided for
+ *     objectfree then it's called for each element to free them, if it's NULL
+ *     just the list is freed.
+ */
+struct ll *llfree(struct ll *curll,
+       void (*objectfree) (void *object));
+
 #endif /* __LL_H__ */