1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:17:35 +00:00

AK: Expose RedBlackTree allocation failures via try_insert

This should help with using the RedBlackTree in a more OOM-safe way in
the kernel.
This commit is contained in:
Idan Horowitz 2021-07-15 01:00:29 +03:00 committed by Andreas Kling
parent a9a54914bf
commit e94dfb7355

View file

@ -446,10 +446,19 @@ public:
insert(key, V(value)); insert(key, V(value));
} }
[[nodiscard]] bool try_insert(K key, V&& value)
{
auto* node = new (nothrow) Node(key, move(value));
if (!node)
return false;
BaseTree::insert(node);
return true;
}
void insert(K key, V&& value) void insert(K key, V&& value)
{ {
auto* node = new Node(key, move(value)); auto success = try_insert(key, move(value));
BaseTree::insert(node); VERIFY(success);
} }
using Iterator = RedBlackTreeIterator<RedBlackTree, V>; using Iterator = RedBlackTreeIterator<RedBlackTree, V>;