2002-10-03 01:49:30 +02:00
|
|
|
/*
|
|
|
|
* Linked lists support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-10-03 01:49:30 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __WINE_SERVER_LIST_H
|
|
|
|
#define __WINE_SERVER_LIST_H
|
|
|
|
|
|
|
|
struct list
|
|
|
|
{
|
|
|
|
struct list *next;
|
|
|
|
struct list *prev;
|
|
|
|
};
|
|
|
|
|
2004-11-06 04:45:49 +01:00
|
|
|
/* Define a list like so:
|
|
|
|
*
|
|
|
|
* struct gadget
|
|
|
|
* {
|
|
|
|
* struct list entry; <-- doesn't have to be the first item in the struct
|
|
|
|
* int a, b;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* static struct list global_gadgets = LIST_INIT( global_gadgets );
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* struct some_global_thing
|
|
|
|
* {
|
|
|
|
* struct list gadgets;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* list_init( &some_global_thing->gadgets );
|
|
|
|
*
|
|
|
|
* Manipulate it like this:
|
|
|
|
*
|
|
|
|
* list_add_head( &global_gadgets, &new_gadget->entry );
|
|
|
|
* list_remove( &new_gadget->entry );
|
|
|
|
* list_add_after( &some_random_gadget->entry, &new_gadget->entry );
|
|
|
|
*
|
|
|
|
* And to iterate over it:
|
|
|
|
*
|
2005-05-18 15:24:46 +02:00
|
|
|
* struct gadget *gadget;
|
|
|
|
* LIST_FOR_EACH_ENTRY( gadget, &global_gadgets, struct gadget, entry )
|
2004-11-06 04:45:49 +01:00
|
|
|
* {
|
2005-05-18 15:24:46 +02:00
|
|
|
* ...
|
2004-11-06 04:45:49 +01:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-21 22:58:44 +02:00
|
|
|
/* add an element after the specified one */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline void list_add_after( struct list *elem, struct list *to_add )
|
2004-05-21 22:58:44 +02:00
|
|
|
{
|
|
|
|
to_add->next = elem->next;
|
|
|
|
to_add->prev = elem;
|
|
|
|
elem->next->prev = to_add;
|
|
|
|
elem->next = to_add;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add an element before the specified one */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline void list_add_before( struct list *elem, struct list *to_add )
|
2004-05-21 22:58:44 +02:00
|
|
|
{
|
|
|
|
to_add->next = elem;
|
|
|
|
to_add->prev = elem->prev;
|
|
|
|
elem->prev->next = to_add;
|
|
|
|
elem->prev = to_add;
|
|
|
|
}
|
|
|
|
|
2002-10-03 01:49:30 +02:00
|
|
|
/* add element at the head of the list */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline void list_add_head( struct list *list, struct list *elem )
|
2002-10-03 01:49:30 +02:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
list_add_after( list, elem );
|
2002-10-03 01:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add element at the tail of the list */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline void list_add_tail( struct list *list, struct list *elem )
|
2002-10-03 01:49:30 +02:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
list_add_before( list, elem );
|
2002-10-03 01:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove an element from its list */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline void list_remove( struct list *elem )
|
2002-10-03 01:49:30 +02:00
|
|
|
{
|
|
|
|
elem->next->prev = elem->prev;
|
|
|
|
elem->prev->next = elem->next;
|
|
|
|
}
|
|
|
|
|
2002-10-29 01:41:42 +01:00
|
|
|
/* get the next element */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline struct list *list_next( const struct list *list, const struct list *elem )
|
2002-10-29 01:41:42 +01:00
|
|
|
{
|
|
|
|
struct list *ret = elem->next;
|
|
|
|
if (elem->next == list) ret = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the previous element */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline struct list *list_prev( const struct list *list, const struct list *elem )
|
2002-10-29 01:41:42 +01:00
|
|
|
{
|
|
|
|
struct list *ret = elem->prev;
|
|
|
|
if (elem->prev == list) ret = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the first element */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline struct list *list_head( const struct list *list )
|
2002-10-29 01:41:42 +01:00
|
|
|
{
|
|
|
|
return list_next( list, list );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the last element */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline struct list *list_tail( const struct list *list )
|
2002-10-29 01:41:42 +01:00
|
|
|
{
|
|
|
|
return list_prev( list, list );
|
|
|
|
}
|
|
|
|
|
2003-03-18 06:04:33 +01:00
|
|
|
/* check if a list is empty */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline int list_empty( const struct list *list )
|
2003-03-18 06:04:33 +01:00
|
|
|
{
|
|
|
|
return list->next == list;
|
|
|
|
}
|
|
|
|
|
2002-10-03 01:49:30 +02:00
|
|
|
/* initialize a list */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline void list_init( struct list *list )
|
2002-10-03 01:49:30 +02:00
|
|
|
{
|
|
|
|
list->next = list->prev = list;
|
|
|
|
}
|
|
|
|
|
2007-01-22 12:16:17 +01:00
|
|
|
/* count the elements of a list */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline unsigned int list_count( const struct list *list )
|
2007-01-22 12:16:17 +01:00
|
|
|
{
|
|
|
|
unsigned count = 0;
|
|
|
|
const struct list *ptr;
|
|
|
|
for (ptr = list->next; ptr != list; ptr = ptr->next) count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2006-12-04 16:12:58 +01:00
|
|
|
/* move all elements from src to the tail of dst */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline void list_move_tail( struct list *dst, struct list *src )
|
2006-12-04 16:12:58 +01:00
|
|
|
{
|
|
|
|
if (list_empty(src)) return;
|
|
|
|
|
|
|
|
dst->prev->next = src->next;
|
|
|
|
src->next->prev = dst->prev;
|
|
|
|
dst->prev = src->prev;
|
|
|
|
src->prev->next = dst;
|
|
|
|
list_init(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move all elements from src to the head of dst */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline void list_move_head( struct list *dst, struct list *src )
|
2006-12-04 16:12:58 +01:00
|
|
|
{
|
|
|
|
if (list_empty(src)) return;
|
|
|
|
|
|
|
|
dst->next->prev = src->prev;
|
|
|
|
src->prev->next = dst->next;
|
|
|
|
dst->next = src->next;
|
|
|
|
src->next->prev = dst;
|
|
|
|
list_init(src);
|
|
|
|
}
|
|
|
|
|
2002-10-03 01:49:30 +02:00
|
|
|
/* iterate through the list */
|
|
|
|
#define LIST_FOR_EACH(cursor,list) \
|
|
|
|
for ((cursor) = (list)->next; (cursor) != (list); (cursor) = (cursor)->next)
|
|
|
|
|
2005-01-12 20:27:35 +01:00
|
|
|
/* iterate through the list, with safety against removal */
|
|
|
|
#define LIST_FOR_EACH_SAFE(cursor, cursor2, list) \
|
|
|
|
for ((cursor) = (list)->next, (cursor2) = (cursor)->next; \
|
|
|
|
(cursor) != (list); \
|
|
|
|
(cursor) = (cursor2), (cursor2) = (cursor)->next)
|
|
|
|
|
2005-01-14 16:11:07 +01:00
|
|
|
/* iterate through the list using a list entry */
|
|
|
|
#define LIST_FOR_EACH_ENTRY(elem, list, type, field) \
|
|
|
|
for ((elem) = LIST_ENTRY((list)->next, type, field); \
|
|
|
|
&(elem)->field != (list); \
|
|
|
|
(elem) = LIST_ENTRY((elem)->field.next, type, field))
|
|
|
|
|
2005-08-29 14:18:15 +02:00
|
|
|
/* iterate through the list using a list entry, with safety against removal */
|
|
|
|
#define LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, list, type, field) \
|
|
|
|
for ((cursor) = LIST_ENTRY((list)->next, type, field), \
|
|
|
|
(cursor2) = LIST_ENTRY((cursor)->field.next, type, field); \
|
|
|
|
&(cursor)->field != (list); \
|
|
|
|
(cursor) = (cursor2), \
|
|
|
|
(cursor2) = LIST_ENTRY((cursor)->field.next, type, field))
|
|
|
|
|
2007-06-06 03:48:21 +02:00
|
|
|
/* iterate through the list in reverse order */
|
|
|
|
#define LIST_FOR_EACH_REV(cursor,list) \
|
|
|
|
for ((cursor) = (list)->prev; (cursor) != (list); (cursor) = (cursor)->prev)
|
|
|
|
|
|
|
|
/* iterate through the list in reverse order, with safety against removal */
|
|
|
|
#define LIST_FOR_EACH_SAFE_REV(cursor, cursor2, list) \
|
|
|
|
for ((cursor) = (list)->prev, (cursor2) = (cursor)->prev; \
|
|
|
|
(cursor) != (list); \
|
|
|
|
(cursor) = (cursor2), (cursor2) = (cursor)->prev)
|
|
|
|
|
|
|
|
/* iterate through the list in reverse order using a list entry */
|
|
|
|
#define LIST_FOR_EACH_ENTRY_REV(elem, list, type, field) \
|
|
|
|
for ((elem) = LIST_ENTRY((list)->prev, type, field); \
|
|
|
|
&(elem)->field != (list); \
|
|
|
|
(elem) = LIST_ENTRY((elem)->field.prev, type, field))
|
|
|
|
|
|
|
|
/* iterate through the list in reverse order using a list entry, with safety against removal */
|
|
|
|
#define LIST_FOR_EACH_ENTRY_SAFE_REV(cursor, cursor2, list, type, field) \
|
|
|
|
for ((cursor) = LIST_ENTRY((list)->prev, type, field), \
|
|
|
|
(cursor2) = LIST_ENTRY((cursor)->field.prev, type, field); \
|
|
|
|
&(cursor)->field != (list); \
|
|
|
|
(cursor) = (cursor2), \
|
|
|
|
(cursor2) = LIST_ENTRY((cursor)->field.prev, type, field))
|
|
|
|
|
2002-10-03 01:49:30 +02:00
|
|
|
/* macros for statically initialized lists */
|
|
|
|
#define LIST_INIT(list) { &(list), &(list) }
|
|
|
|
|
|
|
|
/* get pointer to object containing list element */
|
|
|
|
#define LIST_ENTRY(elem, type, field) \
|
|
|
|
((type *)((char *)(elem) - (unsigned int)(&((type *)0)->field)))
|
|
|
|
|
|
|
|
#endif /* __WINE_SERVER_LIST_H */
|