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>
15using namespace llvm;
16
17void 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(a: unsigned(Size), b: unsigned(BufferSize - (Cur - BufferArray)));
27 memcpy(dest: Cur, src: Ptr, n: 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
38void circular_raw_ostream::flushBufferWithBanner() {
39 if (BufferSize != 0) {
40 // Write out the buffer
41 TheStream->write(Ptr: Banner, Size: std::strlen(s: Banner));
42 flushBuffer();
43 }
44}
45