浏览代码

Kernel: Add KBuffer::bytes() and use it

(Instead of hand-wrapping { data(), size() } in a bunch of places.)
Andreas Kling 3 年之前
父节点
当前提交
524ef5e475

+ 1 - 2
Kernel/FileSystem/Inode.cpp

@@ -75,8 +75,7 @@ KResultOr<NonnullRefPtr<Custody>> Inode::resolve_as_link(Custody& base, RefPtr<C
     // contents as a path and resolves that. That is, it
     // behaves exactly how you would expect a symlink to work.
     auto contents = TRY(read_entire());
-    auto path = StringView(contents->data(), contents->size());
-    return VirtualFileSystem::the().resolve_path(path, base, out_parent, options, symlink_recursion_level);
+    return VirtualFileSystem::the().resolve_path(StringView { contents->bytes() }, base, out_parent, options, symlink_recursion_level);
 }
 
 Inode::Inode(FileSystem& fs, InodeIndex index)

+ 1 - 1
Kernel/FileSystem/Plan9FileSystem.cpp

@@ -333,7 +333,7 @@ Plan9FS::Message::Message(Plan9FS& fs, Type type)
 }
 
 Plan9FS::Message::Message(NonnullOwnPtr<KBuffer>&& buffer)
-    : m_built { move(buffer), Decoder({ buffer->data(), buffer->size() }) }
+    : m_built { move(buffer), Decoder({ buffer->bytes() }) }
     , m_have_been_built(true)
 {
     u32 size;

+ 3 - 0
Kernel/KBuffer.h

@@ -42,6 +42,9 @@ public:
     [[nodiscard]] size_t size() const { return m_size; }
     [[nodiscard]] size_t capacity() const { return m_region->size(); }
 
+    [[nodiscard]] ReadonlyBytes bytes() const { return { data(), size() }; }
+    [[nodiscard]] Bytes bytes() { return { data(), size() }; }
+
     void set_size(size_t size)
     {
         VERIFY(size <= capacity());

+ 1 - 1
Kernel/KBufferBuilder.h

@@ -48,7 +48,7 @@ public:
     {
         if (!m_buffer)
             return {};
-        return ReadonlyBytes { m_buffer->data(), m_buffer->size() };
+        return m_buffer->bytes();
     }
 
 private:

+ 1 - 1
Kernel/Net/IPv4Socket.cpp

@@ -372,7 +372,7 @@ KResultOr<size_t> IPv4Socket::receive_packet_buffered(OpenFileDescription& descr
         return bytes_written;
     }
 
-    return protocol_receive(ReadonlyBytes { packet->data->data(), packet->data->size() }, buffer, buffer_length, flags);
+    return protocol_receive(packet->data->bytes(), buffer, buffer_length, flags);
 }
 
 KResultOr<size_t> IPv4Socket::recvfrom(OpenFileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*> user_addr, Userspace<socklen_t*> user_addr_length, Time& packet_timestamp)

+ 1 - 1
Kernel/Net/NetworkAdapter.h

@@ -34,7 +34,7 @@ struct PacketWithTimestamp : public RefCounted<PacketWithTimestamp> {
     {
     }
 
-    ReadonlyBytes bytes() { return { buffer->data(), buffer->size() }; };
+    ReadonlyBytes bytes() { return buffer->bytes(); }
 
     NonnullOwnPtr<KBuffer> buffer;
     Time timestamp;

+ 1 - 1
Kernel/Syscalls/module.cpp

@@ -27,7 +27,7 @@ KResultOr<FlatPtr> Process::sys$module_load(Userspace<const char*> user_path, si
     auto description = TRY(VirtualFileSystem::the().open(path->view(), O_RDONLY, 0, current_directory()));
     auto payload = TRY(description->read_entire_file());
 
-    auto storage = TRY(KBuffer::try_create_with_bytes(ReadonlyBytes { payload->data(), payload->size() }));
+    auto storage = TRY(KBuffer::try_create_with_bytes(payload->bytes()));
 
     auto elf_image = try_make<ELF::Image>(storage->data(), storage->size());
     if (!elf_image)