From e94dfb7355bd6283e9f8a3d2063c1a4bf9120bd3 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 15 Jul 2021 01:00:29 +0300 Subject: [PATCH] AK: Expose RedBlackTree allocation failures via try_insert This should help with using the RedBlackTree in a more OOM-safe way in the kernel. --- AK/RedBlackTree.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/AK/RedBlackTree.h b/AK/RedBlackTree.h index b02c97952ca..4728608f522 100644 --- a/AK/RedBlackTree.h +++ b/AK/RedBlackTree.h @@ -446,10 +446,19 @@ public: 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) { - auto* node = new Node(key, move(value)); - BaseTree::insert(node); + auto success = try_insert(key, move(value)); + VERIFY(success); } using Iterator = RedBlackTreeIterator;