Skip to content
Merged
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
158 changes: 120 additions & 38 deletions include/fast_io_core_impl/allocation/adapters.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ class generic_allocator_adapter
::fast_io::details::has_allocate_zero_impl<alloc> ||
::fast_io::details::has_allocate_aligned_zero_impl<alloc>)};

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
constexpr
void *
allocate(::std::size_t n) noexcept
allocate(::std::size_t n) noexcept
requires(!has_status)
{
#if __cpp_constexpr_dynamic_alloc >= 201907L
Expand Down Expand Up @@ -149,6 +152,9 @@ class generic_allocator_adapter
}
}

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline void *allocate_zero(::std::size_t n) noexcept
requires(!has_status)
{
Expand Down Expand Up @@ -185,6 +191,9 @@ class generic_allocator_adapter
::fast_io::details::has_reallocate_aligned_zero_impl<alloc> ||
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>);

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline void *reallocate(void *p, ::std::size_t n) noexcept
requires(!has_status && has_reallocate)
{
Expand Down Expand Up @@ -228,6 +237,9 @@ class generic_allocator_adapter
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>);


#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline void *reallocate_zero(void *p, ::std::size_t n) noexcept
requires(!has_status && has_reallocate_zero)
{
Expand All @@ -249,9 +261,16 @@ class generic_allocator_adapter
}
}

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline void *reallocate_n(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
requires(!has_status)
{
if (p != nullptr && oldn == n)
{
return p;
}
if constexpr (::fast_io::details::has_reallocate_n_impl<alloc>)
{
return allocator_type::reallocate_n(p, oldn, n);
Expand Down Expand Up @@ -319,22 +338,32 @@ class generic_allocator_adapter
else
{
auto newptr{generic_allocator_adapter::allocate(n)};
if (p != nullptr && n)
if (p != nullptr)
{
if (oldn < n)
if (n)
{
n = oldn;
if (oldn < n)
{
n = oldn;
}
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
}
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
generic_allocator_adapter::deallocate_n(p, oldn);
}
return newptr;
}
}

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline void *reallocate_zero_n(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
requires(!has_status)
{
if (p != nullptr && oldn == n)
{
return p;
}
if constexpr (::fast_io::details::has_reallocate_zero_n_impl<alloc>)
{
return allocator_type::reallocate_zero_n(p, oldn, n);
Expand Down Expand Up @@ -437,6 +466,9 @@ class generic_allocator_adapter
}
}

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
constexpr
void *
Expand All @@ -455,6 +487,9 @@ class generic_allocator_adapter
}
}

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline constexpr
void *
allocate_aligned_zero(::std::size_t alignment, ::std::size_t n) noexcept
Expand Down Expand Up @@ -620,9 +655,12 @@ class generic_allocator_adapter
::fast_io::details::has_reallocate_aligned_zero_impl<alloc> ||
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>);

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
void *
reallocate_aligned(void *p, ::std::size_t alignment, ::std::size_t n) noexcept
void *
reallocate_aligned(void *p, ::std::size_t alignment, ::std::size_t n) noexcept
requires(!has_status && has_reallocate_aligned)
{
if constexpr (::fast_io::details::has_reallocate_aligned_impl<alloc>)
Expand All @@ -645,9 +683,12 @@ class generic_allocator_adapter

static inline constexpr bool has_reallocate_aligned_zero = (::fast_io::details::has_reallocate_aligned_zero_impl<alloc> ||
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>);
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
void *
reallocate_aligned_zero(void *p, ::std::size_t alignment, ::std::size_t n) noexcept
void *
reallocate_aligned_zero(void *p, ::std::size_t alignment, ::std::size_t n) noexcept
requires(!has_status && has_reallocate_aligned_zero)
{
if constexpr (::fast_io::details::has_reallocate_aligned_zero_impl<alloc>)
Expand All @@ -660,11 +701,18 @@ class generic_allocator_adapter
}
}

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
void *
reallocate_aligned_n(void *p, ::std::size_t oldn, ::std::size_t alignment, ::std::size_t n) noexcept
void *
reallocate_aligned_n(void *p, ::std::size_t oldn, ::std::size_t alignment, ::std::size_t n) noexcept
requires(!has_status)
{
if (p != nullptr && oldn == n)
{
return p;
}
if constexpr (::fast_io::details::has_reallocate_aligned_n_impl<alloc>)
{
return allocator_type::reallocate_aligned_n(p, oldn, alignment, n);
Expand Down Expand Up @@ -710,24 +758,34 @@ class generic_allocator_adapter
}
}
auto newptr{::fast_io::details::allocator_pointer_aligned_impl<alloc, false>(alignment, n)};
if (p != nullptr && n)
if (p != nullptr)
{
if (oldn < n)
if (n)
{
n = oldn;
if (oldn < n)
{
n = oldn;
}
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
}
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
generic_allocator_adapter::deallocate_aligned_n(p, alignment, oldn);
}
return newptr;
}
}

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
void *
reallocate_aligned_zero_n(void *p, ::std::size_t oldn, ::std::size_t alignment, ::std::size_t n) noexcept
void *
reallocate_aligned_zero_n(void *p, ::std::size_t oldn, ::std::size_t alignment, ::std::size_t n) noexcept
requires(!has_status)
{
if (p != nullptr && oldn == n)
{
return p;
}
if constexpr (::fast_io::details::has_reallocate_aligned_zero_n_impl<alloc>)
{
return allocator_type::reallocate_aligned_zero_n(p, oldn, alignment, n);
Expand All @@ -753,7 +811,7 @@ class generic_allocator_adapter
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>));
static inline
::fast_io::allocation_least_result
reallocate_at_least(void *p, ::std::size_t n) noexcept
reallocate_at_least(void *p, ::std::size_t n) noexcept
requires(!has_status && has_reallocate)
{
if constexpr (::fast_io::details::has_reallocate_at_least_impl<alloc>)
Expand Down Expand Up @@ -793,10 +851,10 @@ class generic_allocator_adapter
static inline constexpr bool has_native_reallocate_zero_at_least = (has_reallocate_zero &&
(::fast_io::details::has_reallocate_zero_at_least_impl<alloc> ||
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>));

static inline
::fast_io::allocation_least_result
reallocate_zero_at_least(void *p, ::std::size_t n) noexcept
reallocate_zero_at_least(void *p, ::std::size_t n) noexcept
requires(!has_status && has_reallocate)
{
if constexpr (::fast_io::details::has_reallocate_zero_at_least_impl<alloc>)
Expand All @@ -819,7 +877,7 @@ class generic_allocator_adapter

static inline
::fast_io::allocation_least_result
reallocate_n_at_least(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
reallocate_n_at_least(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
requires(!has_status)
{
if constexpr (::fast_io::details::has_reallocate_n_at_least_impl<alloc>)
Expand Down Expand Up @@ -887,25 +945,28 @@ class generic_allocator_adapter
return {allocator_type::reallocate_aligned_zero(p, default_alignment, n), n};
}
else
{
auto newres{generic_allocator_adapter::allocate_at_least(n)};
auto newptr{newres.ptr};
if (p != nullptr && n)
{
if (oldn < n)
auto newres{generic_allocator_adapter::allocate_at_least(n)};
auto newptr{newres.ptr};
if (p != nullptr)
{
n = oldn;
if (n)
{
if (oldn < n)
{
n = oldn;
}
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
}
generic_allocator_adapter::deallocate_n(p, oldn);
}
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
generic_allocator_adapter::deallocate_n(p, oldn);
return newres;
}
return newres;
}
}

static inline
::fast_io::allocation_least_result
reallocate_zero_n_at_least(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
reallocate_zero_n_at_least(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
requires(!has_status)
{
if constexpr (::fast_io::details::has_reallocate_zero_n_at_least_impl<alloc>)
Expand Down Expand Up @@ -1035,13 +1096,16 @@ class generic_allocator_adapter
}
auto newres{::fast_io::details::allocator_pointer_aligned_at_least_impl<alloc, false>(alignment, n)};
auto newptr{newres.ptr};
if (p != nullptr && n)
if (p != nullptr)
{
if (oldn < n)
if (n)
{
n = oldn;
if (oldn < n)
{
n = oldn;
}
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
}
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
generic_allocator_adapter::deallocate_aligned_n(p, alignment, oldn);
}
return newres;
Expand Down Expand Up @@ -1140,6 +1204,9 @@ class typed_generic_allocator_adapter
using allocator_adaptor = alloc;
static inline constexpr bool has_status{allocator_adaptor::has_status};
using handle_type = typename allocator_adaptor::handle_type;
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
#if __cpp_constexpr_dynamic_alloc >= 201907L
constexpr
Expand Down Expand Up @@ -1202,6 +1269,9 @@ class typed_generic_allocator_adapter
}
}

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
#if (__cpp_if_consteval >= 202106L || __cpp_lib_is_constant_evaluated >= 201811L) && \
__cpp_constexpr_dynamic_alloc >= 201907L
Expand Down Expand Up @@ -1260,6 +1330,9 @@ class typed_generic_allocator_adapter

static inline constexpr bool has_reallocate = allocator_adaptor::has_reallocate;

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
#if (__cpp_if_consteval >= 202106L || __cpp_lib_is_constant_evaluated >= 201811L) && \
__cpp_constexpr_dynamic_alloc >= 201907L
Expand Down Expand Up @@ -1311,6 +1384,9 @@ class typed_generic_allocator_adapter
}

static inline constexpr bool has_reallocate_zero = allocator_adaptor::has_reallocate_zero;
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
#if (__cpp_if_consteval >= 202106L || __cpp_lib_is_constant_evaluated >= 201811L) && \
__cpp_constexpr_dynamic_alloc >= 201907L
Expand Down Expand Up @@ -1361,6 +1437,9 @@ class typed_generic_allocator_adapter
}
}

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
#if (__cpp_if_consteval >= 202106L || __cpp_lib_is_constant_evaluated >= 201811L) && \
__cpp_constexpr_dynamic_alloc >= 201907L
Expand Down Expand Up @@ -1411,6 +1490,9 @@ class typed_generic_allocator_adapter
}
}

#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
[[__gnu__::__returns_nonnull__]]
#endif
static inline
#if (__cpp_if_consteval >= 202106L || __cpp_lib_is_constant_evaluated >= 201811L) && \
__cpp_constexpr_dynamic_alloc >= 201907L
Expand Down Expand Up @@ -1755,7 +1837,7 @@ namespace details

template <typename alloc, bool zero>
inline constexpr void *allocator_pointer_aligned_impl(::std::size_t alignment, ::std::size_t n) noexcept
{
{
static_assert(::fast_io::generic_allocator_adapter<alloc>::has_native_allocate);

constexpr ::std::size_t defaultalignment{::fast_io::details::calculate_default_alignment<alloc>()};
Expand All @@ -1782,7 +1864,7 @@ inline constexpr void *allocator_pointer_aligned_impl(::std::size_t alignment, :

template <typename alloc, bool zero>
inline constexpr ::fast_io::allocation_least_result allocator_pointer_aligned_at_least_impl(::std::size_t alignment, ::std::size_t n) noexcept
{
{
static_assert(::fast_io::generic_allocator_adapter<alloc>::has_native_allocate);

constexpr ::std::size_t defaultalignment{::fast_io::details::calculate_default_alignment<alloc>()};
Expand Down
Loading