]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/linklist.c
*: make consistent & update GPLv2 file headers
[mirror_frr.git] / lib / linklist.c
index 5a2b696930458672fe72f6ceca0d37bc089717c9..0aee54d44ce25a81c5e65f541cfdf7fde5945505 100644 (file)
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with GNU Zebra; see the file COPYING.  If not, write to the Free
- * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <zebra.h>
 
 #include "linklist.h"
 #include "memory.h"
-\f
+
+DEFINE_MTYPE_STATIC(LIB, LINK_LIST, "Link List")
+DEFINE_MTYPE_STATIC(LIB, LINK_NODE, "Link Node")
+
 /* Allocate new list. */
 struct list *
-list_new ()
+list_new (void)
 {
-  struct list *new;
-
-  new = XMALLOC (MTYPE_LINK_LIST, sizeof (struct list));
-  memset (new, 0, sizeof (struct list));
-  return new;
+  return XCALLOC (MTYPE_LINK_LIST, sizeof (struct list));
 }
 
 /* Free list. */
@@ -44,13 +42,9 @@ list_free (struct list *l)
 
 /* Allocate new listnode.  Internal use only. */
 static struct listnode *
-listnode_new ()
+listnode_new (void)
 {
-  struct listnode *node;
-
-  node = XMALLOC (MTYPE_LINK_NODE, sizeof (struct listnode));
-  memset (node, 0, sizeof (struct listnode));
-  return node;
+  return XCALLOC (MTYPE_LINK_NODE, sizeof (struct listnode));
 }
 
 /* Free listnode. */
@@ -59,13 +53,15 @@ listnode_free (struct listnode *node)
 {
   XFREE (MTYPE_LINK_NODE, node);
 }
-\f
+
 /* Add new data to the list. */
 void
 listnode_add (struct list *list, void *val)
 {
   struct listnode *node;
-
+  
+  assert (val != NULL);
+  
   node = listnode_new ();
 
   node->prev = list->tail;
@@ -80,13 +76,20 @@ listnode_add (struct list *list, void *val)
   list->count++;
 }
 
-/* Add new node with sort function. */
+/*
+ * Add a node to the list.  If the list was sorted according to the
+ * cmp function, insert a new node with the given val such that the
+ * list remains sorted.  The new node is always inserted; there is no
+ * notion of omitting duplicates.
+ */
 void
 listnode_add_sort (struct list *list, void *val)
 {
   struct listnode *n;
   struct listnode *new;
-
+  
+  assert (val != NULL);
+  
   new = listnode_new ();
   new->data = val;
 
@@ -121,11 +124,13 @@ listnode_add_sort (struct list *list, void *val)
   list->count++;
 }
 
-void
+struct listnode *
 listnode_add_after (struct list *list, struct listnode *pp, void *val)
 {
   struct listnode *nn;
-
+  
+  assert (val != NULL);
+  
   nn = listnode_new ();
   nn->data = val;
 
@@ -153,8 +158,55 @@ listnode_add_after (struct list *list, struct listnode *pp, void *val)
 
       pp->next = nn;
     }
+  list->count++;
+  return nn;
+}
+
+struct listnode *
+listnode_add_before (struct list *list, struct listnode *pp, void *val)
+{
+  struct listnode *nn;
+
+  assert (val != NULL);
+
+  nn = listnode_new ();
+  nn->data = val;
+
+  if (pp == NULL)
+    {
+      if (list->tail)
+        list->tail->next = nn;
+      else
+        list->head = nn;
+
+      nn->prev = list->tail;
+      nn->next = pp;
+
+      list->tail = nn;
+    }
+  else
+    {
+      if (pp->prev)
+       pp->prev->next = nn;
+      else
+       list->head = nn;
+
+      nn->prev = pp->prev;
+      nn->next = pp;
+
+      pp->prev = nn;
+    }
+  list->count++;
+  return nn;
 }
 
+/* Move given listnode to tail of the list */
+void
+listnode_move_to_tail (struct list *l, struct listnode *n)
+{
+  LISTNODE_DETACH(l,n);
+  LISTNODE_ATTACH(l,n);
+}
 
 /* Delete specific date pointer from the list. */
 void
@@ -162,6 +214,7 @@ listnode_delete (struct list *list, void *val)
 {
   struct listnode *node;
 
+  assert(list);
   for (node = list->head; node; node = node->next)
     {
       if (node->data == val)
@@ -189,6 +242,7 @@ listnode_head (struct list *list)
 {
   struct listnode *node;
 
+  assert(list);
   node = list->head;
 
   if (node)
@@ -203,6 +257,7 @@ list_delete_all_node (struct list *list)
   struct listnode *node;
   struct listnode *next;
 
+  assert(list);
   for (node = list->head; node; node = next)
     {
       next = node->next;
@@ -218,16 +273,8 @@ list_delete_all_node (struct list *list)
 void
 list_delete (struct list *list)
 {
-  struct listnode *node;
-  struct listnode *next;
-
-  for (node = list->head; node; node = next)
-    {
-      next = node->next;
-      if (list->del)
-       (*list->del) (node->data);
-      listnode_free (node);
-    }
+  assert(list);
+  list_delete_all_node (list);
   list_free (list);
 }
 
@@ -235,17 +282,18 @@ list_delete (struct list *list)
 struct listnode *
 listnode_lookup (struct list *list, void *data)
 {
-  listnode node;
+  struct listnode *node;
 
-  for (node = list->head; node; nextnode (node))
-    if (data == getdata (node))
+  assert(list);
+  for (node = listhead(list); node; node = listnextnode (node))
+    if (data == listgetdata (node))
       return node;
   return NULL;
 }
-\f
+
 /* Delete the node from list.  For ospfd and ospf6d. */
 void
-list_delete_node (list list, listnode node)
+list_delete_node (struct list *list, struct listnode *node)
 {
   if (node->prev)
     node->prev->next = node->next;
@@ -258,48 +306,6 @@ list_delete_node (list list, listnode node)
   list->count--;
   listnode_free (node);
 }
-\f
-/* ospf_spf.c */
-void
-list_add_node_prev (list list, listnode current, void *val)
-{
-  struct listnode *node;
-
-  node = listnode_new ();
-  node->next = current;
-  node->data = val;
-
-  if (current->prev == NULL)
-    list->head = node;
-  else
-    current->prev->next = node;
-
-  node->prev = current->prev;
-  current->prev = node;
-
-  list->count++;
-}
-
-/* ospf_spf.c */
-void
-list_add_node_next (list list, listnode current, void *val)
-{
-  struct listnode *node;
-
-  node = listnode_new ();
-  node->prev = current;
-  node->data = val;
-
-  if (current->next == NULL)
-    list->tail = node;
-  else
-    current->next->prev = node;
-
-  node->next = current->next;
-  current->next = node;
-
-  list->count++;
-}
 
 /* ospf_spf.c */
 void
@@ -307,6 +313,6 @@ list_add_list (struct list *l, struct list *m)
 {
   struct listnode *n;
 
-  for (n = listhead (m); n; nextnode (n))
+  for (n = listhead (m); n; n = listnextnode (n))
     listnode_add (l, n->data);
 }