diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-08-18 23:42:36 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-08-18 23:42:36 +0000 |
commit | 906d5b4455c6d605ab62f26d45cad2e49bf948bb (patch) | |
tree | 743e9979d244bf74457651b8b7325ba4eff89446 /lib/Support/raw_ostream.cpp | |
parent | db8dc2b9faf346fbfa04c07f501981250948f5e2 (diff) | |
download | external_llvm-906d5b4455c6d605ab62f26d45cad2e49bf948bb.zip external_llvm-906d5b4455c6d605ab62f26d45cad2e49bf948bb.tar.gz external_llvm-906d5b4455c6d605ab62f26d45cad2e49bf948bb.tar.bz2 |
raw_ostream: Add the capability for subclasses to manually install an external
buffer.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79382 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/raw_ostream.cpp')
-rw-r--r-- | lib/Support/raw_ostream.cpp | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index ac9bc64..044b963 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -52,7 +52,8 @@ raw_ostream::~raw_ostream() { assert(OutBufCur == OutBufStart && "raw_ostream destructor called with non-empty buffer!"); - delete [] OutBufStart; + if (BufferMode == InternalBuffer) + delete [] OutBufStart; // If there are any pending errors, report them now. Clients wishing // to avoid llvm_report_error calls should check for errors with @@ -79,24 +80,21 @@ void raw_ostream::SetBuffered() { SetUnbuffered(); } -void raw_ostream::SetBufferSize(size_t Size) { - assert(Size >= 64 && - "Buffer size must be somewhat large for invariants to hold"); - flush(); +void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size, + BufferKind Mode) { + assert(((Mode == Unbuffered && BufferStart == 0 && Size == 0) || + (Mode != Unbuffered && BufferStart && Size >= 64)) && + "stream must be unbuffered, or have >= 64 bytes of buffer"); + // Make sure the current buffer is free of content (we can't flush here; the + // child buffer management logic will be in write_impl). + assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!"); - delete [] OutBufStart; - OutBufStart = new char[Size]; + if (BufferMode == InternalBuffer) + delete [] OutBufStart; + OutBufStart = BufferStart; OutBufEnd = OutBufStart+Size; OutBufCur = OutBufStart; - Unbuffered = false; -} - -void raw_ostream::SetUnbuffered() { - flush(); - - delete [] OutBufStart; - OutBufStart = OutBufEnd = OutBufCur = 0; - Unbuffered = true; + BufferMode = Mode; } raw_ostream &raw_ostream::operator<<(unsigned long N) { @@ -180,14 +178,15 @@ raw_ostream &raw_ostream::operator<<(const void *P) { void raw_ostream::flush_nonempty() { assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty."); - write_impl(OutBufStart, OutBufCur - OutBufStart); - OutBufCur = OutBufStart; + size_t Length = OutBufCur - OutBufStart; + OutBufCur = OutBufStart; + write_impl(OutBufStart, Length); } raw_ostream &raw_ostream::write(unsigned char C) { // Group exceptional cases into a single branch. if (OutBufCur >= OutBufEnd) { - if (Unbuffered) { + if (BufferMode == Unbuffered) { write_impl(reinterpret_cast<char*>(&C), 1); return *this; } @@ -198,7 +197,7 @@ raw_ostream &raw_ostream::write(unsigned char C) { SetBuffered(); // It's possible for the underlying stream to decline // buffering, so check this condition again. - if (Unbuffered) { + if (BufferMode == Unbuffered) { write_impl(reinterpret_cast<char*>(&C), 1); return *this; } @@ -213,7 +212,7 @@ raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) { // Group exceptional cases into a single branch. if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) { if (BUILTIN_EXPECT(!OutBufStart, false)) { - if (Unbuffered) { + if (BufferMode == Unbuffered) { write_impl(Ptr, Size); return *this; } @@ -498,6 +497,9 @@ void raw_string_ostream::write_impl(const char *Ptr, size_t Size) { // raw_svector_ostream //===----------------------------------------------------------------------===// +raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) { +} + raw_svector_ostream::~raw_svector_ostream() { flush(); } |