C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Static Allocator (All Containers Compatible with Microcontrollers)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 10 Aug 2022 14:44:09 +0100
On Wed, Aug 3, 2022 at 5:55 PM Jason McKesson wrote:
>
> Broadly speaking, what you're asking for is not really viable.
>
> The problem with simplistic, fixed-bound allocators like these is that
> it only works while communicating effectively with the container
> itself.
>
> `vector`'s allocation strategy is entirely *opaque* to the user.


std::vector has the "reserve(size_t)" method, taking away some of that
opaqueness.


> your example, how do you as the user of `vector` know that the first
> `push_back` won't try to allocate more than 4 characters? There's no
> requirement than, when using `push_back` on an empty `vector`, it will
> allocate a buffer of no more than 4 values. There's no requirement
> that calling `reserve(x)` will only allocate `x` items.


Each allocator could be mandated to have a constexpr boolean member
named "conservative".

    template<typename T, std::size_t t_capacity>
    class StaticAllocator {
    public:
        bool constexpr conservative = true;
    };

When "conservative" is set to 'true', the container should never use
the allocator to allocate more memory than the container needs.


> Everything you want ultimately requires a tighter coupling between the
> container and the allocator than C++ provides. The only way to resolve
> this is to make some fundamental changes to the allocator/container
> model.


I have suggested one such small change.

Received on 2022-08-10 13:44:21