Chris Lattner | 5c289f2 | 2010-04-09 20:43:54 +0000 | [diff] [blame] | 1 | //===- circular_raw_ostream.cpp - Implement circular_raw_ostream ----------===// |
David Greene | 7ffbb50 | 2009-12-23 16:08:15 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
David Greene | 7ffbb50 | 2009-12-23 16:08:15 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This implements support for circular buffered streams. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Support/circular_raw_ostream.h" |
David Greene | 7ffbb50 | 2009-12-23 16:08:15 +0000 | [diff] [blame] | 14 | #include <algorithm> |
David Greene | 7ffbb50 | 2009-12-23 16:08:15 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 5c289f2 | 2010-04-09 20:43:54 +0000 | [diff] [blame] | 25 | unsigned Bytes = |
| 26 | std::min(unsigned(Size), unsigned(BufferSize - (Cur - BufferArray))); |
David Greene | 7ffbb50 | 2009-12-23 16:08:15 +0000 | [diff] [blame] | 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 | } |
Fangrui Song | 8ca769d | 2018-06-02 06:00:35 +0000 | [diff] [blame] | 35 | } |
David Greene | 7ffbb50 | 2009-12-23 16:08:15 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Chris Lattner | 5c289f2 | 2010-04-09 20:43:54 +0000 | [diff] [blame] | 38 | void circular_raw_ostream::flushBufferWithBanner() { |
David Greene | 7ffbb50 | 2009-12-23 16:08:15 +0000 | [diff] [blame] | 39 | if (BufferSize != 0) { |
| 40 | // Write out the buffer |
Chris Lattner | 5c289f2 | 2010-04-09 20:43:54 +0000 | [diff] [blame] | 41 | TheStream->write(Banner, std::strlen(Banner)); |
David Greene | 7ffbb50 | 2009-12-23 16:08:15 +0000 | [diff] [blame] | 42 | flushBuffer(); |
| 43 | } |
| 44 | } |