bit-radix.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <linux/module.h>
  19. #include "bit-radix.h"
  20. #define BIT_ARRAY_BYTES 256
  21. #define BIT_RADIX_BITS_PER_ARRAY ((BIT_ARRAY_BYTES - sizeof(unsigned long)) * 8)
  22. extern struct kmem_cache *btrfs_bit_radix_cachep;
  23. int set_radix_bit(struct radix_tree_root *radix, unsigned long bit)
  24. {
  25. unsigned long *bits;
  26. unsigned long slot;
  27. int bit_slot;
  28. int ret;
  29. slot = bit / BIT_RADIX_BITS_PER_ARRAY;
  30. bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY;
  31. bits = radix_tree_lookup(radix, slot);
  32. if (!bits) {
  33. bits = kmem_cache_alloc(btrfs_bit_radix_cachep, GFP_NOFS);
  34. if (!bits)
  35. return -ENOMEM;
  36. memset(bits + 1, 0, BIT_ARRAY_BYTES - sizeof(unsigned long));
  37. bits[0] = slot;
  38. ret = radix_tree_insert(radix, slot, bits);
  39. if (ret)
  40. return ret;
  41. }
  42. ret = test_and_set_bit(bit_slot, bits + 1);
  43. if (ret < 0)
  44. ret = 1;
  45. return ret;
  46. }
  47. int test_radix_bit(struct radix_tree_root *radix, unsigned long bit)
  48. {
  49. unsigned long *bits;
  50. unsigned long slot;
  51. int bit_slot;
  52. slot = bit / BIT_RADIX_BITS_PER_ARRAY;
  53. bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY;
  54. bits = radix_tree_lookup(radix, slot);
  55. if (!bits)
  56. return 0;
  57. return test_bit(bit_slot, bits + 1);
  58. }
  59. int clear_radix_bit(struct radix_tree_root *radix, unsigned long bit)
  60. {
  61. unsigned long *bits;
  62. unsigned long slot;
  63. int bit_slot;
  64. int i;
  65. int empty = 1;
  66. slot = bit / BIT_RADIX_BITS_PER_ARRAY;
  67. bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY;
  68. bits = radix_tree_lookup(radix, slot);
  69. if (!bits)
  70. return 0;
  71. clear_bit(bit_slot, bits + 1);
  72. for (i = 1; i < BIT_ARRAY_BYTES / sizeof(unsigned long); i++) {
  73. if (bits[i]) {
  74. empty = 0;
  75. break;
  76. }
  77. }
  78. if (empty) {
  79. bits = radix_tree_delete(radix, slot);
  80. BUG_ON(!bits);
  81. kmem_cache_free(btrfs_bit_radix_cachep, bits);
  82. }
  83. return 0;
  84. }
  85. int find_first_radix_bit(struct radix_tree_root *radix, unsigned long *retbits,
  86. unsigned long start, int nr)
  87. {
  88. unsigned long *bits;
  89. unsigned long *gang[4];
  90. int found;
  91. int ret;
  92. int i;
  93. int total_found = 0;
  94. unsigned long slot;
  95. slot = start / BIT_RADIX_BITS_PER_ARRAY;
  96. ret = radix_tree_gang_lookup(radix, (void **)gang, slot,
  97. ARRAY_SIZE(gang));
  98. found = start % BIT_RADIX_BITS_PER_ARRAY;
  99. for (i = 0; i < ret && nr > 0; i++) {
  100. bits = gang[i];
  101. while(nr > 0) {
  102. found = find_next_bit(bits + 1,
  103. BIT_RADIX_BITS_PER_ARRAY,
  104. found);
  105. if (found < BIT_RADIX_BITS_PER_ARRAY) {
  106. *retbits = bits[0] *
  107. BIT_RADIX_BITS_PER_ARRAY + found;
  108. retbits++;
  109. nr--;
  110. total_found++;
  111. found++;
  112. } else
  113. break;
  114. }
  115. found = 0;
  116. }
  117. return total_found;
  118. }