| 1 |
/************************************************************************** |
| 2 |
* |
| 3 |
* Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA. |
| 4 |
* All Rights Reserved. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 7 |
* copy of this software and associated documentation files (the |
| 8 |
* "Software"), to deal in the Software without restriction, including |
| 9 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 10 |
* distribute, sub license, and/or sell copies of the Software, and to |
| 11 |
* permit persons to whom the Software is furnished to do so, subject to |
| 12 |
* the following conditions: |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 18 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 |
* USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
* |
| 22 |
* The above copyright notice and this permission notice (including the |
| 23 |
* next paragraph) shall be included in all copies or substantial portions |
| 24 |
* of the Software. |
| 25 |
*/ |
| 26 |
|
| 27 |
/* |
| 28 |
* List macros heavily inspired by the Linux kernel |
| 29 |
* list handling. No list looping yet. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <stddef.h> |
| 33 |
|
| 34 |
#if defined(__cplusplus) |
| 35 |
extern "C" { |
| 36 |
#endif |
| 37 |
|
| 38 |
typedef struct _drmMMListHead |
| 39 |
{ |
| 40 |
struct _drmMMListHead *prev; |
| 41 |
struct _drmMMListHead *next; |
| 42 |
} drmMMListHead; |
| 43 |
|
| 44 |
#define DRMINITLISTHEAD(__item) \ |
| 45 |
do{ \ |
| 46 |
(__item)->prev = (__item); \ |
| 47 |
(__item)->next = (__item); \ |
| 48 |
} while (0) |
| 49 |
|
| 50 |
#define DRMLISTADD(__item, __list) \ |
| 51 |
do { \ |
| 52 |
(__item)->prev = (__list); \ |
| 53 |
(__item)->next = (__list)->next; \ |
| 54 |
(__list)->next->prev = (__item); \ |
| 55 |
(__list)->next = (__item); \ |
| 56 |
} while (0) |
| 57 |
|
| 58 |
#define DRMLISTADDTAIL(__item, __list) \ |
| 59 |
do { \ |
| 60 |
(__item)->next = (__list); \ |
| 61 |
(__item)->prev = (__list)->prev; \ |
| 62 |
(__list)->prev->next = (__item); \ |
| 63 |
(__list)->prev = (__item); \ |
| 64 |
} while(0) |
| 65 |
|
| 66 |
#define DRMLISTDEL(__item) \ |
| 67 |
do { \ |
| 68 |
(__item)->prev->next = (__item)->next; \ |
| 69 |
(__item)->next->prev = (__item)->prev; \ |
| 70 |
} while(0) |
| 71 |
|
| 72 |
#define DRMLISTDELINIT(__item) \ |
| 73 |
do { \ |
| 74 |
(__item)->prev->next = (__item)->next; \ |
| 75 |
(__item)->next->prev = (__item)->prev; \ |
| 76 |
(__item)->next = (__item); \ |
| 77 |
(__item)->prev = (__item); \ |
| 78 |
} while(0) |
| 79 |
|
| 80 |
#define DRMLISTENTRY(__type, __item, __field) \ |
| 81 |
((__type *)(((char *) (__item)) - offsetof(__type, __field))) |
| 82 |
|
| 83 |
#define DRMLISTEMPTY(__item) ((__item)->next == (__item)) |
| 84 |
|
| 85 |
#define DRMLISTSINGLE(__list) \ |
| 86 |
(!DRMLISTEMPTY(__list) && ((__list)->next == (__list)->prev)) |
| 87 |
|
| 88 |
#define DRMLISTFOREACH(__item, __list) \ |
| 89 |
for ((__item) = (__list)->next; \ |
| 90 |
(__item) != (__list); (__item) = (__item)->next) |
| 91 |
|
| 92 |
#define DRMLISTFOREACHSAFE(__item, __temp, __list) \ |
| 93 |
for ((__item) = (__list)->next, (__temp) = (__item)->next; \ |
| 94 |
(__item) != (__list); \ |
| 95 |
(__item) = (__temp), (__temp) = (__item)->next) |
| 96 |
|
| 97 |
#define DRMLISTFOREACHSAFEREVERSE(__item, __temp, __list) \ |
| 98 |
for ((__item) = (__list)->prev, (__temp) = (__item)->prev; \ |
| 99 |
(__item) != (__list); \ |
| 100 |
(__item) = (__temp), (__temp) = (__item)->prev) |
| 101 |
|
| 102 |
#define DRMLISTFOREACHENTRY(__item, __list, __head) \ |
| 103 |
for ((__item) = DRMLISTENTRY(__typeof__(*__item), (__list)->next, __head); \ |
| 104 |
&(__item)->__head != (__list); \ |
| 105 |
(__item) = DRMLISTENTRY(__typeof__(*__item), \ |
| 106 |
(__item)->__head.next, __head)) |
| 107 |
|
| 108 |
#define DRMLISTFOREACHENTRYSAFE(__item, __temp, __list, __head) \ |
| 109 |
for ((__item) = DRMLISTENTRY(__typeof__(*__item), (__list)->next, __head), \ |
| 110 |
(__temp) = DRMLISTENTRY(__typeof__(*__item), \ |
| 111 |
(__item)->__head.next, __head); \ |
| 112 |
&(__item)->__head != (__list); \ |
| 113 |
(__item) = (__temp), \ |
| 114 |
(__temp) = DRMLISTENTRY(__typeof__(*__item), \ |
| 115 |
(__temp)->__head.next, __head)) |
| 116 |
|
| 117 |
#define DRMLISTJOIN(__list, __join) if (!DRMLISTEMPTY(__list)) { \ |
| 118 |
(__list)->next->prev = (__join); \ |
| 119 |
(__list)->prev->next = (__join)->next; \ |
| 120 |
(__join)->next->prev = (__list)->prev; \ |
| 121 |
(__join)->next = (__list)->next; \ |
| 122 |
} |
| 123 |
#if defined(__cplusplus) |
| 124 |
} |
| 125 |
#endif |