blob: 18d724867064bc885e06f87d3fe915a80b4b901d [file] [log] [blame]
Matthew Wilcoxa309d5d2018-10-15 16:28:21 -04001.. SPDX-License-Identifier: GPL-2.0+
Matthew Wilcoxac665d92018-02-06 15:05:49 -05002
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
Matthew Wilcox (Oracle)85656ec2022-07-06 17:39:21 -040020The IDR interface is deprecated; please use the :doc:`XArray <xarray>`
21instead.
22
Matthew Wilcoxac665d92018-02-06 15:05:49 -050023IDR usage
24=========
25
Puranjay Mohanec8213f2020-08-11 00:00:19 +053026Start by initialising an IDR, either with DEFINE_IDR()
27for statically allocated IDRs or idr_init() for dynamically
Matthew Wilcoxac665d92018-02-06 15:05:49 -050028allocated IDRs.
29
Puranjay Mohanec8213f2020-08-11 00:00:19 +053030You can call idr_alloc() to allocate an unused ID. Look up
31the pointer you associated with the ID by calling idr_find()
32and free the ID by calling idr_remove().
Matthew Wilcoxac665d92018-02-06 15:05:49 -050033
34If you need to change the pointer associated with an ID, you can call
Puranjay Mohanec8213f2020-08-11 00:00:19 +053035idr_replace(). One common reason to do this is to reserve an
Matthew Wilcoxac665d92018-02-06 15:05:49 -050036ID by passing a ``NULL`` pointer to the allocation function; initialise the
37object with the reserved ID and finally insert the initialised object
38into the IDR.
39
40Some users need to allocate IDs larger than ``INT_MAX``. So far all of
41these users have been content with a ``UINT_MAX`` limit, and they use
Puranjay Mohanec8213f2020-08-11 00:00:19 +053042idr_alloc_u32(). If you need IDs that will not fit in a u32,
Matthew Wilcoxac665d92018-02-06 15:05:49 -050043we will work with you to address your needs.
44
45If you need to allocate IDs sequentially, you can use
Puranjay Mohanec8213f2020-08-11 00:00:19 +053046idr_alloc_cyclic(). The IDR becomes less efficient when dealing
Matthew Wilcoxac665d92018-02-06 15:05:49 -050047with larger IDs, so using this function comes at a slight cost.
48
49To perform an action on all pointers used by the IDR, you can
Puranjay Mohanec8213f2020-08-11 00:00:19 +053050either use the callback-based idr_for_each() or the
51iterator-style idr_for_each_entry(). You may need to use
52idr_for_each_entry_continue() to continue an iteration. You can
53also use idr_get_next() if the iterator doesn't fit your needs.
Matthew Wilcoxac665d92018-02-06 15:05:49 -050054
Puranjay Mohanec8213f2020-08-11 00:00:19 +053055When you have finished using an IDR, you can call idr_destroy()
Matthew Wilcoxac665d92018-02-06 15:05:49 -050056to release the memory used by the IDR. This will not free the objects
57pointed to from the IDR; if you want to do that, use one of the iterators
58to do it.
59
Puranjay Mohanec8213f2020-08-11 00:00:19 +053060You can use idr_is_empty() to find out whether there are any
Matthew Wilcoxac665d92018-02-06 15:05:49 -050061IDs currently allocated.
62
63If you need to take a lock while allocating a new ID from the IDR,
64you may need to pass a restrictive set of GFP flags, which can lead
65to the IDR being unable to allocate memory. To work around this,
Puranjay Mohanec8213f2020-08-11 00:00:19 +053066you can call idr_preload() before taking the lock, and then
67idr_preload_end() after the allocation.
Matthew Wilcoxac665d92018-02-06 15:05:49 -050068
69.. kernel-doc:: include/linux/idr.h
70 :doc: idr sync
71
72IDA usage
73=========
74
75.. kernel-doc:: lib/idr.c
76 :doc: IDA description
77
78Functions and structures
79========================
80
81.. kernel-doc:: include/linux/idr.h
Mike Rapoport51057302018-06-30 00:05:11 +030082 :functions:
Matthew Wilcoxac665d92018-02-06 15:05:49 -050083.. kernel-doc:: lib/idr.c
Mike Rapoport51057302018-06-30 00:05:11 +030084 :functions: