#ifndef __WINE_SERVER_LIST_H
#define __WINE_SERVER_LIST_H
#include <stddef.h>
struct list
{
struct list
*next
;
struct list
*prev
;
};
static inline void list_add_after( struct list
*elem
, struct list
*to_add
)
{
to_add
->next
= elem
->next
;
to_add
->prev
= elem
;
elem
->next
->prev
= to_add
;
elem
->next
= to_add
;
}
static inline void list_add_before( struct list
*elem
, struct list
*to_add
)
{
to_add
->next
= elem
;
to_add
->prev
= elem
->prev
;
elem
->prev
->next
= to_add
;
elem
->prev
= to_add
;
}
static inline void list_add_head( struct list
*list
, struct list
*elem
)
{
list_add_after( list
, elem
);
}
static inline void list_add_tail( struct list
*list
, struct list
*elem
)
{
list_add_before( list
, elem
);
}
static inline void list_remove( struct list
*elem
)
{
elem
->next
->prev
= elem
->prev
;
elem
->prev
->next
= elem
->next
;
}
static inline struct list
*list_next( const struct list
*list
, const struct list
*elem
)
{
struct list
*ret
= elem
->next
;
if (elem
->next
== list
) ret
= NULL;
return ret
;
}
static inline struct list
*list_prev( const struct list
*list
, const struct list
*elem
)
{
struct list
*ret
= elem
->prev
;
if (elem
->prev
== list
) ret
= NULL;
return ret
;
}
static inline struct list
*list_head( const struct list
*list
)
{
return list_next( list
, list
);
}
static inline struct list
*list_tail( const struct list
*list
)
{
return list_prev( list
, list
);
}
static inline int list_empty( const struct list
*list
)
{
return list
->next
== list
;
}
static inline void list_init( struct list
*list
)
{
list
->next
= list
->prev
= list
;
}
static inline unsigned int list_count( const struct list
*list
)
{
unsigned count
= 0;
const struct list
*ptr
;
for (ptr
= list
->next
; ptr
!= list
; ptr
= ptr
->next
) count
++;
return count
;
}
static inline void list_move_tail( struct list
*dst
, struct list
*src
)
{
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
);
}
static inline void list_move_head( struct list
*dst
, struct list
*src
)
{
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
);
}
#define LIST_FOR_EACH(cursor,list) \
for ((cursor) = (list)->next; (cursor) != (list); (cursor) = (cursor)->next)
#define LIST_FOR_EACH_SAFE(cursor, cursor2, list) \
for ((cursor) = (list)->next, (cursor2) = (cursor)->next; \
(cursor) != (list); \
(cursor) = (cursor2), (cursor2) = (cursor)->next)
#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))
#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))
#define LIST_FOR_EACH_REV(cursor,list) \
for ((cursor) = (list)->prev; (cursor) != (list); (cursor) = (cursor)->prev)
#define LIST_FOR_EACH_SAFE_REV(cursor, cursor2, list) \
for ((cursor) = (list)->prev, (cursor2) = (cursor)->prev; \
(cursor) != (list); \
(cursor) = (cursor2), (cursor2) = (cursor)->prev)
#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))
#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))
#undef LIST_INIT
#define LIST_INIT(list) { &(list), &(list) }
#undef LIST_ENTRY
#define LIST_ENTRY(elem, type, field) \
((type *)((char *)(elem) - offsetof(type, field)))
#endif
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/hyhnoproblem/article/details/42676023
转载请注明原文地址:https://ipadbbs.8miu.com/read-61162.html