summaryrefslogtreecommitdiff
path: root/viengoos/list.h
diff options
context:
space:
mode:
authorneal <neal>2008-01-04 14:53:01 +0000
committerneal <neal>2008-01-04 14:53:01 +0000
commit28daee9dd33b21f5084a980f027d60797d098391 (patch)
tree212f8fd58847b057b24c96d4f385a6fb27f4c95a /viengoos/list.h
parent4524086e47770e8fd1843265a71f9072f5d14cba (diff)
2008-01-04 Neal H. Walfield <neal@gnu.org>
* list.h (struct list_node): Add comment that about initialization requirements. (list_unlink): Always set ITEM->NEXT and ITEM->PREV to NULL. (list_node_attached): New function. (list_count): Add an assert.
Diffstat (limited to 'viengoos/list.h')
-rw-r--r--viengoos/list.h37
1 files changed, 26 insertions, 11 deletions
diff --git a/viengoos/list.h b/viengoos/list.h
index b3ca0e8..8a76512 100644
--- a/viengoos/list.h
+++ b/viengoos/list.h
@@ -1,5 +1,5 @@
/* list.h - Linked list interface.
- Copyright (C) 2007 Free Software Foundation, Inc.
+ Copyright (C) 2007, 2008 Free Software Foundation, Inc.
Written by Neal H. Walfield <neal@gnu.org>.
This file is part of the GNU Hurd.
@@ -23,6 +23,8 @@
/* A circular linked-list implementation. */
+/* New nodes must be initialized to NULL before being added to a list.
+ (list_unlink will clear them.) */
struct list_node
{
struct list_node *next;
@@ -42,6 +44,13 @@ struct list
};
typedef struct list list_t;
+/* Return whether a node is attached to a list. */
+static inline bool
+list_node_attached (struct list_node *node)
+{
+ return !! node->next;
+}
+
/* Initialize a list. Equivalently, zero initialization is
sufficient. */
static inline void
@@ -64,13 +73,6 @@ list_init (struct list *list)
#define LIST_PTR(__ls_ptr) \
((struct list_node *) ((uintptr_t) (__ls_ptr) & ~1))
-/* Return the number of items attach to list LIST. */
-static inline int
-list_count (struct list *list)
-{
- return list->count;
-}
-
/* Return LIST's head. If the list is empty, returns NULL. */
static inline struct list_node *
list_head (struct list *list)
@@ -110,6 +112,22 @@ list_prev (struct list_node *node)
return node->prev;
}
+/* Return the number of items attach to list LIST. */
+static inline int
+list_count (struct list *list)
+{
+#ifndef NDEBUG
+ int count = 0;
+ struct list_node *node;
+ for (node = list_head (list); node; node = list_next (node))
+ count ++;
+
+ assert (count == list->count);
+#endif
+
+ return list->count;
+}
+
/* Add ITEM to the head of the list LIST. */
static inline void
list_push (struct list *list, struct list_node *item)
@@ -238,11 +256,8 @@ list_unlink (struct list *list, struct list_node *item)
item->prev->next = item->next;
}
-#ifndef NDEBUG
- /* Try to detect multiple unlink. */
item->next = NULL;
item->prev = NULL;
-#endif
list->count --;
}