idr.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * include/linux/idr.h
  3. *
  4. * 2002-10-18 written by Jim Houston jim.houston@ccur.com
  5. * Copyright (C) 2002 by Concurrent Computer Corporation
  6. * Distributed under the GNU GPL license version 2.
  7. *
  8. * Small id to pointer translation service avoiding fixed sized
  9. * tables.
  10. */
  11. #ifndef __IDR_H__
  12. #define __IDR_H__
  13. #include <linux/types.h>
  14. #include <linux/bitops.h>
  15. #include <linux/init.h>
  16. #include <linux/rcupdate.h>
  17. #if BITS_PER_LONG == 32
  18. # define IDR_BITS 5
  19. # define IDR_FULL 0xfffffffful
  20. /* We can only use two of the bits in the top level because there is
  21. only one possible bit in the top level (5 bits * 7 levels = 35
  22. bits, but you only use 31 bits in the id). */
  23. # define TOP_LEVEL_FULL (IDR_FULL >> 30)
  24. #elif BITS_PER_LONG == 64
  25. # define IDR_BITS 6
  26. # define IDR_FULL 0xfffffffffffffffful
  27. /* We can only use two of the bits in the top level because there is
  28. only one possible bit in the top level (6 bits * 6 levels = 36
  29. bits, but you only use 31 bits in the id). */
  30. # define TOP_LEVEL_FULL (IDR_FULL >> 62)
  31. #else
  32. # error "BITS_PER_LONG is not 32 or 64"
  33. #endif
  34. #define IDR_SIZE (1 << IDR_BITS)
  35. #define IDR_MASK ((1 << IDR_BITS)-1)
  36. #define MAX_ID_SHIFT (sizeof(int)*8 - 1)
  37. #define MAX_ID_BIT (1U << MAX_ID_SHIFT)
  38. #define MAX_ID_MASK (MAX_ID_BIT - 1)
  39. /* Leave the possibility of an incomplete final layer */
  40. #define MAX_LEVEL (MAX_ID_SHIFT + IDR_BITS - 1) / IDR_BITS
  41. /* Number of id_layer structs to leave in free list */
  42. #define IDR_FREE_MAX MAX_LEVEL + MAX_LEVEL
  43. struct idr_layer {
  44. unsigned long bitmap; /* A zero bit means "space here" */
  45. struct idr_layer *ary[1<<IDR_BITS];
  46. int count; /* When zero, we can release it */
  47. struct rcu_head rcu_head;
  48. };
  49. struct idr {
  50. struct idr_layer *top;
  51. struct idr_layer *id_free;
  52. int layers;
  53. int id_free_cnt;
  54. spinlock_t lock;
  55. };
  56. #define IDR_INIT(name) \
  57. { \
  58. .top = NULL, \
  59. .id_free = NULL, \
  60. .layers = 0, \
  61. .id_free_cnt = 0, \
  62. .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
  63. }
  64. #define DEFINE_IDR(name) struct idr name = IDR_INIT(name)
  65. /* Actions to be taken after a call to _idr_sub_alloc */
  66. #define IDR_NEED_TO_GROW -2
  67. #define IDR_NOMORE_SPACE -3
  68. #define _idr_rc_to_errno(rc) ((rc) == -1 ? -EAGAIN : -ENOSPC)
  69. /*
  70. * This is what we export.
  71. */
  72. void *idr_find(struct idr *idp, int id);
  73. int idr_pre_get(struct idr *idp, gfp_t gfp_mask);
  74. int idr_get_new(struct idr *idp, void *ptr, int *id);
  75. int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id);
  76. int idr_for_each(struct idr *idp,
  77. int (*fn)(int id, void *p, void *data), void *data);
  78. void *idr_replace(struct idr *idp, void *ptr, int id);
  79. void idr_remove(struct idr *idp, int id);
  80. void idr_remove_all(struct idr *idp);
  81. void idr_destroy(struct idr *idp);
  82. void idr_init(struct idr *idp);
  83. /*
  84. * IDA - IDR based id allocator, use when translation from id to
  85. * pointer isn't necessary.
  86. */
  87. #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
  88. #define IDA_BITMAP_LONGS (128 / sizeof(long) - 1)
  89. #define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
  90. struct ida_bitmap {
  91. long nr_busy;
  92. unsigned long bitmap[IDA_BITMAP_LONGS];
  93. };
  94. struct ida {
  95. struct idr idr;
  96. struct ida_bitmap *free_bitmap;
  97. };
  98. #define IDA_INIT(name) { .idr = IDR_INIT(name), .free_bitmap = NULL, }
  99. #define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
  100. int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
  101. int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
  102. int ida_get_new(struct ida *ida, int *p_id);
  103. void ida_remove(struct ida *ida, int id);
  104. void ida_destroy(struct ida *ida);
  105. void ida_init(struct ida *ida);
  106. void __init idr_init_cache(void);
  107. #endif /* __IDR_H__ */