diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-06-16 15:33:14 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-16 21:29:36 +0200 |
commit | ca3cae81eb8e20d63e36e7359a776af5eb4d5ea2 (patch) | |
tree | b0a86391684aca6c2fa73fc74e0767afec4f9ab2 /Kernel | |
parent | 1c3346e3ceb442de8d52a8f9f756a4c0c737b294 (diff) | |
download | serenity-ca3cae81eb8e20d63e36e7359a776af5eb4d5ea2.zip |
Kernel: Use KResultOr<size_t> for the DoubleBuffer class
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/DoubleBuffer.cpp | 18 | ||||
-rw-r--r-- | Kernel/DoubleBuffer.h | 12 | ||||
-rw-r--r-- | Kernel/Net/IPv4Socket.cpp | 16 | ||||
-rw-r--r-- | Kernel/Net/LocalSocket.cpp | 16 |
4 files changed, 31 insertions, 31 deletions
diff --git a/Kernel/DoubleBuffer.cpp b/Kernel/DoubleBuffer.cpp index 41236e4c2b..21336bbf30 100644 --- a/Kernel/DoubleBuffer.cpp +++ b/Kernel/DoubleBuffer.cpp @@ -40,7 +40,7 @@ void DoubleBuffer::flip() compute_lockfree_metadata(); } -ssize_t DoubleBuffer::write(const UserOrKernelBuffer& data, size_t size) +KResultOr<size_t> DoubleBuffer::write(const UserOrKernelBuffer& data, size_t size) { if (!size || m_storage.is_null()) return 0; @@ -48,15 +48,15 @@ ssize_t DoubleBuffer::write(const UserOrKernelBuffer& data, size_t size) size_t bytes_to_write = min(size, m_space_for_writing); u8* write_ptr = m_write_buffer->data + m_write_buffer->size; if (!data.read(write_ptr, bytes_to_write)) - return -EFAULT; + return EFAULT; m_write_buffer->size += bytes_to_write; compute_lockfree_metadata(); if (m_unblock_callback && !m_empty) m_unblock_callback(); - return (ssize_t)bytes_to_write; + return bytes_to_write; } -ssize_t DoubleBuffer::read(UserOrKernelBuffer& data, size_t size) +KResultOr<size_t> DoubleBuffer::read(UserOrKernelBuffer& data, size_t size) { if (!size || m_storage.is_null()) return 0; @@ -67,15 +67,15 @@ ssize_t DoubleBuffer::read(UserOrKernelBuffer& data, size_t size) return 0; size_t nread = min(m_read_buffer->size - m_read_buffer_index, size); if (!data.write(m_read_buffer->data + m_read_buffer_index, nread)) - return -EFAULT; + return EFAULT; m_read_buffer_index += nread; compute_lockfree_metadata(); if (m_unblock_callback && m_space_for_writing > 0) m_unblock_callback(); - return (ssize_t)nread; + return nread; } -ssize_t DoubleBuffer::peek(UserOrKernelBuffer& data, size_t size) +KResultOr<size_t> DoubleBuffer::peek(UserOrKernelBuffer& data, size_t size) { if (!size || m_storage.is_null()) return 0; @@ -87,11 +87,11 @@ ssize_t DoubleBuffer::peek(UserOrKernelBuffer& data, size_t size) return 0; size_t nread = min(m_read_buffer->size - m_read_buffer_index, size); if (!data.write(m_read_buffer->data + m_read_buffer_index, nread)) - return -EFAULT; + return EFAULT; compute_lockfree_metadata(); if (m_unblock_callback && m_space_for_writing > 0) m_unblock_callback(); - return (ssize_t)nread; + return nread; } } diff --git a/Kernel/DoubleBuffer.h b/Kernel/DoubleBuffer.h index 8bf58f7532..664f2a2308 100644 --- a/Kernel/DoubleBuffer.h +++ b/Kernel/DoubleBuffer.h @@ -18,19 +18,19 @@ class DoubleBuffer { public: explicit DoubleBuffer(size_t capacity = 65536); - [[nodiscard]] ssize_t write(const UserOrKernelBuffer&, size_t); - [[nodiscard]] ssize_t write(const u8* data, size_t size) + [[nodiscard]] KResultOr<size_t> write(const UserOrKernelBuffer&, size_t); + [[nodiscard]] KResultOr<size_t> write(const u8* data, size_t size) { return write(UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>(data)), size); } - [[nodiscard]] ssize_t read(UserOrKernelBuffer&, size_t); - [[nodiscard]] ssize_t read(u8* data, size_t size) + [[nodiscard]] KResultOr<size_t> read(UserOrKernelBuffer&, size_t); + [[nodiscard]] KResultOr<size_t> read(u8* data, size_t size) { auto buffer = UserOrKernelBuffer::for_kernel_buffer(data); return read(buffer, size); } - [[nodiscard]] ssize_t peek(UserOrKernelBuffer&, size_t); - [[nodiscard]] ssize_t peek(u8* data, size_t size) + [[nodiscard]] KResultOr<size_t> peek(UserOrKernelBuffer&, size_t); + [[nodiscard]] KResultOr<size_t> peek(u8* data, size_t size) { auto buffer = UserOrKernelBuffer::for_kernel_buffer(data); return peek(buffer, size); diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index c1c1d5bb1d..7268e71549 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -270,17 +270,17 @@ KResultOr<size_t> IPv4Socket::receive_byte_buffered(FileDescription& description VERIFY(!m_receive_buffer.is_empty()); - int nreceived; + KResultOr<size_t> nreceived_or_error { 0 }; if (flags & MSG_PEEK) - nreceived = m_receive_buffer.peek(buffer, buffer_length); + nreceived_or_error = m_receive_buffer.peek(buffer, buffer_length); else - nreceived = m_receive_buffer.read(buffer, buffer_length); + nreceived_or_error = m_receive_buffer.read(buffer, buffer_length); - if (nreceived > 0 && !(flags & MSG_PEEK)) - Thread::current()->did_ipv4_socket_read((size_t)nreceived); + if (!nreceived_or_error.is_error() && nreceived_or_error.value() > 0 && !(flags & MSG_PEEK)) + Thread::current()->did_ipv4_socket_read(nreceived_or_error.value()); set_can_read(!m_receive_buffer.is_empty()); - return nreceived; + return nreceived_or_error; } KResultOr<size_t> IPv4Socket::receive_packet_buffered(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*> addr, Userspace<socklen_t*> addr_length, Time& packet_timestamp) @@ -418,8 +418,8 @@ bool IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port, auto nreceived_or_error = protocol_receive(ReadonlyBytes { packet.data(), packet.size() }, scratch_buffer, m_scratch_buffer.value().size(), 0); if (nreceived_or_error.is_error()) return false; - ssize_t nwritten = m_receive_buffer.write(scratch_buffer, nreceived_or_error.value()); - if (nwritten < 0) + auto nwritten_or_error = m_receive_buffer.write(scratch_buffer, nreceived_or_error.value()); + if (nwritten_or_error.is_error()) return false; set_can_read(!m_receive_buffer.is_empty()); } else { diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index f5be34babc..2cd6e4adeb 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -293,10 +293,10 @@ KResultOr<size_t> LocalSocket::sendto(FileDescription& description, const UserOr auto* socket_buffer = send_buffer_for(description); if (!socket_buffer) return EINVAL; - ssize_t nwritten = socket_buffer->write(data, data_size); - if (nwritten > 0) - Thread::current()->did_unix_socket_write(nwritten); - return nwritten; + auto nwritten_or_error = socket_buffer->write(data, data_size); + if (!nwritten_or_error.is_error() && nwritten_or_error.value() > 0) + Thread::current()->did_unix_socket_write(nwritten_or_error.value()); + return nwritten_or_error; } DoubleBuffer* LocalSocket::receive_buffer_for(FileDescription& description) @@ -338,10 +338,10 @@ KResultOr<size_t> LocalSocket::recvfrom(FileDescription& description, UserOrKern if (!has_attached_peer(description) && socket_buffer->is_empty()) return 0; VERIFY(!socket_buffer->is_empty()); - auto nread = socket_buffer->read(buffer, buffer_size); - if (nread > 0) - Thread::current()->did_unix_socket_read(nread); - return nread; + auto nread_or_error = socket_buffer->read(buffer, buffer_size); + if (!nread_or_error.is_error() && nread_or_error.value() > 0) + Thread::current()->did_unix_socket_read(nread_or_error.value()); + return nread_or_error; } StringView LocalSocket::socket_path() const |