Skip to content

Commit c64c696

Browse files
authored
Merge pull request #1246 from puji4810/adaptor
Add [[__gnu__::__returns_nonnull__]] attribute to allocation function…
2 parents 345d071 + 09c02b7 commit c64c696

File tree

2 files changed

+200
-38
lines changed

2 files changed

+200
-38
lines changed

include/fast_io_core_impl/allocation/adapters.h

Lines changed: 120 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,13 @@ class generic_allocator_adapter
104104
::fast_io::details::has_allocate_zero_impl<alloc> ||
105105
::fast_io::details::has_allocate_aligned_zero_impl<alloc>)};
106106

107+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
108+
[[__gnu__::__returns_nonnull__]]
109+
#endif
107110
static inline
108111
constexpr
109112
void *
110-
allocate(::std::size_t n) noexcept
113+
allocate(::std::size_t n) noexcept
111114
requires(!has_status)
112115
{
113116
#if __cpp_constexpr_dynamic_alloc >= 201907L
@@ -149,6 +152,9 @@ class generic_allocator_adapter
149152
}
150153
}
151154

155+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
156+
[[__gnu__::__returns_nonnull__]]
157+
#endif
152158
static inline void *allocate_zero(::std::size_t n) noexcept
153159
requires(!has_status)
154160
{
@@ -185,6 +191,9 @@ class generic_allocator_adapter
185191
::fast_io::details::has_reallocate_aligned_zero_impl<alloc> ||
186192
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>);
187193

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

230239

240+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
241+
[[__gnu__::__returns_nonnull__]]
242+
#endif
231243
static inline void *reallocate_zero(void *p, ::std::size_t n) noexcept
232244
requires(!has_status && has_reallocate_zero)
233245
{
@@ -249,9 +261,16 @@ class generic_allocator_adapter
249261
}
250262
}
251263

264+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
265+
[[__gnu__::__returns_nonnull__]]
266+
#endif
252267
static inline void *reallocate_n(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
253268
requires(!has_status)
254269
{
270+
if (p != nullptr && oldn == n)
271+
{
272+
return p;
273+
}
255274
if constexpr (::fast_io::details::has_reallocate_n_impl<alloc>)
256275
{
257276
return allocator_type::reallocate_n(p, oldn, n);
@@ -319,22 +338,32 @@ class generic_allocator_adapter
319338
else
320339
{
321340
auto newptr{generic_allocator_adapter::allocate(n)};
322-
if (p != nullptr && n)
341+
if (p != nullptr)
323342
{
324-
if (oldn < n)
343+
if (n)
325344
{
326-
n = oldn;
345+
if (oldn < n)
346+
{
347+
n = oldn;
348+
}
349+
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
327350
}
328-
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
329351
generic_allocator_adapter::deallocate_n(p, oldn);
330352
}
331353
return newptr;
332354
}
333355
}
334356

357+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
358+
[[__gnu__::__returns_nonnull__]]
359+
#endif
335360
static inline void *reallocate_zero_n(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
336361
requires(!has_status)
337362
{
363+
if (p != nullptr && oldn == n)
364+
{
365+
return p;
366+
}
338367
if constexpr (::fast_io::details::has_reallocate_zero_n_impl<alloc>)
339368
{
340369
return allocator_type::reallocate_zero_n(p, oldn, n);
@@ -437,6 +466,9 @@ class generic_allocator_adapter
437466
}
438467
}
439468

469+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
470+
[[__gnu__::__returns_nonnull__]]
471+
#endif
440472
static inline
441473
constexpr
442474
void *
@@ -455,6 +487,9 @@ class generic_allocator_adapter
455487
}
456488
}
457489

490+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
491+
[[__gnu__::__returns_nonnull__]]
492+
#endif
458493
static inline constexpr
459494
void *
460495
allocate_aligned_zero(::std::size_t alignment, ::std::size_t n) noexcept
@@ -620,9 +655,12 @@ class generic_allocator_adapter
620655
::fast_io::details::has_reallocate_aligned_zero_impl<alloc> ||
621656
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>);
622657

658+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
659+
[[__gnu__::__returns_nonnull__]]
660+
#endif
623661
static inline
624-
void *
625-
reallocate_aligned(void *p, ::std::size_t alignment, ::std::size_t n) noexcept
662+
void *
663+
reallocate_aligned(void *p, ::std::size_t alignment, ::std::size_t n) noexcept
626664
requires(!has_status && has_reallocate_aligned)
627665
{
628666
if constexpr (::fast_io::details::has_reallocate_aligned_impl<alloc>)
@@ -645,9 +683,12 @@ class generic_allocator_adapter
645683

646684
static inline constexpr bool has_reallocate_aligned_zero = (::fast_io::details::has_reallocate_aligned_zero_impl<alloc> ||
647685
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>);
686+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
687+
[[__gnu__::__returns_nonnull__]]
688+
#endif
648689
static inline
649-
void *
650-
reallocate_aligned_zero(void *p, ::std::size_t alignment, ::std::size_t n) noexcept
690+
void *
691+
reallocate_aligned_zero(void *p, ::std::size_t alignment, ::std::size_t n) noexcept
651692
requires(!has_status && has_reallocate_aligned_zero)
652693
{
653694
if constexpr (::fast_io::details::has_reallocate_aligned_zero_impl<alloc>)
@@ -660,11 +701,18 @@ class generic_allocator_adapter
660701
}
661702
}
662703

704+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
705+
[[__gnu__::__returns_nonnull__]]
706+
#endif
663707
static inline
664-
void *
665-
reallocate_aligned_n(void *p, ::std::size_t oldn, ::std::size_t alignment, ::std::size_t n) noexcept
708+
void *
709+
reallocate_aligned_n(void *p, ::std::size_t oldn, ::std::size_t alignment, ::std::size_t n) noexcept
666710
requires(!has_status)
667711
{
712+
if (p != nullptr && oldn == n)
713+
{
714+
return p;
715+
}
668716
if constexpr (::fast_io::details::has_reallocate_aligned_n_impl<alloc>)
669717
{
670718
return allocator_type::reallocate_aligned_n(p, oldn, alignment, n);
@@ -710,24 +758,34 @@ class generic_allocator_adapter
710758
}
711759
}
712760
auto newptr{::fast_io::details::allocator_pointer_aligned_impl<alloc, false>(alignment, n)};
713-
if (p != nullptr && n)
761+
if (p != nullptr)
714762
{
715-
if (oldn < n)
763+
if (n)
716764
{
717-
n = oldn;
765+
if (oldn < n)
766+
{
767+
n = oldn;
768+
}
769+
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
718770
}
719-
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
720771
generic_allocator_adapter::deallocate_aligned_n(p, alignment, oldn);
721772
}
722773
return newptr;
723774
}
724775
}
725776

777+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
778+
[[__gnu__::__returns_nonnull__]]
779+
#endif
726780
static inline
727-
void *
728-
reallocate_aligned_zero_n(void *p, ::std::size_t oldn, ::std::size_t alignment, ::std::size_t n) noexcept
781+
void *
782+
reallocate_aligned_zero_n(void *p, ::std::size_t oldn, ::std::size_t alignment, ::std::size_t n) noexcept
729783
requires(!has_status)
730784
{
785+
if (p != nullptr && oldn == n)
786+
{
787+
return p;
788+
}
731789
if constexpr (::fast_io::details::has_reallocate_aligned_zero_n_impl<alloc>)
732790
{
733791
return allocator_type::reallocate_aligned_zero_n(p, oldn, alignment, n);
@@ -753,7 +811,7 @@ class generic_allocator_adapter
753811
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>));
754812
static inline
755813
::fast_io::allocation_least_result
756-
reallocate_at_least(void *p, ::std::size_t n) noexcept
814+
reallocate_at_least(void *p, ::std::size_t n) noexcept
757815
requires(!has_status && has_reallocate)
758816
{
759817
if constexpr (::fast_io::details::has_reallocate_at_least_impl<alloc>)
@@ -793,10 +851,10 @@ class generic_allocator_adapter
793851
static inline constexpr bool has_native_reallocate_zero_at_least = (has_reallocate_zero &&
794852
(::fast_io::details::has_reallocate_zero_at_least_impl<alloc> ||
795853
::fast_io::details::has_reallocate_aligned_zero_at_least_impl<alloc>));
796-
854+
797855
static inline
798856
::fast_io::allocation_least_result
799-
reallocate_zero_at_least(void *p, ::std::size_t n) noexcept
857+
reallocate_zero_at_least(void *p, ::std::size_t n) noexcept
800858
requires(!has_status && has_reallocate)
801859
{
802860
if constexpr (::fast_io::details::has_reallocate_zero_at_least_impl<alloc>)
@@ -819,7 +877,7 @@ class generic_allocator_adapter
819877

820878
static inline
821879
::fast_io::allocation_least_result
822-
reallocate_n_at_least(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
880+
reallocate_n_at_least(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
823881
requires(!has_status)
824882
{
825883
if constexpr (::fast_io::details::has_reallocate_n_at_least_impl<alloc>)
@@ -887,25 +945,28 @@ class generic_allocator_adapter
887945
return {allocator_type::reallocate_aligned_zero(p, default_alignment, n), n};
888946
}
889947
else
890-
{
891-
auto newres{generic_allocator_adapter::allocate_at_least(n)};
892-
auto newptr{newres.ptr};
893-
if (p != nullptr && n)
894948
{
895-
if (oldn < n)
949+
auto newres{generic_allocator_adapter::allocate_at_least(n)};
950+
auto newptr{newres.ptr};
951+
if (p != nullptr)
896952
{
897-
n = oldn;
953+
if (n)
954+
{
955+
if (oldn < n)
956+
{
957+
n = oldn;
958+
}
959+
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
960+
}
961+
generic_allocator_adapter::deallocate_n(p, oldn);
898962
}
899-
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
900-
generic_allocator_adapter::deallocate_n(p, oldn);
963+
return newres;
901964
}
902-
return newres;
903-
}
904965
}
905966

906967
static inline
907968
::fast_io::allocation_least_result
908-
reallocate_zero_n_at_least(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
969+
reallocate_zero_n_at_least(void *p, ::std::size_t oldn, ::std::size_t n) noexcept
909970
requires(!has_status)
910971
{
911972
if constexpr (::fast_io::details::has_reallocate_zero_n_at_least_impl<alloc>)
@@ -1035,13 +1096,16 @@ class generic_allocator_adapter
10351096
}
10361097
auto newres{::fast_io::details::allocator_pointer_aligned_at_least_impl<alloc, false>(alignment, n)};
10371098
auto newptr{newres.ptr};
1038-
if (p != nullptr && n)
1099+
if (p != nullptr)
10391100
{
1040-
if (oldn < n)
1101+
if (n)
10411102
{
1042-
n = oldn;
1103+
if (oldn < n)
1104+
{
1105+
n = oldn;
1106+
}
1107+
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
10431108
}
1044-
::fast_io::freestanding::nonoverlapped_bytes_copy_n(reinterpret_cast<::std::byte const *>(p), n, reinterpret_cast<::std::byte *>(newptr));
10451109
generic_allocator_adapter::deallocate_aligned_n(p, alignment, oldn);
10461110
}
10471111
return newres;
@@ -1140,6 +1204,9 @@ class typed_generic_allocator_adapter
11401204
using allocator_adaptor = alloc;
11411205
static inline constexpr bool has_status{allocator_adaptor::has_status};
11421206
using handle_type = typename allocator_adaptor::handle_type;
1207+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
1208+
[[__gnu__::__returns_nonnull__]]
1209+
#endif
11431210
static inline
11441211
#if __cpp_constexpr_dynamic_alloc >= 201907L
11451212
constexpr
@@ -1202,6 +1269,9 @@ class typed_generic_allocator_adapter
12021269
}
12031270
}
12041271

1272+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
1273+
[[__gnu__::__returns_nonnull__]]
1274+
#endif
12051275
static inline
12061276
#if (__cpp_if_consteval >= 202106L || __cpp_lib_is_constant_evaluated >= 201811L) && \
12071277
__cpp_constexpr_dynamic_alloc >= 201907L
@@ -1260,6 +1330,9 @@ class typed_generic_allocator_adapter
12601330

12611331
static inline constexpr bool has_reallocate = allocator_adaptor::has_reallocate;
12621332

1333+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
1334+
[[__gnu__::__returns_nonnull__]]
1335+
#endif
12631336
static inline
12641337
#if (__cpp_if_consteval >= 202106L || __cpp_lib_is_constant_evaluated >= 201811L) && \
12651338
__cpp_constexpr_dynamic_alloc >= 201907L
@@ -1311,6 +1384,9 @@ class typed_generic_allocator_adapter
13111384
}
13121385

13131386
static inline constexpr bool has_reallocate_zero = allocator_adaptor::has_reallocate_zero;
1387+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
1388+
[[__gnu__::__returns_nonnull__]]
1389+
#endif
13141390
static inline
13151391
#if (__cpp_if_consteval >= 202106L || __cpp_lib_is_constant_evaluated >= 201811L) && \
13161392
__cpp_constexpr_dynamic_alloc >= 201907L
@@ -1361,6 +1437,9 @@ class typed_generic_allocator_adapter
13611437
}
13621438
}
13631439

1440+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
1441+
[[__gnu__::__returns_nonnull__]]
1442+
#endif
13641443
static inline
13651444
#if (__cpp_if_consteval >= 202106L || __cpp_lib_is_constant_evaluated >= 201811L) && \
13661445
__cpp_constexpr_dynamic_alloc >= 201907L
@@ -1411,6 +1490,9 @@ class typed_generic_allocator_adapter
14111490
}
14121491
}
14131492

1493+
#if __has_cpp_attribute(__gnu__::__returns_nonnull__)
1494+
[[__gnu__::__returns_nonnull__]]
1495+
#endif
14141496
static inline
14151497
#if (__cpp_if_consteval >= 202106L || __cpp_lib_is_constant_evaluated >= 201811L) && \
14161498
__cpp_constexpr_dynamic_alloc >= 201907L
@@ -1755,7 +1837,7 @@ namespace details
17551837

17561838
template <typename alloc, bool zero>
17571839
inline constexpr void *allocator_pointer_aligned_impl(::std::size_t alignment, ::std::size_t n) noexcept
1758-
{
1840+
{
17591841
static_assert(::fast_io::generic_allocator_adapter<alloc>::has_native_allocate);
17601842

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

17831865
template <typename alloc, bool zero>
17841866
inline constexpr ::fast_io::allocation_least_result allocator_pointer_aligned_at_least_impl(::std::size_t alignment, ::std::size_t n) noexcept
1785-
{
1867+
{
17861868
static_assert(::fast_io::generic_allocator_adapter<alloc>::has_native_allocate);
17871869

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

0 commit comments

Comments
 (0)