| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: linux_idr.c,v 1.1.2.6 2013/07/24 03:16:15 riastradh Exp $ */ | | 1 | /* $NetBSD: linux_idr.c,v 1.1.2.7 2013/07/24 03:16:32 riastradh Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2013 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2013 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Taylor R. Campbell. | | 8 | * by Taylor R. Campbell. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -20,27 +20,27 @@ | | | @@ -20,27 +20,27 @@ |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include <sys/cdefs.h> | | 32 | #include <sys/cdefs.h> |
33 | __KERNEL_RCSID(0, "$NetBSD: linux_idr.c,v 1.1.2.6 2013/07/24 03:16:15 riastradh Exp $"); | | 33 | __KERNEL_RCSID(0, "$NetBSD: linux_idr.c,v 1.1.2.7 2013/07/24 03:16:32 riastradh Exp $"); |
34 | | | 34 | |
35 | #include <sys/param.h> | | 35 | #include <sys/param.h> |
36 | #include <sys/atomic.h> | | 36 | #include <sys/atomic.h> |
37 | #include <sys/kmem.h> | | 37 | #include <sys/kmem.h> |
38 | #include <sys/rbtree.h> | | 38 | #include <sys/rbtree.h> |
39 | | | 39 | |
40 | #include <linux/err.h> | | 40 | #include <linux/err.h> |
41 | #include <linux/idr.h> | | 41 | #include <linux/idr.h> |
42 | | | 42 | |
43 | struct idr_node { | | 43 | struct idr_node { |
44 | rb_node_t in_rb_node; | | 44 | rb_node_t in_rb_node; |
45 | int in_index; | | 45 | int in_index; |
46 | void *in_data; | | 46 | void *in_data; |
| @@ -150,28 +150,27 @@ idr_remove(struct idr *idr, int id) | | | @@ -150,28 +150,27 @@ idr_remove(struct idr *idr, int id) |
150 | node = rb_tree_find_node(&idr->idr_tree, &id); | | 150 | node = rb_tree_find_node(&idr->idr_tree, &id); |
151 | KASSERT(node != NULL); | | 151 | KASSERT(node != NULL); |
152 | rb_tree_remove_node(&idr->idr_tree, node); | | 152 | rb_tree_remove_node(&idr->idr_tree, node); |
153 | rw_exit(&idr->idr_lock); | | 153 | rw_exit(&idr->idr_lock); |
154 | kmem_free(node, sizeof(*node)); | | 154 | kmem_free(node, sizeof(*node)); |
155 | } | | 155 | } |
156 | | | 156 | |
157 | void | | 157 | void |
158 | idr_remove_all(struct idr *idr) | | 158 | idr_remove_all(struct idr *idr) |
159 | { | | 159 | { |
160 | struct idr_node *node; | | 160 | struct idr_node *node; |
161 | | | 161 | |
162 | rw_enter(&idr->idr_lock, RW_WRITER); | | 162 | rw_enter(&idr->idr_lock, RW_WRITER); |
163 | while ((node = rb_tree_iterate(&idr->idr_tree, NULL, RB_DIR_RIGHT)) != | | 163 | while ((node = RB_TREE_MIN(&idr->idr_tree)) != NULL) { |
164 | NULL) { | | | |
165 | rb_tree_remove_node(&idr->idr_tree, node); | | 164 | rb_tree_remove_node(&idr->idr_tree, node); |
166 | rw_exit(&idr->idr_lock); | | 165 | rw_exit(&idr->idr_lock); |
167 | kmem_free(node, sizeof(*node)); | | 166 | kmem_free(node, sizeof(*node)); |
168 | rw_enter(&idr->idr_lock, RW_WRITER); | | 167 | rw_enter(&idr->idr_lock, RW_WRITER); |
169 | } | | 168 | } |
170 | rw_exit(&idr->idr_lock); | | 169 | rw_exit(&idr->idr_lock); |
171 | } | | 170 | } |
172 | | | 171 | |
173 | int | | 172 | int |
174 | idr_pre_get(struct idr *idr, int flags __unused /* XXX */) | | 173 | idr_pre_get(struct idr *idr, int flags __unused /* XXX */) |
175 | { | | 174 | { |
176 | struct idr_node *temp = kmem_alloc(sizeof(*temp), KM_SLEEP); | | 175 | struct idr_node *temp = kmem_alloc(sizeof(*temp), KM_SLEEP); |
177 | | | 176 | |
| @@ -213,27 +212,26 @@ idr_get_new_above(struct idr *idr, void | | | @@ -213,27 +212,26 @@ idr_get_new_above(struct idr *idr, void |
213 | node->in_data = data; | | 212 | node->in_data = data; |
214 | | | 213 | |
215 | collision = rb_tree_insert_node(&idr->idr_tree, node); | | 214 | collision = rb_tree_insert_node(&idr->idr_tree, node); |
216 | KASSERT(collision == node); | | 215 | KASSERT(collision == node); |
217 | | | 216 | |
218 | rw_exit(&idr->idr_lock); | | 217 | rw_exit(&idr->idr_lock); |
219 | | | 218 | |
220 | return 0; | | 219 | return 0; |
221 | } | | 220 | } |
222 | | | 221 | |
223 | int | | 222 | int |
224 | idr_for_each(struct idr *idr, int (*proc)(int, void *, void *), void *arg) | | 223 | idr_for_each(struct idr *idr, int (*proc)(int, void *, void *), void *arg) |
225 | { | | 224 | { |
226 | struct idr_node *node = NULL; | | 225 | struct idr_node *node; |
227 | int error = 0; | | 226 | int error = 0; |
228 | | | 227 | |
229 | rw_enter(&idr->idr_lock, RW_READER); | | 228 | rw_enter(&idr->idr_lock, RW_READER); |
230 | while ((node = rb_tree_iterate(&idr->idr_tree, node, RB_DIR_RIGHT)) | | 229 | RB_TREE_FOREACH(node, &idr->idr_tree) { |
231 | != NULL) { | | | |
232 | error = (*proc)(node->in_index, node->in_data, arg); | | 230 | error = (*proc)(node->in_index, node->in_data, arg); |
233 | if (error) | | 231 | if (error) |
234 | break; | | 232 | break; |
235 | } | | 233 | } |
236 | rw_exit(&idr->idr_lock); | | 234 | rw_exit(&idr->idr_lock); |
237 | | | 235 | |
238 | return error; | | 236 | return error; |
239 | } | | 237 | } |