[ThinLTO] Add code comment. NFC
[llvm-complete.git] / lib / Support / circular_raw_ostream.cpp
blobacd230704ff8de45de7ba6cfb47a357cc8905c99
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/circular_raw_ostream.h"
14 #include <algorithm>
15 using namespace llvm;
17 void circular_raw_ostream::write_impl(const char *Ptr, size_t Size) {
18 if (BufferSize == 0) {
19 TheStream->write(Ptr, Size);
20 return;
23 // Write into the buffer, wrapping if necessary.
24 while (Size != 0) {
25 unsigned Bytes =
26 std::min(unsigned(Size), unsigned(BufferSize - (Cur - BufferArray)));
27 memcpy(Cur, Ptr, Bytes);
28 Size -= Bytes;
29 Cur += Bytes;
30 if (Cur == BufferArray + BufferSize) {
31 // Reset the output pointer to the start of the buffer.
32 Cur = BufferArray;
33 Filled = true;
38 void circular_raw_ostream::flushBufferWithBanner() {
39 if (BufferSize != 0) {
40 // Write out the buffer
41 TheStream->write(Banner, std::strlen(Banner));
42 flushBuffer();