fifo.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX 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. XNIX 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 XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/types.h>
  21. #include <lib/fifo.h>
  22. void fifo_init(struct fifo *fifo, __fifo_buf_t *buf, int total) {
  23. fifo->buf = buf;
  24. fifo->total = total;
  25. fifo->next_wr = fifo->next_rd = 0;
  26. fifo->need_read = 0;
  27. return;
  28. }
  29. __fifo_buf_t fifo_get(struct fifo *fifo) {
  30. __fifo_buf_t res;
  31. if (!fifo->need_read)
  32. return 0;
  33. fifo->need_read--;
  34. res = fifo->buf[fifo->next_rd++];
  35. if (fifo->next_rd == fifo->total)
  36. fifo->next_rd = 0;
  37. return res;
  38. }
  39. void fifo_put(struct fifo *fifo, __fifo_buf_t value) {
  40. fifo->buf[fifo->next_wr++] = value;
  41. fifo->need_read++;
  42. if (fifo->next_wr == fifo->total)
  43. fifo->next_wr = 0;
  44. return;
  45. }