Back to index

glibc  2.9
list.h
Go to the documentation of this file.
00001 /* Copyright (C) 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef _LIST_H
00021 #define _LIST_H      1
00022 
00023 /* The definitions of this file are adopted from those which can be
00024    found in the Linux kernel headers to enable people familiar with
00025    the latter find their way in these sources as well.  */
00026 
00027 
00028 /* Basic type for the double-link list.  */
00029 typedef struct list_head
00030 {
00031   struct list_head *next;
00032   struct list_head *prev;
00033 } list_t;
00034 
00035 
00036 /* Define a variable with the head and tail of the list.  */
00037 #define LIST_HEAD(name) \
00038   list_t name = { &(name), &(name) }
00039 
00040 /* Initialize a new list head.  */
00041 #define INIT_LIST_HEAD(ptr) \
00042   (ptr)->next = (ptr)->prev = (ptr)
00043 
00044 
00045 /* Add new element at the head of the list.  */
00046 static inline void
00047 list_add (list_t *newp, list_t *head)
00048 {
00049   head->next->prev = newp;
00050   newp->next = head->next;
00051   newp->prev = head;
00052   head->next = newp;
00053 }
00054 
00055 
00056 /* Add new element at the tail of the list.  */
00057 static inline void
00058 list_add_tail (list_t *newp, list_t *head)
00059 {
00060   head->prev->next = newp;
00061   newp->next = head;
00062   newp->prev = head->prev;
00063   head->prev = newp;
00064 }
00065 
00066 
00067 /* Remove element from list.  */
00068 static inline void
00069 list_del (list_t *elem)
00070 {
00071   elem->next->prev = elem->prev;
00072   elem->prev->next = elem->next;
00073 }
00074 
00075 
00076 /* Join two lists.  */
00077 static inline void
00078 list_splice (list_t *add, list_t *head)
00079 {
00080   /* Do nothing if the list which gets added is empty.  */
00081   if (add != add->next)
00082     {
00083       add->next->prev = head;
00084       add->prev->next = head->next;
00085       head->next->prev = add->prev;
00086       head->next = add->next;
00087     }
00088 }
00089 
00090 
00091 /* Get typed element from list at a given position.  */
00092 #define list_entry(ptr, type, member) \
00093   ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
00094 
00095 
00096 
00097 /* Iterate forward over the elements of the list.  */
00098 #define list_for_each(pos, head) \
00099   for (pos = (head)->next; pos != (head); pos = pos->next)
00100 
00101 
00102 /* Iterate forward over the elements of the list.  */
00103 #define list_for_each_prev(pos, head) \
00104   for (pos = (head)->prev; pos != (head); pos = pos->prev)
00105 
00106 
00107 /* Iterate backwards over the elements list.  The list elements can be
00108    removed from the list while doing this.  */
00109 #define list_for_each_prev_safe(pos, p, head) \
00110   for (pos = (head)->prev, p = pos->prev; \
00111        pos != (head); \
00112        pos = p, p = pos->prev)
00113 
00114 #endif /* list.h */