1 /* SPDX-License-Identifier: GPL-2.0-only */
5 * 2002-10-18 written by Jim Houston jim.houston@ccur.com
6 * Copyright (C) 2002 by Concurrent Computer Corporation
8 * Small id to pointer translation service avoiding fixed sized
15 #include <linux/radix-tree.h>
16 #include <linux/gfp.h>
17 #include <linux/percpu.h>
20 struct radix_tree_root idr_rt
;
21 unsigned int idr_base
;
22 unsigned int idr_next
;
26 * The IDR API does not expose the tagging functionality of the radix tree
27 * to users. Use tag 0 to track whether a node has free space below it.
31 /* Set the IDR flag and the IDR_FREE tag */
32 #define IDR_RT_MARKER (ROOT_IS_IDR | (__force gfp_t) \
33 (1 << (ROOT_TAG_SHIFT + IDR_FREE)))
35 #define IDR_INIT_BASE(name, base) { \
36 .idr_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER), \
42 * IDR_INIT() - Initialise an IDR.
45 * A freshly-initialised IDR contains no IDs.
47 #define IDR_INIT(name) IDR_INIT_BASE(name, 0)
50 * DEFINE_IDR() - Define a statically-allocated IDR.
53 * An IDR defined using this macro is ready for use with no additional
54 * initialisation required. It contains no IDs.
56 #define DEFINE_IDR(name) struct idr name = IDR_INIT(name)
59 * idr_get_cursor - Return the current position of the cyclic allocator
62 * The value returned is the value that will be next returned from
63 * idr_alloc_cyclic() if it is free (otherwise the search will start from
66 static inline unsigned int idr_get_cursor(const struct idr
*idr
)
68 return READ_ONCE(idr
->idr_next
);
72 * idr_set_cursor - Set the current position of the cyclic allocator
76 * The next call to idr_alloc_cyclic() will return @val if it is free
77 * (otherwise the search will start from this position).
79 static inline void idr_set_cursor(struct idr
*idr
, unsigned int val
)
81 WRITE_ONCE(idr
->idr_next
, val
);
86 * idr synchronization (stolen from radix-tree.h)
88 * idr_find() is able to be called locklessly, using RCU. The caller must
89 * ensure calls to this function are made within rcu_read_lock() regions.
90 * Other readers (lock-free or otherwise) and modifications may be running
93 * It is still required that the caller manage the synchronization and
94 * lifetimes of the items. So if RCU lock-free lookups are used, typically
95 * this would mean that the items have their own locks, or are amenable to
96 * lock-free access; and that the items are freed by RCU (or only freed after
97 * having been deleted from the idr tree *and* a synchronize_rcu() grace
101 #define idr_lock(idr) xa_lock(&(idr)->idr_rt)
102 #define idr_unlock(idr) xa_unlock(&(idr)->idr_rt)
103 #define idr_lock_bh(idr) xa_lock_bh(&(idr)->idr_rt)
104 #define idr_unlock_bh(idr) xa_unlock_bh(&(idr)->idr_rt)
105 #define idr_lock_irq(idr) xa_lock_irq(&(idr)->idr_rt)
106 #define idr_unlock_irq(idr) xa_unlock_irq(&(idr)->idr_rt)
107 #define idr_lock_irqsave(idr, flags) \
108 xa_lock_irqsave(&(idr)->idr_rt, flags)
109 #define idr_unlock_irqrestore(idr, flags) \
110 xa_unlock_irqrestore(&(idr)->idr_rt, flags)
112 void idr_preload(gfp_t gfp_mask
);
114 int idr_alloc(struct idr
*, void *ptr
, int start
, int end
, gfp_t
);
115 int __must_check
idr_alloc_u32(struct idr
*, void *ptr
, u32
*id
,
116 unsigned long max
, gfp_t
);
117 int idr_alloc_cyclic(struct idr
*, void *ptr
, int start
, int end
, gfp_t
);
118 void *idr_remove(struct idr
*, unsigned long id
);
119 void *idr_find(const struct idr
*, unsigned long id
);
120 int idr_for_each(const struct idr
*,
121 int (*fn
)(int id
, void *p
, void *data
), void *data
);
122 void *idr_get_next(struct idr
*, int *nextid
);
123 void *idr_get_next_ul(struct idr
*, unsigned long *nextid
);
124 void *idr_replace(struct idr
*, void *, unsigned long id
);
125 void idr_destroy(struct idr
*);
128 * idr_init_base() - Initialise an IDR.
130 * @base: The base value for the IDR.
132 * This variation of idr_init() creates an IDR which will allocate IDs
135 static inline void idr_init_base(struct idr
*idr
, int base
)
137 INIT_RADIX_TREE(&idr
->idr_rt
, IDR_RT_MARKER
);
138 idr
->idr_base
= base
;
143 * idr_init() - Initialise an IDR.
146 * Initialise a dynamically allocated IDR. To initialise a
147 * statically allocated IDR, use DEFINE_IDR().
149 static inline void idr_init(struct idr
*idr
)
151 idr_init_base(idr
, 0);
155 * idr_is_empty() - Are there any IDs allocated?
158 * Return: %true if any IDs have been allocated from this IDR.
160 static inline bool idr_is_empty(const struct idr
*idr
)
162 return radix_tree_empty(&idr
->idr_rt
) &&
163 radix_tree_tagged(&idr
->idr_rt
, IDR_FREE
);
167 * idr_preload_end - end preload section started with idr_preload()
169 * Each idr_preload() should be matched with an invocation of this
170 * function. See idr_preload() for details.
172 static inline void idr_preload_end(void)
174 local_unlock(&radix_tree_preloads
.lock
);
178 * idr_for_each_entry() - Iterate over an IDR's elements of a given type.
180 * @entry: The type * to use as cursor
183 * @entry and @id do not need to be initialized before the loop, and
184 * after normal termination @entry is left with the value NULL. This
185 * is convenient for a "not found" value.
187 #define idr_for_each_entry(idr, entry, id) \
188 for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; id += 1U)
191 * idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type.
193 * @entry: The type * to use as cursor.
194 * @tmp: A temporary placeholder for ID.
197 * @entry and @id do not need to be initialized before the loop, and
198 * after normal termination @entry is left with the value NULL. This
199 * is convenient for a "not found" value.
201 #define idr_for_each_entry_ul(idr, entry, tmp, id) \
202 for (tmp = 0, id = 0; \
203 ((entry) = tmp <= id ? idr_get_next_ul(idr, &(id)) : NULL) != NULL; \
207 * idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type
209 * @entry: The type * to use as a cursor.
212 * Continue to iterate over entries, continuing after the current position.
214 #define idr_for_each_entry_continue(idr, entry, id) \
215 for ((entry) = idr_get_next((idr), &(id)); \
217 ++id, (entry) = idr_get_next((idr), &(id)))
220 * idr_for_each_entry_continue_ul() - Continue iteration over an IDR's elements of a given type
222 * @entry: The type * to use as a cursor.
223 * @tmp: A temporary placeholder for ID.
226 * Continue to iterate over entries, continuing after the current position.
227 * After normal termination @entry is left with the value NULL. This
228 * is convenient for a "not found" value.
230 #define idr_for_each_entry_continue_ul(idr, entry, tmp, id) \
232 ((entry) = tmp <= id ? idr_get_next_ul(idr, &(id)) : NULL) != NULL; \
236 * IDA - ID Allocator, use when translation from id to pointer isn't necessary.
238 #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
239 #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long))
240 #define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
243 unsigned long bitmap
[IDA_BITMAP_LONGS
];
250 #define IDA_INIT_FLAGS (XA_FLAGS_LOCK_IRQ | XA_FLAGS_ALLOC)
252 #define IDA_INIT(name) { \
253 .xa = XARRAY_INIT(name, IDA_INIT_FLAGS) \
255 #define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
257 int ida_alloc_range(struct ida
*, unsigned int min
, unsigned int max
, gfp_t
);
258 void ida_free(struct ida
*, unsigned int id
);
259 void ida_destroy(struct ida
*ida
);
262 * ida_alloc() - Allocate an unused ID.
264 * @gfp: Memory allocation flags.
266 * Allocate an ID between 0 and %INT_MAX, inclusive.
268 * Context: Any context. It is safe to call this function without
269 * locking in your code.
270 * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
271 * or %-ENOSPC if there are no free IDs.
273 static inline int ida_alloc(struct ida
*ida
, gfp_t gfp
)
275 return ida_alloc_range(ida
, 0, ~0, gfp
);
279 * ida_alloc_min() - Allocate an unused ID.
281 * @min: Lowest ID to allocate.
282 * @gfp: Memory allocation flags.
284 * Allocate an ID between @min and %INT_MAX, inclusive.
286 * Context: Any context. It is safe to call this function without
287 * locking in your code.
288 * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
289 * or %-ENOSPC if there are no free IDs.
291 static inline int ida_alloc_min(struct ida
*ida
, unsigned int min
, gfp_t gfp
)
293 return ida_alloc_range(ida
, min
, ~0, gfp
);
297 * ida_alloc_max() - Allocate an unused ID.
299 * @max: Highest ID to allocate.
300 * @gfp: Memory allocation flags.
302 * Allocate an ID between 0 and @max, inclusive.
304 * Context: Any context. It is safe to call this function without
305 * locking in your code.
306 * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
307 * or %-ENOSPC if there are no free IDs.
309 static inline int ida_alloc_max(struct ida
*ida
, unsigned int max
, gfp_t gfp
)
311 return ida_alloc_range(ida
, 0, max
, gfp
);
314 static inline void ida_init(struct ida
*ida
)
316 xa_init_flags(&ida
->xa
, IDA_INIT_FLAGS
);
320 * ida_simple_get() and ida_simple_remove() are deprecated. Use
321 * ida_alloc() and ida_free() instead respectively.
323 #define ida_simple_get(ida, start, end, gfp) \
324 ida_alloc_range(ida, start, (end) - 1, gfp)
325 #define ida_simple_remove(ida, id) ida_free(ida, id)
327 static inline bool ida_is_empty(const struct ida
*ida
)
329 return xa_empty(&ida
->xa
);
331 #endif /* __IDR_H__ */