xref: /openbsd/sys/dev/pci/drm/include/linux/idr.h (revision 1bb76ff1)
1 /*	$OpenBSD: idr.h,v 1.6 2023/01/01 01:34:58 jsg Exp $	*/
2 /*
3  * Copyright (c) 2016 Mark Kettenis
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _LINUX_IDR_H
19 #define _LINUX_IDR_H
20 
21 #include <sys/types.h>
22 #include <sys/tree.h>
23 
24 #include <linux/radix-tree.h>
25 
26 struct idr_entry {
27 	SPLAY_ENTRY(idr_entry) entry;
28 	unsigned long id;
29 	void *ptr;
30 };
31 
32 struct idr {
33 	SPLAY_HEAD(idr_tree, idr_entry) tree;
34 };
35 
36 void idr_init(struct idr *);
37 void idr_preload(unsigned int);
38 int idr_alloc(struct idr *, void *, int, int, gfp_t);
39 void *idr_find(struct idr *, unsigned long);
40 void *idr_replace(struct idr *, void *, unsigned long);
41 void *idr_remove(struct idr *, unsigned long);
42 void idr_destroy(struct idr *);
43 int idr_for_each(struct idr *, int (*)(int, void *, void *), void *);
44 void *idr_get_next(struct idr *, int *);
45 
46 #define idr_for_each_entry(idp, entry, id) \
47 	for (id = 0; ((entry) = idr_get_next(idp, &(id))) != NULL; id++)
48 
49 static inline void
idr_init_base(struct idr * idr,int base)50 idr_init_base(struct idr *idr, int base)
51 {
52 	idr_init(idr);
53 }
54 
55 static inline void
idr_preload_end(void)56 idr_preload_end(void)
57 {
58 }
59 
60 static inline bool
idr_is_empty(const struct idr * idr)61 idr_is_empty(const struct idr *idr)
62 {
63 	return SPLAY_EMPTY(&idr->tree);
64 }
65 
66 struct ida {
67 	struct idr idr;
68 };
69 
70 #define DEFINE_IDA(name)					\
71 	struct ida name = {					\
72 	    .idr = { SPLAY_INITIALIZER(&name.idr.tree) }	\
73 	}
74 
75 void ida_init(struct ida *);
76 void ida_destroy(struct ida *);
77 int ida_simple_get(struct ida *, unsigned int, unsigned int, gfp_t);
78 void ida_simple_remove(struct ida *, unsigned int);
79 
80 int ida_alloc_min(struct ida *, unsigned int, gfp_t);
81 int ida_alloc_max(struct ida *, unsigned int, gfp_t);
82 void ida_free(struct ida *, unsigned int);
83 
84 #endif
85