Home | History | Annotate | Line # | Download | only in linux
linux_idr.c revision 1.1.2.7
      1  1.1.2.1  riastrad /*	$NetBSD: linux_idr.c,v 1.1.2.7 2013/07/24 03:16:32 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.7 2013/07/24 03:16:32 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.1  riastrad #include <sys/kmem.h>
     38  1.1.2.1  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.1  riastrad struct idr_node {
     44  1.1.2.1  riastrad 	rb_node_t in_rb_node;
     45  1.1.2.1  riastrad 	int in_index;
     46  1.1.2.1  riastrad 	void *in_data;
     47  1.1.2.1  riastrad };
     48  1.1.2.1  riastrad 
     49  1.1.2.1  riastrad static signed int idr_tree_compare_nodes(void *, const void *, const void *);
     50  1.1.2.1  riastrad static signed int idr_tree_compare_key(void *, const void *, const void *);
     51  1.1.2.1  riastrad 
     52  1.1.2.1  riastrad static const rb_tree_ops_t idr_rb_ops = {
     53  1.1.2.1  riastrad 	.rbto_compare_nodes = &idr_tree_compare_nodes,
     54  1.1.2.1  riastrad 	.rbto_compare_key = &idr_tree_compare_key,
     55  1.1.2.1  riastrad 	.rbto_node_offset = offsetof(struct idr_node, in_rb_node),
     56  1.1.2.1  riastrad 	.rbto_context = NULL,
     57  1.1.2.1  riastrad };
     58  1.1.2.1  riastrad 
     59  1.1.2.1  riastrad static signed int
     60  1.1.2.1  riastrad idr_tree_compare_nodes(void *ctx __unused, const void *na, const void *nb)
     61  1.1.2.1  riastrad {
     62  1.1.2.1  riastrad 	const int a = ((const struct idr_node *)na)->in_index;
     63  1.1.2.1  riastrad 	const int b = ((const struct idr_node *)nb)->in_index;
     64  1.1.2.1  riastrad 
     65  1.1.2.1  riastrad 	if (a < b)
     66  1.1.2.1  riastrad 		return -1;
     67  1.1.2.1  riastrad 	else if (b < a)
     68  1.1.2.1  riastrad 		 return +1;
     69  1.1.2.1  riastrad 	else
     70  1.1.2.1  riastrad 		return 0;
     71  1.1.2.1  riastrad }
     72  1.1.2.1  riastrad 
     73  1.1.2.1  riastrad static signed int
     74  1.1.2.1  riastrad idr_tree_compare_key(void *ctx __unused, const void *n, const void *key)
     75  1.1.2.1  riastrad {
     76  1.1.2.1  riastrad 	const int a = ((const struct idr_node *)n)->in_index;
     77  1.1.2.1  riastrad 	const int b = *(const int *)key;
     78  1.1.2.1  riastrad 
     79  1.1.2.1  riastrad 	if (a < b)
     80  1.1.2.1  riastrad 		return -1;
     81  1.1.2.1  riastrad 	else if (b < a)
     82  1.1.2.1  riastrad 		return +1;
     83  1.1.2.1  riastrad 	else
     84  1.1.2.1  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.1  riastrad idr_init(struct idr *idr)
     89  1.1.2.1  riastrad {
     90  1.1.2.1  riastrad 
     91  1.1.2.1  riastrad 	rw_init(&idr->idr_lock);
     92  1.1.2.1  riastrad 	rb_tree_init(&idr->idr_tree, &idr_rb_ops);
     93  1.1.2.1  riastrad 	idr->idr_temp = NULL;
     94  1.1.2.1  riastrad }
     95  1.1.2.1  riastrad 
     96  1.1.2.1  riastrad void
     97  1.1.2.1  riastrad idr_destroy(struct idr *idr)
     98  1.1.2.1  riastrad {
     99  1.1.2.1  riastrad 
    100  1.1.2.1  riastrad 	if (idr->idr_temp != NULL) {
    101  1.1.2.1  riastrad 		/* XXX Probably shouldn't happen.  */
    102  1.1.2.1  riastrad 		kmem_free(idr->idr_temp, sizeof(*idr->idr_temp));
    103  1.1.2.1  riastrad 		idr->idr_temp = NULL;
    104  1.1.2.1  riastrad 	}
    105  1.1.2.1  riastrad #if 0				/* XXX No rb_tree_destroy?  */
    106  1.1.2.1  riastrad 	rb_tree_destroy(&idr->idr_tree);
    107  1.1.2.1  riastrad #endif
    108  1.1.2.1  riastrad 	rw_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.1  riastrad 	const struct idr_node *node;
    115  1.1.2.1  riastrad 	void *data;
    116  1.1.2.1  riastrad 
    117  1.1.2.1  riastrad 	rw_enter(&idr->idr_lock, RW_READER);
    118  1.1.2.1  riastrad 	node = rb_tree_find_node(&idr->idr_tree, &id);
    119  1.1.2.1  riastrad 	data = (node == NULL? NULL : node->in_data);
    120  1.1.2.1  riastrad 	rw_exit(&idr->idr_lock);
    121  1.1.2.1  riastrad 
    122  1.1.2.1  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.4  riastrad 	struct idr_node *node;
    129  1.1.2.4  riastrad 	void *result;
    130  1.1.2.4  riastrad 
    131  1.1.2.4  riastrad 	rw_enter(&idr->idr_lock, RW_WRITER);
    132  1.1.2.4  riastrad 	node = rb_tree_find_node(&idr->idr_tree, &id);
    133  1.1.2.4  riastrad 	if (node == NULL) {
    134  1.1.2.4  riastrad 		result = ERR_PTR(-ENOENT);
    135  1.1.2.4  riastrad 	} else {
    136  1.1.2.4  riastrad 		result = node->in_data;
    137  1.1.2.4  riastrad 		node->in_data = replacement;
    138  1.1.2.4  riastrad 	}
    139  1.1.2.4  riastrad 	rw_exit(&idr->idr_lock);
    140  1.1.2.4  riastrad 
    141  1.1.2.4  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.1  riastrad 	struct idr_node *node;
    148  1.1.2.1  riastrad 
    149  1.1.2.1  riastrad 	rw_enter(&idr->idr_lock, RW_WRITER);
    150  1.1.2.1  riastrad 	node = rb_tree_find_node(&idr->idr_tree, &id);
    151  1.1.2.1  riastrad 	KASSERT(node != NULL);
    152  1.1.2.1  riastrad 	rb_tree_remove_node(&idr->idr_tree, node);
    153  1.1.2.1  riastrad 	rw_exit(&idr->idr_lock);
    154  1.1.2.1  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.3  riastrad 	struct idr_node *node;
    161  1.1.2.3  riastrad 
    162  1.1.2.3  riastrad 	rw_enter(&idr->idr_lock, RW_WRITER);
    163  1.1.2.7  riastrad 	while ((node = RB_TREE_MIN(&idr->idr_tree)) != NULL) {
    164  1.1.2.3  riastrad 		rb_tree_remove_node(&idr->idr_tree, node);
    165  1.1.2.3  riastrad 		rw_exit(&idr->idr_lock);
    166  1.1.2.3  riastrad 		kmem_free(node, sizeof(*node));
    167  1.1.2.3  riastrad 		rw_enter(&idr->idr_lock, RW_WRITER);
    168  1.1.2.3  riastrad 	}
    169  1.1.2.3  riastrad 	rw_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.1  riastrad idr_pre_get(struct idr *idr, int flags __unused /* XXX */)
    174  1.1.2.1  riastrad {
    175  1.1.2.6  riastrad 	struct idr_node *temp = kmem_alloc(sizeof(*temp), KM_SLEEP);
    176  1.1.2.1  riastrad 
    177  1.1.2.2  riastrad 	rw_enter(&idr->idr_lock, RW_WRITER);
    178  1.1.2.6  riastrad 	if (idr->idr_temp == NULL) {
    179  1.1.2.2  riastrad 		idr->idr_temp = temp;
    180  1.1.2.6  riastrad 		temp = NULL;
    181  1.1.2.6  riastrad 	}
    182  1.1.2.2  riastrad 	rw_exit(&idr->idr_lock);
    183  1.1.2.1  riastrad 
    184  1.1.2.6  riastrad 	if (temp != NULL)
    185  1.1.2.6  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.1  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.1  riastrad 	struct idr_node *node, *search, *collision __unused;
    194  1.1.2.1  riastrad 	int want_id = min_id;
    195  1.1.2.1  riastrad 
    196  1.1.2.1  riastrad 	rw_enter(&idr->idr_lock, RW_WRITER);
    197  1.1.2.1  riastrad 
    198  1.1.2.2  riastrad 	node = idr->idr_temp;
    199  1.1.2.2  riastrad 	if (node == NULL) {
    200  1.1.2.2  riastrad 		rw_exit(&idr->idr_lock);
    201  1.1.2.2  riastrad 		return -EAGAIN;
    202  1.1.2.2  riastrad 	}
    203  1.1.2.2  riastrad 	idr->idr_temp = NULL;
    204  1.1.2.2  riastrad 
    205  1.1.2.1  riastrad 	search = rb_tree_find_node_geq(&idr->idr_tree, &min_id);
    206  1.1.2.1  riastrad 	while ((search != NULL) && (search->in_index == want_id)) {
    207  1.1.2.1  riastrad 		search = rb_tree_iterate(&idr->idr_tree, search, RB_DIR_RIGHT);
    208  1.1.2.1  riastrad 		want_id++;
    209  1.1.2.1  riastrad 	}
    210  1.1.2.1  riastrad 
    211  1.1.2.1  riastrad 	node->in_index = want_id;
    212  1.1.2.1  riastrad 	node->in_data = data;
    213  1.1.2.1  riastrad 
    214  1.1.2.1  riastrad 	collision = rb_tree_insert_node(&idr->idr_tree, node);
    215  1.1.2.1  riastrad 	KASSERT(collision == node);
    216  1.1.2.1  riastrad 
    217  1.1.2.1  riastrad 	rw_exit(&idr->idr_lock);
    218  1.1.2.1  riastrad 
    219  1.1.2.1  riastrad 	return 0;
    220  1.1.2.1  riastrad }
    221  1.1.2.5  riastrad 
    222  1.1.2.5  riastrad int
    223  1.1.2.5  riastrad idr_for_each(struct idr *idr, int (*proc)(int, void *, void *), void *arg)
    224  1.1.2.5  riastrad {
    225  1.1.2.7  riastrad 	struct idr_node *node;
    226  1.1.2.5  riastrad 	int error = 0;
    227  1.1.2.5  riastrad 
    228  1.1.2.5  riastrad 	rw_enter(&idr->idr_lock, RW_READER);
    229  1.1.2.7  riastrad 	RB_TREE_FOREACH(node, &idr->idr_tree) {
    230  1.1.2.5  riastrad 		error = (*proc)(node->in_index, node->in_data, arg);
    231  1.1.2.5  riastrad 		if (error)
    232  1.1.2.5  riastrad 			break;
    233  1.1.2.5  riastrad 	}
    234  1.1.2.5  riastrad 	rw_exit(&idr->idr_lock);
    235  1.1.2.5  riastrad 
    236  1.1.2.5  riastrad 	return error;
    237  1.1.2.5  riastrad }
    238