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