idr.rst 2.9 KB

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