2018-10-15 20:28:21 +00:00
|
|
|
.. SPDX-License-Identifier: GPL-2.0+
|
2018-02-06 20:05:49 +00:00
|
|
|
|
|
|
|
=============
|
|
|
|
ID Allocation
|
|
|
|
=============
|
|
|
|
|
|
|
|
:Author: Matthew Wilcox
|
|
|
|
|
|
|
|
Overview
|
|
|
|
========
|
|
|
|
|
|
|
|
A common problem to solve is allocating identifiers (IDs); generally
|
|
|
|
small numbers which identify a thing. Examples include file descriptors,
|
|
|
|
process IDs, packet identifiers in networking protocols, SCSI tags
|
|
|
|
and device instance numbers. The IDR and the IDA provide a reasonable
|
|
|
|
solution to the problem to avoid everybody inventing their own. The IDR
|
|
|
|
provides the ability to map an ID to a pointer, while the IDA provides
|
|
|
|
only ID allocation, and as a result is much more memory-efficient.
|
|
|
|
|
|
|
|
IDR usage
|
|
|
|
=========
|
|
|
|
|
2020-08-10 18:30:19 +00:00
|
|
|
Start by initialising an IDR, either with DEFINE_IDR()
|
|
|
|
for statically allocated IDRs or idr_init() for dynamically
|
2018-02-06 20:05:49 +00:00
|
|
|
allocated IDRs.
|
|
|
|
|
2020-08-10 18:30:19 +00:00
|
|
|
You can call idr_alloc() to allocate an unused ID. Look up
|
|
|
|
the pointer you associated with the ID by calling idr_find()
|
|
|
|
and free the ID by calling idr_remove().
|
2018-02-06 20:05:49 +00:00
|
|
|
|
|
|
|
If you need to change the pointer associated with an ID, you can call
|
2020-08-10 18:30:19 +00:00
|
|
|
idr_replace(). One common reason to do this is to reserve an
|
2018-02-06 20:05:49 +00:00
|
|
|
ID by passing a ``NULL`` pointer to the allocation function; initialise the
|
|
|
|
object with the reserved ID and finally insert the initialised object
|
|
|
|
into the IDR.
|
|
|
|
|
|
|
|
Some users need to allocate IDs larger than ``INT_MAX``. So far all of
|
|
|
|
these users have been content with a ``UINT_MAX`` limit, and they use
|
2020-08-10 18:30:19 +00:00
|
|
|
idr_alloc_u32(). If you need IDs that will not fit in a u32,
|
2018-02-06 20:05:49 +00:00
|
|
|
we will work with you to address your needs.
|
|
|
|
|
|
|
|
If you need to allocate IDs sequentially, you can use
|
2020-08-10 18:30:19 +00:00
|
|
|
idr_alloc_cyclic(). The IDR becomes less efficient when dealing
|
2018-02-06 20:05:49 +00:00
|
|
|
with larger IDs, so using this function comes at a slight cost.
|
|
|
|
|
|
|
|
To perform an action on all pointers used by the IDR, you can
|
2020-08-10 18:30:19 +00:00
|
|
|
either use the callback-based idr_for_each() or the
|
|
|
|
iterator-style idr_for_each_entry(). You may need to use
|
|
|
|
idr_for_each_entry_continue() to continue an iteration. You can
|
|
|
|
also use idr_get_next() if the iterator doesn't fit your needs.
|
2018-02-06 20:05:49 +00:00
|
|
|
|
2020-08-10 18:30:19 +00:00
|
|
|
When you have finished using an IDR, you can call idr_destroy()
|
2018-02-06 20:05:49 +00:00
|
|
|
to release the memory used by the IDR. This will not free the objects
|
|
|
|
pointed to from the IDR; if you want to do that, use one of the iterators
|
|
|
|
to do it.
|
|
|
|
|
2020-08-10 18:30:19 +00:00
|
|
|
You can use idr_is_empty() to find out whether there are any
|
2018-02-06 20:05:49 +00:00
|
|
|
IDs currently allocated.
|
|
|
|
|
|
|
|
If you need to take a lock while allocating a new ID from the IDR,
|
|
|
|
you may need to pass a restrictive set of GFP flags, which can lead
|
|
|
|
to the IDR being unable to allocate memory. To work around this,
|
2020-08-10 18:30:19 +00:00
|
|
|
you can call idr_preload() before taking the lock, and then
|
|
|
|
idr_preload_end() after the allocation.
|
2018-02-06 20:05:49 +00:00
|
|
|
|
|
|
|
.. kernel-doc:: include/linux/idr.h
|
|
|
|
:doc: idr sync
|
|
|
|
|
|
|
|
IDA usage
|
|
|
|
=========
|
|
|
|
|
|
|
|
.. kernel-doc:: lib/idr.c
|
|
|
|
:doc: IDA description
|
|
|
|
|
|
|
|
Functions and structures
|
|
|
|
========================
|
|
|
|
|
|
|
|
.. kernel-doc:: include/linux/idr.h
|
2018-06-29 21:05:11 +00:00
|
|
|
:functions:
|
2018-02-06 20:05:49 +00:00
|
|
|
.. kernel-doc:: lib/idr.c
|
2018-06-29 21:05:11 +00:00
|
|
|
:functions:
|