Skip to content

fix #1174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 25, 2025
Merged

fix #1174

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions include/fast_io_core_impl/allocation/adapters.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,13 @@ class generic_allocator_adapter
{
return allocator_type::reallocate_at_least(p, n).ptr;
}
else if constexpr (::fast_io::details::has_reallocate_aligned_impl<alloc>)
else if constexpr (::fast_io::details::has_reallocate_aligned_n_impl<alloc>)
{
return allocator_type::reallocate_aligned_n(p, default_alignment, n);
return allocator_type::reallocate_aligned_n(p, oldn, default_alignment, n);
}
else if constexpr (::fast_io::details::has_reallocate_aligned_at_least_impl<alloc>)
else if constexpr (::fast_io::details::has_reallocate_aligned_n_at_least_impl<alloc>)
{
return allocator_type::reallocate_aligned_n_at_least(p, default_alignment, n).ptr;
return allocator_type::reallocate_aligned_n_at_least(p, oldn, default_alignment, n).ptr;
}
else if constexpr (::fast_io::details::has_reallocate_zero_impl<alloc>)
{
Expand All @@ -304,11 +304,11 @@ class generic_allocator_adapter
}
else if constexpr (::fast_io::details::has_reallocate_aligned_zero_impl<alloc>)
{
return allocator_type::reallocate_aligned_zero_n(p, default_alignment, n);
return allocator_type::reallocate_aligned_zero_n(p, oldn, default_alignment, n);
}
else if constexpr (::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>)
{
return allocator_type::reallocate_aligned_zero_n_at_least(p, default_alignment, n).ptr;
return allocator_type::reallocate_aligned_zero_n_at_least(p, oldn, default_alignment, n).ptr;
}
else
{
Expand Down Expand Up @@ -683,7 +683,7 @@ class generic_allocator_adapter
}
else if constexpr (::fast_io::details::has_reallocate_aligned_zero_n_impl<alloc>)
{
return allocator_type::reallocate_aligned_zero_n(p, alignment, n).ptr;
return allocator_type::reallocate_aligned_zero_n(p, oldn, alignment, n).ptr;
}
else
{
Expand Down Expand Up @@ -981,31 +981,31 @@ class generic_allocator_adapter
{
if constexpr (::fast_io::details::has_reallocate_aligned_n_at_least_impl<alloc>)
{
return allocator_type::reallocate_aligned_n_at_least(p, alignment, n);
return allocator_type::reallocate_aligned_n_at_least(p, oldn, alignment, n);
}
else if constexpr (::fast_io::details::has_reallocate_aligned_at_least_impl<alloc>)
{
return allocator_type::reallocate_aligned_at_least(p, alignment, n);
}
else if constexpr (::fast_io::details::has_reallocate_aligned_zero_n_at_least_impl<alloc>)
{
return allocator_type::reallocate_aligned_zero_n_at_least(p, alignment, n);
return allocator_type::reallocate_aligned_zero_n_at_least(p, oldn, alignment, n);
}
else if constexpr (::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>)
{
return allocator_type::reallocate_aligned_zero_at_least(p, alignment, n);
}
else if constexpr (::fast_io::details::has_reallocate_aligned_impl<alloc>)
{
return allocator_type::reallocate_aligned_n(p, alignment, n);
return allocator_type::reallocate_aligned_n(p, oldn, alignment, n);
}
else if constexpr (::fast_io::details::has_reallocate_aligned_impl<alloc>)
{
return allocator_type::reallocate_aligned(p, alignment, n);
}
else if constexpr (::fast_io::details::has_reallocate_aligned_zero_n_impl<alloc>)
{
return allocator_type::reallocate_aligned_zero_n(p, alignment, n);
return allocator_type::reallocate_aligned_zero_n(p, oldn, alignment, n);
}
else if constexpr (::fast_io::details::has_reallocate_aligned_zero_impl<alloc>)
{
Expand Down Expand Up @@ -1046,11 +1046,11 @@ class generic_allocator_adapter
{
if constexpr (::fast_io::details::has_reallocate_aligned_zero_n_at_least_impl<alloc>)
{
return allocator_type::reallocate_aligned_zero_n_at_least(p, alignment, n);
return allocator_type::reallocate_aligned_zero_n_at_least(p, oldn, alignment, n);
}
else if constexpr (::fast_io::details::has_reallocate_aligned_zero_n_impl<alloc>)
{
return {allocator_type::reallocate_aligned_zero_n(p, alignment, n), n};
return {allocator_type::reallocate_aligned_zero_n(p, oldn, alignment, n), n};
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion include/fast_io_core_impl/allocation/c_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ class c_malloc_allocator
{
return;
}

#if FAST_IO_HAS_BUILTIN(__builtin_free)
__builtin_free
__builtin_free
#else
::std::free
#endif
Expand Down
2 changes: 1 addition & 1 deletion include/fast_io_core_impl/allocation/linux_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class linux_kmalloc_allocator
}
static inline void deallocate(void *p) noexcept
{
if (p == nullptr)
if (p == nullptr) [[unlikely]]
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion include/fast_io_core_impl/allocation/mimalloc_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class mimalloc_allocator

inline static void deallocate_aligned(void *p, ::std::size_t alignment) noexcept
{
if (p == nullptr)
if (p == nullptr) [[unlikely]]
{
return;
}
Expand Down
5 changes: 4 additions & 1 deletion include/fast_io_core_impl/allocation/nt_rtlheapalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ inline void *nt_rtlallocate_heap_handle_common_impl(void *heaphandle, ::std::siz
}
auto p{::fast_io::win32::nt::RtlAllocateHeap(heaphandle, flag, n)};
if (p == nullptr)
#if __has_cpp_attribute(unlikely)
[[unlikely]]
#endif
{
::fast_io::fast_terminate();
}
Expand Down Expand Up @@ -108,7 +111,7 @@ class nt_rtlallocateheap_allocator
}
static inline void deallocate(void *addr) noexcept
{
if (addr == nullptr)
if (addr == nullptr) [[unlikely]]
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion include/fast_io_core_impl/allocation/win32_heapalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class win32_heapalloc_allocator
}
static inline void deallocate(void *addr) noexcept
{
if (addr == nullptr)
if (addr == nullptr) [[unlikely]]
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion include/fast_io_core_impl/allocation/wincrt_malloc_dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class wincrt_malloc_dbg_allocator
}
static inline void deallocate(void *p) noexcept
{
if (p == nullptr)
if (p == nullptr) [[unlikely]]
{
return;
}
Expand Down
1 change: 1 addition & 0 deletions include/fast_io_core_impl/bitops/rtz.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ inline constexpr ::fast_io::bitops::rtz_result<T> rtz_impl(T n) noexcept
s += b;
n = b ? r : n;

// c1 == d1 == 42949673u
constexpr ::std::uint_least32_t d1{42949673u};
r = ::std::rotr(static_cast<std::uint32_t>(n * d1), 2);
b = r < d1;
Expand Down
2 changes: 1 addition & 1 deletion include/fast_io_core_impl/buffer_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct basic_ibuffer_view
template <::std::contiguous_iterator Iter>
requires ::std::same_as<::std::remove_cvref_t<::std::iter_value_t<Iter>>, char_type>
inline constexpr basic_ibuffer_view(Iter first, Iter last) noexcept
: begin_ptr{::std::to_address(first)}, curr_ptr{begin_ptr}, end_ptr{curr_ptr + (last - first)}
: begin_ptr{::std::to_address(first)}, curr_ptr{begin_ptr}, end_ptr{::std::to_address(last)}
{
}
template <::std::ranges::contiguous_range rg>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#pragma once

namespace fast_io::char_category
{
Expand Down
2 changes: 1 addition & 1 deletion include/fast_io_core_impl/char_category/impl.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#pragma once

#include "char_category.h"
#include "char_category_traits.h"
10 changes: 7 additions & 3 deletions include/fast_io_core_impl/codecvt/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ general_code_cvt(src_char_type const *src_first, src_char_type const *src_last,
}
else
{
*dst = static_cast<dest_char_type>(0xFDFF0000);
constexpr dest_char_type val{byte_swap(static_cast<dest_char_type>(0xFFFD))};
*dst = val;
}
++dst;
}
Expand Down Expand Up @@ -410,7 +411,8 @@ inline constexpr dest_char_type *general_code_cvt(state_type &__restrict state,
}
else
{
*dst = static_cast<dest_char_type>(0xFDFF0000);
constexpr dest_char_type val{byte_swap(static_cast<dest_char_type>(0xFFFD))};
*dst = val;
}
++dst;
}
Expand Down Expand Up @@ -516,6 +518,7 @@ template <encoding_scheme src_encoding = encoding_scheme::execution_charset,
inline constexpr dest_char_type *general_code_cvt_full(src_char_type const *src_first, src_char_type const *src_last,
dest_char_type *__restrict dst) noexcept
{
// No need to consider dst_last
if constexpr (src_encoding == encoding_scheme::execution_charset)
{
constexpr auto src_scheme = get_execution_charset_encoding_scheme<src_char_type>(src_encoding);
Expand All @@ -535,7 +538,8 @@ inline constexpr dest_char_type *general_code_cvt_full(src_char_type const *src_
}
else
{
*new_dst = static_cast<dest_char_type>(0xFDFF0000);
constexpr dest_char_type val{byte_swap(static_cast<dest_char_type>(0xFFFD))};
*new_dst = val;
}
++new_dst;
}
Expand Down
2 changes: 1 addition & 1 deletion include/fast_io_core_impl/concat/concat_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ inline constexpr T basic_general_concat_decay_impl_precise(T &str, Arg arg)
constexpr ::std::size_t local_cap{strlike_sso_size(io_strlike_type<ch_type, T>)};
if (local_cap < precise_size_with_line)
{
strlike_reserve(io_strlike_type<ch_type, T>, str, local_cap);
strlike_reserve(io_strlike_type<ch_type, T>, str, precise_size_with_line);
}
auto first{strlike_begin(io_strlike_type<ch_type, T>, str)};
print_reserve_precise_define(io_reserve_type<ch_type, Arg>, first, precise_size, arg);
Expand Down
23 changes: 19 additions & 4 deletions include/fast_io_core_impl/dynamic_output_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class basic_generic_dynamic_output_buffer

inline constexpr ~basic_generic_dynamic_output_buffer()
{
if (begin_ptr && begin_ptr != buffer)
if (begin_ptr != buffer)
{
using typed_allocator = typed_generic_allocator_adapter<allocator_type, char_type>;
if constexpr (typed_allocator::has_deallocate)
Expand Down Expand Up @@ -86,12 +86,14 @@ write_all_overflow_define_impl(basic_generic_dynamic_output_buffer<char_type, bu
char_type *pbuffer;
if (bob.begin_ptr != bob.buffer)
{
pbuffer = typed_allocator::reallocate_n(bob.buffer, to_allocate);
// heap
pbuffer = typed_allocator::reallocate_n(bob.begin_ptr, to_allocate);
}
else
{
// Stack buffer to heap
pbuffer = typed_allocator::allocate(to_allocate);
::fast_io::details::non_overlapped_copy_n(bob.begin_ptr, to_allocate, pbuffer);
::fast_io::details::non_overlapped_copy_n(bob.buffer, buffersize, pbuffer);
}
bob.begin_ptr = pbuffer;
bob.end_ptr = pbuffer + to_allocate;
Expand Down Expand Up @@ -119,7 +121,20 @@ grow_twice_define_impl(basic_generic_dynamic_output_buffer<char_type, buffersize
{
twicebfsz = bfsz << 1u;
}
char_type *pbuffer{typed_allocator::reallocate_n(bob.buffer, twicebfsz)};

char_type *pbuffer;
if (bob.begin_ptr != bob.buffer)
{
// heap
pbuffer = typed_allocator::reallocate_n(bob.begin_ptr, twicebfsz);
}
else
{
// Stack buffer to heap
pbuffer = typed_allocator::allocate(twicebfsz);
::fast_io::details::non_overlapped_copy_n(bob.buffer, buffersize, pbuffer);
}

bob.begin_ptr = pbuffer;
bob.curr_ptr = pbuffer + bfsz;
bob.end_ptr = pbuffer + twicebfsz;
Expand Down
2 changes: 1 addition & 1 deletion include/fast_io_core_impl/enums/flock.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace fast_io
{

enum class file_lock_mode : char unsigned
enum class file_lock_mode : unsigned char
{
shared_lock,
exclusive_lock,
Expand Down
2 changes: 1 addition & 1 deletion include/fast_io_core_impl/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ concept error_domain =
template <error_domain D>
inline constexpr bool is_domain(error e) noexcept
{
constexpr ::std::size_t v{error_domain_define(error_type<D>)};
constexpr ::std::size_t v{domain_define(error_type<D>)};
return v == e.domain;
}

Expand Down
8 changes: 6 additions & 2 deletions include/fast_io_core_impl/freestanding/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ inline constexpr output_iter copy(input_iter first, input_iter last, output_iter
{
*result = static_cast<::std::byte>(*first);
}
else
{
*result = *first;
}
++first;
++result;
}
Expand Down Expand Up @@ -407,8 +411,8 @@ inline constexpr output_iter my_copy(input_iter first, input_iter second, output
::std::is_trivially_copyable_v<output_value_type> &&
(::std::same_as<input_value_type, output_value_type> ||
(::std::integral<input_value_type> && ::std::integral<output_value_type> &&
sizeof(::std::is_trivially_copyable_v<input_value_type>) ==
sizeof(::std::is_trivially_copyable_v<output_value_type>))))
::std::is_trivially_copyable_v<input_value_type> ==
::std::is_trivially_copyable_v<output_value_type>)))
{
my_copy_n(first, static_cast<::std::size_t>(second - first), result);
return result + (second - first);
Expand Down
5 changes: 3 additions & 2 deletions include/fast_io_core_impl/freestanding/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct allocator
__builtin_trap();
}
#if FAST_IO_HAS_BUILTIN(__builtin_operator_new)
return static_cast<T *>(__builtin_operator_new(n * sizeof(T)));
return static_cast<T *>(__builtin_operator_new(n * sizeof(T)));
#else
__builtin_trap();
return nullptr;
Expand All @@ -23,8 +23,9 @@ struct allocator
inline constexpr void deallocate([[maybe_unused]] T *ptr, [[maybe_unused]] ::std::size_t n) noexcept
{
#if FAST_IO_HAS_BUILTIN(__builtin_operator_delete)
__builtin_operator_delete(ptr, sizeof(T) * n);
__builtin_operator_delete(ptr, sizeof(T) * n);
#endif
ptr = nullptr;
}
};

Expand Down
8 changes: 4 additions & 4 deletions include/fast_io_core_impl/integers/sto/sto_contiguous.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ inline constexpr bool char_digit_to_literal(my_make_unsigned_t<char_type> &ch) n
unsigned_char_type ch3(ch);
ch3 -= 0x81;
unsigned_char_type ch4(ch);
ch2 -= 0xD1;
ch4 -= 0xD1;
unsigned_char_type ch5(ch);
ch3 -= 0x91;
ch5 -= 0x91;
ch -= 0xF0;
if (ch4 < mns)
{
Expand Down Expand Up @@ -207,9 +207,9 @@ inline constexpr bool char_is_digit(my_make_unsigned_t<char_type> ch) noexcept
unsigned_char_type ch3(ch);
ch3 -= 0x81;
unsigned_char_type ch4(ch);
ch2 -= 0xD1;
ch4 -= 0xD1;
unsigned_char_type ch5(ch);
ch3 -= 0x91;
ch5 -= 0x91;
ch -= 0xF0;
return (ch4 < mns) | (ch5 < mns) | (ch2 < mns) | (ch3 < 9) | (ch < 10u);
}
Expand Down
Loading