Index: kernel/generic/include/adt/list.h
===================================================================
--- kernel/generic/include/adt/list.h	(revision 7a0359b903311f67678b4a0a3ebbcf0daf6eab38)
+++ kernel/generic/include/adt/list.h	(revision 74464e831800483feba22672d950a2eddcc0d953)
@@ -1,4 +1,5 @@
 /*
  * Copyright (c) 2001-2004 Jakub Jermar
+ * Copyright (c) 2011 Jiri Svoboda
  * All rights reserved.
  *
@@ -39,16 +40,37 @@
 #include <trace.h>
 
-/** Doubly linked list head and link type. */
+/** Doubly linked list link. */
 typedef struct link {
-	struct link *prev;	/**< Pointer to the previous item in the list. */
-	struct link *next;	/**< Pointer to the next item in the list. */
+	struct link *prev;  /**< Pointer to the previous item in the list. */
+	struct link *next;  /**< Pointer to the next item in the list. */
 } link_t;
 
+/** Doubly linked list. */
+typedef struct list {
+	link_t head;  /**< List head. Does not have any data. */
+} list_t;
+
 /** Declare and initialize statically allocated list.
  *
  * @param name Name of the new statically allocated list.
+ *
  */
 #define LIST_INITIALIZE(name) \
-	link_t name = { .prev = &name, .next = &name }
+	list_t name = { \
+		.head = { \
+			.prev = &(name).head, \
+			.next = &(name).head \
+		} \
+	}
+
+#define list_get_instance(link, type, member) \
+	((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
+
+#define list_foreach(list, iterator) \
+	for (link_t *iterator = (list).head.next; \
+	    iterator != &(list).head; iterator = iterator->next)
+
+#define assert_link_not_used(link) \
+	ASSERT((link)->prev == NULL && (link)->next == NULL)
 
 /** Initialize doubly-linked circular list link
@@ -57,4 +79,5 @@
  *
  * @param link Pointer to link_t structure to be initialized.
+ *
  */
 NO_TRACE static inline void link_initialize(link_t *link)
@@ -68,10 +91,33 @@
  * Initialize doubly-linked circular list.
  *
- * @param head Pointer to link_t structure representing head of the list.
- */
-NO_TRACE static inline void list_initialize(link_t *head)
-{
-	head->prev = head;
-	head->next = head;
+ * @param list Pointer to list_t structure.
+ *
+ */
+NO_TRACE static inline void list_initialize(list_t *list)
+{
+	list->head.prev = &list->head;
+	list->head.next = &list->head;
+}
+
+/** Insert item before another item in doubly-linked circular list.
+ *
+ */
+static inline void list_insert_before(link_t *lnew, link_t *lold)
+{
+	lnew->next = lold;
+	lnew->prev = lold->prev;
+	lold->prev->next = lnew;
+	lold->prev = lnew;
+}
+
+/** Insert item after another item in doubly-linked circular list.
+ *
+ */
+static inline void list_insert_after(link_t *lnew, link_t *lold)
+{
+	lnew->prev = lold;
+	lnew->next = lold->next;
+	lold->next->prev = lnew;
+	lold->next = lnew;
 }
 
@@ -81,12 +127,10 @@
  *
  * @param link Pointer to link_t structure to be added.
- * @param head Pointer to link_t structure representing head of the list.
- */
-NO_TRACE static inline void list_prepend(link_t *link, link_t *head)
-{
-	link->next = head->next;
-	link->prev = head;
-	head->next->prev = link;
-	head->next = link;
+ * @param list Pointer to list_t structure.
+ *
+ */
+NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
+{
+	list_insert_after(link, &list->head);
 }
 
@@ -96,12 +140,10 @@
  *
  * @param link Pointer to link_t structure to be added.
- * @param head Pointer to link_t structure representing head of the list.
- */
-NO_TRACE static inline void list_append(link_t *link, link_t *head)
-{
-	link->prev = head->prev;
-	link->next = head;
-	head->prev->next = link;
-	head->prev = link;
+ * @param list Pointer to list_t structure.
+ *
+ */
+NO_TRACE static inline void list_append(link_t *link, list_t *list)
+{
+	list_insert_before(link, &list->head);
 }
 
@@ -110,6 +152,7 @@
  * Remove item from doubly-linked circular list.
  *
- * @param link 	Pointer to link_t structure to be removed from the list it is
- * 		contained in.
+ * @param link Pointer to link_t structure to be removed from the list
+ *             it is contained in.
+ *
  */
 NO_TRACE static inline void list_remove(link_t *link)
@@ -124,11 +167,37 @@
  * Query emptiness of doubly-linked circular list.
  *
- * @param head Pointer to link_t structure representing head of the list.
- */
-NO_TRACE static inline bool list_empty(link_t *head)
-{
-	return head->next == head ? true : false;
-}
-
+ * @param list Pointer to lins_t structure.
+ *
+ */
+NO_TRACE static inline int list_empty(list_t *list)
+{
+	return (list->head.next == &list->head);
+}
+
+/** Get first item in list.
+ *
+ * @param list Pointer to list_t structure.
+ *
+ * @return Head item of the list.
+ * @return NULL if the list is empty.
+ *
+ */
+static inline link_t *list_first(list_t *list)
+{
+	return ((list->head.next == &list->head) ? NULL : list->head.next);
+}
+
+/** Get last item in list.
+ *
+ * @param list Pointer to list_t structure.
+ *
+ * @return Head item of the list.
+ * @return NULL if the list is empty.
+ *
+ */
+static inline link_t *list_last(list_t *list)
+{
+	return ((list->head.prev == &list->head) ? NULL : list->head.prev);
+}
 
 /** Split or concatenate headless doubly-linked circular list
@@ -139,29 +208,30 @@
  * concatenates splitted lists and splits concatenated lists.
  *
- * @param part1	Pointer to link_t structure leading the first (half of the
- *		headless) list.
- * @param part2	Pointer to link_t structure leading the second (half of the
- *		headless) list. 
+ * @param part1 Pointer to link_t structure leading the first
+ *              (half of the headless) list.
+ * @param part2 Pointer to link_t structure leading the second
+ *              (half of the headless) list.
+ *
  */
 NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
 {
-	link_t *hlp;
-
 	part1->prev->next = part2;
-	part2->prev->next = part1;	
-	hlp = part1->prev;
+	part2->prev->next = part1;
+	
+	link_t *hlp = part1->prev;
+	
 	part1->prev = part2->prev;
 	part2->prev = hlp;
 }
 
-
 /** Split headless doubly-linked circular list
  *
  * Split headless doubly-linked circular list.
  *
- * @param part1	Pointer to link_t structure leading the first half of the
- *		headless list.
- * @param part2	Pointer to link_t structure leading the second half of the
- *		headless list. 
+ * @param part1 Pointer to link_t structure leading
+ *              the first half of the headless list.
+ * @param part2 Pointer to link_t structure leading
+ *              the second half of the headless list.
+ *
  */
 NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
@@ -174,6 +244,9 @@
  * Concatenate two headless doubly-linked circular lists.
  *
- * @param part1 Pointer to link_t structure leading the first headless list.
- * @param part2 Pointer to link_t structure leading the second headless list.
+ * @param part1 Pointer to link_t structure leading
+ *              the first headless list.
+ * @param part2 Pointer to link_t structure leading
+ *              the second headless list.
+ *
  */
 NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
@@ -182,9 +255,30 @@
 }
 
-#define list_get_instance(link, type, member) \
-	((type *)(((uint8_t *)(link)) - ((uint8_t *)&(((type *)NULL)->member))))
-
-extern bool list_member(const link_t *link, const link_t *head);
-extern void list_concat(link_t *head1, link_t *head2);
+/** Get n-th item in a list.
+ *
+ * @param list Pointer to link_t structure representing the list.
+ * @param n    Item number (indexed from zero).
+ *
+ * @return n-th item of the list.
+ * @return NULL if no n-th item found.
+ *
+ */
+static inline link_t *list_nth(list_t *list, unsigned int n)
+{
+	unsigned int cnt = 0;
+	
+	list_foreach(*list, link) {
+		if (cnt == n)
+			return link;
+		
+		cnt++;
+	}
+	
+	return NULL;
+}
+
+extern int list_member(const link_t *, const list_t *);
+extern void list_concat(list_t *, list_t *);
+extern unsigned int list_count(const list_t *);
 
 #endif
