[PATCH] libstdc++: Add allocate_at_least (P0401) [PR118030]
Jonathan Wakely
jwakely.gcc@gmail.com
Tue Mar 10 13:29:03 GMT 2026
On Mon, 9 Mar 2026 at 15:18, Nathan Myers <ncm@cantrip.org> wrote:
>
> On 3/9/26 6:01 AM, Jonathan Wakely wrote:
> >
> >
> > On Sun, 8 Mar 2026, 23:39 Nathan Myers, <ncm@cantrip.org
> > <mailto:ncm@cantrip.org>> wrote:
> >
> > Have you thought about how std::allocator<>::allocate_at_least
> > might usefully discover whether the ::op new (it is obliged by the
> > Standard to use) is the one in libstdc++, and will supply extra
> > details about memory allocated, not one the user has supplied and
> > the linker has substituted in its place? Ideally it would all be
> > decided at link time, and not conditionally in every call, but
> > linker magic is fragile even without LTO. And users can call ::op
> > new directly, with no #include to declare it.
> >
> > Maybe pass an extra argument to ::op new that the user's version
> > won't notice, and that ours may discover is present by looking at
> > the stack frame? The extra argument might be a pointer to a place
> > to scribble extra details. (A hint argument might be placed there,
> > besides.) But stack frame conventions vary too.
> >
> >
> > We don't need to do that. This feature is a customisation point for non-
> > standard allocators (and program-defined specializations of
> > std::allocator<User type>), we don't need to make std::allocator use
> > it. P0901 proposed changes to operator new which would have been useful
> > here, but that got abandoned.
>
> I don't understand that. If a user displaces ::operator new, they
> will expect std::allocator members to call it. The Standard seems
> to require all members obtain whatever memory they deliver by
> calling ::operator new. So, when calls to ::operator new are visible,
> we seem to be obliged to use it.
I didn't respond to this part. We're obliged to use it, but it's not
specified *how*. std::allocator is allowed to make one huge call to
operator new on the first allocation, then chop that up and hand it
out for later calls. Or just call operator new every time for every
std::allocator::allocate call (which is what we do currently with the
default --enable-libstdcxx-allocator=new configuration).
But I don't understand what you don't understand ... your reply
doesn't seem related to what I wrote. I wasn't claiming we don't need
to call operator new, I was claiming that we don't need to do some
magic call to a new kind of operator new.
std::allocator::allocate_at_least(n) is not required to return more
than n. We don't need to invent new ways to allocate to make it do
something, because it's not required to do anything special. It can
just return {allocate(n), n}.
The point of P0401 is not to make std::allocator do anything
different, it's to allow custom allocators to do things differently.
The customization point is
std::allocator_traits<Alloc>::allocate_at_least not
std::allocator::allocate_at_least. So if you provide a custom Alloc
that supports allocate_at_least, it will be used.
More information about the Libstdc++
mailing list