From: Matthew Wilcox <mawil...@microsoft.com> All current users of idr_alloc_ext() actually want to allocate a u32 and it's a little painful for them to use idr_alloc_ext(). This convenience function makes it simple.
Signed-off-by: Matthew Wilcox <mawil...@microsoft.com> --- include/linux/idr.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/linux/idr.h b/include/linux/idr.h index 12514ec0cd28..9b2fd6f408b2 100644 --- a/include/linux/idr.h +++ b/include/linux/idr.h @@ -139,6 +139,35 @@ void *idr_get_next_ext(struct idr *idr, unsigned long *nextid); void *idr_replace(struct idr *, void *, unsigned long id); void idr_destroy(struct idr *); +/** + * idr_alloc_u32() - Allocate an ID. + * @idr: IDR handle. + * @ptr: Pointer to be associated with the new ID. + * @nextid: The new ID. + * @max: The maximum ID to allocate (inclusive). + * @gfp: Memory allocation flags. + * + * Allocates an unused ID in the range [*nextid, max] and updates the @nextid + * pointer with the newly assigned ID. Returns -ENOSPC and does not modify + * @nextid if there are no unused IDs in the range. + * + * The caller should provide their own locking to ensure that two concurrent + * modifications to the IDR are not possible. Read-only accesses to the + * IDR may be done under the RCU read lock or may exclude simultaneous + * writers. + * + * Return: 0 on success, -ENOMEM for memory allocation errors, -ENOSPC if + * there are no free IDs in the range. + */ +static inline int __must_check idr_alloc_u32(struct idr *idr, void *ptr, + u32 *nextid, unsigned long max, gfp_t gfp) +{ + unsigned long tmp = *nextid; + int ret = idr_alloc_ext(idr, ptr, &tmp, tmp, max + 1, gfp); + *nextid = tmp; + return ret; +} + static inline void *idr_remove(struct idr *idr, unsigned long id) { return radix_tree_delete_item(&idr->idr_rt, id, NULL); -- 2.15.0