idr_alloc_cyclic(9)
π idr_alloc_cyclic β Kernel API Manual, bestie edition π
okay so this is idr_alloc_cyclic(9) from the Kernel API Manual (./lib), and it's literally the moment your IDR gets a fresh ID dropped in its lap, round-robin style. Let's get into it π
β¨ NAME β¨
idr_alloc_cyclic β allocate an ID cyclically. That's it, that's the vibe π¦
πΈ SYNOPSIS πΈ
int idr_alloc_cyclic (struct idr *idr , void *ptr , int start , int end
, gfp_t gfp );
copy it exactly, bestie, no cap π
π ARGUMENTS π
idrβ your IDR handle. the main character of this whole operation πptrβ the pointer you want associated with the shiny new ID. it's giving "plus one" energy π«Άstartβ the minimum ID, and yes it's inclusive βstartcan absolutely get picked β¨endβ the maximum ID, exclusive β soenditself is not invited to the party πgfpβ memory allocation flags. the boring-but-essential bit, we respect it π
π¦ DESCRIPTION π¦
So what it does: it allocates an unused ID inside the range specified by start and end. Simple. Iconic. π
Now here's the quirky bit β if end is <= 0, it gets treated as one larger than INT_MAX. Which means callers can flex with start + N as their end, as long as N is within integer range. iykyk π
And the search? It does not start from scratch every time, because that would be lowkey wasteful. It starts at the last ID allocated, and wraps around to start if no free IDs turn up before reaching end. cyclic behaviour, hence the name, we been knew π₯
β οΈ okay real talk for a sec β the caller should provide their own locking to make sure two concurrent modifications to the IDR are not possible. no free rides, bestie. as for read-only accesses to the IDR, those may be done under the RCU read lock, or may exclude simultaneous writers. pick your lane π
β RETURN β
- the newly allocated ID β slay, congrats π
-ENOMEMβ if memory allocation failed π-ENOSPCβ if no free IDs could be found π
π SEE ALSO π
Kernel file ./lib/idr.c, plus the whole friend group:
idr_alloc_u32(9)idr_alloc(9)idr_remove(9)idr_find(9)idr_for_each(9)idr_get_next_ul(9)idr_get_next(9)idr_replace(9)IDA description(9)ida_alloc_range(9)ida_find_first_range(9)ida_free(9)ida_destroy(9)
and that's the whole page, bestie π§ from the ./lib source in the Kernel API Manual, dated October 2026 β stay cyclical, stay locked, mwah ππ
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms