idr_alloc_cyclic(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… 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 β€” start can absolutely get picked ✨
  • end β€” the maximum ID, exclusive β€” so end itself 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:


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