Back to index

libdrm  2.4.37
libdrm_lists.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 /*
00028  * List macros heavily inspired by the Linux kernel
00029  * list handling. No list looping yet.
00030  */
00031 
00032 #include <stddef.h>
00033 
00034 typedef struct _drmMMListHead
00035 {
00036     struct _drmMMListHead *prev;
00037     struct _drmMMListHead *next;
00038 } drmMMListHead;
00039 
00040 #define DRMINITLISTHEAD(__item)                  \
00041   do{                                     \
00042     (__item)->prev = (__item);                   \
00043     (__item)->next = (__item);                   \
00044   } while (0)
00045 
00046 #define DRMLISTADD(__item, __list)        \
00047   do {                                    \
00048     (__item)->prev = (__list);                   \
00049     (__item)->next = (__list)->next;             \
00050     (__list)->next->prev = (__item);             \
00051     (__list)->next = (__item);                   \
00052   } while (0)
00053 
00054 #define DRMLISTADDTAIL(__item, __list)           \
00055   do {                                    \
00056     (__item)->next = (__list);                   \
00057     (__item)->prev = (__list)->prev;             \
00058     (__list)->prev->next = (__item);             \
00059     (__list)->prev = (__item);                   \
00060   } while(0)
00061 
00062 #define DRMLISTDEL(__item)                \
00063   do {                                    \
00064     (__item)->prev->next = (__item)->next;       \
00065     (__item)->next->prev = (__item)->prev;       \
00066   } while(0)
00067 
00068 #define DRMLISTDELINIT(__item)                   \
00069   do {                                    \
00070     (__item)->prev->next = (__item)->next;       \
00071     (__item)->next->prev = (__item)->prev;       \
00072     (__item)->next = (__item);                   \
00073     (__item)->prev = (__item);                   \
00074   } while(0)
00075 
00076 #define DRMLISTENTRY(__type, __item, __field)   \
00077     ((__type *)(((char *) (__item)) - offsetof(__type, __field)))
00078 
00079 #define DRMLISTEMPTY(__item) ((__item)->next == (__item))
00080 
00081 #define DRMLISTSINGLE(__list) \
00082        (!DRMLISTEMPTY(__list) && ((__list)->next == (__list)->prev))
00083 
00084 #define DRMLISTFOREACH(__item, __list)                                \
00085        for ((__item) = (__list)->next;                                \
00086             (__item) != (__list); (__item) = (__item)->next)
00087 
00088 #define DRMLISTFOREACHSAFE(__item, __temp, __list)                    \
00089        for ((__item) = (__list)->next, (__temp) = (__item)->next;     \
00090             (__item) != (__list);                              \
00091             (__item) = (__temp), (__temp) = (__item)->next)
00092 
00093 #define DRMLISTFOREACHSAFEREVERSE(__item, __temp, __list)             \
00094        for ((__item) = (__list)->prev, (__temp) = (__item)->prev;     \
00095             (__item) != (__list);                              \
00096             (__item) = (__temp), (__temp) = (__item)->prev)
00097 
00098 #define DRMLISTFOREACHENTRY(__item, __list, __head)                            \
00099        for ((__item) = DRMLISTENTRY(typeof(*__item), (__list)->next, __head); \
00100             &(__item)->__head != (__list);                                    \
00101             (__item) = DRMLISTENTRY(typeof(*__item),                          \
00102                                  (__item)->__head.next, __head))
00103 
00104 #define DRMLISTFOREACHENTRYSAFE(__item, __temp, __list, __head)                \
00105        for ((__item) = DRMLISTENTRY(typeof(*__item), (__list)->next, __head), \
00106             (__temp) = DRMLISTENTRY(typeof(*__item),                          \
00107                                  (__item)->__head.next, __head);           \
00108             &(__item)->__head != (__list);                                    \
00109             (__item) = (__temp),                                              \
00110             (__temp) = DRMLISTENTRY(typeof(*__item),                          \
00111                                  (__temp)->__head.next, __head))
00112 
00113 #define DRMLISTJOIN(__list, __join) if (!DRMLISTEMPTY(__list)) {      \
00114        (__list)->next->prev = (__join);                        \
00115        (__list)->prev->next = (__join)->next;                         \
00116        (__join)->next->prev = (__list)->prev;                         \
00117        (__join)->next = (__list)->next;                        \
00118 }