Skip to content

Commit f2dfdc4

Browse files
afrindmeta-codesync[bot]
authored andcommitted
container: count allocate_at_least in heap_vector test allocator (C++23) (#2665)
Summary: HeapVectorTypes.GrowthPolicy checks CountingAllocator::nAllocations, but the allocator only overrode allocate(). Since C++23, std::vector grows via std::allocator_traits::allocate_at_least, which CountingAllocator inherited from std::allocator and which calls the base allocate(), bypassing the override. On Mac CI (Homebrew LLVM, libc++ in C++23 mode) this left nAllocations at 0 and failed the test; it passed locally under older libc++ / C++20, which still routes growth through allocate(). Override allocate_at_least() to also count, guarded on __cpp_lib_allocate_at_least so C++20 builds are unaffected. Reproduced and verified with -std=c++23 vs c++20. Pull Request resolved: #2665 Reviewed By: ilvokhin Differential Revision: D109797758 Pulled By: afrind fbshipit-source-id: 4fc0be316c1f69fef1881253d9bfb6f6b88be0de
1 parent 870e3f2 commit f2dfdc4

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

‎folly/container/test/heap_vector_types_test.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ struct CountingAllocator : std::allocator<T> {
6969
nAllocations += 1;
7070
return std::allocator<T>::allocate(n);
7171
}
72+
#ifdef __cpp_lib_allocate_at_least
73+
// Since C++23, std::vector allocates via allocator_traits::allocate_at_least,
74+
// which std::allocator provides; without this override that inherited method
75+
// would bypass the counting allocate() above and leave nAllocations at 0.
76+
auto allocate_at_least(std::size_t n) {
77+
nAllocations += 1;
78+
return std::allocator<T>::allocate_at_least(n);
79+
}
80+
#endif
7281
int nAllocations{0};
7382

7483
template <typename U>

0 commit comments

Comments
 (0)