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