xref: /freebsd/contrib/llvm-project/llvm/lib/Support/circular_raw_ostream.cpp (revision 401ab69cff8fa2320a9f8ea4baa114a6da6c952b)
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 
13 #include "llvm/Support/circular_raw_ostream.h"
14 #include <algorithm>
15 using namespace llvm;
16 
17 void circular_raw_ostream::write_impl(const char *Ptr, size_t Size) {
18   if (BufferSize == 0) {
19     TheStream->write(Ptr, Size);
20     return;
21   }
22 
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;
34     }
35   }
36 }
37 
38 void circular_raw_ostream::flushBufferWithBanner() {
39   if (BufferSize != 0) {
40     // Write out the buffer
41     TheStream->write(Banner, std::strlen(Banner));
42     flushBuffer();
43   }
44 }
45