diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-07 13:46:11 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-07 13:53:14 +0200 |
commit | 01993d0af34bb41c9ac8363e2fb584f42e191d38 (patch) | |
tree | 9a45435991ef5902f712e137c5db0f4f0c7a36d5 /Kernel/Net | |
parent | 213b8868af6028c98cf96b0a668fc3dda8ba5958 (diff) | |
download | serenity-01993d0af34bb41c9ac8363e2fb584f42e191d38.zip |
Kernel: Make DoubleBuffer::try() return KResultOr
This tidies up error propagation in a number of places.
Diffstat (limited to 'Kernel/Net')
-rw-r--r-- | Kernel/Net/IPv4Socket.cpp | 12 | ||||
-rw-r--r-- | Kernel/Net/IPv4Socket.h | 2 | ||||
-rw-r--r-- | Kernel/Net/LocalSocket.cpp | 13 | ||||
-rw-r--r-- | Kernel/Net/TCPSocket.cpp | 6 |
4 files changed, 12 insertions, 21 deletions
diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index 0a2ad95a26..88cdf95cc3 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -35,23 +35,21 @@ MutexProtected<IPv4Socket::List>& IPv4Socket::all_sockets() return *s_all_sockets; } -OwnPtr<DoubleBuffer> IPv4Socket::create_receive_buffer() +KResultOr<NonnullOwnPtr<DoubleBuffer>> IPv4Socket::try_create_receive_buffer() { return DoubleBuffer::try_create(256 * KiB); } KResultOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol) { - auto receive_buffer = IPv4Socket::create_receive_buffer(); - if (!receive_buffer) - return ENOMEM; + auto receive_buffer = TRY(IPv4Socket::try_create_receive_buffer()); if (type == SOCK_STREAM) - return TRY(TCPSocket::try_create(protocol, receive_buffer.release_nonnull())); + return TRY(TCPSocket::try_create(protocol, move(receive_buffer))); if (type == SOCK_DGRAM) - return TRY(UDPSocket::try_create(protocol, receive_buffer.release_nonnull())); + return TRY(UDPSocket::try_create(protocol, move(receive_buffer))); if (type == SOCK_RAW) { - auto raw_socket = adopt_ref_if_nonnull(new (nothrow) IPv4Socket(type, protocol, receive_buffer.release_nonnull(), {})); + auto raw_socket = adopt_ref_if_nonnull(new (nothrow) IPv4Socket(type, protocol, move(receive_buffer), {})); if (raw_socket) return raw_socket.release_nonnull(); return ENOMEM; diff --git a/Kernel/Net/IPv4Socket.h b/Kernel/Net/IPv4Socket.h index 4d6cbcc35b..3ec3521acc 100644 --- a/Kernel/Net/IPv4Socket.h +++ b/Kernel/Net/IPv4Socket.h @@ -90,7 +90,7 @@ protected: void set_local_address(IPv4Address address) { m_local_address = address; } void set_peer_address(IPv4Address address) { m_peer_address = address; } - static OwnPtr<DoubleBuffer> create_receive_buffer(); + static KResultOr<NonnullOwnPtr<DoubleBuffer>> try_create_receive_buffer(); private: virtual bool is_ipv4() const override { return true; } diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index 43c59e0a46..9deaa786d2 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -36,16 +36,9 @@ void LocalSocket::for_each(Function<void(const LocalSocket&)> callback) KResultOr<NonnullRefPtr<LocalSocket>> LocalSocket::try_create(int type) { - auto client_buffer = DoubleBuffer::try_create(); - if (!client_buffer) - return ENOMEM; - auto server_buffer = DoubleBuffer::try_create(); - if (!server_buffer) - return ENOMEM; - auto socket = adopt_ref_if_nonnull(new (nothrow) LocalSocket(type, client_buffer.release_nonnull(), server_buffer.release_nonnull())); - if (socket) - return socket.release_nonnull(); - return ENOMEM; + auto client_buffer = TRY(DoubleBuffer::try_create()); + auto server_buffer = TRY(DoubleBuffer::try_create()); + return adopt_nonnull_ref_or_enomem(new (nothrow) LocalSocket(type, move(client_buffer), move(server_buffer))); } KResultOr<SocketPair> LocalSocket::try_create_connected_pair(int type) diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index a3b8dd7ac0..22905a61c8 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -97,10 +97,10 @@ RefPtr<TCPSocket> TCPSocket::create_client(const IPv4Address& new_local_address, if (table.contains(tuple)) return {}; - auto receive_buffer = create_receive_buffer(); - if (!receive_buffer) + auto receive_buffer = try_create_receive_buffer(); + if (receive_buffer.is_error()) return {}; - auto client_or_error = TCPSocket::try_create(protocol(), receive_buffer.release_nonnull()); + auto client_or_error = TCPSocket::try_create(protocol(), receive_buffer.release_value()); if (client_or_error.is_error()) return {}; |