bit-radix.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include "bit-radix.h"
  19. #define BIT_ARRAY_BYTES 256
  20. #define BIT_RADIX_BITS_PER_ARRAY ((BIT_ARRAY_BYTES - sizeof(unsigned long)) * 8)
  21. extern struct kmem_cache *btrfs_bit_radix_cachep;
  22. int set_radix_bit(struct radix_tree_root *radix, unsigned long bit)
  23. {
  24. unsigned long *bits;
  25. unsigned long slot;
  26. int bit_slot;
  27. int ret;
  28. slot = bit / BIT_RADIX_BITS_PER_ARRAY;
  29. bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY;
  30. bits = radix_tree_lookup(radix, slot);
  31. if (!bits) {
  32. bits = kmem_cache_alloc(btrfs_bit_radix_cachep, GFP_NOFS);
  33. if (!bits)
  34. return -ENOMEM;
  35. memset(bits + 1, 0, BIT_ARRAY_BYTES - sizeof(unsigned long));
  36. bits[0] = slot;
  37. ret = radix_tree_insert(radix, slot, bits);
  38. if (ret)
  39. return ret;
  40. }
  41. ret = test_and_set_bit(bit_slot, bits + 1);
  42. if (ret < 0)
  43. ret = 1;
  44. return ret;
  45. }
  46. int test_radix_bit(struct radix_tree_root *radix, unsigned long bit)
  47. {
  48. unsigned long *bits;
  49. unsigned long slot;
  50. int bit_slot;
  51. slot = bit / BIT_RADIX_BITS_PER_ARRAY;
  52. bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY;
  53. bits = radix_tree_lookup(radix, slot);
  54. if (!bits)
  55. return 0;
  56. return test_bit(bit_slot, bits + 1);
  57. }
  58. int clear_radix_bit(struct radix_tree_root *radix, unsigned long bit)
  59. {
  60. unsigned long *bits;
  61. unsigned long slot;
  62. int bit_slot;
  63. int i;
  64. int empty = 1;
  65. slot = bit / BIT_RADIX_BITS_PER_ARRAY;
  66. bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY;
  67. bits = radix_tree_lookup(radix, slot);
  68. if (!bits)
  69. return 0;
  70. clear_bit(bit_slot, bits + 1);
  71. for (i = 1; i < BIT_ARRAY_BYTES / sizeof(unsigned long); i++) {
  72. if (bits[i]) {
  73. empty = 0;
  74. break;
  75. }
  76. }
  77. if (empty) {
  78. bits = radix_tree_delete(radix, slot);
  79. BUG_ON(!bits);
  80. kmem_cache_free(btrfs_bit_radix_cachep, bits);
  81. }
  82. return 0;
  83. }
  84. int find_first_radix_bit(struct radix_tree_root *radix, unsigned long *retbits,
  85. unsigned long start, int nr)
  86. {
  87. unsigned long *bits;
  88. unsigned long *gang[4];
  89. int found;
  90. int ret;
  91. int i;
  92. int total_found = 0;
  93. unsigned long slot;
  94. slot = start / BIT_RADIX_BITS_PER_ARRAY;
  95. ret = radix_tree_gang_lookup(radix, (void **)gang, slot,
  96. ARRAY_SIZE(gang));
  97. found = start % BIT_RADIX_BITS_PER_ARRAY;
  98. for (i = 0; i < ret && nr > 0; i++) {
  99. bits = gang[i];
  100. while(nr > 0) {
  101. found = find_next_bit(bits + 1,
  102. BIT_RADIX_BITS_PER_ARRAY,
  103. found);
  104. if (found < BIT_RADIX_BITS_PER_ARRAY) {
  105. *retbits = bits[0] *
  106. BIT_RADIX_BITS_PER_ARRAY + found;
  107. retbits++;
  108. nr--;
  109. total_found++;
  110. found++;
  111. } else
  112. break;
  113. }
  114. found = 0;
  115. }
  116. return total_found;
  117. }