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
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) {
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;
}
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;
}
// 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;
}
// raw_svector_ostream
//===----------------------------------------------------------------------===//
+raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
+}
+
raw_svector_ostream::~raw_svector_ostream() {
flush();
}