diff --git a/AK/DoublyLinkedList.h b/AK/DoublyLinkedList.h index 9d22d31e22f..bfdb9c6e821 100644 --- a/AK/DoublyLinkedList.h +++ b/AK/DoublyLinkedList.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include @@ -91,42 +92,62 @@ public: } template - void append(U&& value) + ErrorOr try_append(U&& value) { static_assert( requires { T(value); }, "Conversion operator is missing."); - auto* node = new Node(forward(value)); + auto* node = new (nothrow) Node(forward(value)); + if (!node) + return Error::from_errno(ENOMEM); if (!m_head) { VERIFY(!m_tail); m_head = node; m_tail = node; - return; + return {}; } VERIFY(m_tail); VERIFY(!node->next); m_tail->next = node; node->prev = m_tail; m_tail = node; + return {}; } template - void prepend(U&& value) + ErrorOr try_prepend(U&& value) { static_assert(IsSame); - auto* node = new Node(forward(value)); + auto* node = new (nothrow) Node(forward(value)); + if (!node) + return Error::from_errno(ENOMEM); if (!m_head) { VERIFY(!m_tail); m_head = node; m_tail = node; - return; + return {}; } VERIFY(m_tail); VERIFY(!node->prev); m_head->prev = node; node->next = m_head; m_head = node; + return {}; } +#ifndef KERNEL + template + void append(U&& value) + { + MUST(try_append(forward(value))); + } + + template + void prepend(U&& value) + { + MUST(try_prepend(forward(value))); + } +#endif + [[nodiscard]] bool contains_slow(const T& value) const { return find(value) != end(); diff --git a/Kernel/Devices/Device.h b/Kernel/Devices/Device.h index 39c921d3e65..8c5bfd2ab69 100644 --- a/Kernel/Devices/Device.h +++ b/Kernel/Devices/Device.h @@ -58,7 +58,7 @@ public: auto request = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) AsyncRequestType(*this, forward(args)...))); SpinlockLocker lock(m_requests_lock); bool was_empty = m_requests.is_empty(); - m_requests.append(request); + TRY(m_requests.try_append(request)); if (was_empty) request->do_start(move(lock)); return request;