bitmap.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2000, 2001, 2002
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Fixed-size array of bits. (Intended for storage management.)
  31. */
  32. #include <types.h>
  33. #include <kern/errno.h>
  34. #include <lib.h>
  35. #include <bitmap.h>
  36. /*
  37. * It would be a lot more efficient on most platforms to use uint32_t
  38. * or unsigned long as the base type for holding bits. But we don't,
  39. * because if one uses any data type more than a single byte wide,
  40. * bitmap data saved on disk becomes endian-dependent, which is a
  41. * severe nuisance.
  42. */
  43. #define BITS_PER_WORD (CHAR_BIT)
  44. #define WORD_TYPE unsigned char
  45. #define WORD_ALLBITS (0xff)
  46. struct bitmap {
  47. unsigned nbits;
  48. WORD_TYPE *v;
  49. };
  50. struct bitmap *
  51. bitmap_create(unsigned nbits)
  52. {
  53. struct bitmap *b;
  54. unsigned words;
  55. words = DIVROUNDUP(nbits, BITS_PER_WORD);
  56. b = kmalloc(sizeof(struct bitmap));
  57. if (b == NULL) {
  58. return NULL;
  59. }
  60. b->v = kmalloc(words*sizeof(WORD_TYPE));
  61. if (b->v == NULL) {
  62. kfree(b);
  63. return NULL;
  64. }
  65. bzero(b->v, words*sizeof(WORD_TYPE));
  66. b->nbits = nbits;
  67. /* Mark any leftover bits at the end in use */
  68. if (words > nbits / BITS_PER_WORD) {
  69. unsigned j, ix = words-1;
  70. unsigned overbits = nbits - ix*BITS_PER_WORD;
  71. KASSERT(nbits / BITS_PER_WORD == words-1);
  72. KASSERT(overbits > 0 && overbits < BITS_PER_WORD);
  73. for (j=overbits; j<BITS_PER_WORD; j++) {
  74. b->v[ix] |= ((WORD_TYPE)1 << j);
  75. }
  76. }
  77. return b;
  78. }
  79. void *
  80. bitmap_getdata(struct bitmap *b)
  81. {
  82. return b->v;
  83. }
  84. int
  85. bitmap_alloc(struct bitmap *b, unsigned *index)
  86. {
  87. unsigned ix;
  88. unsigned maxix = DIVROUNDUP(b->nbits, BITS_PER_WORD);
  89. unsigned offset;
  90. for (ix=0; ix<maxix; ix++) {
  91. if (b->v[ix]!=WORD_ALLBITS) {
  92. for (offset = 0; offset < BITS_PER_WORD; offset++) {
  93. WORD_TYPE mask = ((WORD_TYPE)1) << offset;
  94. if ((b->v[ix] & mask)==0) {
  95. b->v[ix] |= mask;
  96. *index = (ix*BITS_PER_WORD)+offset;
  97. KASSERT(*index < b->nbits);
  98. return 0;
  99. }
  100. }
  101. KASSERT(0);
  102. }
  103. }
  104. return ENOSPC;
  105. }
  106. static
  107. inline
  108. void
  109. bitmap_translate(unsigned bitno, unsigned *ix, WORD_TYPE *mask)
  110. {
  111. unsigned offset;
  112. *ix = bitno / BITS_PER_WORD;
  113. offset = bitno % BITS_PER_WORD;
  114. *mask = ((WORD_TYPE)1) << offset;
  115. }
  116. void
  117. bitmap_mark(struct bitmap *b, unsigned index)
  118. {
  119. unsigned ix;
  120. WORD_TYPE mask;
  121. KASSERT(index < b->nbits);
  122. bitmap_translate(index, &ix, &mask);
  123. KASSERT((b->v[ix] & mask)==0);
  124. b->v[ix] |= mask;
  125. }
  126. void
  127. bitmap_unmark(struct bitmap *b, unsigned index)
  128. {
  129. unsigned ix;
  130. WORD_TYPE mask;
  131. KASSERT(index < b->nbits);
  132. bitmap_translate(index, &ix, &mask);
  133. KASSERT((b->v[ix] & mask)!=0);
  134. b->v[ix] &= ~mask;
  135. }
  136. int
  137. bitmap_isset(struct bitmap *b, unsigned index)
  138. {
  139. unsigned ix;
  140. WORD_TYPE mask;
  141. bitmap_translate(index, &ix, &mask);
  142. return (b->v[ix] & mask);
  143. }
  144. void
  145. bitmap_destroy(struct bitmap *b)
  146. {
  147. kfree(b->v);
  148. kfree(b);
  149. }