]> git.proxmox.com Git - mirror_spl.git/blame - include/sys/list.h
Public Release Prep
[mirror_spl.git] / include / sys / list.h
CommitLineData
716154c5
BB
1/*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23\*****************************************************************************/
24
f5b92a66
BB
25#ifndef _SPL_LIST_H
26#define _SPL_LIST_H
27
dc0f9207 28#include <sys/types.h>
d702c04f 29#include <linux/list.h>
dc0f9207 30
716154c5
BB
31/*
32 * NOTE: I have implemented the Solaris list API in terms of the native
d702c04f
BB
33 * linux API. This has certain advantages in terms of leveraging the linux
34 * list debugging infrastructure, but it also means that the internals of a
35 * list differ slightly than on Solaris. This is not a problem as long as
36 * all callers stick to the published API. The two major differences are:
37 *
38 * 1) A list_node_t is mapped to a linux list_head struct which changes
39 * the name of the list_next/list_prev pointers to next/prev respectively.
40 *
41 * 2) A list_node_t which is not attached to a list on Solaris is denoted
42 * by having its list_next/list_prev pointers set to NULL. Under linux
43 * the next/prev pointers are set to LIST_POISON1 and LIST_POISON2
44 * respectively. At this moment this only impacts the implementation
45 * of the list_link_init() and list_link_active() functions.
46 */
47
48typedef struct list_head list_node_t;
dc0f9207
BB
49
50typedef struct list {
d702c04f
BB
51 size_t list_size;
52 size_t list_offset;
53 list_node_t list_head;
dc0f9207
BB
54} list_t;
55
d702c04f
BB
56#define list_d2l(a, obj) ((list_node_t *)(((char *)obj) + (a)->list_offset))
57#define list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))
58
59static inline int
60list_is_empty(list_t *list)
61{
62 return list_empty(&list->list_head);
63}
64
65static inline void
66list_link_init(list_node_t *node)
67{
68 node->next = LIST_POISON1;
69 node->prev = LIST_POISON2;
70}
71
72static inline void
73list_create(list_t *list, size_t size, size_t offset)
74{
75 ASSERT(list);
76 ASSERT(size > 0);
77 ASSERT(size >= offset + sizeof(list_node_t));
78
79 list->list_size = size;
80 list->list_offset = offset;
81 INIT_LIST_HEAD(&list->list_head);
82}
83
84static inline void
85list_destroy(list_t *list)
86{
87 ASSERT(list);
88 ASSERT(list_is_empty(list));
89
90 list_del(&list->list_head);
91}
92
93static inline void
94list_insert_head(list_t *list, void *object)
95{
96 list_add(list_d2l(list, object), &list->list_head);
97}
98
99static inline void
100list_insert_tail(list_t *list, void *object)
101{
102 list_add_tail(list_d2l(list, object), &list->list_head);
103}
104
105static inline void
106list_insert_after(list_t *list, void *object, void *nobject)
107{
108 if (object == NULL)
109 list_insert_head(list, nobject);
110 else
111 list_add(list_d2l(list, nobject), list_d2l(list, object));
112}
113
114static inline void
115list_insert_before(list_t *list, void *object, void *nobject)
116{
117 if (object == NULL)
118 list_insert_tail(list, nobject);
119 else
120 list_add_tail(list_d2l(list, nobject), list_d2l(list, object));
121}
122
123static inline void
124list_remove(list_t *list, void *object)
125{
126 ASSERT(!list_is_empty(list));
127 list_del(list_d2l(list, object));
128}
129
130static inline void *
131list_remove_head(list_t *list)
132{
133 list_node_t *head = list->list_head.next;
134 if (head == &list->list_head)
135 return NULL;
136
137 list_del(head);
138 return list_object(list, head);
139}
140
141static inline void *
142list_remove_tail(list_t *list)
143{
144 list_node_t *tail = list->list_head.prev;
145 if (tail == &list->list_head)
146 return NULL;
147
148 list_del(tail);
149 return list_object(list, tail);
150}
151
152static inline void *
153list_head(list_t *list)
154{
155 if (list_is_empty(list))
156 return NULL;
157
158 return list_object(list, list->list_head.next);
159}
160
161static inline void *
162list_tail(list_t *list)
163{
164 if (list_is_empty(list))
165 return NULL;
166
167 return list_object(list, list->list_head.prev);
168}
169
170static inline void *
171list_next(list_t *list, void *object)
172{
173 list_node_t *node = list_d2l(list, object);
174
175 if (node->next != &list->list_head)
176 return list_object(list, node->next);
177
178 return NULL;
179}
dc0f9207 180
d702c04f
BB
181static inline void *
182list_prev(list_t *list, void *object)
183{
184 list_node_t *node = list_d2l(list, object);
dc0f9207 185
d702c04f
BB
186 if (node->prev != &list->list_head)
187 return list_object(list, node->prev);
dc0f9207 188
d702c04f
BB
189 return NULL;
190}
dc0f9207 191
d702c04f
BB
192static inline int
193list_link_active(list_node_t *node)
194{
f7e8739c 195 return (node->next != LIST_POISON1) && (node->prev != LIST_POISON2);
d702c04f 196}
dc0f9207 197
759dfe7d
BB
198static inline void
199spl_list_move_tail(list_t *dst, list_t *src)
200{
201 list_splice_init(&src->list_head, dst->list_head.prev);
202}
203
204#define list_move_tail(dst, src) spl_list_move_tail(dst, src)
205
f5b92a66 206#endif /* SPL_LIST_H */