idr.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <common/errno.h>
  2. #include <common/spinlock.h>
  3. /**
  4. * idr: 基于radix-tree的ID-pointer的数据结构
  5. * 主要功能:
  6. * 1. 获取一个ID, 并且将该ID与一个指针绑定 - 需要外部加锁
  7. * 2. 删除一个已分配的ID - 需要外部加锁
  8. * 3. 根据ID查找对应的指针 (读操作,看情况加锁)
  9. * 4. 根据ID使用新的ptr替换旧的ptr - 需要外部加锁
  10. *
  11. * 附加功能:
  12. * 1. 给定starting_id, 查询下一个已分配的next_id (即:next_id>starting_id)
  13. * 2. 销毁整个idr
  14. *
  15. *
  16. * .... 待实现
  17. */
  18. // 默认64位机器
  19. #define IDR_BITS 6
  20. #define IDR_FULL 0xfffffffffffffffful
  21. // size = 64
  22. #define IDR_SIZE (1 << IDR_BITS)
  23. #define IDR_MASK ((1 << IDR_BITS) - 1)
  24. // 能管理的ID范围[0:1<<31]
  25. #define MAX_ID_SHIFT (sizeof(int) * 8 - 1)
  26. #define MAX_ID_BIT (1U << MAX_ID_SHIFT)
  27. #define MAX_ID_MASK (MAX_ID_BIT - 1)
  28. // IDR可能最大的层次 以及 IDR预分配空间的最大限制
  29. #define MAX_LEVEL (MAX_ID_SHIFT + IDR_BITS - 1) / IDR_BITS
  30. #define IDR_FREE_MAX (MAX_LEVEL << 1)
  31. // 给定layer, 计算完全64叉树的大小
  32. #define TREE_SIZE(layer) ((layer >= 0) ? (1ull << ((layer + 1) * IDR_BITS)) : 1)
  33. // 计算最后(最低位)一个1的位置 (注意使用64位的版本)
  34. #define __lowbit_id(x) ((x) ? (__builtin_ctzll(x)) : -1)
  35. // 计算最前(最高位)一个1的位置 (注意使用64位的版本)
  36. #define __mostbit_id(x) ((x) ? (__builtin_clzll(x)) : -1)
  37. // radix-tree 节点定义
  38. struct idr_layer
  39. {
  40. struct idr_layer *ary[IDR_SIZE]; // IDR_SIZE叉树
  41. unsigned long bitmap; // 每一位表示这个子树是否被使用
  42. unsigned long full; // 64个儿子子树, 每一位代表一个子树是否满了
  43. int layer; // 层数(从底向上)
  44. };
  45. // idr: 将id与pointer绑定的数据结构
  46. struct idr
  47. {
  48. struct idr_layer *top;
  49. struct idr_layer *free_list;
  50. int id_free_cnt;
  51. spinlock_t lock;
  52. };
  53. #define DECLARE_IDR(name) \
  54. struct idr name = {0}; \
  55. name.top = (NULL); \
  56. name.free_list = (NULL); \
  57. name.id_free_cnt = (0); \
  58. spin_init(&name.lock);
  59. #define DECLARE_IDR_LAYER(name) \
  60. struct idr_layer name = {0}; \
  61. memset(name, 0, sizeof(struct idr_layer));
  62. /**
  63. * 对外函数声明
  64. **/
  65. int idr_pre_get(struct idr *idp, gfp_t gfp_mask);
  66. int idr_get_new(struct idr *idp, void *ptr, int *id);
  67. void idr_remove(struct idr *idp, int id);
  68. void idr_remove_all(struct idr *idp);
  69. void idr_destroy(struct idr *idp);
  70. void *idr_find(struct idr *idp, int id);
  71. void *idr_find_next(struct idr *idp, int start_id);
  72. void *idr_find_next_getid(struct idr *idp, int start_id, int *nextid);
  73. int idr_replace_get_old(struct idr *idp, void *ptr, int id, void **oldptr);
  74. int idr_replace(struct idr *idp, void *ptr, int id);
  75. void idr_init(struct idr *idp);
  76. /**
  77. * ida: 基于IDR实现的ID分配器
  78. * 主要功能:
  79. * 1. 获取一个未分配的ID
  80. * 2. 询问一个ID是否被分配
  81. * 3. 删除一个已分配ID
  82. *
  83. * 附加功能:
  84. * 1. 暂定
  85. */
  86. // 一个块的大小 - 即 sizeof(struct ida_bitmap)
  87. #define IDA_CHUNK_SIZE 128
  88. // ida_bitmap的长度
  89. #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long) - 1)
  90. // 对应linux的IDA_BITMAP_BITS = 960 = 15 * 64
  91. #define IDA_FULL (IDA_BITMAP_LONGS * sizeof(long) * 8)
  92. #define IDA_BITMAP_BITS IDA_FULL
  93. #define IDA_BMP_SIZE (8 * sizeof(long))
  94. // 自定义bitmap
  95. struct ida_bitmap
  96. {
  97. unsigned long count; // bitmap中已经分配的id数量
  98. unsigned long bitmap[IDA_BITMAP_LONGS]; // bitmap本身, 每一个bit代表一个ID
  99. };
  100. // id-allocater 管理+分配ID的数据结构
  101. struct ida
  102. {
  103. struct idr idr;
  104. struct ida_bitmap *free_list; // 预分配的数据块
  105. };
  106. #define DECLARE_IDA(name) \
  107. struct ida name = {0}; \
  108. idr_init(&name.idr); \
  109. name.free_list = (NULL);
  110. /**
  111. * 对外函数声明
  112. */
  113. void ida_init(struct ida *ida_p);
  114. int ida_pre_get(struct ida *ida_p, gfp_t gfp_mask);
  115. int ida_get_new(struct ida *ida_p, int *p_id);
  116. bool ida_count(struct ida *ida_p, int id);
  117. void ida_remove(struct ida *ida_p, int id);
  118. void ida_destroy(struct ida *ida_p);