printf.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include "pal_internal.h"
  14. #include "api.h"
  15. #ifndef NO_INTERNAL_PRINTF
  16. // Collect up to PRINTBUF_SIZE characters into a buffer
  17. // and perform ONE system call to print all of them,
  18. // in order to make the lines output to the console atomic
  19. // and prevent interrupts from causing context switches
  20. // in the middle of a console output line and such.
  21. #define PRINTBUF_SIZE 256
  22. struct printbuf {
  23. int idx; // current buffer index
  24. int cnt; // total bytes printed so far
  25. char buf[PRINTBUF_SIZE];
  26. };
  27. static int
  28. fputch(void * f, int ch, struct printbuf * b)
  29. {
  30. __UNUSED(f);
  31. b->buf[b->idx++] = ch;
  32. if (b->idx == PRINTBUF_SIZE - 1) {
  33. _DkPrintConsole(b->buf, b->idx);
  34. b->idx = 0;
  35. }
  36. b->cnt++;
  37. return 0;
  38. }
  39. int
  40. vprintf(const char * fmt, va_list ap)
  41. {
  42. struct printbuf b;
  43. b.idx = 0;
  44. b.cnt = 0;
  45. vfprintfmt((void *) &fputch, NULL, &b, fmt, ap);
  46. _DkPrintConsole(b.buf, b.idx);
  47. return b.cnt;
  48. }
  49. int
  50. printf(const char * fmt, ...)
  51. {
  52. va_list ap;
  53. int cnt;
  54. va_start(ap, fmt);
  55. cnt = vprintf(fmt, ap);
  56. va_end(ap);
  57. return cnt;
  58. }
  59. EXTERN_ALIAS(printf);
  60. #endif