bitman.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of Cunix. */
  3. /*
  4. Cunix is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. Cunix is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with Cunix. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <lib/rlib.h>
  20. /*
  21. find a new bit in bm->bitmap.
  22. STEPS:
  23. 1. save free position.
  24. 2. find a new free position.
  25. 3. save new free position.
  26. */
  27. unsigned int bitman_alloc(struct bitmap_manager *bm) {
  28. unsigned int p = bm->next_free;
  29. bitmap_set(bm->bitmap, bm->next_free, 1);
  30. for (; bitmap_read(bm->bitmap, bm->next_free) == 1; bm->next_free++);
  31. return p;
  32. }
  33. /*
  34. free a bit.
  35. STEPS:
  36. 1. free bit pos.
  37. 2. if pos is lowwer than bm->next_free
  38. 2a. set bm->next_free = pos.
  39. */
  40. void bitman_free(struct bitmap_manager *bm, unsigned int pos) {
  41. bitmap_set(bm->bitmap, pos, 0);
  42. if (pos < bm->next_free) {
  43. bm->next_free = pos;
  44. }
  45. return;
  46. }