Path: blob/main/contrib/llvm-project/libcxx/src/ios.cpp
35147 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <__config>9#include <__locale>10#include <algorithm>11#include <ios>12#include <limits>13#include <memory>14#include <new>15#include <stdlib.h>16#include <string>1718#include "include/config_elast.h"1920_LIBCPP_PUSH_MACROS21#include <__undef_macros>2223_LIBCPP_BEGIN_NAMESPACE_STD2425class _LIBCPP_HIDDEN __iostream_category : public __do_message {26public:27virtual const char* name() const noexcept;28virtual string message(int ev) const;29};3031const char* __iostream_category::name() const noexcept { return "iostream"; }3233string __iostream_category::message(int ev) const {34if (ev != static_cast<int>(io_errc::stream)35#ifdef _LIBCPP_ELAST36&& ev <= _LIBCPP_ELAST37#endif // _LIBCPP_ELAST38)39return __do_message::message(ev);40return string("unspecified iostream_category error");41}4243const error_category& iostream_category() noexcept {44union AvoidDestroyingIostreamCategory {45__iostream_category iostream_error_category;46constexpr explicit AvoidDestroyingIostreamCategory() : iostream_error_category() {}47~AvoidDestroyingIostreamCategory() {}48};49constinit static AvoidDestroyingIostreamCategory helper;50return helper.iostream_error_category;51}5253// ios_base::failure5455ios_base::failure::failure(const string& msg, const error_code& ec) : system_error(ec, msg) {}5657ios_base::failure::failure(const char* msg, const error_code& ec) : system_error(ec, msg) {}5859ios_base::failure::~failure() throw() {}6061// ios_base locale6263const ios_base::fmtflags ios_base::boolalpha;64const ios_base::fmtflags ios_base::dec;65const ios_base::fmtflags ios_base::fixed;66const ios_base::fmtflags ios_base::hex;67const ios_base::fmtflags ios_base::internal;68const ios_base::fmtflags ios_base::left;69const ios_base::fmtflags ios_base::oct;70const ios_base::fmtflags ios_base::right;71const ios_base::fmtflags ios_base::scientific;72const ios_base::fmtflags ios_base::showbase;73const ios_base::fmtflags ios_base::showpoint;74const ios_base::fmtflags ios_base::showpos;75const ios_base::fmtflags ios_base::skipws;76const ios_base::fmtflags ios_base::unitbuf;77const ios_base::fmtflags ios_base::uppercase;78const ios_base::fmtflags ios_base::adjustfield;79const ios_base::fmtflags ios_base::basefield;80const ios_base::fmtflags ios_base::floatfield;8182const ios_base::iostate ios_base::badbit;83const ios_base::iostate ios_base::eofbit;84const ios_base::iostate ios_base::failbit;85const ios_base::iostate ios_base::goodbit;8687const ios_base::openmode ios_base::app;88const ios_base::openmode ios_base::ate;89const ios_base::openmode ios_base::binary;90const ios_base::openmode ios_base::in;91const ios_base::openmode ios_base::out;92const ios_base::openmode ios_base::trunc;9394void ios_base::__call_callbacks(event ev) {95for (size_t i = __event_size_; i;) {96--i;97__fn_[i](ev, *this, __index_[i]);98}99}100101// locale102103locale ios_base::imbue(const locale& newloc) {104static_assert(sizeof(locale) == sizeof(__loc_), "");105locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);106locale oldloc = loc_storage;107loc_storage = newloc;108__call_callbacks(imbue_event);109return oldloc;110}111112locale ios_base::getloc() const {113const locale& loc_storage = *reinterpret_cast<const locale*>(&__loc_);114return loc_storage;115}116117// xalloc118#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)119atomic<int> ios_base::__xindex_{0};120#else121int ios_base::__xindex_ = 0;122#endif123124template <typename _Tp>125static size_t __ios_new_cap(size_t __req_size, size_t __current_cap) { // Precondition: __req_size > __current_cap126const size_t mx = std::numeric_limits<size_t>::max() / sizeof(_Tp);127if (__req_size < mx / 2)128return std::max(2 * __current_cap, __req_size);129else130return mx;131}132133int ios_base::xalloc() { return __xindex_++; }134135long& ios_base::iword(int index) {136size_t req_size = static_cast<size_t>(index) + 1;137if (req_size > __iarray_cap_) {138size_t newcap = __ios_new_cap<long>(req_size, __iarray_cap_);139long* iarray = static_cast<long*>(realloc(__iarray_, newcap * sizeof(long)));140if (iarray == 0) {141setstate(badbit);142static long error;143error = 0;144return error;145}146__iarray_ = iarray;147for (long* p = __iarray_ + __iarray_size_; p < __iarray_ + newcap; ++p)148*p = 0;149__iarray_cap_ = newcap;150}151__iarray_size_ = max<size_t>(__iarray_size_, req_size);152return __iarray_[index];153}154155void*& ios_base::pword(int index) {156size_t req_size = static_cast<size_t>(index) + 1;157if (req_size > __parray_cap_) {158size_t newcap = __ios_new_cap<void*>(req_size, __iarray_cap_);159void** parray = static_cast<void**>(realloc(__parray_, newcap * sizeof(void*)));160if (parray == 0) {161setstate(badbit);162static void* error;163error = 0;164return error;165}166__parray_ = parray;167for (void** p = __parray_ + __parray_size_; p < __parray_ + newcap; ++p)168*p = 0;169__parray_cap_ = newcap;170}171__parray_size_ = max<size_t>(__parray_size_, req_size);172return __parray_[index];173}174175// register_callback176177void ios_base::register_callback(event_callback fn, int index) {178size_t req_size = __event_size_ + 1;179if (req_size > __event_cap_) {180size_t newcap = __ios_new_cap<event_callback>(req_size, __event_cap_);181event_callback* fns = static_cast<event_callback*>(realloc(__fn_, newcap * sizeof(event_callback)));182if (fns == 0)183setstate(badbit);184__fn_ = fns;185int* indxs = static_cast<int*>(realloc(__index_, newcap * sizeof(int)));186if (indxs == 0)187setstate(badbit);188__index_ = indxs;189__event_cap_ = newcap;190}191__fn_[__event_size_] = fn;192__index_[__event_size_] = index;193++__event_size_;194}195196ios_base::~ios_base() {197// Avoid UB when not properly initialized. See ios_base::ios_base for198// more information.199if (!__loc_)200return;201__call_callbacks(erase_event);202locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);203loc_storage.~locale();204free(__fn_);205free(__index_);206free(__iarray_);207free(__parray_);208}209210// iostate211212void ios_base::clear(iostate state) {213if (__rdbuf_)214__rdstate_ = state;215else216__rdstate_ = state | badbit;217218if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0)219__throw_failure("ios_base::clear");220}221222// init223224void ios_base::init(void* sb) {225__rdbuf_ = sb;226__rdstate_ = __rdbuf_ ? goodbit : badbit;227__exceptions_ = goodbit;228__fmtflags_ = skipws | dec;229__width_ = 0;230__precision_ = 6;231__fn_ = 0;232__index_ = 0;233__event_size_ = 0;234__event_cap_ = 0;235__iarray_ = 0;236__iarray_size_ = 0;237__iarray_cap_ = 0;238__parray_ = 0;239__parray_size_ = 0;240__parray_cap_ = 0;241::new (&__loc_) locale;242}243244void ios_base::copyfmt(const ios_base& rhs) {245// If we can't acquire the needed resources, throw bad_alloc (can't set badbit)246// Don't alter *this until all needed resources are acquired247unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free);248unique_ptr<int, void (*)(void*)> new_ints(0, free);249unique_ptr<long, void (*)(void*)> new_longs(0, free);250unique_ptr<void*, void (*)(void*)> new_pointers(0, free);251if (__event_cap_ < rhs.__event_size_) {252size_t newesize = sizeof(event_callback) * rhs.__event_size_;253new_callbacks.reset(static_cast<event_callback*>(malloc(newesize)));254if (!new_callbacks)255__throw_bad_alloc();256257size_t newisize = sizeof(int) * rhs.__event_size_;258new_ints.reset(static_cast<int*>(malloc(newisize)));259if (!new_ints)260__throw_bad_alloc();261}262if (__iarray_cap_ < rhs.__iarray_size_) {263size_t newsize = sizeof(long) * rhs.__iarray_size_;264new_longs.reset(static_cast<long*>(malloc(newsize)));265if (!new_longs)266__throw_bad_alloc();267}268if (__parray_cap_ < rhs.__parray_size_) {269size_t newsize = sizeof(void*) * rhs.__parray_size_;270new_pointers.reset(static_cast<void**>(malloc(newsize)));271if (!new_pointers)272__throw_bad_alloc();273}274// Got everything we need. Copy everything but __rdstate_, __rdbuf_ and __exceptions_275__fmtflags_ = rhs.__fmtflags_;276__precision_ = rhs.__precision_;277__width_ = rhs.__width_;278locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);279const locale& rhs_loc = *reinterpret_cast<const locale*>(&rhs.__loc_);280lhs_loc = rhs_loc;281if (__event_cap_ < rhs.__event_size_) {282free(__fn_);283__fn_ = new_callbacks.release();284free(__index_);285__index_ = new_ints.release();286__event_cap_ = rhs.__event_size_;287}288for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_) {289__fn_[__event_size_] = rhs.__fn_[__event_size_];290__index_[__event_size_] = rhs.__index_[__event_size_];291}292if (__iarray_cap_ < rhs.__iarray_size_) {293free(__iarray_);294__iarray_ = new_longs.release();295__iarray_cap_ = rhs.__iarray_size_;296}297for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_)298__iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_];299if (__parray_cap_ < rhs.__parray_size_) {300free(__parray_);301__parray_ = new_pointers.release();302__parray_cap_ = rhs.__parray_size_;303}304for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_)305__parray_[__parray_size_] = rhs.__parray_[__parray_size_];306}307308void ios_base::move(ios_base& rhs) {309// *this is uninitialized310__fmtflags_ = rhs.__fmtflags_;311__precision_ = rhs.__precision_;312__width_ = rhs.__width_;313__rdstate_ = rhs.__rdstate_;314__exceptions_ = rhs.__exceptions_;315__rdbuf_ = 0;316locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);317::new (&__loc_) locale(rhs_loc);318__fn_ = rhs.__fn_;319rhs.__fn_ = 0;320__index_ = rhs.__index_;321rhs.__index_ = 0;322__event_size_ = rhs.__event_size_;323rhs.__event_size_ = 0;324__event_cap_ = rhs.__event_cap_;325rhs.__event_cap_ = 0;326__iarray_ = rhs.__iarray_;327rhs.__iarray_ = 0;328__iarray_size_ = rhs.__iarray_size_;329rhs.__iarray_size_ = 0;330__iarray_cap_ = rhs.__iarray_cap_;331rhs.__iarray_cap_ = 0;332__parray_ = rhs.__parray_;333rhs.__parray_ = 0;334__parray_size_ = rhs.__parray_size_;335rhs.__parray_size_ = 0;336__parray_cap_ = rhs.__parray_cap_;337rhs.__parray_cap_ = 0;338}339340void ios_base::swap(ios_base& rhs) noexcept {341std::swap(__fmtflags_, rhs.__fmtflags_);342std::swap(__precision_, rhs.__precision_);343std::swap(__width_, rhs.__width_);344std::swap(__rdstate_, rhs.__rdstate_);345std::swap(__exceptions_, rhs.__exceptions_);346locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);347locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);348std::swap(lhs_loc, rhs_loc);349std::swap(__fn_, rhs.__fn_);350std::swap(__index_, rhs.__index_);351std::swap(__event_size_, rhs.__event_size_);352std::swap(__event_cap_, rhs.__event_cap_);353std::swap(__iarray_, rhs.__iarray_);354std::swap(__iarray_size_, rhs.__iarray_size_);355std::swap(__iarray_cap_, rhs.__iarray_cap_);356std::swap(__parray_, rhs.__parray_);357std::swap(__parray_size_, rhs.__parray_size_);358std::swap(__parray_cap_, rhs.__parray_cap_);359}360361void ios_base::__set_badbit_and_consider_rethrow() {362__rdstate_ |= badbit;363#ifndef _LIBCPP_HAS_NO_EXCEPTIONS364if (__exceptions_ & badbit)365throw;366#endif // _LIBCPP_HAS_NO_EXCEPTIONS367}368369void ios_base::__set_failbit_and_consider_rethrow() {370__rdstate_ |= failbit;371#ifndef _LIBCPP_HAS_NO_EXCEPTIONS372if (__exceptions_ & failbit)373throw;374#endif // _LIBCPP_HAS_NO_EXCEPTIONS375}376377bool ios_base::sync_with_stdio(bool sync) {378static bool previous_state = true;379bool r = previous_state;380previous_state = sync;381return r;382}383384_LIBCPP_END_NAMESPACE_STD385386_LIBCPP_POP_MACROS387388389