queue.c 980 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "io.h"
  15. int
  16. qgetc(IOQ *q)
  17. {
  18. int c;
  19. if(q->in == q->out)
  20. return -1;
  21. c = *q->out;
  22. if(q->out == q->buf+sizeof(q->buf)-1)
  23. q->out = q->buf;
  24. else
  25. q->out++;
  26. return c;
  27. }
  28. static int
  29. qputc(IOQ *q, int c)
  30. {
  31. uint8_t *nextin;
  32. if(q->in >= &q->buf[sizeof(q->buf)-1])
  33. nextin = q->buf;
  34. else
  35. nextin = q->in+1;
  36. if(nextin == q->out)
  37. return -1;
  38. *q->in = c;
  39. q->in = nextin;
  40. return 0;
  41. }
  42. void
  43. qinit(IOQ *q)
  44. {
  45. q->in = q->out = q->buf;
  46. q->getc = qgetc;
  47. q->putc = qputc;
  48. }