xref: /openbsd-src/sys/dev/pci/drm/include/linux/xarray.h (revision c020cf82e0cc147236f01a8dca7052034cf9d30d)
1 /* Public domain. */
2 
3 #ifndef _LINUX_XARRAY_H
4 #define _LINUX_XARRAY_H
5 
6 #include <linux/gfp.h>
7 
8 #include <sys/tree.h>
9 #include <sys/pool.h>
10 
11 #define XA_FLAGS_ALLOC		1
12 #define XA_FLAGS_ALLOC1		2
13 
14 struct xarray_entry {
15 	SPLAY_ENTRY(xarray_entry) entry;
16 	int id;
17 	void *ptr;
18 };
19 
20 struct xarray {
21 	gfp_t		xa_flags;
22 	SPLAY_HEAD(xarray_tree, xarray_entry) xa_tree;
23 };
24 
25 void xa_init_flags(struct xarray *, gfp_t);
26 void xa_destroy(struct xarray *);
27 int xa_alloc(struct xarray *, u32 *, void *, int, gfp_t);
28 void *xa_load(struct xarray *, unsigned long);
29 void *xa_erase(struct xarray *, unsigned long);
30 void *xa_get_next(struct xarray *, unsigned long *);
31 
32 #define xa_for_each(xa, index, entry) \
33 	for (index = 0; ((entry) = xa_get_next(xa, &(index))) != NULL; index++)
34 
35 #define xa_limit_32b	0
36 
37 static inline void *
38 xa_mk_value(unsigned long v)
39 {
40 	unsigned long r = (v << 1) | 1;
41 	return (void *)r;
42 }
43 
44 static inline bool
45 xa_is_value(const void *e)
46 {
47 	unsigned long v = (unsigned long)e;
48 	return v & 1;
49 }
50 
51 static inline unsigned long
52 xa_to_value(const void *e)
53 {
54 	unsigned long v = (unsigned long)e;
55 	return v >> 1;
56 }
57 
58 #endif
59