linux_idr.c revision 1.2 1 1.2 riastrad /* $NetBSD: linux_idr.c,v 1.2 2014/03/18 18:20:43 riastradh Exp $ */
2 1.2 riastrad
3 1.2 riastrad /*-
4 1.2 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2 riastrad * All rights reserved.
6 1.2 riastrad *
7 1.2 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.2 riastrad * by Taylor R. Campbell.
9 1.2 riastrad *
10 1.2 riastrad * Redistribution and use in source and binary forms, with or without
11 1.2 riastrad * modification, are permitted provided that the following conditions
12 1.2 riastrad * are met:
13 1.2 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.2 riastrad * notice, this list of conditions and the following disclaimer.
15 1.2 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.2 riastrad * documentation and/or other materials provided with the distribution.
18 1.2 riastrad *
19 1.2 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.2 riastrad */
31 1.2 riastrad
32 1.2 riastrad #include <sys/cdefs.h>
33 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_idr.c,v 1.2 2014/03/18 18:20:43 riastradh Exp $");
34 1.2 riastrad
35 1.2 riastrad #include <sys/param.h>
36 1.2 riastrad #include <sys/atomic.h>
37 1.2 riastrad #include <sys/kmem.h>
38 1.2 riastrad #include <sys/rbtree.h>
39 1.2 riastrad
40 1.2 riastrad #include <linux/err.h>
41 1.2 riastrad #include <linux/idr.h>
42 1.2 riastrad
43 1.2 riastrad struct idr_node {
44 1.2 riastrad rb_node_t in_rb_node;
45 1.2 riastrad int in_index;
46 1.2 riastrad void *in_data;
47 1.2 riastrad };
48 1.2 riastrad
49 1.2 riastrad static signed int idr_tree_compare_nodes(void *, const void *, const void *);
50 1.2 riastrad static signed int idr_tree_compare_key(void *, const void *, const void *);
51 1.2 riastrad
52 1.2 riastrad static const rb_tree_ops_t idr_rb_ops = {
53 1.2 riastrad .rbto_compare_nodes = &idr_tree_compare_nodes,
54 1.2 riastrad .rbto_compare_key = &idr_tree_compare_key,
55 1.2 riastrad .rbto_node_offset = offsetof(struct idr_node, in_rb_node),
56 1.2 riastrad .rbto_context = NULL,
57 1.2 riastrad };
58 1.2 riastrad
59 1.2 riastrad static signed int
60 1.2 riastrad idr_tree_compare_nodes(void *ctx __unused, const void *na, const void *nb)
61 1.2 riastrad {
62 1.2 riastrad const int a = ((const struct idr_node *)na)->in_index;
63 1.2 riastrad const int b = ((const struct idr_node *)nb)->in_index;
64 1.2 riastrad
65 1.2 riastrad if (a < b)
66 1.2 riastrad return -1;
67 1.2 riastrad else if (b < a)
68 1.2 riastrad return +1;
69 1.2 riastrad else
70 1.2 riastrad return 0;
71 1.2 riastrad }
72 1.2 riastrad
73 1.2 riastrad static signed int
74 1.2 riastrad idr_tree_compare_key(void *ctx __unused, const void *n, const void *key)
75 1.2 riastrad {
76 1.2 riastrad const int a = ((const struct idr_node *)n)->in_index;
77 1.2 riastrad const int b = *(const int *)key;
78 1.2 riastrad
79 1.2 riastrad if (a < b)
80 1.2 riastrad return -1;
81 1.2 riastrad else if (b < a)
82 1.2 riastrad return +1;
83 1.2 riastrad else
84 1.2 riastrad return 0;
85 1.2 riastrad }
86 1.2 riastrad
87 1.2 riastrad void
88 1.2 riastrad idr_init(struct idr *idr)
89 1.2 riastrad {
90 1.2 riastrad
91 1.2 riastrad mutex_init(&idr->idr_lock, MUTEX_DEFAULT, IPL_VM);
92 1.2 riastrad rb_tree_init(&idr->idr_tree, &idr_rb_ops);
93 1.2 riastrad idr->idr_temp = NULL;
94 1.2 riastrad }
95 1.2 riastrad
96 1.2 riastrad void
97 1.2 riastrad idr_destroy(struct idr *idr)
98 1.2 riastrad {
99 1.2 riastrad
100 1.2 riastrad if (idr->idr_temp != NULL) {
101 1.2 riastrad /* XXX Probably shouldn't happen. */
102 1.2 riastrad kmem_free(idr->idr_temp, sizeof(*idr->idr_temp));
103 1.2 riastrad idr->idr_temp = NULL;
104 1.2 riastrad }
105 1.2 riastrad #if 0 /* XXX No rb_tree_destroy? */
106 1.2 riastrad rb_tree_destroy(&idr->idr_tree);
107 1.2 riastrad #endif
108 1.2 riastrad mutex_destroy(&idr->idr_lock);
109 1.2 riastrad }
110 1.2 riastrad
111 1.2 riastrad void *
112 1.2 riastrad idr_find(struct idr *idr, int id)
113 1.2 riastrad {
114 1.2 riastrad const struct idr_node *node;
115 1.2 riastrad void *data;
116 1.2 riastrad
117 1.2 riastrad mutex_spin_enter(&idr->idr_lock);
118 1.2 riastrad node = rb_tree_find_node(&idr->idr_tree, &id);
119 1.2 riastrad data = (node == NULL? NULL : node->in_data);
120 1.2 riastrad mutex_spin_exit(&idr->idr_lock);
121 1.2 riastrad
122 1.2 riastrad return data;
123 1.2 riastrad }
124 1.2 riastrad
125 1.2 riastrad void *
126 1.2 riastrad idr_replace(struct idr *idr, void *replacement, int id)
127 1.2 riastrad {
128 1.2 riastrad struct idr_node *node;
129 1.2 riastrad void *result;
130 1.2 riastrad
131 1.2 riastrad mutex_spin_enter(&idr->idr_lock);
132 1.2 riastrad node = rb_tree_find_node(&idr->idr_tree, &id);
133 1.2 riastrad if (node == NULL) {
134 1.2 riastrad result = ERR_PTR(-ENOENT);
135 1.2 riastrad } else {
136 1.2 riastrad result = node->in_data;
137 1.2 riastrad node->in_data = replacement;
138 1.2 riastrad }
139 1.2 riastrad mutex_spin_exit(&idr->idr_lock);
140 1.2 riastrad
141 1.2 riastrad return result;
142 1.2 riastrad }
143 1.2 riastrad
144 1.2 riastrad void
145 1.2 riastrad idr_remove(struct idr *idr, int id)
146 1.2 riastrad {
147 1.2 riastrad struct idr_node *node;
148 1.2 riastrad
149 1.2 riastrad mutex_spin_enter(&idr->idr_lock);
150 1.2 riastrad node = rb_tree_find_node(&idr->idr_tree, &id);
151 1.2 riastrad KASSERT(node != NULL);
152 1.2 riastrad rb_tree_remove_node(&idr->idr_tree, node);
153 1.2 riastrad mutex_spin_exit(&idr->idr_lock);
154 1.2 riastrad kmem_free(node, sizeof(*node));
155 1.2 riastrad }
156 1.2 riastrad
157 1.2 riastrad void
158 1.2 riastrad idr_remove_all(struct idr *idr)
159 1.2 riastrad {
160 1.2 riastrad struct idr_node *node;
161 1.2 riastrad
162 1.2 riastrad mutex_spin_enter(&idr->idr_lock);
163 1.2 riastrad while ((node = RB_TREE_MIN(&idr->idr_tree)) != NULL) {
164 1.2 riastrad rb_tree_remove_node(&idr->idr_tree, node);
165 1.2 riastrad mutex_spin_exit(&idr->idr_lock);
166 1.2 riastrad kmem_free(node, sizeof(*node));
167 1.2 riastrad mutex_spin_enter(&idr->idr_lock);
168 1.2 riastrad }
169 1.2 riastrad mutex_spin_exit(&idr->idr_lock);
170 1.2 riastrad }
171 1.2 riastrad
172 1.2 riastrad int
173 1.2 riastrad idr_pre_get(struct idr *idr, int flags __unused /* XXX */)
174 1.2 riastrad {
175 1.2 riastrad struct idr_node *temp = kmem_alloc(sizeof(*temp), KM_SLEEP);
176 1.2 riastrad
177 1.2 riastrad mutex_spin_enter(&idr->idr_lock);
178 1.2 riastrad if (idr->idr_temp == NULL) {
179 1.2 riastrad idr->idr_temp = temp;
180 1.2 riastrad temp = NULL;
181 1.2 riastrad }
182 1.2 riastrad mutex_spin_exit(&idr->idr_lock);
183 1.2 riastrad
184 1.2 riastrad if (temp != NULL)
185 1.2 riastrad kmem_free(temp, sizeof(*temp));
186 1.2 riastrad
187 1.2 riastrad return 1;
188 1.2 riastrad }
189 1.2 riastrad
190 1.2 riastrad int
191 1.2 riastrad idr_get_new_above(struct idr *idr, void *data, int min_id, int *id)
192 1.2 riastrad {
193 1.2 riastrad struct idr_node *node, *search, *collision __unused;
194 1.2 riastrad int want_id = min_id;
195 1.2 riastrad int error;
196 1.2 riastrad
197 1.2 riastrad mutex_spin_enter(&idr->idr_lock);
198 1.2 riastrad
199 1.2 riastrad node = idr->idr_temp;
200 1.2 riastrad if (node == NULL) {
201 1.2 riastrad error = -EAGAIN;
202 1.2 riastrad goto out;
203 1.2 riastrad }
204 1.2 riastrad idr->idr_temp = NULL;
205 1.2 riastrad
206 1.2 riastrad search = rb_tree_find_node_geq(&idr->idr_tree, &min_id);
207 1.2 riastrad while ((search != NULL) && (search->in_index == want_id)) {
208 1.2 riastrad if (want_id == INT_MAX) {
209 1.2 riastrad error = -ENOSPC;
210 1.2 riastrad goto out;
211 1.2 riastrad }
212 1.2 riastrad search = rb_tree_iterate(&idr->idr_tree, search, RB_DIR_RIGHT);
213 1.2 riastrad want_id++;
214 1.2 riastrad }
215 1.2 riastrad
216 1.2 riastrad node->in_index = want_id;
217 1.2 riastrad node->in_data = data;
218 1.2 riastrad
219 1.2 riastrad collision = rb_tree_insert_node(&idr->idr_tree, node);
220 1.2 riastrad KASSERT(collision == node);
221 1.2 riastrad
222 1.2 riastrad *id = want_id;
223 1.2 riastrad error = 0;
224 1.2 riastrad
225 1.2 riastrad out: mutex_spin_exit(&idr->idr_lock);
226 1.2 riastrad return error;
227 1.2 riastrad }
228 1.2 riastrad
229 1.2 riastrad int
230 1.2 riastrad idr_for_each(struct idr *idr, int (*proc)(int, void *, void *), void *arg)
231 1.2 riastrad {
232 1.2 riastrad struct idr_node *node;
233 1.2 riastrad int error = 0;
234 1.2 riastrad
235 1.2 riastrad /* XXX Caller must exclude modifications. */
236 1.2 riastrad membar_consumer();
237 1.2 riastrad RB_TREE_FOREACH(node, &idr->idr_tree) {
238 1.2 riastrad error = (*proc)(node->in_index, node->in_data, arg);
239 1.2 riastrad if (error)
240 1.2 riastrad break;
241 1.2 riastrad }
242 1.2 riastrad
243 1.2 riastrad return error;
244 1.2 riastrad }
245