Browse Source

Kernel: Make KBuffer::try_create_with_size() return KResultOr

This allows us to use TRY() in a lot of new places.
Andreas Kling 3 years ago
parent
commit
899cee8185

+ 2 - 5
Kernel/DoubleBuffer.cpp

@@ -19,11 +19,8 @@ inline void DoubleBuffer::compute_lockfree_metadata()
 
 KResultOr<NonnullOwnPtr<DoubleBuffer>> DoubleBuffer::try_create(size_t capacity)
 {
-    auto storage = KBuffer::try_create_with_size(capacity * 2, Memory::Region::Access::ReadWrite, "DoubleBuffer");
-    if (!storage)
-        return ENOMEM;
-
-    return adopt_nonnull_own_or_enomem(new (nothrow) DoubleBuffer(capacity, storage.release_nonnull()));
+    auto storage = TRY(KBuffer::try_create_with_size(capacity * 2, Memory::Region::Access::ReadWrite, "DoubleBuffer"));
+    return adopt_nonnull_own_or_enomem(new (nothrow) DoubleBuffer(capacity, move(storage)));
 }
 
 DoubleBuffer::DoubleBuffer(size_t capacity, NonnullOwnPtr<KBuffer> storage)

+ 3 - 12
Kernel/FileSystem/BlockBasedFileSystem.cpp

@@ -117,22 +117,13 @@ BlockBasedFileSystem::~BlockBasedFileSystem()
 KResult BlockBasedFileSystem::initialize()
 {
     VERIFY(block_size() != 0);
-    auto cached_block_data = KBuffer::try_create_with_size(DiskCache::EntryCount * block_size());
-    if (!cached_block_data)
-        return ENOMEM;
-
-    auto entries_data = KBuffer::try_create_with_size(DiskCache::EntryCount * sizeof(CacheEntry));
-    if (!entries_data)
-        return ENOMEM;
-
-    auto disk_cache = adopt_own_if_nonnull(new (nothrow) DiskCache(*this, cached_block_data.release_nonnull(), entries_data.release_nonnull()));
-    if (!disk_cache)
-        return ENOMEM;
+    auto cached_block_data = TRY(KBuffer::try_create_with_size(DiskCache::EntryCount * block_size()));
+    auto entries_data = TRY(KBuffer::try_create_with_size(DiskCache::EntryCount * sizeof(CacheEntry)));
+    auto disk_cache = TRY(adopt_nonnull_own_or_enomem(new (nothrow) DiskCache(*this, move(cached_block_data), move(entries_data))));
 
     m_cache.with_exclusive([&](auto& cache) {
         cache = move(disk_cache);
     });
-
     return KSuccess;
 }
 

+ 3 - 11
Kernel/FileSystem/Ext2FileSystem.cpp

@@ -132,11 +132,7 @@ KResult Ext2FS::initialize()
 
     auto blocks_to_read = ceil_div(m_block_group_count * sizeof(ext2_group_desc), block_size());
     BlockIndex first_block_of_bgdt = block_size() == 1024 ? 2 : 1;
-    m_cached_group_descriptor_table = KBuffer::try_create_with_size(block_size() * blocks_to_read, Memory::Region::Access::ReadWrite, "Ext2FS: Block group descriptors");
-    if (!m_cached_group_descriptor_table) {
-        dbgln("Ext2FS: Failed to allocate memory for group descriptor table");
-        return ENOMEM;
-    }
+    m_cached_group_descriptor_table = TRY(KBuffer::try_create_with_size(block_size() * blocks_to_read, Memory::Region::Access::ReadWrite, "Ext2FS: Block group descriptors"));
     auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_group_descriptor_table->data());
     TRY(read_blocks(first_block_of_bgdt, blocks_to_read, buffer));
 
@@ -1460,17 +1456,13 @@ KResultOr<Ext2FS::CachedBitmap*> Ext2FS::get_bitmap_block(BlockIndex bitmap_bloc
             return cached_bitmap;
     }
 
-    auto block = KBuffer::try_create_with_size(block_size(), Memory::Region::Access::ReadWrite, "Ext2FS: Cached bitmap block");
-    if (!block)
-        return ENOMEM;
+    auto block = TRY(KBuffer::try_create_with_size(block_size(), Memory::Region::Access::ReadWrite, "Ext2FS: Cached bitmap block"));
     auto buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
     if (auto result = read_block(bitmap_block_index, &buffer, block_size()); result.is_error()) {
         dbgln("Ext2FS: Failed to load bitmap block {}", bitmap_block_index);
         return result;
     }
-    auto new_bitmap = adopt_own_if_nonnull(new (nothrow) CachedBitmap(bitmap_block_index, block.release_nonnull()));
-    if (!new_bitmap)
-        return ENOMEM;
+    auto new_bitmap = TRY(adopt_nonnull_own_or_enomem(new (nothrow) CachedBitmap(bitmap_block_index, move(block))));
     if (!m_cached_bitmaps.try_append(move(new_bitmap)))
         return ENOMEM;
     return m_cached_bitmaps.last();

+ 3 - 14
Kernel/FileSystem/ISO9660FileSystem.cpp

@@ -232,11 +232,7 @@ KResult ISO9660FS::parse_volume_set()
 {
     VERIFY(!m_primary_volume);
 
-    auto block = KBuffer::try_create_with_size(m_logical_block_size, Memory::Region::Access::Read | Memory::Region::Access::Write, "ISO9660FS: Temporary volume descriptor storage");
-    if (!block) {
-        return ENOMEM;
-    }
-
+    auto block = TRY(KBuffer::try_create_with_size(m_logical_block_size, Memory::Region::Access::Read | Memory::Region::Access::Write, "ISO9660FS: Temporary volume descriptor storage"));
     auto block_buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
 
     auto current_block_index = first_data_area_block;
@@ -392,11 +388,7 @@ KResultOr<NonnullRefPtr<ISO9660FS::DirectoryEntry>> ISO9660FS::directory_entry_f
         return EIO;
     }
 
-    auto blocks = KBuffer::try_create_with_size(data_length, Memory::Region::Access::Read | Memory::Region::Access::Write, "ISO9660FS: Directory traversal buffer");
-    if (!blocks) {
-        return ENOMEM;
-    }
-
+    auto blocks = TRY(KBuffer::try_create_with_size(data_length, Memory::Region::Access::Read | Memory::Region::Access::Write, "ISO9660FS: Directory traversal buffer"));
     auto blocks_buffer = UserOrKernelBuffer::for_kernel_buffer(blocks->data());
     auto did_read = raw_read_blocks(BlockBasedFileSystem::BlockIndex { extent_location }, data_length / logical_block_size(), blocks_buffer);
     if (!did_read) {
@@ -425,10 +417,7 @@ KResultOr<size_t> ISO9660Inode::read_bytes(off_t offset, size_t size, UserOrKern
     if (static_cast<u64>(offset) >= data_length)
         return 0;
 
-    auto block = KBuffer::try_create_with_size(fs().m_logical_block_size);
-    if (!block) {
-        return ENOMEM;
-    }
+    auto block = TRY(KBuffer::try_create_with_size(fs().m_logical_block_size));
     auto block_buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
 
     size_t total_bytes = min(size, data_length - offset);

+ 2 - 4
Kernel/FileSystem/Plan9FileSystem.cpp

@@ -553,9 +553,7 @@ KResult Plan9FS::read_and_dispatch_one_message()
     Header header;
     TRY(do_read(reinterpret_cast<u8*>(&header), sizeof(header)));
 
-    auto buffer = KBuffer::try_create_with_size(header.size, Memory::Region::Access::ReadWrite);
-    if (!buffer)
-        return ENOMEM;
+    auto buffer = TRY(KBuffer::try_create_with_size(header.size, Memory::Region::Access::ReadWrite));
     // Copy the already read header into the buffer.
     memcpy(buffer->data(), &header, sizeof(header));
     TRY(do_read(buffer->data() + sizeof(header), header.size - sizeof(header)));
@@ -567,7 +565,7 @@ KResult Plan9FS::read_and_dispatch_one_message()
         auto completion = optional_completion.value();
         SpinlockLocker lock(completion->lock);
         completion->result = KSuccess;
-        completion->message = adopt_own_if_nonnull(new (nothrow) Message { buffer.release_nonnull() });
+        completion->message = adopt_own_if_nonnull(new (nothrow) Message { move(buffer) });
         completion->completed = true;
 
         m_completions.remove(header.tag);

+ 3 - 9
Kernel/FileSystem/TmpFS.cpp

@@ -169,9 +169,7 @@ KResultOr<size_t> TmpFSInode::write_bytes(off_t offset, size_t size, const UserO
             // FIXME: Fix this so that no memcpy() is necessary, and we can just grow the
             //        KBuffer and it will add physical pages as needed while keeping the
             //        existing ones.
-            auto tmp = KBuffer::try_create_with_size(new_size * 2);
-            if (!tmp)
-                return ENOMEM;
+            auto tmp = TRY(KBuffer::try_create_with_size(new_size * 2));
             tmp->set_size(new_size);
             if (m_content)
                 memcpy(tmp->data(), m_content->data(), old_size);
@@ -316,9 +314,7 @@ KResult TmpFSInode::truncate(u64 size)
     if (size == 0)
         m_content.clear();
     else if (!m_content) {
-        m_content = KBuffer::try_create_with_size(size);
-        if (!m_content)
-            return ENOMEM;
+        m_content = TRY(KBuffer::try_create_with_size(size));
     } else if (static_cast<size_t>(size) < m_content->capacity()) {
         size_t prev_size = m_metadata.size;
         m_content->set_size(size);
@@ -326,9 +322,7 @@ KResult TmpFSInode::truncate(u64 size)
             memset(m_content->data() + prev_size, 0, size - prev_size);
     } else {
         size_t prev_size = m_metadata.size;
-        auto tmp = KBuffer::try_create_with_size(size);
-        if (!tmp)
-            return ENOMEM;
+        auto tmp = TRY(KBuffer::try_create_with_size(size));
         memcpy(tmp->data(), m_content->data(), prev_size);
         m_content = move(tmp);
     }

+ 3 - 3
Kernel/KBuffer.h

@@ -106,12 +106,12 @@ public:
     {
     }
 
-    [[nodiscard]] static OwnPtr<KBuffer> try_create_with_size(size_t size, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)
+    static KResultOr<NonnullOwnPtr<KBuffer>> try_create_with_size(size_t size, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)
     {
         auto impl = KBufferImpl::try_create_with_size(size, access, name, strategy);
         if (!impl)
-            return {};
-        return adopt_own_if_nonnull(new (nothrow) KBuffer(impl.release_nonnull()));
+            return ENOMEM;
+        return adopt_nonnull_own_or_enomem(new (nothrow) KBuffer(impl.release_nonnull()));
     }
 
     [[nodiscard]] static OwnPtr<KBuffer> try_create_with_bytes(ReadonlyBytes bytes, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)

+ 7 - 6
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);

+ 2 - 5
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)

+ 3 - 3
Kernel/PerformanceEventBuffer.cpp

@@ -274,10 +274,10 @@ bool PerformanceEventBuffer::to_json(KBufferBuilder& builder) const
 
 OwnPtr<PerformanceEventBuffer> PerformanceEventBuffer::try_create_with_size(size_t buffer_size)
 {
-    auto buffer = KBuffer::try_create_with_size(buffer_size, Memory::Region::Access::ReadWrite, "Performance events", AllocationStrategy::AllocateNow);
-    if (!buffer)
+    auto buffer_or_error = KBuffer::try_create_with_size(buffer_size, Memory::Region::Access::ReadWrite, "Performance events", AllocationStrategy::AllocateNow);
+    if (buffer_or_error.is_error())
         return {};
-    return adopt_own_if_nonnull(new (nothrow) PerformanceEventBuffer(buffer.release_nonnull()));
+    return adopt_own_if_nonnull(new (nothrow) PerformanceEventBuffer(buffer_or_error.release_value()));
 }
 
 void PerformanceEventBuffer::add_process(const Process& process, ProcessEventType event_type)