idr.h revision 1.3
1/*	$OpenBSD: idr.h,v 1.3 2020/06/08 04:48:14 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
26struct idr_entry {
27	SPLAY_ENTRY(idr_entry) entry;
28	int id;
29	void *ptr;
30};
31
32struct idr {
33	SPLAY_HEAD(idr_tree, idr_entry) tree;
34};
35
36void idr_init(struct idr *);
37void idr_preload(unsigned int);
38int idr_alloc(struct idr *, void *, int, int, unsigned int);
39#define idr_preload_end()
40void *idr_find(struct idr *, int);
41void *idr_replace(struct idr *, void *ptr, int);
42void *idr_remove(struct idr *, int);
43void idr_destroy(struct idr *);
44int idr_for_each(struct idr *, int (*)(int, void *, void *), void *);
45void *idr_get_next(struct idr *, int *);
46#define idr_init_base(idr, base)	idr_init(idr)
47
48#define idr_for_each_entry(idp, entry, id) \
49	for (id = 0; ((entry) = idr_get_next(idp, &(id))) != NULL; id++)
50
51static inline bool
52idr_is_empty(const struct idr *idr)
53{
54	return SPLAY_EMPTY(&idr->tree);
55}
56
57struct ida {
58	int counter;
59};
60
61#define DEFINE_IDA(name)	\
62	struct ida name = {	\
63		.counter = 0	\
64	}
65
66void ida_init(struct ida *);
67void ida_destroy(struct ida *);
68int ida_simple_get(struct ida *, unsigned int, unsigned nt, int);
69void ida_remove(struct ida *, int);
70void ida_simple_remove(struct ida *, int);
71
72#endif
73