]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - Documentation/core-api/idr.rst
Merge tag 'hyperv-fixes-signed' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-hirsute-kernel.git] / Documentation / core-api / idr.rst
CommitLineData
a309d5db 1.. SPDX-License-Identifier: GPL-2.0+
ac665d94
MW
2
3=============
4ID Allocation
5=============
6
7:Author: Matthew Wilcox
8
9Overview
10========
11
12A common problem to solve is allocating identifiers (IDs); generally
13small numbers which identify a thing. Examples include file descriptors,
14process IDs, packet identifiers in networking protocols, SCSI tags
15and device instance numbers. The IDR and the IDA provide a reasonable
16solution to the problem to avoid everybody inventing their own. The IDR
17provides the ability to map an ID to a pointer, while the IDA provides
18only ID allocation, and as a result is much more memory-efficient.
19
20IDR usage
21=========
22
ec8213f8
PM
23Start by initialising an IDR, either with DEFINE_IDR()
24for statically allocated IDRs or idr_init() for dynamically
ac665d94
MW
25allocated IDRs.
26
ec8213f8
PM
27You can call idr_alloc() to allocate an unused ID. Look up
28the pointer you associated with the ID by calling idr_find()
29and free the ID by calling idr_remove().
ac665d94
MW
30
31If you need to change the pointer associated with an ID, you can call
ec8213f8 32idr_replace(). One common reason to do this is to reserve an
ac665d94
MW
33ID by passing a ``NULL`` pointer to the allocation function; initialise the
34object with the reserved ID and finally insert the initialised object
35into the IDR.
36
37Some users need to allocate IDs larger than ``INT_MAX``. So far all of
38these users have been content with a ``UINT_MAX`` limit, and they use
ec8213f8 39idr_alloc_u32(). If you need IDs that will not fit in a u32,
ac665d94
MW
40we will work with you to address your needs.
41
42If you need to allocate IDs sequentially, you can use
ec8213f8 43idr_alloc_cyclic(). The IDR becomes less efficient when dealing
ac665d94
MW
44with larger IDs, so using this function comes at a slight cost.
45
46To perform an action on all pointers used by the IDR, you can
ec8213f8
PM
47either use the callback-based idr_for_each() or the
48iterator-style idr_for_each_entry(). You may need to use
49idr_for_each_entry_continue() to continue an iteration. You can
50also use idr_get_next() if the iterator doesn't fit your needs.
ac665d94 51
ec8213f8 52When you have finished using an IDR, you can call idr_destroy()
ac665d94
MW
53to release the memory used by the IDR. This will not free the objects
54pointed to from the IDR; if you want to do that, use one of the iterators
55to do it.
56
ec8213f8 57You can use idr_is_empty() to find out whether there are any
ac665d94
MW
58IDs currently allocated.
59
60If you need to take a lock while allocating a new ID from the IDR,
61you may need to pass a restrictive set of GFP flags, which can lead
62to the IDR being unable to allocate memory. To work around this,
ec8213f8
PM
63you can call idr_preload() before taking the lock, and then
64idr_preload_end() after the allocation.
ac665d94
MW
65
66.. kernel-doc:: include/linux/idr.h
67 :doc: idr sync
68
69IDA usage
70=========
71
72.. kernel-doc:: lib/idr.c
73 :doc: IDA description
74
75Functions and structures
76========================
77
78.. kernel-doc:: include/linux/idr.h
5105730f 79 :functions:
ac665d94 80.. kernel-doc:: lib/idr.c
5105730f 81 :functions: