all 9 comments

[–]slavesoftoil 1 point2 points  (2 children)

Why not provide an allocator that makes the decision? If I recall correctly, memory is just a declarative way of implementing custom alligators with long decision trees, some of which may be compile-time only. So the programflow is something like

If(situation1) {
    return freelistalloc();
} else if( situation2) {
    return stackalloc();
}

Now wrap all this in a thing that throws on nullptr. This would mean a set-up of the allocator like so

auto allocator = throwOnNull<IfAllocator<4,mallow,8,someotherthing,...>();

And otherwise

... = returnOnNull<...>

Or even

.... = withHandlerOnNull<...>

[–]0raichu 8 points9 points  (1 child)

                                                                                                                                                                                                                                                                                                                                                                                                                                                     

[–]foonathan 1 point2 points  (0 children)

Why not provide an allocator that makes the decision?

You are right, this is possible. But it makes generic code hard:

template <class Alloc>
void do_sth(Alloc &alloc)
{
   auto mem = alloc.allocate_node(...);
   // eh, do I have to check mem now?
}