base: introduce Allocator::try_alloc

This patch changes the 'Allocator' interface to the use of 'Attempt'
return values instead of using exceptions for propagating errors.

To largely uphold compatibility with components using the original
exception-based interface - in particluar use cases where an 'Allocator'
is passed to the 'new' operator - the traditional 'alloc' is still
supported. But it existes merely as a wrapper around the new
'try_alloc'.

Issue #4324
This commit is contained in:
Norman Feske
2021-11-10 12:01:32 +01:00
committed by Christian Helmuth
parent 9591e6caee
commit dc39a8db62
102 changed files with 2128 additions and 1710 deletions

View File

@@ -40,13 +40,17 @@ extern "C" void *malloc(size_t size)
* the subsequent address. This way, we can retrieve
* the size information when freeing the block.
*/
unsigned long real_size = size + sizeof(unsigned long);
void *addr = 0;
if (!alloc().alloc(real_size, &addr))
return 0;
unsigned long const real_size = size + sizeof(unsigned long);
*(unsigned long *)addr = real_size;
return (unsigned long *)addr + 1;
return alloc().try_alloc(real_size).convert<void *>(
[&] (void *ptr) {
*(unsigned long *)ptr = real_size;
return (unsigned long *)ptr + 1; },
[&] (Allocator::Alloc_error) {
return nullptr; });
}