blob: acd230704ff8de45de7ba6cfb47a357cc8905c99 [file] [log] [blame]
Chris Lattner5c289f22010-04-09 20:43:54 +00001//===- circular_raw_ostream.cpp - Implement circular_raw_ostream ----------===//
David Greene7ffbb502009-12-23 16:08:15 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David Greene7ffbb502009-12-23 16:08:15 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This implements support for circular buffered streams.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/circular_raw_ostream.h"
David Greene7ffbb502009-12-23 16:08:15 +000014#include <algorithm>
David Greene7ffbb502009-12-23 16:08:15 +000015using 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) {
Chris Lattner5c289f22010-04-09 20:43:54 +000025 unsigned Bytes =
26 std::min(unsigned(Size), unsigned(BufferSize - (Cur - BufferArray)));
David Greene7ffbb502009-12-23 16:08:15 +000027 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 }
Fangrui Song8ca769d2018-06-02 06:00:35 +000035 }
David Greene7ffbb502009-12-23 16:08:15 +000036}
37
Chris Lattner5c289f22010-04-09 20:43:54 +000038void circular_raw_ostream::flushBufferWithBanner() {
David Greene7ffbb502009-12-23 16:08:15 +000039 if (BufferSize != 0) {
40 // Write out the buffer
Chris Lattner5c289f22010-04-09 20:43:54 +000041 TheStream->write(Banner, std::strlen(Banner));
David Greene7ffbb502009-12-23 16:08:15 +000042 flushBuffer();
43 }
44}