xref: /freebsd/contrib/llvm-project/libcxx/src/ios.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
981ad6265SDimitry Andric #include <__config>
1081ad6265SDimitry Andric #include <__locale>
1181ad6265SDimitry Andric #include <algorithm>
1281ad6265SDimitry Andric #include <ios>
1381ad6265SDimitry Andric #include <limits>
1481ad6265SDimitry Andric #include <memory>
1581ad6265SDimitry Andric #include <new>
160b57cec5SDimitry Andric #include <stdlib.h>
1781ad6265SDimitry Andric #include <string>
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #include "include/config_elast.h"
2081ad6265SDimitry Andric 
2181ad6265SDimitry Andric _LIBCPP_PUSH_MACROS
2281ad6265SDimitry Andric #include <__undef_macros>
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
250b57cec5SDimitry Andric 
26cb14a3feSDimitry Andric class _LIBCPP_HIDDEN __iostream_category : public __do_message {
270b57cec5SDimitry Andric public:
28fe6060f1SDimitry Andric   virtual const char* name() const noexcept;
290b57cec5SDimitry Andric   virtual string message(int ev) const;
300b57cec5SDimitry Andric };
310b57cec5SDimitry Andric 
name() const32cb14a3feSDimitry Andric const char* __iostream_category::name() const noexcept { return "iostream"; }
330b57cec5SDimitry Andric 
message(int ev) const34cb14a3feSDimitry Andric string __iostream_category::message(int ev) const {
350b57cec5SDimitry Andric   if (ev != static_cast<int>(io_errc::stream)
360b57cec5SDimitry Andric #ifdef _LIBCPP_ELAST
370b57cec5SDimitry Andric       && ev <= _LIBCPP_ELAST
380b57cec5SDimitry Andric #endif // _LIBCPP_ELAST
390b57cec5SDimitry Andric   )
400b57cec5SDimitry Andric     return __do_message::message(ev);
410b57cec5SDimitry Andric   return string("unspecified iostream_category error");
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
iostream_category()44cb14a3feSDimitry Andric const error_category& iostream_category() noexcept {
455f757f3fSDimitry Andric   union AvoidDestroyingIostreamCategory {
465f757f3fSDimitry Andric     __iostream_category iostream_error_category;
475f757f3fSDimitry Andric     constexpr explicit AvoidDestroyingIostreamCategory() : iostream_error_category() {}
485f757f3fSDimitry Andric     ~AvoidDestroyingIostreamCategory() {}
495f757f3fSDimitry Andric   };
505f757f3fSDimitry Andric   constinit static AvoidDestroyingIostreamCategory helper;
515f757f3fSDimitry Andric   return helper.iostream_error_category;
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric // ios_base::failure
550b57cec5SDimitry Andric 
failure(const string & msg,const error_code & ec)56cb14a3feSDimitry Andric ios_base::failure::failure(const string& msg, const error_code& ec) : system_error(ec, msg) {}
570b57cec5SDimitry Andric 
failure(const char * msg,const error_code & ec)58cb14a3feSDimitry Andric ios_base::failure::failure(const char* msg, const error_code& ec) : system_error(ec, msg) {}
590b57cec5SDimitry Andric 
~failure()60cb14a3feSDimitry Andric ios_base::failure::~failure() throw() {}
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric // ios_base locale
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric const ios_base::fmtflags ios_base::boolalpha;
650b57cec5SDimitry Andric const ios_base::fmtflags ios_base::dec;
660b57cec5SDimitry Andric const ios_base::fmtflags ios_base::fixed;
670b57cec5SDimitry Andric const ios_base::fmtflags ios_base::hex;
680b57cec5SDimitry Andric const ios_base::fmtflags ios_base::internal;
690b57cec5SDimitry Andric const ios_base::fmtflags ios_base::left;
700b57cec5SDimitry Andric const ios_base::fmtflags ios_base::oct;
710b57cec5SDimitry Andric const ios_base::fmtflags ios_base::right;
720b57cec5SDimitry Andric const ios_base::fmtflags ios_base::scientific;
730b57cec5SDimitry Andric const ios_base::fmtflags ios_base::showbase;
740b57cec5SDimitry Andric const ios_base::fmtflags ios_base::showpoint;
750b57cec5SDimitry Andric const ios_base::fmtflags ios_base::showpos;
760b57cec5SDimitry Andric const ios_base::fmtflags ios_base::skipws;
770b57cec5SDimitry Andric const ios_base::fmtflags ios_base::unitbuf;
780b57cec5SDimitry Andric const ios_base::fmtflags ios_base::uppercase;
790b57cec5SDimitry Andric const ios_base::fmtflags ios_base::adjustfield;
800b57cec5SDimitry Andric const ios_base::fmtflags ios_base::basefield;
810b57cec5SDimitry Andric const ios_base::fmtflags ios_base::floatfield;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric const ios_base::iostate ios_base::badbit;
840b57cec5SDimitry Andric const ios_base::iostate ios_base::eofbit;
850b57cec5SDimitry Andric const ios_base::iostate ios_base::failbit;
860b57cec5SDimitry Andric const ios_base::iostate ios_base::goodbit;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric const ios_base::openmode ios_base::app;
890b57cec5SDimitry Andric const ios_base::openmode ios_base::ate;
900b57cec5SDimitry Andric const ios_base::openmode ios_base::binary;
910b57cec5SDimitry Andric const ios_base::openmode ios_base::in;
920b57cec5SDimitry Andric const ios_base::openmode ios_base::out;
930b57cec5SDimitry Andric const ios_base::openmode ios_base::trunc;
940b57cec5SDimitry Andric 
__call_callbacks(event ev)95cb14a3feSDimitry Andric void ios_base::__call_callbacks(event ev) {
96cb14a3feSDimitry Andric   for (size_t i = __event_size_; i;) {
970b57cec5SDimitry Andric     --i;
980b57cec5SDimitry Andric     __fn_[i](ev, *this, __index_[i]);
990b57cec5SDimitry Andric   }
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric // locale
1030b57cec5SDimitry Andric 
imbue(const locale & newloc)104cb14a3feSDimitry Andric locale ios_base::imbue(const locale& newloc) {
1050b57cec5SDimitry Andric   static_assert(sizeof(locale) == sizeof(__loc_), "");
1060b57cec5SDimitry Andric   locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
1070b57cec5SDimitry Andric   locale oldloc       = loc_storage;
1080b57cec5SDimitry Andric   loc_storage         = newloc;
1090b57cec5SDimitry Andric   __call_callbacks(imbue_event);
1100b57cec5SDimitry Andric   return oldloc;
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric 
getloc() const113cb14a3feSDimitry Andric locale ios_base::getloc() const {
1140b57cec5SDimitry Andric   const locale& loc_storage = *reinterpret_cast<const locale*>(&__loc_);
1150b57cec5SDimitry Andric   return loc_storage;
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric // xalloc
1190b57cec5SDimitry Andric #if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
1200eae32dcSDimitry Andric atomic<int> ios_base::__xindex_{0};
1210b57cec5SDimitry Andric #else
1220b57cec5SDimitry Andric int ios_base::__xindex_ = 0;
1230b57cec5SDimitry Andric #endif
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric template <typename _Tp>
__ios_new_cap(size_t __req_size,size_t __current_cap)126cb14a3feSDimitry Andric static size_t __ios_new_cap(size_t __req_size, size_t __current_cap) { // Precondition: __req_size > __current_cap
1270b57cec5SDimitry Andric   const size_t mx = std::numeric_limits<size_t>::max() / sizeof(_Tp);
1280b57cec5SDimitry Andric   if (__req_size < mx / 2)
1295f757f3fSDimitry Andric     return std::max(2 * __current_cap, __req_size);
1300b57cec5SDimitry Andric   else
1310b57cec5SDimitry Andric     return mx;
1320b57cec5SDimitry Andric }
1330b57cec5SDimitry Andric 
xalloc()134cb14a3feSDimitry Andric int ios_base::xalloc() { return __xindex_++; }
1350b57cec5SDimitry Andric 
iword(int index)136cb14a3feSDimitry Andric long& ios_base::iword(int index) {
1370b57cec5SDimitry Andric   size_t req_size = static_cast<size_t>(index) + 1;
138cb14a3feSDimitry Andric   if (req_size > __iarray_cap_) {
1390b57cec5SDimitry Andric     size_t newcap = __ios_new_cap<long>(req_size, __iarray_cap_);
1400b57cec5SDimitry Andric     long* iarray  = static_cast<long*>(realloc(__iarray_, newcap * sizeof(long)));
141cb14a3feSDimitry Andric     if (iarray == 0) {
1420b57cec5SDimitry Andric       setstate(badbit);
1430b57cec5SDimitry Andric       static long error;
1440b57cec5SDimitry Andric       error = 0;
1450b57cec5SDimitry Andric       return error;
1460b57cec5SDimitry Andric     }
1470b57cec5SDimitry Andric     __iarray_ = iarray;
1480b57cec5SDimitry Andric     for (long* p = __iarray_ + __iarray_size_; p < __iarray_ + newcap; ++p)
1490b57cec5SDimitry Andric       *p = 0;
1500b57cec5SDimitry Andric     __iarray_cap_ = newcap;
1510b57cec5SDimitry Andric   }
1520b57cec5SDimitry Andric   __iarray_size_ = max<size_t>(__iarray_size_, req_size);
1530b57cec5SDimitry Andric   return __iarray_[index];
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
pword(int index)156cb14a3feSDimitry Andric void*& ios_base::pword(int index) {
1570b57cec5SDimitry Andric   size_t req_size = static_cast<size_t>(index) + 1;
158cb14a3feSDimitry Andric   if (req_size > __parray_cap_) {
1590b57cec5SDimitry Andric     size_t newcap = __ios_new_cap<void*>(req_size, __iarray_cap_);
1600b57cec5SDimitry Andric     void** parray = static_cast<void**>(realloc(__parray_, newcap * sizeof(void*)));
161cb14a3feSDimitry Andric     if (parray == 0) {
1620b57cec5SDimitry Andric       setstate(badbit);
1630b57cec5SDimitry Andric       static void* error;
1640b57cec5SDimitry Andric       error = 0;
1650b57cec5SDimitry Andric       return error;
1660b57cec5SDimitry Andric     }
1670b57cec5SDimitry Andric     __parray_ = parray;
1680b57cec5SDimitry Andric     for (void** p = __parray_ + __parray_size_; p < __parray_ + newcap; ++p)
1690b57cec5SDimitry Andric       *p = 0;
1700b57cec5SDimitry Andric     __parray_cap_ = newcap;
1710b57cec5SDimitry Andric   }
1720b57cec5SDimitry Andric   __parray_size_ = max<size_t>(__parray_size_, req_size);
1730b57cec5SDimitry Andric   return __parray_[index];
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric // register_callback
1770b57cec5SDimitry Andric 
register_callback(event_callback fn,int index)178cb14a3feSDimitry Andric void ios_base::register_callback(event_callback fn, int index) {
1790b57cec5SDimitry Andric   size_t req_size = __event_size_ + 1;
180cb14a3feSDimitry Andric   if (req_size > __event_cap_) {
1810b57cec5SDimitry Andric     size_t newcap       = __ios_new_cap<event_callback>(req_size, __event_cap_);
1820b57cec5SDimitry Andric     event_callback* fns = static_cast<event_callback*>(realloc(__fn_, newcap * sizeof(event_callback)));
1830b57cec5SDimitry Andric     if (fns == 0)
1840b57cec5SDimitry Andric       setstate(badbit);
1850b57cec5SDimitry Andric     __fn_      = fns;
1860b57cec5SDimitry Andric     int* indxs = static_cast<int*>(realloc(__index_, newcap * sizeof(int)));
1870b57cec5SDimitry Andric     if (indxs == 0)
1880b57cec5SDimitry Andric       setstate(badbit);
1890b57cec5SDimitry Andric     __index_     = indxs;
1900b57cec5SDimitry Andric     __event_cap_ = newcap;
1910b57cec5SDimitry Andric   }
1920b57cec5SDimitry Andric   __fn_[__event_size_]    = fn;
1930b57cec5SDimitry Andric   __index_[__event_size_] = index;
1940b57cec5SDimitry Andric   ++__event_size_;
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric 
~ios_base()197cb14a3feSDimitry Andric ios_base::~ios_base() {
198*0fca6ea1SDimitry Andric   // Avoid UB when not properly initialized. See ios_base::ios_base for
199*0fca6ea1SDimitry Andric   // more information.
200*0fca6ea1SDimitry Andric   if (!__loc_)
201*0fca6ea1SDimitry Andric     return;
2020b57cec5SDimitry Andric   __call_callbacks(erase_event);
2030b57cec5SDimitry Andric   locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
2040b57cec5SDimitry Andric   loc_storage.~locale();
2050b57cec5SDimitry Andric   free(__fn_);
2060b57cec5SDimitry Andric   free(__index_);
2070b57cec5SDimitry Andric   free(__iarray_);
2080b57cec5SDimitry Andric   free(__parray_);
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric // iostate
2120b57cec5SDimitry Andric 
clear(iostate state)213cb14a3feSDimitry Andric void ios_base::clear(iostate state) {
2140b57cec5SDimitry Andric   if (__rdbuf_)
2150b57cec5SDimitry Andric     __rdstate_ = state;
2160b57cec5SDimitry Andric   else
2170b57cec5SDimitry Andric     __rdstate_ = state | badbit;
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric   if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0)
2200b57cec5SDimitry Andric     __throw_failure("ios_base::clear");
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric // init
2240b57cec5SDimitry Andric 
init(void * sb)225cb14a3feSDimitry Andric void ios_base::init(void* sb) {
2260b57cec5SDimitry Andric   __rdbuf_       = sb;
2270b57cec5SDimitry Andric   __rdstate_     = __rdbuf_ ? goodbit : badbit;
2280b57cec5SDimitry Andric   __exceptions_  = goodbit;
2290b57cec5SDimitry Andric   __fmtflags_    = skipws | dec;
2300b57cec5SDimitry Andric   __width_       = 0;
2310b57cec5SDimitry Andric   __precision_   = 6;
2320b57cec5SDimitry Andric   __fn_          = 0;
2330b57cec5SDimitry Andric   __index_       = 0;
2340b57cec5SDimitry Andric   __event_size_  = 0;
2350b57cec5SDimitry Andric   __event_cap_   = 0;
2360b57cec5SDimitry Andric   __iarray_      = 0;
2370b57cec5SDimitry Andric   __iarray_size_ = 0;
2380b57cec5SDimitry Andric   __iarray_cap_  = 0;
2390b57cec5SDimitry Andric   __parray_      = 0;
2400b57cec5SDimitry Andric   __parray_size_ = 0;
2410b57cec5SDimitry Andric   __parray_cap_  = 0;
2420b57cec5SDimitry Andric   ::new (&__loc_) locale;
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
copyfmt(const ios_base & rhs)245cb14a3feSDimitry Andric void ios_base::copyfmt(const ios_base& rhs) {
2460b57cec5SDimitry Andric   // If we can't acquire the needed resources, throw bad_alloc (can't set badbit)
2470b57cec5SDimitry Andric   // Don't alter *this until all needed resources are acquired
2480b57cec5SDimitry Andric   unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free);
2490b57cec5SDimitry Andric   unique_ptr<int, void (*)(void*)> new_ints(0, free);
2500b57cec5SDimitry Andric   unique_ptr<long, void (*)(void*)> new_longs(0, free);
2510b57cec5SDimitry Andric   unique_ptr<void*, void (*)(void*)> new_pointers(0, free);
252cb14a3feSDimitry Andric   if (__event_cap_ < rhs.__event_size_) {
2530b57cec5SDimitry Andric     size_t newesize = sizeof(event_callback) * rhs.__event_size_;
2540b57cec5SDimitry Andric     new_callbacks.reset(static_cast<event_callback*>(malloc(newesize)));
2550b57cec5SDimitry Andric     if (!new_callbacks)
2560b57cec5SDimitry Andric       __throw_bad_alloc();
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric     size_t newisize = sizeof(int) * rhs.__event_size_;
2590b57cec5SDimitry Andric     new_ints.reset(static_cast<int*>(malloc(newisize)));
2600b57cec5SDimitry Andric     if (!new_ints)
2610b57cec5SDimitry Andric       __throw_bad_alloc();
2620b57cec5SDimitry Andric   }
263cb14a3feSDimitry Andric   if (__iarray_cap_ < rhs.__iarray_size_) {
2640b57cec5SDimitry Andric     size_t newsize = sizeof(long) * rhs.__iarray_size_;
2650b57cec5SDimitry Andric     new_longs.reset(static_cast<long*>(malloc(newsize)));
2660b57cec5SDimitry Andric     if (!new_longs)
2670b57cec5SDimitry Andric       __throw_bad_alloc();
2680b57cec5SDimitry Andric   }
269cb14a3feSDimitry Andric   if (__parray_cap_ < rhs.__parray_size_) {
2700b57cec5SDimitry Andric     size_t newsize = sizeof(void*) * rhs.__parray_size_;
2710b57cec5SDimitry Andric     new_pointers.reset(static_cast<void**>(malloc(newsize)));
2720b57cec5SDimitry Andric     if (!new_pointers)
2730b57cec5SDimitry Andric       __throw_bad_alloc();
2740b57cec5SDimitry Andric   }
2750b57cec5SDimitry Andric   // Got everything we need.  Copy everything but __rdstate_, __rdbuf_ and __exceptions_
2760b57cec5SDimitry Andric   __fmtflags_           = rhs.__fmtflags_;
2770b57cec5SDimitry Andric   __precision_          = rhs.__precision_;
2780b57cec5SDimitry Andric   __width_              = rhs.__width_;
2790b57cec5SDimitry Andric   locale& lhs_loc       = *reinterpret_cast<locale*>(&__loc_);
2800b57cec5SDimitry Andric   const locale& rhs_loc = *reinterpret_cast<const locale*>(&rhs.__loc_);
2810b57cec5SDimitry Andric   lhs_loc               = rhs_loc;
282cb14a3feSDimitry Andric   if (__event_cap_ < rhs.__event_size_) {
2830b57cec5SDimitry Andric     free(__fn_);
2840b57cec5SDimitry Andric     __fn_ = new_callbacks.release();
2850b57cec5SDimitry Andric     free(__index_);
2860b57cec5SDimitry Andric     __index_     = new_ints.release();
2870b57cec5SDimitry Andric     __event_cap_ = rhs.__event_size_;
2880b57cec5SDimitry Andric   }
289cb14a3feSDimitry Andric   for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_) {
2900b57cec5SDimitry Andric     __fn_[__event_size_]    = rhs.__fn_[__event_size_];
2910b57cec5SDimitry Andric     __index_[__event_size_] = rhs.__index_[__event_size_];
2920b57cec5SDimitry Andric   }
293cb14a3feSDimitry Andric   if (__iarray_cap_ < rhs.__iarray_size_) {
2940b57cec5SDimitry Andric     free(__iarray_);
2950b57cec5SDimitry Andric     __iarray_     = new_longs.release();
2960b57cec5SDimitry Andric     __iarray_cap_ = rhs.__iarray_size_;
2970b57cec5SDimitry Andric   }
2980b57cec5SDimitry Andric   for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_)
2990b57cec5SDimitry Andric     __iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_];
300cb14a3feSDimitry Andric   if (__parray_cap_ < rhs.__parray_size_) {
3010b57cec5SDimitry Andric     free(__parray_);
3020b57cec5SDimitry Andric     __parray_     = new_pointers.release();
3030b57cec5SDimitry Andric     __parray_cap_ = rhs.__parray_size_;
3040b57cec5SDimitry Andric   }
3050b57cec5SDimitry Andric   for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_)
3060b57cec5SDimitry Andric     __parray_[__parray_size_] = rhs.__parray_[__parray_size_];
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric 
move(ios_base & rhs)309cb14a3feSDimitry Andric void ios_base::move(ios_base& rhs) {
3100b57cec5SDimitry Andric   // *this is uninitialized
3110b57cec5SDimitry Andric   __fmtflags_     = rhs.__fmtflags_;
3120b57cec5SDimitry Andric   __precision_    = rhs.__precision_;
3130b57cec5SDimitry Andric   __width_        = rhs.__width_;
3140b57cec5SDimitry Andric   __rdstate_      = rhs.__rdstate_;
3150b57cec5SDimitry Andric   __exceptions_   = rhs.__exceptions_;
3160b57cec5SDimitry Andric   __rdbuf_        = 0;
3170b57cec5SDimitry Andric   locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
3180b57cec5SDimitry Andric   ::new (&__loc_) locale(rhs_loc);
3190b57cec5SDimitry Andric   __fn_              = rhs.__fn_;
3200b57cec5SDimitry Andric   rhs.__fn_          = 0;
3210b57cec5SDimitry Andric   __index_           = rhs.__index_;
3220b57cec5SDimitry Andric   rhs.__index_       = 0;
3230b57cec5SDimitry Andric   __event_size_      = rhs.__event_size_;
3240b57cec5SDimitry Andric   rhs.__event_size_  = 0;
3250b57cec5SDimitry Andric   __event_cap_       = rhs.__event_cap_;
3260b57cec5SDimitry Andric   rhs.__event_cap_   = 0;
3270b57cec5SDimitry Andric   __iarray_          = rhs.__iarray_;
3280b57cec5SDimitry Andric   rhs.__iarray_      = 0;
3290b57cec5SDimitry Andric   __iarray_size_     = rhs.__iarray_size_;
3300b57cec5SDimitry Andric   rhs.__iarray_size_ = 0;
3310b57cec5SDimitry Andric   __iarray_cap_      = rhs.__iarray_cap_;
3320b57cec5SDimitry Andric   rhs.__iarray_cap_  = 0;
3330b57cec5SDimitry Andric   __parray_          = rhs.__parray_;
3340b57cec5SDimitry Andric   rhs.__parray_      = 0;
3350b57cec5SDimitry Andric   __parray_size_     = rhs.__parray_size_;
3360b57cec5SDimitry Andric   rhs.__parray_size_ = 0;
3370b57cec5SDimitry Andric   __parray_cap_      = rhs.__parray_cap_;
3380b57cec5SDimitry Andric   rhs.__parray_cap_  = 0;
3390b57cec5SDimitry Andric }
3400b57cec5SDimitry Andric 
swap(ios_base & rhs)341cb14a3feSDimitry Andric void ios_base::swap(ios_base& rhs) noexcept {
3425f757f3fSDimitry Andric   std::swap(__fmtflags_, rhs.__fmtflags_);
3435f757f3fSDimitry Andric   std::swap(__precision_, rhs.__precision_);
3445f757f3fSDimitry Andric   std::swap(__width_, rhs.__width_);
3455f757f3fSDimitry Andric   std::swap(__rdstate_, rhs.__rdstate_);
3465f757f3fSDimitry Andric   std::swap(__exceptions_, rhs.__exceptions_);
3470b57cec5SDimitry Andric   locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
3480b57cec5SDimitry Andric   locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
3495f757f3fSDimitry Andric   std::swap(lhs_loc, rhs_loc);
3505f757f3fSDimitry Andric   std::swap(__fn_, rhs.__fn_);
3515f757f3fSDimitry Andric   std::swap(__index_, rhs.__index_);
3525f757f3fSDimitry Andric   std::swap(__event_size_, rhs.__event_size_);
3535f757f3fSDimitry Andric   std::swap(__event_cap_, rhs.__event_cap_);
3545f757f3fSDimitry Andric   std::swap(__iarray_, rhs.__iarray_);
3555f757f3fSDimitry Andric   std::swap(__iarray_size_, rhs.__iarray_size_);
3565f757f3fSDimitry Andric   std::swap(__iarray_cap_, rhs.__iarray_cap_);
3575f757f3fSDimitry Andric   std::swap(__parray_, rhs.__parray_);
3585f757f3fSDimitry Andric   std::swap(__parray_size_, rhs.__parray_size_);
3595f757f3fSDimitry Andric   std::swap(__parray_cap_, rhs.__parray_cap_);
3600b57cec5SDimitry Andric }
3610b57cec5SDimitry Andric 
__set_badbit_and_consider_rethrow()362cb14a3feSDimitry Andric void ios_base::__set_badbit_and_consider_rethrow() {
3630b57cec5SDimitry Andric   __rdstate_ |= badbit;
36406c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
3650b57cec5SDimitry Andric   if (__exceptions_ & badbit)
3660b57cec5SDimitry Andric     throw;
36706c3fb27SDimitry Andric #endif // _LIBCPP_HAS_NO_EXCEPTIONS
3680b57cec5SDimitry Andric }
3690b57cec5SDimitry Andric 
__set_failbit_and_consider_rethrow()370cb14a3feSDimitry Andric void ios_base::__set_failbit_and_consider_rethrow() {
3710b57cec5SDimitry Andric   __rdstate_ |= failbit;
37206c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
3730b57cec5SDimitry Andric   if (__exceptions_ & failbit)
3740b57cec5SDimitry Andric     throw;
37506c3fb27SDimitry Andric #endif // _LIBCPP_HAS_NO_EXCEPTIONS
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric 
sync_with_stdio(bool sync)378cb14a3feSDimitry Andric bool ios_base::sync_with_stdio(bool sync) {
3790b57cec5SDimitry Andric   static bool previous_state = true;
3800b57cec5SDimitry Andric   bool r                     = previous_state;
3810b57cec5SDimitry Andric   previous_state             = sync;
3820b57cec5SDimitry Andric   return r;
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
38681ad6265SDimitry Andric 
38781ad6265SDimitry Andric _LIBCPP_POP_MACROS
388