mirror of
https://github.com/mmueller41/genode.git
synced 2026-01-21 12:32:56 +01:00
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:
committed by
Christian Helmuth
parent
9591e6caee
commit
dc39a8db62
@@ -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; });
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user