diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-07 15:15:08 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-07 15:15:08 +0200 |
commit | 899cee8185d06c7fe9b08e6b1a846cd6bc4363e1 (patch) | |
tree | 0ac90e5f3a61d166845e5c0d03e2182ba6cbb921 /Kernel/Net | |
parent | c69035c630178459e5a518fb6ef843eb01b8e661 (diff) | |
download | serenity-899cee8185d06c7fe9b08e6b1a846cd6bc4363e1.zip |
Kernel: Make KBuffer::try_create_with_size() return KResultOr
This allows us to use TRY() in a lot of new places.
Diffstat (limited to 'Kernel/Net')
-rw-r--r-- | Kernel/Net/NetworkAdapter.cpp | 13 | ||||
-rw-r--r-- | Kernel/Net/TCPSocket.cpp | 7 |
2 files changed, 9 insertions, 11 deletions
diff --git a/Kernel/Net/NetworkAdapter.cpp b/Kernel/Net/NetworkAdapter.cpp index 9bfc025f96..fa19933643 100644 --- a/Kernel/Net/NetworkAdapter.cpp +++ b/Kernel/Net/NetworkAdapter.cpp @@ -116,10 +116,11 @@ RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size) { InterruptDisabler disabler; if (m_unused_packets.is_empty()) { - auto buffer = KBuffer::try_create_with_size(size, Memory::Region::Access::ReadWrite, "Packet Buffer", AllocationStrategy::AllocateNow); - if (!buffer) + auto buffer_or_error = KBuffer::try_create_with_size(size, Memory::Region::Access::ReadWrite, "Packet Buffer", AllocationStrategy::AllocateNow); + if (buffer_or_error.is_error()) return {}; - auto packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { buffer.release_nonnull(), kgettimeofday() }); + auto buffer = buffer_or_error.release_value(); + auto packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { move(buffer), kgettimeofday() }); if (!packet) return {}; packet->buffer->set_size(size); @@ -133,10 +134,10 @@ RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size) return packet; } - auto buffer = KBuffer::try_create_with_size(size, Memory::Region::Access::ReadWrite, "Packet Buffer", AllocationStrategy::AllocateNow); - if (!buffer) + auto buffer_or_error = KBuffer::try_create_with_size(size, Memory::Region::Access::ReadWrite, "Packet Buffer", AllocationStrategy::AllocateNow); + if (buffer_or_error.is_error()) return {}; - packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { buffer.release_nonnull(), kgettimeofday() }); + packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { buffer_or_error.release_value(), kgettimeofday() }); if (!packet) return {}; packet->buffer->set_size(size); diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index a82d6bfcc9..854deede4b 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -150,11 +150,8 @@ TCPSocket::~TCPSocket() KResultOr<NonnullRefPtr<TCPSocket>> TCPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer) { // Note: Scratch buffer is only used for SOCK_STREAM sockets. - auto scratch_buffer = KBuffer::try_create_with_size(65536); - if (!scratch_buffer) - return ENOMEM; - - return adopt_nonnull_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), scratch_buffer.release_nonnull())); + auto scratch_buffer = TRY(KBuffer::try_create_with_size(65536)); + return adopt_nonnull_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer))); } KResultOr<size_t> TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags) |