idset.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright IBM Corp. 2007, 2012
  3. * Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
  4. */
  5. #include <linux/vmalloc.h>
  6. #include <linux/bitmap.h>
  7. #include <linux/bitops.h>
  8. #include "idset.h"
  9. #include "css.h"
  10. struct idset {
  11. int num_ssid;
  12. int num_id;
  13. unsigned long bitmap[0];
  14. };
  15. static inline unsigned long bitmap_size(int num_ssid, int num_id)
  16. {
  17. return BITS_TO_LONGS(num_ssid * num_id) * sizeof(unsigned long);
  18. }
  19. static struct idset *idset_new(int num_ssid, int num_id)
  20. {
  21. struct idset *set;
  22. set = vmalloc(sizeof(struct idset) + bitmap_size(num_ssid, num_id));
  23. if (set) {
  24. set->num_ssid = num_ssid;
  25. set->num_id = num_id;
  26. memset(set->bitmap, 0, bitmap_size(num_ssid, num_id));
  27. }
  28. return set;
  29. }
  30. void idset_free(struct idset *set)
  31. {
  32. vfree(set);
  33. }
  34. void idset_fill(struct idset *set)
  35. {
  36. memset(set->bitmap, 0xff, bitmap_size(set->num_ssid, set->num_id));
  37. }
  38. static inline void idset_add(struct idset *set, int ssid, int id)
  39. {
  40. set_bit(ssid * set->num_id + id, set->bitmap);
  41. }
  42. static inline void idset_del(struct idset *set, int ssid, int id)
  43. {
  44. clear_bit(ssid * set->num_id + id, set->bitmap);
  45. }
  46. static inline int idset_contains(struct idset *set, int ssid, int id)
  47. {
  48. return test_bit(ssid * set->num_id + id, set->bitmap);
  49. }
  50. static inline int idset_get_first(struct idset *set, int *ssid, int *id)
  51. {
  52. int bitnum;
  53. bitnum = find_first_bit(set->bitmap, set->num_ssid * set->num_id);
  54. if (bitnum >= set->num_ssid * set->num_id)
  55. return 0;
  56. *ssid = bitnum / set->num_id;
  57. *id = bitnum % set->num_id;
  58. return 1;
  59. }
  60. struct idset *idset_sch_new(void)
  61. {
  62. return idset_new(max_ssid + 1, __MAX_SUBCHANNEL + 1);
  63. }
  64. void idset_sch_add(struct idset *set, struct subchannel_id schid)
  65. {
  66. idset_add(set, schid.ssid, schid.sch_no);
  67. }
  68. void idset_sch_del(struct idset *set, struct subchannel_id schid)
  69. {
  70. idset_del(set, schid.ssid, schid.sch_no);
  71. }
  72. /* Clear ids starting from @schid up to end of subchannel set. */
  73. void idset_sch_del_subseq(struct idset *set, struct subchannel_id schid)
  74. {
  75. int pos = schid.ssid * set->num_id + schid.sch_no;
  76. bitmap_clear(set->bitmap, pos, set->num_id - schid.sch_no);
  77. }
  78. int idset_sch_contains(struct idset *set, struct subchannel_id schid)
  79. {
  80. return idset_contains(set, schid.ssid, schid.sch_no);
  81. }
  82. int idset_is_empty(struct idset *set)
  83. {
  84. return bitmap_empty(set->bitmap, set->num_ssid * set->num_id);
  85. }
  86. void idset_add_set(struct idset *to, struct idset *from)
  87. {
  88. int len = min(to->num_ssid * to->num_id, from->num_ssid * from->num_id);
  89. bitmap_or(to->bitmap, to->bitmap, from->bitmap, len);
  90. }