circular_raw_ostream.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //===- circular_raw_ostream.cpp - Implement circular_raw_ostream ----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This implements support for circular buffered streams.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Support/circular_raw_ostream.h"
  13. #include <algorithm>
  14. using namespace llvm;
  15. void circular_raw_ostream::write_impl(const char *Ptr, size_t Size) {
  16. if (BufferSize == 0) {
  17. TheStream->write(Ptr, Size);
  18. return;
  19. }
  20. // Write into the buffer, wrapping if necessary.
  21. while (Size != 0) {
  22. unsigned Bytes =
  23. std::min(unsigned(Size), unsigned(BufferSize - (Cur - BufferArray)));
  24. memcpy(Cur, Ptr, Bytes);
  25. Size -= Bytes;
  26. Cur += Bytes;
  27. if (Cur == BufferArray + BufferSize) {
  28. // Reset the output pointer to the start of the buffer.
  29. Cur = BufferArray;
  30. Filled = true;
  31. }
  32. }
  33. }
  34. void circular_raw_ostream::flushBufferWithBanner() {
  35. if (BufferSize != 0) {
  36. // Write out the buffer
  37. TheStream->write(Banner, std::strlen(Banner));
  38. flushBuffer();
  39. }
  40. }