1/* drm_linux_list.h -- linux list functions for the BSDs.
2 * Created: Mon Apr 7 14:30:16 1999 by anholt@FreeBSD.org
3 */
4/*-
5 * Copyright 2003 Eric Anholt
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
28 *    Eric Anholt <anholt@FreeBSD.org>
29 *
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#ifndef _DRM_LINUX_LIST_H_
36#define _DRM_LINUX_LIST_H_
37
38struct list_head {
39	struct list_head *next, *prev;
40};
41
42#define list_entry(ptr, type, member) container_of(ptr,type,member)
43#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
44
45static __inline__ void
46INIT_LIST_HEAD(struct list_head *head) {
47	(head)->next = head;
48	(head)->prev = head;
49}
50
51static __inline__ int
52list_empty(struct list_head *head) {
53	return (head)->next == head;
54}
55
56static __inline__ void
57list_add(struct list_head *new, struct list_head *head) {
58        (head)->next->prev = new;
59        (new)->next = (head)->next;
60        (new)->prev = head;
61        (head)->next = new;
62}
63
64static __inline__ void
65list_add_tail(struct list_head *entry, struct list_head *head) {
66	(entry)->prev = (head)->prev;
67	(entry)->next = head;
68	(head)->prev->next = entry;
69	(head)->prev = entry;
70}
71
72static __inline__ void
73list_del(struct list_head *entry) {
74	(entry)->next->prev = (entry)->prev;
75	(entry)->prev->next = (entry)->next;
76}
77
78static __inline__ void
79list_del_init(struct list_head *entry) {
80	(entry)->next->prev = (entry)->prev;
81	(entry)->prev->next = (entry)->next;
82	INIT_LIST_HEAD(entry);
83}
84
85#define list_for_each(entry, head)				\
86    for (entry = (head)->next; entry != head; entry = (entry)->next)
87
88#define list_for_each_prev(entry, head) \
89        for (entry = (head)->prev; entry != (head); \
90                entry = entry->prev)
91
92#define list_for_each_safe(entry, temp, head)			\
93    for (entry = (head)->next, temp = (entry)->next;		\
94	entry != head; 						\
95	entry = temp, temp = entry->next)
96
97/**
98 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
99 * @pos:        the type * to use as a loop cursor.
100 * @n:          another type * to use as temporary storage
101 * @head:       the head for your list.
102 * @member:     the name of the list_struct within the struct.
103 */
104#define list_for_each_entry_safe(pos, n, head, member)			\
105	for (pos = list_entry((head)->next, __typeof(*pos), member),	\
106	    n = list_entry(pos->member.next, __typeof(*pos), member);	\
107	    &pos->member != (head);					\
108	    pos = n, n = list_entry(n->member.next, __typeof(*n), member))
109
110#endif /* _DRM_LINUX_LIST_H_ */
111