| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include <__config> |
| 10 | #include <__locale_dir/locale.h> |
| 11 | #include <algorithm> |
| 12 | #include <atomic> |
| 13 | #include <ios> |
| 14 | #include <limits> |
| 15 | #include <memory> |
| 16 | #include <new> |
| 17 | #include <stdlib.h> |
| 18 | #include <string> |
| 19 | |
| 20 | #include "include/config_elast.h" |
| 21 | |
| 22 | _LIBCPP_PUSH_MACROS |
| 23 | #include <__undef_macros> |
| 24 | |
| 25 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 26 | _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS |
| 27 | |
| 28 | class _LIBCPP_HIDDEN __iostream_category : public __do_message { |
| 29 | public: |
| 30 | virtual const char* name() const noexcept; |
| 31 | virtual string message(int ev) const; |
| 32 | }; |
| 33 | |
| 34 | const char* __iostream_category::name() const noexcept { return "iostream" ; } |
| 35 | |
| 36 | string __iostream_category::message(int ev) const { |
| 37 | if (ev != static_cast<int>(io_errc::stream) |
| 38 | #ifdef _LIBCPP_ELAST |
| 39 | && ev <= _LIBCPP_ELAST |
| 40 | #endif // _LIBCPP_ELAST |
| 41 | ) |
| 42 | return __do_message::message(ev: ev); |
| 43 | return string("unspecified iostream_category error" ); |
| 44 | } |
| 45 | |
| 46 | const error_category& iostream_category() noexcept { |
| 47 | union AvoidDestroyingIostreamCategory { |
| 48 | __iostream_category iostream_error_category; |
| 49 | constexpr explicit AvoidDestroyingIostreamCategory() : iostream_error_category() {} |
| 50 | ~AvoidDestroyingIostreamCategory() {} |
| 51 | }; |
| 52 | // Use no-destroy since this removes the function local static guard variable. |
| 53 | _LIBCPP_NO_DESTROY constinit static AvoidDestroyingIostreamCategory helper; |
| 54 | return helper.iostream_error_category; |
| 55 | } |
| 56 | |
| 57 | // ios_base::failure |
| 58 | |
| 59 | ios_base::failure::failure(const string& msg, const error_code& ec) : system_error(ec, msg) {} |
| 60 | |
| 61 | ios_base::failure::failure(const char* msg, const error_code& ec) : system_error(ec, msg) {} |
| 62 | |
| 63 | ios_base::failure::~failure() throw() {} |
| 64 | |
| 65 | // ios_base locale |
| 66 | |
| 67 | const ios_base::fmtflags ios_base::boolalpha; |
| 68 | const ios_base::fmtflags ios_base::dec; |
| 69 | const ios_base::fmtflags ios_base::fixed; |
| 70 | const ios_base::fmtflags ios_base::hex; |
| 71 | const ios_base::fmtflags ios_base::internal; |
| 72 | const ios_base::fmtflags ios_base::left; |
| 73 | const ios_base::fmtflags ios_base::oct; |
| 74 | const ios_base::fmtflags ios_base::right; |
| 75 | const ios_base::fmtflags ios_base::scientific; |
| 76 | const ios_base::fmtflags ios_base::showbase; |
| 77 | const ios_base::fmtflags ios_base::showpoint; |
| 78 | const ios_base::fmtflags ios_base::showpos; |
| 79 | const ios_base::fmtflags ios_base::skipws; |
| 80 | const ios_base::fmtflags ios_base::unitbuf; |
| 81 | const ios_base::fmtflags ios_base::uppercase; |
| 82 | const ios_base::fmtflags ios_base::adjustfield; |
| 83 | const ios_base::fmtflags ios_base::basefield; |
| 84 | const ios_base::fmtflags ios_base::floatfield; |
| 85 | |
| 86 | const ios_base::iostate ios_base::badbit; |
| 87 | const ios_base::iostate ios_base::eofbit; |
| 88 | const ios_base::iostate ios_base::failbit; |
| 89 | const ios_base::iostate ios_base::goodbit; |
| 90 | |
| 91 | const ios_base::openmode ios_base::app; |
| 92 | const ios_base::openmode ios_base::ate; |
| 93 | const ios_base::openmode ios_base::binary; |
| 94 | const ios_base::openmode ios_base::in; |
| 95 | const ios_base::openmode ios_base::out; |
| 96 | const ios_base::openmode ios_base::trunc; |
| 97 | |
| 98 | void ios_base::__call_callbacks(event ev) { |
| 99 | for (size_t i = __event_size_; i;) { |
| 100 | --i; |
| 101 | __fn_[i](ev, *this, __index_[i]); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // locale |
| 106 | |
| 107 | locale ios_base::imbue(const locale& newloc) { |
| 108 | locale loc = newloc; |
| 109 | std::swap(lhs&: __loc_, rhs&: loc); |
| 110 | __call_callbacks(ev: imbue_event); |
| 111 | return loc; |
| 112 | } |
| 113 | |
| 114 | locale ios_base::getloc() const { return __loc_; } |
| 115 | |
| 116 | template <typename _Tp> |
| 117 | static size_t __ios_new_cap(size_t __req_size, size_t __current_cap) { // Precondition: __req_size > __current_cap |
| 118 | const size_t mx = std::numeric_limits<size_t>::max() / sizeof(_Tp); |
| 119 | if (__req_size < mx / 2) |
| 120 | return std::max(a: 2 * __current_cap, b: __req_size); |
| 121 | else |
| 122 | return mx; |
| 123 | } |
| 124 | |
| 125 | int ios_base::xalloc() { |
| 126 | #if _LIBCPP_HAS_THREADS |
| 127 | constinit static atomic<int> xindex = 0; |
| 128 | #else |
| 129 | // If we don't have atomics, fall back to single-threaded implementation. |
| 130 | // FIXME: Should "single-threaded" be a separate option from |
| 131 | // _LIBCPP_HAS_THREADS? |
| 132 | static int xindex = 0; |
| 133 | #endif // _LIBCPP_HAS_THREADS |
| 134 | return xindex++; |
| 135 | } |
| 136 | |
| 137 | long& ios_base::iword(int index) { |
| 138 | size_t req_size = static_cast<size_t>(index) + 1; |
| 139 | if (req_size > __iarray_cap_) { |
| 140 | size_t newcap = __ios_new_cap<long>(req_size: req_size, current_cap: __iarray_cap_); |
| 141 | long* iarray = static_cast<long*>(realloc(ptr: __iarray_, size: newcap * sizeof(long))); |
| 142 | if (iarray == 0) { |
| 143 | setstate(badbit); |
| 144 | static long error; |
| 145 | error = 0; |
| 146 | return error; |
| 147 | } |
| 148 | __iarray_ = iarray; |
| 149 | for (long* p = __iarray_ + __iarray_size_; p < __iarray_ + newcap; ++p) |
| 150 | *p = 0; |
| 151 | __iarray_cap_ = newcap; |
| 152 | } |
| 153 | __iarray_size_ = max<size_t>(a: __iarray_size_, b: req_size); |
| 154 | return __iarray_[index]; |
| 155 | } |
| 156 | |
| 157 | void*& ios_base::pword(int index) { |
| 158 | size_t req_size = static_cast<size_t>(index) + 1; |
| 159 | if (req_size > __parray_cap_) { |
| 160 | size_t newcap = __ios_new_cap<void*>(req_size: req_size, current_cap: __iarray_cap_); |
| 161 | void** parray = static_cast<void**>(realloc(ptr: __parray_, size: newcap * sizeof(void*))); |
| 162 | if (parray == 0) { |
| 163 | setstate(badbit); |
| 164 | static void* error; |
| 165 | error = 0; |
| 166 | return error; |
| 167 | } |
| 168 | __parray_ = parray; |
| 169 | for (void** p = __parray_ + __parray_size_; p < __parray_ + newcap; ++p) |
| 170 | *p = 0; |
| 171 | __parray_cap_ = newcap; |
| 172 | } |
| 173 | __parray_size_ = max<size_t>(a: __parray_size_, b: req_size); |
| 174 | return __parray_[index]; |
| 175 | } |
| 176 | |
| 177 | // register_callback |
| 178 | |
| 179 | void ios_base::register_callback(event_callback fn, int index) { |
| 180 | size_t req_size = __event_size_ + 1; |
| 181 | if (req_size > __event_cap_) { |
| 182 | size_t newcap = __ios_new_cap<event_callback>(req_size: req_size, current_cap: __event_cap_); |
| 183 | event_callback* fns = static_cast<event_callback*>(realloc(ptr: __fn_, size: newcap * sizeof(event_callback))); |
| 184 | if (fns == 0) { |
| 185 | setstate(badbit); |
| 186 | return; |
| 187 | } |
| 188 | __fn_ = fns; |
| 189 | int* indxs = static_cast<int*>(realloc(ptr: __index_, size: newcap * sizeof(int))); |
| 190 | if (indxs == 0) { |
| 191 | setstate(badbit); |
| 192 | return; |
| 193 | } |
| 194 | __index_ = indxs; |
| 195 | __event_cap_ = newcap; |
| 196 | } |
| 197 | __fn_[__event_size_] = fn; |
| 198 | __index_[__event_size_] = index; |
| 199 | ++__event_size_; |
| 200 | } |
| 201 | |
| 202 | ios_base::~ios_base() { |
| 203 | // Avoid UB when not properly initialized. See ios_base::ios_base for |
| 204 | // more information. |
| 205 | if (!__loc_.__locale_) |
| 206 | return; |
| 207 | __call_callbacks(ev: erase_event); |
| 208 | __loc_.~locale(); |
| 209 | free(ptr: __fn_); |
| 210 | free(ptr: __index_); |
| 211 | free(ptr: __iarray_); |
| 212 | free(ptr: __parray_); |
| 213 | } |
| 214 | |
| 215 | // iostate |
| 216 | |
| 217 | void ios_base::clear(iostate state) { |
| 218 | if (__rdbuf_) |
| 219 | __rdstate_ = state; |
| 220 | else |
| 221 | __rdstate_ = state | badbit; |
| 222 | |
| 223 | if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0) |
| 224 | std::__throw_failure(msg: "ios_base::clear" ); |
| 225 | } |
| 226 | |
| 227 | // init |
| 228 | |
| 229 | void ios_base::init(void* sb) { |
| 230 | __rdbuf_ = sb; |
| 231 | __rdstate_ = __rdbuf_ ? goodbit : badbit; |
| 232 | __exceptions_ = goodbit; |
| 233 | __fmtflags_ = skipws | dec; |
| 234 | __width_ = 0; |
| 235 | __precision_ = 6; |
| 236 | __fn_ = 0; |
| 237 | __index_ = 0; |
| 238 | __event_size_ = 0; |
| 239 | __event_cap_ = 0; |
| 240 | __iarray_ = 0; |
| 241 | __iarray_size_ = 0; |
| 242 | __iarray_cap_ = 0; |
| 243 | __parray_ = 0; |
| 244 | __parray_size_ = 0; |
| 245 | __parray_cap_ = 0; |
| 246 | ::new (&__loc_) locale; |
| 247 | } |
| 248 | |
| 249 | void ios_base::copyfmt(const ios_base& rhs) { |
| 250 | // If we can't acquire the needed resources, throw bad_alloc (can't set badbit) |
| 251 | // Don't alter *this until all needed resources are acquired |
| 252 | unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free); |
| 253 | unique_ptr<int, void (*)(void*)> new_ints(0, free); |
| 254 | unique_ptr<long, void (*)(void*)> new_longs(0, free); |
| 255 | unique_ptr<void*, void (*)(void*)> new_pointers(0, free); |
| 256 | if (__event_cap_ < rhs.__event_size_) { |
| 257 | size_t newesize = sizeof(event_callback) * rhs.__event_size_; |
| 258 | new_callbacks.reset(p: static_cast<event_callback*>(malloc(size: newesize))); |
| 259 | if (!new_callbacks) |
| 260 | std::__throw_bad_alloc(); |
| 261 | |
| 262 | size_t newisize = sizeof(int) * rhs.__event_size_; |
| 263 | new_ints.reset(p: static_cast<int*>(malloc(size: newisize))); |
| 264 | if (!new_ints) |
| 265 | std::__throw_bad_alloc(); |
| 266 | } |
| 267 | if (__iarray_cap_ < rhs.__iarray_size_) { |
| 268 | size_t newsize = sizeof(long) * rhs.__iarray_size_; |
| 269 | new_longs.reset(p: static_cast<long*>(malloc(size: newsize))); |
| 270 | if (!new_longs) |
| 271 | std::__throw_bad_alloc(); |
| 272 | } |
| 273 | if (__parray_cap_ < rhs.__parray_size_) { |
| 274 | size_t newsize = sizeof(void*) * rhs.__parray_size_; |
| 275 | new_pointers.reset(p: static_cast<void**>(malloc(size: newsize))); |
| 276 | if (!new_pointers) |
| 277 | std::__throw_bad_alloc(); |
| 278 | } |
| 279 | // Got everything we need. Copy everything but __rdstate_, __rdbuf_ and __exceptions_ |
| 280 | __fmtflags_ = rhs.__fmtflags_; |
| 281 | __precision_ = rhs.__precision_; |
| 282 | __width_ = rhs.__width_; |
| 283 | __loc_ = rhs.__loc_; |
| 284 | if (__event_cap_ < rhs.__event_size_) { |
| 285 | free(ptr: __fn_); |
| 286 | __fn_ = new_callbacks.release(); |
| 287 | free(ptr: __index_); |
| 288 | __index_ = new_ints.release(); |
| 289 | __event_cap_ = rhs.__event_size_; |
| 290 | } |
| 291 | for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_) { |
| 292 | __fn_[__event_size_] = rhs.__fn_[__event_size_]; |
| 293 | __index_[__event_size_] = rhs.__index_[__event_size_]; |
| 294 | } |
| 295 | if (__iarray_cap_ < rhs.__iarray_size_) { |
| 296 | free(ptr: __iarray_); |
| 297 | __iarray_ = new_longs.release(); |
| 298 | __iarray_cap_ = rhs.__iarray_size_; |
| 299 | } |
| 300 | for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_) |
| 301 | __iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_]; |
| 302 | if (__parray_cap_ < rhs.__parray_size_) { |
| 303 | free(ptr: __parray_); |
| 304 | __parray_ = new_pointers.release(); |
| 305 | __parray_cap_ = rhs.__parray_size_; |
| 306 | } |
| 307 | for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_) |
| 308 | __parray_[__parray_size_] = rhs.__parray_[__parray_size_]; |
| 309 | } |
| 310 | |
| 311 | void ios_base::move(ios_base& rhs) { |
| 312 | // *this is uninitialized |
| 313 | __fmtflags_ = rhs.__fmtflags_; |
| 314 | __precision_ = rhs.__precision_; |
| 315 | __width_ = rhs.__width_; |
| 316 | __rdstate_ = rhs.__rdstate_; |
| 317 | __exceptions_ = rhs.__exceptions_; |
| 318 | __rdbuf_ = 0; |
| 319 | ::new (&__loc_) locale(rhs.__loc_); |
| 320 | __fn_ = rhs.__fn_; |
| 321 | rhs.__fn_ = 0; |
| 322 | __index_ = rhs.__index_; |
| 323 | rhs.__index_ = 0; |
| 324 | __event_size_ = rhs.__event_size_; |
| 325 | rhs.__event_size_ = 0; |
| 326 | __event_cap_ = rhs.__event_cap_; |
| 327 | rhs.__event_cap_ = 0; |
| 328 | __iarray_ = rhs.__iarray_; |
| 329 | rhs.__iarray_ = 0; |
| 330 | __iarray_size_ = rhs.__iarray_size_; |
| 331 | rhs.__iarray_size_ = 0; |
| 332 | __iarray_cap_ = rhs.__iarray_cap_; |
| 333 | rhs.__iarray_cap_ = 0; |
| 334 | __parray_ = rhs.__parray_; |
| 335 | rhs.__parray_ = 0; |
| 336 | __parray_size_ = rhs.__parray_size_; |
| 337 | rhs.__parray_size_ = 0; |
| 338 | __parray_cap_ = rhs.__parray_cap_; |
| 339 | rhs.__parray_cap_ = 0; |
| 340 | } |
| 341 | |
| 342 | void ios_base::swap(ios_base& rhs) noexcept { |
| 343 | std::swap(x&: __fmtflags_, y&: rhs.__fmtflags_); |
| 344 | std::swap(x&: __precision_, y&: rhs.__precision_); |
| 345 | std::swap(x&: __width_, y&: rhs.__width_); |
| 346 | std::swap(x&: __rdstate_, y&: rhs.__rdstate_); |
| 347 | std::swap(x&: __exceptions_, y&: rhs.__exceptions_); |
| 348 | std::swap(lhs&: __loc_, rhs&: rhs.__loc_); |
| 349 | std::swap(x&: __fn_, y&: rhs.__fn_); |
| 350 | std::swap(x&: __index_, y&: rhs.__index_); |
| 351 | std::swap(x&: __event_size_, y&: rhs.__event_size_); |
| 352 | std::swap(x&: __event_cap_, y&: rhs.__event_cap_); |
| 353 | std::swap(x&: __iarray_, y&: rhs.__iarray_); |
| 354 | std::swap(x&: __iarray_size_, y&: rhs.__iarray_size_); |
| 355 | std::swap(x&: __iarray_cap_, y&: rhs.__iarray_cap_); |
| 356 | std::swap(x&: __parray_, y&: rhs.__parray_); |
| 357 | std::swap(x&: __parray_size_, y&: rhs.__parray_size_); |
| 358 | std::swap(x&: __parray_cap_, y&: rhs.__parray_cap_); |
| 359 | } |
| 360 | |
| 361 | void ios_base::__set_badbit_and_consider_rethrow() { |
| 362 | __rdstate_ |= badbit; |
| 363 | #if _LIBCPP_HAS_EXCEPTIONS |
| 364 | if (__exceptions_ & badbit) |
| 365 | throw; |
| 366 | #endif // _LIBCPP_HAS_EXCEPTIONS |
| 367 | } |
| 368 | |
| 369 | void ios_base::__set_failbit_and_consider_rethrow() { |
| 370 | __rdstate_ |= failbit; |
| 371 | #if _LIBCPP_HAS_EXCEPTIONS |
| 372 | if (__exceptions_ & failbit) |
| 373 | throw; |
| 374 | #endif // _LIBCPP_HAS_EXCEPTIONS |
| 375 | } |
| 376 | |
| 377 | _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS |
| 378 | _LIBCPP_END_NAMESPACE_STD |
| 379 | |
| 380 | _LIBCPP_POP_MACROS |
| 381 | |