circular_raw_ostream.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //===- circular_raw_ostream.cpp - Implement circular_raw_ostream ----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This implements support for circular buffered streams.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/circular_raw_ostream.h"
  14. #include <algorithm>
  15. using namespace llvm;
  16. void circular_raw_ostream::write_impl(const char *Ptr, size_t Size) {
  17. if (BufferSize == 0) {
  18. TheStream->write(Ptr, Size);
  19. return;
  20. }
  21. // Write into the buffer, wrapping if necessary.
  22. while (Size != 0) {
  23. unsigned Bytes =
  24. std::min(unsigned(Size), unsigned(BufferSize - (Cur - BufferArray)));
  25. memcpy(Cur, Ptr, Bytes);
  26. Size -= Bytes;
  27. Cur += Bytes;
  28. if (Cur == BufferArray + BufferSize) {
  29. // Reset the output pointer to the start of the buffer.
  30. Cur = BufferArray;
  31. Filled = true;
  32. }
  33. }
  34. }
  35. void circular_raw_ostream::flushBufferWithBanner() {
  36. if (BufferSize != 0) {
  37. // Write out the buffer
  38. TheStream->write(Banner, std::strlen(Banner));
  39. flushBuffer();
  40. }
  41. }