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