1 /* $OpenBSD: idr.h,v 1.4 2020/11/14 15:00:20 kettenis 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 #define idr_preload_end() 40 void *idr_find(struct idr *, unsigned long); 41 void *idr_replace(struct idr *, void *, unsigned long); 42 void *idr_remove(struct idr *, unsigned long); 43 void idr_destroy(struct idr *); 44 int idr_for_each(struct idr *, int (*)(int, void *, void *), void *); 45 void *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 51 static inline bool 52 idr_is_empty(const struct idr *idr) 53 { 54 return SPLAY_EMPTY(&idr->tree); 55 } 56 57 struct ida { 58 struct idr idr; 59 }; 60 61 #define DEFINE_IDA(name) \ 62 struct ida name = { \ 63 .idr = { SPLAY_INITIALIZER(&name.idr.tree) } \ 64 } 65 66 void ida_init(struct ida *); 67 void ida_destroy(struct ida *); 68 int ida_simple_get(struct ida *, unsigned int, unsigned int, gfp_t); 69 void ida_simple_remove(struct ida *, unsigned int); 70 71 #endif 72