bitext.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * bitext.c: kernel little helper (of bit shuffling variety).
  3. *
  4. * Copyright (C) 2002 Pete Zaitcev <zaitcev@yahoo.com>
  5. *
  6. * The algorithm to search a zero bit string is geared towards its application.
  7. * We expect a couple of fixed sizes of requests, so a rotating counter, reset
  8. * by align size, should provide fast enough search while maintaining low
  9. * fragmentation.
  10. */
  11. #include <linux/string.h>
  12. #include <linux/bitmap.h>
  13. #include <asm/bitext.h>
  14. /**
  15. * bit_map_string_get - find and set a bit string in bit map.
  16. * @t: the bit map.
  17. * @len: requested string length
  18. * @align: requested alignment
  19. *
  20. * Returns offset in the map or -1 if out of space.
  21. *
  22. * Not safe to call from an interrupt (uses spin_lock).
  23. */
  24. int bit_map_string_get(struct bit_map *t, int len, int align)
  25. {
  26. int offset, count; /* siamese twins */
  27. int off_new;
  28. int align1;
  29. int i, color;
  30. if (t->num_colors) {
  31. /* align is overloaded to be the page color */
  32. color = align;
  33. align = t->num_colors;
  34. } else {
  35. color = 0;
  36. if (align == 0)
  37. align = 1;
  38. }
  39. align1 = align - 1;
  40. if ((align & align1) != 0)
  41. BUG();
  42. if (align < 0 || align >= t->size)
  43. BUG();
  44. if (len <= 0 || len > t->size)
  45. BUG();
  46. color &= align1;
  47. spin_lock(&t->lock);
  48. if (len < t->last_size)
  49. offset = t->first_free;
  50. else
  51. offset = t->last_off & ~align1;
  52. count = 0;
  53. for (;;) {
  54. off_new = find_next_zero_bit(t->map, t->size, offset);
  55. off_new = ((off_new + align1) & ~align1) + color;
  56. count += off_new - offset;
  57. offset = off_new;
  58. if (offset >= t->size)
  59. offset = 0;
  60. if (count + len > t->size) {
  61. spin_unlock(&t->lock);
  62. /* P3 */ printk(KERN_ERR
  63. "bitmap out: size %d used %d off %d len %d align %d count %d\n",
  64. t->size, t->used, offset, len, align, count);
  65. return -1;
  66. }
  67. if (offset + len > t->size) {
  68. count += t->size - offset;
  69. offset = 0;
  70. continue;
  71. }
  72. i = 0;
  73. while (test_bit(offset + i, t->map) == 0) {
  74. i++;
  75. if (i == len) {
  76. bitmap_set(t->map, offset, len);
  77. if (offset == t->first_free)
  78. t->first_free = find_next_zero_bit
  79. (t->map, t->size,
  80. t->first_free + len);
  81. if ((t->last_off = offset + len) >= t->size)
  82. t->last_off = 0;
  83. t->used += len;
  84. t->last_size = len;
  85. spin_unlock(&t->lock);
  86. return offset;
  87. }
  88. }
  89. count += i + 1;
  90. if ((offset += i + 1) >= t->size)
  91. offset = 0;
  92. }
  93. }
  94. void bit_map_clear(struct bit_map *t, int offset, int len)
  95. {
  96. int i;
  97. if (t->used < len)
  98. BUG(); /* Much too late to do any good, but alas... */
  99. spin_lock(&t->lock);
  100. for (i = 0; i < len; i++) {
  101. if (test_bit(offset + i, t->map) == 0)
  102. BUG();
  103. __clear_bit(offset + i, t->map);
  104. }
  105. if (offset < t->first_free)
  106. t->first_free = offset;
  107. t->used -= len;
  108. spin_unlock(&t->lock);
  109. }
  110. void bit_map_init(struct bit_map *t, unsigned long *map, int size)
  111. {
  112. bitmap_zero(map, size);
  113. memset(t, 0, sizeof *t);
  114. spin_lock_init(&t->lock);
  115. t->map = map;
  116. t->size = size;
  117. }