summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/raw_ostream.cpp
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2009-08-18 23:42:36 +0000
committerDaniel Dunbar <daniel@zuster.org>2009-08-18 23:42:36 +0000
commit317a6cd0103322ffe593a379b51b5cbd7345117f (patch)
treeed6f69684367be2e3b33a11c1fb75a8c597ac483 /llvm/lib/Support/raw_ostream.cpp
parent4906f73a9f2a0c788428b92187707b394a4d022a (diff)
downloadbcm5719-llvm-317a6cd0103322ffe593a379b51b5cbd7345117f.tar.gz
bcm5719-llvm-317a6cd0103322ffe593a379b51b5cbd7345117f.zip
raw_ostream: Add the capability for subclasses to manually install an external
buffer. llvm-svn: 79382
Diffstat (limited to 'llvm/lib/Support/raw_ostream.cpp')
-rw-r--r--llvm/lib/Support/raw_ostream.cpp44
1 files changed, 23 insertions, 21 deletions
diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp
index ac9bc6418f4..044b963ee12 100644
--- a/llvm/lib/Support/raw_ostream.cpp
+++ b/llvm/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();
}
OpenPOWER on IntegriCloud