idr_alloc_u32(9)
π
idr_alloc_u32 β Kernel API Manual (yes, that's the idr_alloc_u32(9) girlies)
okay so bestie, welcome to the idr_alloc_u32(9) glow-up era β¨ this is the kernel API manual page, and it's literally about handing out IDs. let's get into it π
β¨ NAME β¨
idr_alloc_u32 β Allocate an ID. that's it. that's the whole vibe π
πΈ SYNOPSIS πΈ
int idr_alloc_u32 (struct idr *idr , void *ptr , u32 *nextid , unsigned
long max , gfp_t gfp );
copy it exactly, bestie, no cap π«Ά
π¦ ARGUMENTS π¦
idrβ the IDR handle. your main character. πptrβ the pointer that's gonna get associated with the shiny new ID πnextidβ a pointer to an ID. yes, a pointer to it. we'll get into why and it's iconic.maxβ the maximum ID to allocate, and it is inclusive π₯gfpβ memory allocation flags. the boring-but-necessary part π
π DESCRIPTION π
So what it does: it allocates an unused ID in the range specified by nextid and max. simple. slay. π
β οΈ but listen βmaxis inclusive, whereas theendparameter toidr_allocis exclusive. ik, ik, different energy. don't get it twisted fr fr.
the new ID is assigned to nextid before the pointer is inserted into the IDR. and the way that actually matters? if nextid points into the object pointed to by ptr, then a concurrent lookup will not find an uninitialised ID. that's the whole reason it's built like that, and honestly? genius. π
π the caller should provide their own locking to make sure two concurrent modifications to the IDR aren't possible. yes, you, bestie β you're on lock duty. no cap.
read-only accesses to the IDR may be done under the RCU read lock, or may exclude simultaneous writers. either way, pick one, don't be messy π§
β RETURN β
0β an ID was allocated. we ate. π-ENOMEMβ memory allocation failed π-ENOSPCβ no free IDs could be found π
π«Ά and if an error occurred, nextid is unchanged. no random half-assigned IDs, we respect the boundary.
π SEE ALSO π
Kernel file ./lib/idr.c β and the whole friend group:
idr_alloc(9)idr_alloc_cyclic(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)
mwah π this little queen lives in ./lib, dated October 2026, and that's the sign-off bestie β go allocate some IDs responsibly πβ¨
π¦ 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