From d75ba1c3573d3d5146d0abe8cb376dca94be94d5 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Thu, 4 Dec 2008 22:51:11 +0000 Subject: [PATCH] Have raw_fd_ostream keep track of the position in the file to make tell() go faster by not requiring a flush(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@60560 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/raw_ostream.h | 5 ++++- lib/Support/raw_ostream.cpp | 14 ++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h index 2b4b17d7b29..1a4f6bf6f47 100644 --- a/include/llvm/Support/raw_ostream.h +++ b/include/llvm/Support/raw_ostream.h @@ -151,6 +151,7 @@ private: class raw_fd_ostream : public raw_ostream { int FD; bool ShouldClose; + uint64_t pos; public: /// raw_fd_ostream - Open the specified file for writing. If an /// error occurs, information about the error is put into ErrorInfo, @@ -178,7 +179,9 @@ public: void close(); /// tell - Return the current offset with the file. - uint64_t tell(); + uint64_t tell() { + return pos + (OutBufCur - OutBufStart); + } }; /// raw_stdout_ostream - This is a stream that always prints to stdout. diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index b10677e3f27..96c9a3a263d 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -202,7 +202,7 @@ void format_object_base::home() { /// stream should be immediately destroyed; the string will be empty /// if no error occurred. raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, - std::string &ErrorInfo) { + std::string &ErrorInfo) : pos(0) { ErrorInfo.clear(); // Handle "-" as stdout. @@ -240,8 +240,10 @@ raw_fd_ostream::~raw_fd_ostream() { void raw_fd_ostream::flush_impl() { assert (FD >= 0 && "File already closed."); - if (OutBufCur-OutBufStart) + if (OutBufCur-OutBufStart) { + pos += (OutBufCur - OutBufStart); ::write(FD, OutBufStart, OutBufCur-OutBufStart); + } HandleFlush(); } @@ -253,14 +255,6 @@ void raw_fd_ostream::close() { FD = -1; } -uint64_t raw_fd_ostream::tell() { - // We have to take into account the bytes waiting in the buffer. For now - // we do the easy thing and just flush the buffer before getting the - // current file offset. - flush(); - return (uint64_t) lseek(FD, 0, SEEK_CUR); -} - //===----------------------------------------------------------------------===// // raw_stdout/err_ostream //===----------------------------------------------------------------------===// -- 2.34.1