Skip to content

Commit

Permalink
fix wincrt
Browse files Browse the repository at this point in the history
  • Loading branch information
MacroModel committed Nov 27, 2024
1 parent 2c929d9 commit e9203da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 79 deletions.
2 changes: 0 additions & 2 deletions include/fast_io_core_impl/operations/scan_freestanding.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,8 @@ template <typename input, typename T>
}
else
{
#if 0
constexpr bool not_scannable{context_scannable<char_type,T>};
static_assert(not_scannable,"type not scannable. need context_scannable");
#endif
return false;
}
}
Expand Down
125 changes: 48 additions & 77 deletions include/fast_io_legacy_impl/c/wincrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,28 @@ inline void wincrt_fp_allocate_buffer_impl(FILE *__restrict fpp) noexcept
inline void wincrt_fp_write_cold_malloc_case_impl(FILE *__restrict fpp, char const *__restrict first,
::std::size_t diff)
{
crt_iobuf *fp{reinterpret_cast<crt_iobuf *>(fpp)};
if (diff == 0)
{
return;
}

crt_iobuf *fp{reinterpret_cast<crt_iobuf *>(fpp)};

::std::size_t allocated_buffer_size{wincrt_internal_buffer_size};

if (fp->_bufsiz >= 4)
{
allocated_buffer_size = static_cast<::std::size_t>(static_cast<::std::uint_least32_t>(fp->_bufsiz));
allocated_buffer_size >>= 2;
allocated_buffer_size <<= 2;
}

if (diff >= allocated_buffer_size)
{
#if 0
posix_write_nolock_impl(static_cast<::std::int_least32_t>(fp->_file),first,diff);
#else
::fast_io::posix_io_observer piob{static_cast<::std::int_least32_t>(fp->_file)};
::fast_io::operations::decay::write_all_decay(piob, first, first + diff);
#endif
::fast_io::details::posix_write_bytes_impl(fp->_file, reinterpret_cast<::std::byte const *>(first), reinterpret_cast<::std::byte const *>(first + diff));
return;
}

auto newbuffer{my_malloc_crt(allocated_buffer_size)};
my_memcpy(newbuffer, first, diff);
fp->_ptr = (fp->_base = reinterpret_cast<char *>(newbuffer)) + diff;
Expand All @@ -179,36 +179,33 @@ inline void wincrt_fp_write_cold_normal_case_impl(FILE *__restrict fpp, char con
::std::size_t diff)
{
crt_iobuf *fp{reinterpret_cast<crt_iobuf *>(fpp)};
::std::size_t remain{static_cast<::std::size_t>(static_cast<::std::uint_least32_t>(fp->_cnt))};
if (diff < remain)
{
remain = diff;
}
non_overlapped_copy_n(first, remain, fp->_ptr);
diff -= remain;
first += remain;
fp->_ptr += remain;
fp->_cnt = 0;
fp->_flag |= crt_dirty_value;
::fast_io::posix_io_observer piob{static_cast<::std::int_least32_t>(fp->_file)};
#if 0
posix_write_simple_impl(static_cast<::std::int_least32_t>(fp->_file),fp->_base,static_cast<::std::size_t>(fp->_ptr-fp->_base));
#else
::fast_io::operations::decay::write_all_decay(piob, fp->_base, fp->_ptr);
#endif
::std::size_t const bufsiz{static_cast<::std::size_t>(static_cast<::std::uint_least32_t>(fp->_bufsiz))};
if (diff >= bufsiz)

if (::std::size_t const remain{static_cast<::std::size_t>(static_cast<::std::uint_least32_t>(fp->_cnt))}; diff < remain)
{
#if 0
posix_write_nolock_impl(static_cast<::std::int_least32_t>(fp->_file),first,diff);
#else
::fast_io::operations::decay::write_all_decay(piob, first, first + diff);
#endif
fp->_ptr = non_overlapped_copy_n(first, diff, fp->_ptr);
fp->_cnt -= static_cast<::std::int_least32_t>(diff);
}
else
{
fp->_ptr = non_overlapped_copy_n(first, diff, fp->_base);
fp->_cnt = static_cast<::std::int_least32_t>(bufsiz - diff);
// flush
::fast_io::details::posix_write_bytes_impl(fp->_file, reinterpret_cast<::std::byte *>(fp->_base), reinterpret_cast<::std::byte *>(fp->_ptr));

if (::std::size_t const bufsiz{static_cast<::std::size_t>(static_cast<::std::uint_least32_t>(fp->_bufsiz))}; diff >= bufsiz)
{
// set to begin
fp->_ptr = fp->_base;
fp->_cnt = static_cast<::std::int_least32_t>(bufsiz);

// directly output
::fast_io::details::posix_write_bytes_impl(fp->_file, reinterpret_cast<::std::byte const *>(first), reinterpret_cast<::std::byte const *>(first + diff));
}
else
{
// Write from buffer begin
fp->_ptr = non_overlapped_copy_n(first, diff, fp->_base);
fp->_cnt = static_cast<::std::int_least32_t>(bufsiz - diff);
}
}
}

Expand Down Expand Up @@ -243,12 +240,10 @@ inline void wincrt_fp_overflow_impl(FILE *__restrict fpp, char_type ch)
}
else
{
#if 0
posix_write_simple_impl(static_cast<::std::int_least32_t>(fp->_file),fp->_base,static_cast<::std::size_t>(static_cast<::std::uint_least32_t>(fp->_bufsiz)));
#else
::fast_io::posix_io_observer piob{static_cast<::std::int_least32_t>(fp->_file)};
::fast_io::operations::decay::write_all_decay(piob, fp->_base, fp->_base + fp->_bufsiz);
#endif
// output existing content
::fast_io::details::posix_write_bytes_impl(fp->_file, reinterpret_cast<::std::byte const *>(fp->_base), reinterpret_cast<::std::byte const *>(fp->_ptr));
// ?? output all content ??
// ::fast_io::details::posix_write_bytes_impl(fp->_file, reinterpret_cast<::std::byte const *>(fp->_base), reinterpret_cast<::std::byte const *>(fp->_base + fp->_bufsiz));
}
fp->_ptr = fp->_base;
my_memcpy(fp->_ptr, __builtin_addressof(ch), sizeof(ch));
Expand All @@ -263,22 +258,12 @@ inline void wincrt_fp_overflow_impl(FILE *__restrict fpp, char_type ch)
inline void wincrt_fp_flush_stdout_impl()
{
crt_iobuf *fp{reinterpret_cast<crt_iobuf *>(::fast_io::win32::wincrt_acrt_iob_func(1))};
#if 1
if (fp->_ptr == fp->_base)
{
return;
}
::fast_io::posix_io_observer piob{static_cast<::std::int_least32_t>(fp->_file)};
::fast_io::operations::decay::write_all_decay(piob, fp->_base, fp->_ptr);
#else
::std::size_t diff{static_cast<::std::size_t>(fp->_ptr - fp->_base)};
// if(diff==0||!wincrt_fp_is_dirty_impl(fp))
if (diff == 0)
if (fp->_ptr == fp->_base) [[unlikely]]
{
return;
}
posix_write_simple_impl(static_cast<::std::int_least32_t>(fp->_file), fp->_base, diff);
#endif
::fast_io::details::posix_write_bytes_impl(fp->_file, reinterpret_cast<::std::byte const *>(fp->_base), reinterpret_cast<::std::byte const *>(fp->_ptr));

fp->_ptr = fp->_base;
}

Expand All @@ -304,12 +289,8 @@ inline char *wincrt_fp_read_cold_impl(FILE *__restrict fpp, char *first, ::std::

if (diff >= allocated_buffer_size)
{
#if 0
return posix_read_impl(static_cast<::std::int_least32_t>(fp->_file),first,diff);
#else
::fast_io::posix_io_observer piob{static_cast<::std::int_least32_t>(fp->_file)};
return ::fast_io::operations::decay::read_some_decay(piob, first, first + diff);
#endif
return reinterpret_cast<char *>(::fast_io::details::posix_read_bytes_impl(
fp->_file, reinterpret_cast<::std::byte *>(first), reinterpret_cast<::std::byte *>(first + diff)));
}
else
{
Expand All @@ -322,16 +303,10 @@ inline char *wincrt_fp_read_cold_impl(FILE *__restrict fpp, char *first, ::std::
fp->_flag |= crt_mybuf_value;
}

#if 0
::std::size_t readed{posix_read_impl(static_cast<::std::int_least32_t>(fp->_file),fp->_base,static_cast<::std::size_t>(static_cast<::std::uint_least32_t>(fp->_bufsiz)))};
#else
::std::size_t readed;
{
::fast_io::posix_io_observer piob{static_cast<::std::int_least32_t>(fp->_file)};
readed = static_cast<::std::size_t>(
::fast_io::operations::decay::read_some_decay(piob, fp->_base, fp->_base + fp->_bufsiz) - fp->_base);
}
#endif
::std::size_t readed{static_cast<::std::size_t>(::fast_io::details::posix_read_bytes_impl(
fp->_file, reinterpret_cast<::std::byte *>(fp->_base), reinterpret_cast<::std::byte *>(fp->_base + fp->_bufsiz)) -
reinterpret_cast<::std::byte *>(fp->_base))};

fp->_cnt = static_cast<::std::int_least32_t>(static_cast<::std::uint_least32_t>(readed));
fp->_ptr = fp->_base;
if (readed < diff)
Expand Down Expand Up @@ -360,16 +335,12 @@ inline bool wincrt_fp_underflow_impl(FILE *__restrict fpp)
{
wincrt_fp_allocate_buffer_impl(fpp);
}
#if 0
::std::size_t size{posix_read_impl(static_cast<::std::int_least32_t>(fp->_file),fp->_base,static_cast<::std::size_t>(static_cast<::std::uint_least32_t>(fp->_bufsiz)))};
#else
::std::size_t size;
{
::fast_io::posix_io_observer piob{static_cast<::std::int_least32_t>(fp->_file)};
size = static_cast<::std::size_t>(
::fast_io::operations::decay::read_some_decay(piob, fp->_base, fp->_base + fp->_bufsiz) - fp->_base);
}
#endif

::std::size_t size{static_cast<::std::size_t>(::fast_io::details::posix_read_bytes_impl(
fp->_file, reinterpret_cast<::std::byte *>(fp->_base), reinterpret_cast<::std::byte *>(fp->_base + fp->_bufsiz)) -
reinterpret_cast<::std::byte *>(fp->_base))};


fp->_ptr = fp->_base;
fp->_cnt = static_cast<::std::int_least32_t>(static_cast<::std::uint_least32_t>(size));
if constexpr (sizeof(char_type) == 1)
Expand Down

0 comments on commit e9203da

Please sign in to comment.