]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - spl/include/sys/list.h
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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
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
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 \*****************************************************************************/
28 #include <sys/types.h>
29 #include <linux/list.h>
32 * NOTE: I have implemented the Solaris list API in terms of the native
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:
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.
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.
48 typedef struct list_head list_node_t
;
53 list_node_t list_head
;
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))
60 list_is_empty(list_t
*list
)
62 return list_empty(&list
->list_head
);
66 list_link_init(list_node_t
*node
)
68 node
->next
= LIST_POISON1
;
69 node
->prev
= LIST_POISON2
;
73 list_create(list_t
*list
, size_t size
, size_t offset
)
77 ASSERT(size
>= offset
+ sizeof(list_node_t
));
79 list
->list_size
= size
;
80 list
->list_offset
= offset
;
81 INIT_LIST_HEAD(&list
->list_head
);
85 list_destroy(list_t
*list
)
88 ASSERT(list_is_empty(list
));
90 list_del(&list
->list_head
);
94 list_insert_head(list_t
*list
, void *object
)
96 list_add(list_d2l(list
, object
), &list
->list_head
);
100 list_insert_tail(list_t
*list
, void *object
)
102 list_add_tail(list_d2l(list
, object
), &list
->list_head
);
106 list_insert_after(list_t
*list
, void *object
, void *nobject
)
109 list_insert_head(list
, nobject
);
111 list_add(list_d2l(list
, nobject
), list_d2l(list
, object
));
115 list_insert_before(list_t
*list
, void *object
, void *nobject
)
118 list_insert_tail(list
, nobject
);
120 list_add_tail(list_d2l(list
, nobject
), list_d2l(list
, object
));
124 list_remove(list_t
*list
, void *object
)
126 ASSERT(!list_is_empty(list
));
127 list_del(list_d2l(list
, object
));
131 list_remove_head(list_t
*list
)
133 list_node_t
*head
= list
->list_head
.next
;
134 if (head
== &list
->list_head
)
138 return list_object(list
, head
);
142 list_remove_tail(list_t
*list
)
144 list_node_t
*tail
= list
->list_head
.prev
;
145 if (tail
== &list
->list_head
)
149 return list_object(list
, tail
);
153 list_head(list_t
*list
)
155 if (list_is_empty(list
))
158 return list_object(list
, list
->list_head
.next
);
162 list_tail(list_t
*list
)
164 if (list_is_empty(list
))
167 return list_object(list
, list
->list_head
.prev
);
171 list_next(list_t
*list
, void *object
)
173 list_node_t
*node
= list_d2l(list
, object
);
175 if (node
->next
!= &list
->list_head
)
176 return list_object(list
, node
->next
);
182 list_prev(list_t
*list
, void *object
)
184 list_node_t
*node
= list_d2l(list
, object
);
186 if (node
->prev
!= &list
->list_head
)
187 return list_object(list
, node
->prev
);
193 list_link_active(list_node_t
*node
)
195 return (node
->next
!= LIST_POISON1
) && (node
->prev
!= LIST_POISON2
);
199 spl_list_move_tail(list_t
*dst
, list_t
*src
)
201 list_splice_init(&src
->list_head
, dst
->list_head
.prev
);
204 #define list_move_tail(dst, src) spl_list_move_tail(dst, src)
207 list_link_replace(list_node_t
*old_node
, list_node_t
*new_node
)
209 ASSERT(list_link_active(old_node
));
210 ASSERT(!list_link_active(new_node
));
212 new_node
->next
= old_node
->next
;
213 new_node
->prev
= old_node
->prev
;
214 old_node
->prev
->next
= new_node
;
215 old_node
->next
->prev
= new_node
;
216 list_link_init(old_node
);
219 #endif /* SPL_LIST_H */