From 903439f7359b1bfaecb59134bbe6bc869501e537 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Tue, 19 Mar 2013 21:34:48 +0000 Subject: [PATCH] This is an optimization which produces improved launching time. There should be no functionality change. Clients should see no ABI differences. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@177443 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/__std_stream | 28 ++++++++++++++-------------- src/iostream.cpp | 14 ++++++++------ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/include/__std_stream b/include/__std_stream index fa194ea8..8ca413eb 100644 --- a/include/__std_stream +++ b/include/__std_stream @@ -41,7 +41,7 @@ public: typedef typename traits_type::off_type off_type; typedef typename traits_type::state_type state_type; - explicit __stdinbuf(FILE* __fp); + __stdinbuf(FILE* __fp, state_type* __st); protected: virtual int_type underflow(); @@ -53,7 +53,7 @@ private: FILE* __file_; const codecvt* __cv_; - state_type __st_; + state_type* __st_; int __encoding_; bool __always_noconv_; @@ -64,9 +64,9 @@ private: }; template -__stdinbuf<_CharT>::__stdinbuf(FILE* __fp) +__stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st) : __file_(__fp), - __st_() + __st_(__st) { imbue(this->getloc()); } @@ -119,15 +119,15 @@ __stdinbuf<_CharT>::__getchar(bool __consume) codecvt_base::result __r; do { - state_type __sv_st = __st_; - __r = __cv_->in(__st_, __extbuf, __extbuf + __nread, __enxt, + state_type __sv_st = *__st_; + __r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt, &__1buf, &__1buf + 1, __inxt); switch (__r) { case _VSTD::codecvt_base::ok: break; case codecvt_base::partial: - __st_ = __sv_st; + *__st_ = __sv_st; if (__nread == sizeof(__extbuf)) return traits_type::eof(); { @@ -167,7 +167,7 @@ __stdinbuf<_CharT>::pbackfail(int_type __c) char* __enxt; const char_type __ci = traits_type::to_char_type(__c); const char_type* __inxt; - switch (__cv_->out(__st_, &__ci, &__ci + 1, __inxt, + switch (__cv_->out(*__st_, &__ci, &__ci + 1, __inxt, __extbuf, __extbuf + sizeof(__extbuf), __enxt)) { case _VSTD::codecvt_base::ok: @@ -200,7 +200,7 @@ public: typedef typename traits_type::off_type off_type; typedef typename traits_type::state_type state_type; - explicit __stdoutbuf(FILE* __fp); + __stdoutbuf(FILE* __fp, state_type* __st); protected: virtual int_type overflow (int_type __c = traits_type::eof()); @@ -210,7 +210,7 @@ protected: private: FILE* __file_; const codecvt* __cv_; - state_type __st_; + state_type* __st_; bool __always_noconv_; __stdoutbuf(const __stdoutbuf&); @@ -218,10 +218,10 @@ private: }; template -__stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp) +__stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st) : __file_(__fp), __cv_(&use_facet >(this->getloc())), - __st_(), + __st_(__st), __always_noconv_(__cv_->always_noconv()) { } @@ -249,7 +249,7 @@ __stdoutbuf<_CharT>::overflow(int_type __c) do { const char_type* __e; - __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, + __r = __cv_->out(*__st_, this->pbase(), this->pptr(), __e, __extbuf, __extbuf + sizeof(__extbuf), __extbe); @@ -289,7 +289,7 @@ __stdoutbuf<_CharT>::sync() do { char* __extbe; - __r = __cv_->unshift(__st_, __extbuf, + __r = __cv_->unshift(*__st_, __extbuf, __extbuf + sizeof(__extbuf), __extbe); size_t __nmemb = static_cast(__extbe - __extbuf); diff --git a/src/iostream.cpp b/src/iostream.cpp index f5b959b4..7fc71df4 100644 --- a/src/iostream.cpp +++ b/src/iostream.cpp @@ -13,6 +13,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD +static mbstate_t state_types[6] = {}; + _ALIGNAS_TYPE (__stdinbuf ) static char __cin [sizeof(__stdinbuf )]; _ALIGNAS_TYPE (__stdoutbuf) static char __cout[sizeof(__stdoutbuf)]; _ALIGNAS_TYPE (__stdoutbuf) static char __cerr[sizeof(__stdoutbuf)]; @@ -33,17 +35,17 @@ ios_base::Init __start_std_streams; ios_base::Init::Init() { - istream* cin_ptr = ::new(cin) istream(::new(__cin) __stdinbuf (stdin) ); - ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf(stdout)); - ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf(stderr)); + istream* cin_ptr = ::new(cin) istream(::new(__cin) __stdinbuf (stdin, state_types+0) ); + ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf(stdout, state_types+1)); + ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf(stderr, state_types+2)); ::new(clog) ostream(cerr_ptr->rdbuf()); cin_ptr->tie(cout_ptr); _VSTD::unitbuf(*cerr_ptr); cerr_ptr->tie(cout_ptr); - wistream* wcin_ptr = ::new(wcin) wistream(::new(__wcin) __stdinbuf (stdin) ); - wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf(stdout)); - wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf(stderr)); + wistream* wcin_ptr = ::new(wcin) wistream(::new(__wcin) __stdinbuf (stdin, state_types+3) ); + wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf(stdout, state_types+4)); + wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf(stderr, state_types+5)); ::new(wclog) wostream(wcerr_ptr->rdbuf()); wcin_ptr->tie(wcout_ptr); _VSTD::unitbuf(*wcerr_ptr);