// Iostreams base classes -*- C++ -*-
-// Copyright (C) 1997-1999 Free Software Foundation, Inc.
+// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
+// Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// ISO C++ 14882: 27.4 Iostreams base classes
//
-#include <bits/std_ios.h>
-#include <bits/std_iostream.h>
-#include <bits/std_fstream.h>
+#include <ios>
+#include <ostream>
+#include <istream>
+#include <fstream>
-namespace std {
+#include <bits/atomicity.h>
- // Out-of-line definitions for static const ios_base members.
+namespace std
+{
+ // Extern declarations for global objects in src/globals.cc.
+ extern istream cin;
+ extern ostream cout;
+ extern ostream cerr;
+ extern ostream clog;
+ extern filebuf buf_cout;
+ extern filebuf buf_cin;
+ extern filebuf buf_cerr;
+
+#ifdef _GLIBCPP_USE_WCHAR_T
+ extern wistream wcin;
+ extern wostream wcout;
+ extern wostream wcerr;
+ extern wostream wclog;
+ extern wfilebuf buf_wcout;
+ extern wfilebuf buf_wcin;
+ extern wfilebuf buf_wcerr;
+#endif
+
+ // Definitions for static const data members of __ios_flags.
+ const __ios_flags::__int_type __ios_flags::_S_boolalpha;
+ const __ios_flags::__int_type __ios_flags::_S_dec;
+ const __ios_flags::__int_type __ios_flags::_S_fixed;
+ const __ios_flags::__int_type __ios_flags::_S_hex;
+ const __ios_flags::__int_type __ios_flags::_S_internal;
+ const __ios_flags::__int_type __ios_flags::_S_left;
+ const __ios_flags::__int_type __ios_flags::_S_oct;
+ const __ios_flags::__int_type __ios_flags::_S_right;
+ const __ios_flags::__int_type __ios_flags::_S_scientific;
+ const __ios_flags::__int_type __ios_flags::_S_showbase;
+ const __ios_flags::__int_type __ios_flags::_S_showpoint;
+ const __ios_flags::__int_type __ios_flags::_S_showpos;
+ const __ios_flags::__int_type __ios_flags::_S_skipws;
+ const __ios_flags::__int_type __ios_flags::_S_unitbuf;
+ const __ios_flags::__int_type __ios_flags::_S_uppercase;
+ const __ios_flags::__int_type __ios_flags::_S_adjustfield;
+ const __ios_flags::__int_type __ios_flags::_S_basefield;
+ const __ios_flags::__int_type __ios_flags::_S_floatfield;
+
+ const __ios_flags::__int_type __ios_flags::_S_badbit;
+ const __ios_flags::__int_type __ios_flags::_S_eofbit;
+ const __ios_flags::__int_type __ios_flags::_S_failbit;
+
+ const __ios_flags::__int_type __ios_flags::_S_app;
+ const __ios_flags::__int_type __ios_flags::_S_ate;
+ const __ios_flags::__int_type __ios_flags::_S_bin;
+ const __ios_flags::__int_type __ios_flags::_S_in;
+ const __ios_flags::__int_type __ios_flags::_S_out;
+ const __ios_flags::__int_type __ios_flags::_S_trunc;
+
+ // Definitions for static const members of ios_base.
const ios_base::fmtflags ios_base::boolalpha;
const ios_base::fmtflags ios_base::dec;
const ios_base::fmtflags ios_base::fixed;
const ios_base::seekdir ios_base::cur;
const ios_base::seekdir ios_base::end;
- ios_base::failure::failure(const string& __str)
+ const int ios_base::_S_local_words;
+ int ios_base::Init::_S_ios_base_init = 0;
+ bool ios_base::Init::_S_synced_with_stdio = true;
+
+ ios_base::failure::failure(const string& __str) throw()
{
strncpy(_M_name, __str.c_str(), _M_bufsize);
_M_name[_M_bufsize - 1] = '\0';
}
- int ios_base::Init::_M_ios_base_init = 0;
+ ios_base::failure::~failure() throw()
+ { }
- ios_base::Init::Init()
+ const char*
+ ios_base::failure::what() const throw()
+ { return _M_name; }
+
+ void
+ ios_base::Init::_S_ios_create(bool __sync)
{
- if (++_M_ios_base_init == 1)
- {
- // NB: std_iostream.h creates the four standard files with
- // default buffers. At this point, we swap out the default
- // buffers for the properly-constructed ones, and dispose of
- // the default buffers.
- streambuf* __old;
- _M_cout = new filebuf(1, "stdout", ios_base::out);
- _M_cin = new filebuf(0, "stdin", ios_base::in);
- _M_cerr = new filebuf(2, "stderr", ios_base::out);
- __old = cout.rdbuf(_M_cout);
- __old->~streambuf();
- __old = cin.rdbuf(_M_cin);
- __old->~streambuf();
- cin.tie(&cout);
- __old = cerr.rdbuf(_M_cerr);
- __old->~streambuf();
- cerr.flags(ios_base::unitbuf);
- __old = clog.rdbuf(_M_cerr);
- __old->~streambuf();
+ int __out_bufsize = __sync ? 0 : static_cast<int>(BUFSIZ);
+ int __in_bufsize = __sync ? 1 : static_cast<int>(BUFSIZ);
+
+#if _GLIBCPP_AVOID_FSEEK
+ // Platforms that prefer to avoid fseek() calls on streams only
+ // get their desire when the C++-layer input buffer size is 1.
+ // This hack hurts performance but keeps correctness across
+ // all types of streams that might be attached to (e.g.) cin.
+ __in_bufsize = 1;
+#endif
+
+ // NB: The file globals.cc creates the four standard files
+ // with NULL buffers. At this point, we swap out the dummy NULL
+ // [io]stream objects and buffers with the real deal.
+ new (&buf_cout) filebuf(stdout, ios_base::out, __out_bufsize);
+ new (&buf_cin) filebuf(stdin, ios_base::in, __in_bufsize);
+ new (&buf_cerr) filebuf(stderr, ios_base::out, __out_bufsize);
+ new (&cout) ostream(&buf_cout);
+ new (&cin) istream(&buf_cin);
+ new (&cerr) ostream(&buf_cerr);
+ new (&clog) ostream(&buf_cerr);
+ cin.tie(&cout);
+ cerr.flags(ios_base::unitbuf);
+
#ifdef _GLIBCPP_USE_WCHAR_T
- wstreambuf* __wold;
- _M_wcout = new wfilebuf(1, "stdout", ios_base::out);
- _M_wcin = new wfilebuf(0, "stdin", ios_base::in);
- _M_wcerr = new wfilebuf(2, "stderr", ios_base::out);
- __wold = wcout.rdbuf(_M_wcout);
- __wold->~wstreambuf();
- __wold = wcin.rdbuf(_M_wcin);
- __wold->~wstreambuf();
- wcin.tie(&wcout);
- __wold = wcerr.rdbuf(_M_wcerr);
- __wold->~wstreambuf();
- wcerr.flags(ios_base::unitbuf);
- __wold = wclog.rdbuf(_M_wcerr);
- __wold->~wstreambuf();
+ new (&buf_wcout) wfilebuf(stdout, ios_base::out, __out_bufsize);
+ new (&buf_wcin) wfilebuf(stdin, ios_base::in, __in_bufsize);
+ new (&buf_wcerr) wfilebuf(stderr, ios_base::out, __out_bufsize);
+ new (&wcout) wostream(&buf_wcout);
+ new (&wcin) wistream(&buf_wcin);
+ new (&wcerr) wostream(&buf_wcerr);
+ new (&wclog) wostream(&buf_wcerr);
+ wcin.tie(&wcout);
+ wcerr.flags(ios_base::unitbuf);
#endif
- }
}
- ios_base::Init::~Init()
+ void
+ ios_base::Init::_S_ios_destroy()
{
- if (--_M_ios_base_init == 0)
- {
- cout.flush();
- cerr.flush();
- clog.flush();
- delete _M_cout;
- delete _M_cin;
- delete _M_cerr;
- _M_cout = NULL;
- _M_cin = NULL;
- _M_cerr = NULL;
+ // Explicitly call dtors to free any memory that is dynamically
+ // allocated by filebuf ctor or member functions, but don't
+ // deallocate all memory by calling operator delete.
+ cout.flush();
+ cerr.flush();
+ clog.flush();
+ buf_cout.~filebuf();
+ buf_cin.~filebuf();
+ buf_cerr.~filebuf();
#ifdef _GLIBCPP_USE_WCHAR_T
- wcout.flush();
- wcerr.flush();
- wclog.flush();
- delete _M_wcout;
- delete _M_wcin;
- delete _M_wcerr;
- _M_wcout = NULL;
- _M_wcin = NULL;
- _M_wcerr = NULL;
+ wcout.flush();
+ wcerr.flush();
+ wclog.flush();
+ buf_wcout.~wfilebuf();
+ buf_wcin.~wfilebuf();
+ buf_wcerr.~wfilebuf();
#endif
+ }
+
+ ios_base::Init::Init()
+ {
+ if (_S_ios_base_init == 0)
+ {
+ // Standard streams default to synced with "C" operations.
+ ios_base::Init::_S_synced_with_stdio = true;
+ _S_ios_create(ios_base::Init::_S_synced_with_stdio);
}
+ ++_S_ios_base_init;
+ }
+
+ ios_base::Init::~Init()
+ {
+ if (--_S_ios_base_init == 0)
+ _S_ios_destroy();
}
// 27.4.2.5 ios_base storage functions
int
ios_base::xalloc() throw()
{
- // XXX MT
// XXX should be a symbol. (Reserve 0..3 for builtins.)
- static int top = 4;
- return top++;
+ static _Atomic_word top = 0;
+ return __exchange_and_add(&top, 1) + 4;
+ // Implementation note: Initialize top to zero to ensure that
+ // initialization occurs before main() is started.
}
// 27.4.2.5 iword/pword storage
// _M_clear(_M_rdstate() | badbit); // may throw
return _M_dummy;
}
- do { words[i] = _M_words[i]; } while (++i < _M_word_limit);
- if (_M_words != _M_word_array) delete [] _M_words;
+ for (; i < _M_word_limit; i++)
+ words[i] = _M_words[i];
+ if (_M_words != _M_word_array)
+ delete [] _M_words;
}
do { words[i] = zero; } while (++i < newlimit);
ios_base::_M_init()
{
// NB: May be called more than once
- _M_flags = skipws | dec;
- _M_width = 0;
_M_precision = 6;
+ _M_width = 0;
+ _M_flags = skipws | dec;
_M_callbacks = 0;
_M_words = 0;
_M_word_limit = 0;
{
locale __old = _M_ios_locale;
_M_ios_locale = __loc;
- // Make sure there's a callback for the format caches so they will be
- // marked dirty.
- _Format_cache<char>::_S_get(*this);
-#ifdef _GLIBCPP_USE_WCHAR_T
- _Format_cache<wchar_t>::_S_get(*this);
-#endif
_M_call_callbacks(imbue_event);
return __old;
}
{
#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
// 49. Underspecification of ios_base::sync_with_stdio
- bool __ret = __ioinit._M_cin->_M_file->get_fileno() == 0;
+ bool __ret = ios_base::Init::_S_synced_with_stdio;
+#endif
- // Turn off sync with C FILE* for cin, cout, cerr, clog.
+ // Turn off sync with C FILE* for cin, cout, cerr, clog iff
+ // currently synchronized.
if (!__sync && __ret)
{
- // Need to dispose of the buffers created at initialization.
- __ioinit._M_cout->~filebuf();
- __ioinit._M_cin->~filebuf();
- __ioinit._M_cerr->~filebuf();
- __ioinit._M_cout = new filebuf();
- __ioinit._M_cin = new filebuf();
- __ioinit._M_cerr = new filebuf();
- __ioinit._M_cout->open("stdout", ios_base::out);
- __ioinit._M_cin->open("stdin", ios_base::in);
- __ioinit._M_cerr->open("stderr", ios_base::out);
- cout.rdbuf(__ioinit._M_cout);
- cin.rdbuf(__ioinit._M_cin);
- cerr.rdbuf(__ioinit._M_cerr);
- cerr.flags(ios_base::unitbuf);
- clog.rdbuf(__ioinit._M_cerr);
-#ifdef _GLIBCPP_USE_WCHAR_T
- __ioinit._M_wcout->~wfilebuf();
- __ioinit._M_wcin->~wfilebuf();
- __ioinit._M_wcerr->~wfilebuf();
- __ioinit._M_wcout = new wfilebuf();
- __ioinit._M_wcin = new wfilebuf();
- __ioinit._M_wcerr = new wfilebuf();
- __ioinit._M_wcout->open("wstdout", ios_base::out);
- __ioinit._M_wcin->open("wstdin", ios_base::in);
- __ioinit._M_wcerr->open("wstderr", ios_base::out);
- wcout.rdbuf(__ioinit._M_wcout);
- wcin.rdbuf(__ioinit._M_wcin);
- wcerr.rdbuf(__ioinit._M_wcerr);
- wcerr.flags(ios_base::unitbuf);
- wclog.rdbuf(__ioinit._M_wcerr);
-#endif
+ ios_base::Init::_S_synced_with_stdio = false;
+ ios_base::Init::_S_ios_destroy();
+ ios_base::Init::_S_ios_create(ios_base::Init::_S_synced_with_stdio);
}
-
return __ret;
-#endif
}
-
} // namespace std
-
-
-
-
-
-
-
-
-
-
-