1130331Sanholt/* drm_linux_list.h -- linux list functions for the BSDs.
2145132Sanholt * Created: Mon Apr 7 14:30:16 1999 by anholt@FreeBSD.org
3145132Sanholt */
4139749Simp/*-
5130331Sanholt * Copyright 2003 Eric Anholt
6130331Sanholt * All Rights Reserved.
7130331Sanholt *
8130331Sanholt * Permission is hereby granted, free of charge, to any person obtaining a
9130331Sanholt * copy of this software and associated documentation files (the "Software"),
10130331Sanholt * to deal in the Software without restriction, including without limitation
11130331Sanholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12130331Sanholt * and/or sell copies of the Software, and to permit persons to whom the
13130331Sanholt * Software is furnished to do so, subject to the following conditions:
14130331Sanholt *
15130331Sanholt * The above copyright notice and this permission notice (including the next
16130331Sanholt * paragraph) shall be included in all copies or substantial portions of the
17130331Sanholt * Software.
18130331Sanholt *
19130331Sanholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20130331Sanholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21130331Sanholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22130331Sanholt * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23130331Sanholt * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24130331Sanholt * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25130331Sanholt * OTHER DEALINGS IN THE SOFTWARE.
26130331Sanholt *
27130331Sanholt * Authors:
28130331Sanholt *    Eric Anholt <anholt@FreeBSD.org>
29130331Sanholt *
30130331Sanholt */
31130331Sanholt
32152909Sanholt#include <sys/cdefs.h>
33152909Sanholt__FBSDID("$FreeBSD$");
34152909Sanholt
35203287Srnoland#ifndef _DRM_LINUX_LIST_H_
36203287Srnoland#define _DRM_LINUX_LIST_H_
37203287Srnoland
38130331Sanholtstruct list_head {
39130331Sanholt	struct list_head *next, *prev;
40130331Sanholt};
41130331Sanholt
42203287Srnoland#define list_entry(ptr, type, member) container_of(ptr,type,member)
43203287Srnoland#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
44130331Sanholt
45130331Sanholtstatic __inline__ void
46130331SanholtINIT_LIST_HEAD(struct list_head *head) {
47130331Sanholt	(head)->next = head;
48130331Sanholt	(head)->prev = head;
49130331Sanholt}
50130331Sanholt
51130331Sanholtstatic __inline__ int
52130331Sanholtlist_empty(struct list_head *head) {
53130331Sanholt	return (head)->next == head;
54130331Sanholt}
55130331Sanholt
56130331Sanholtstatic __inline__ void
57203287Srnolandlist_add(struct list_head *new, struct list_head *head) {
58203287Srnoland        (head)->next->prev = new;
59203287Srnoland        (new)->next = (head)->next;
60203287Srnoland        (new)->prev = head;
61203287Srnoland        (head)->next = new;
62203287Srnoland}
63203287Srnoland
64203287Srnolandstatic __inline__ void
65130331Sanholtlist_add_tail(struct list_head *entry, struct list_head *head) {
66130331Sanholt	(entry)->prev = (head)->prev;
67130331Sanholt	(entry)->next = head;
68130331Sanholt	(head)->prev->next = entry;
69130331Sanholt	(head)->prev = entry;
70130331Sanholt}
71130331Sanholt
72130331Sanholtstatic __inline__ void
73130331Sanholtlist_del(struct list_head *entry) {
74130331Sanholt	(entry)->next->prev = (entry)->prev;
75130331Sanholt	(entry)->prev->next = (entry)->next;
76130331Sanholt}
77130331Sanholt
78203287Srnolandstatic __inline__ void
79203287Srnolandlist_del_init(struct list_head *entry) {
80203287Srnoland	(entry)->next->prev = (entry)->prev;
81203287Srnoland	(entry)->prev->next = (entry)->next;
82203287Srnoland	INIT_LIST_HEAD(entry);
83203287Srnoland}
84203287Srnoland
85130331Sanholt#define list_for_each(entry, head)				\
86130331Sanholt    for (entry = (head)->next; entry != head; entry = (entry)->next)
87130331Sanholt
88189913Srnoland#define list_for_each_prev(entry, head) \
89189913Srnoland        for (entry = (head)->prev; entry != (head); \
90189913Srnoland                entry = entry->prev)
91189913Srnoland
92130331Sanholt#define list_for_each_safe(entry, temp, head)			\
93130331Sanholt    for (entry = (head)->next, temp = (entry)->next;		\
94183832Srnoland	entry != head; 						\
95183832Srnoland	entry = temp, temp = entry->next)
96130331Sanholt
97203287Srnoland/**
98203287Srnoland * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
99203287Srnoland * @pos:        the type * to use as a loop cursor.
100203287Srnoland * @n:          another type * to use as temporary storage
101203287Srnoland * @head:       the head for your list.
102203287Srnoland * @member:     the name of the list_struct within the struct.
103203287Srnoland */
104203287Srnoland#define list_for_each_entry_safe(pos, n, head, member)			\
105203287Srnoland	for (pos = list_entry((head)->next, __typeof(*pos), member),	\
106203287Srnoland	    n = list_entry(pos->member.next, __typeof(*pos), member);	\
107203287Srnoland	    &pos->member != (head);					\
108203287Srnoland	    pos = n, n = list_entry(n->member.next, __typeof(*n), member))
109203287Srnoland
110203287Srnoland#endif /* _DRM_LINUX_LIST_H_ */
111