Back to index

libdrm  2.4.37
list.h
Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
00004  * All Rights Reserved.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
00017  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
00018  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
00019  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
00020  * USE OR OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * The above copyright notice and this permission notice (including the
00023  * next paragraph) shall be included in all copies or substantial portions
00024  * of the Software.
00025  *
00026  */
00027 
00036 #ifndef _U_DOUBLE_LIST_H_
00037 #define _U_DOUBLE_LIST_H_
00038 
00039 #include <stddef.h>
00040 
00041 struct list_head
00042 {
00043     struct list_head *prev;
00044     struct list_head *next;
00045 };
00046 
00047 static void list_inithead(struct list_head *item)
00048 {
00049     item->prev = item;
00050     item->next = item;
00051 }
00052 
00053 static void list_add(struct list_head *item, struct list_head *list)
00054 {
00055     item->prev = list;
00056     item->next = list->next;
00057     list->next->prev = item;
00058     list->next = item;
00059 }
00060 
00061 static void list_addtail(struct list_head *item, struct list_head *list)
00062 {
00063     item->next = list;
00064     item->prev = list->prev;
00065     list->prev->next = item;
00066     list->prev = item;
00067 }
00068 
00069 static void list_replace(struct list_head *from, struct list_head *to)
00070 {
00071     to->prev = from->prev;
00072     to->next = from->next;
00073     from->next->prev = to;
00074     from->prev->next = to;
00075 }
00076 
00077 static void list_del(struct list_head *item)
00078 {
00079     item->prev->next = item->next;
00080     item->next->prev = item->prev;
00081 }
00082 
00083 static void list_delinit(struct list_head *item)
00084 {
00085     item->prev->next = item->next;
00086     item->next->prev = item->prev;
00087     item->next = item;
00088     item->prev = item;
00089 }
00090 
00091 #define LIST_INITHEAD(__item) list_inithead(__item)
00092 #define LIST_ADD(__item, __list) list_add(__item, __list)
00093 #define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
00094 #define LIST_REPLACE(__from, __to) list_replace(__from, __to)
00095 #define LIST_DEL(__item) list_del(__item)
00096 #define LIST_DELINIT(__item) list_delinit(__item)
00097 
00098 #define LIST_ENTRY(__type, __item, __field)   \
00099     ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
00100 
00101 #define LIST_IS_EMPTY(__list)                   \
00102     ((__list)->next == (__list))
00103 
00104 #ifndef container_of
00105 #define container_of(ptr, sample, member)                      \
00106     (void *)((char *)(ptr)                                     \
00107             - ((char *)&(sample)->member - (char *)(sample)))
00108 #endif
00109 
00110 #define LIST_FOR_EACH_ENTRY(pos, head, member)                        \
00111    for (pos = container_of((head)->next, pos, member);                \
00112        &pos->member != (head);                                        \
00113        pos = container_of(pos->member.next, pos, member))
00114 
00115 #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member)   \
00116    for (pos = container_of((head)->next, pos, member),                \
00117        storage = container_of(pos->member.next, pos, member);  \
00118        &pos->member != (head);                                        \
00119        pos = storage, storage = container_of(storage->member.next, storage, member))
00120 
00121 #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member)      \
00122    for (pos = container_of((head)->prev, pos, member),                \
00123        storage = container_of(pos->member.prev, pos, member);         \
00124        &pos->member != (head);                                        \
00125        pos = storage, storage = container_of(storage->member.prev, storage, member))
00126 
00127 #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member)            \
00128    for (pos = container_of((start), pos, member);                     \
00129        &pos->member != (head);                                        \
00130        pos = container_of(pos->member.next, pos, member))
00131 
00132 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member)        \
00133    for (pos = container_of((start), pos, member);                     \
00134        &pos->member != (head);                                        \
00135        pos = container_of(pos->member.prev, pos, member))
00136 
00137 #endif /*_U_DOUBLE_LIST_H_*/