diff --git a/Kernel/DoubleBuffer.cpp b/Kernel/DoubleBuffer.cpp index d8869135a0..dbf5c1fba4 100644 --- a/Kernel/DoubleBuffer.cpp +++ b/Kernel/DoubleBuffer.cpp @@ -19,11 +19,8 @@ inline void DoubleBuffer::compute_lockfree_metadata() KResultOr> 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 storage) diff --git a/Kernel/FileSystem/BlockBasedFileSystem.cpp b/Kernel/FileSystem/BlockBasedFileSystem.cpp index 34bf759db6..416558bf09 100644 --- a/Kernel/FileSystem/BlockBasedFileSystem.cpp +++ b/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; } diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index 567412d45c..f50ba21849 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/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::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(); diff --git a/Kernel/FileSystem/ISO9660FileSystem.cpp b/Kernel/FileSystem/ISO9660FileSystem.cpp index cb5dd95068..a964f1945f 100644 --- a/Kernel/FileSystem/ISO9660FileSystem.cpp +++ b/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> 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 ISO9660Inode::read_bytes(off_t offset, size_t size, UserOrKern if (static_cast(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); diff --git a/Kernel/FileSystem/Plan9FileSystem.cpp b/Kernel/FileSystem/Plan9FileSystem.cpp index 8508f90b0b..0cdca6caa1 100644 --- a/Kernel/FileSystem/Plan9FileSystem.cpp +++ b/Kernel/FileSystem/Plan9FileSystem.cpp @@ -553,9 +553,7 @@ KResult Plan9FS::read_and_dispatch_one_message() Header header; TRY(do_read(reinterpret_cast(&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); diff --git a/Kernel/FileSystem/TmpFS.cpp b/Kernel/FileSystem/TmpFS.cpp index 0a671061bd..69df11c9ee 100644 --- a/Kernel/FileSystem/TmpFS.cpp +++ b/Kernel/FileSystem/TmpFS.cpp @@ -169,9 +169,7 @@ KResultOr 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) < 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); } diff --git a/Kernel/KBuffer.h b/Kernel/KBuffer.h index e3e005db1d..7ac17dbe96 100644 --- a/Kernel/KBuffer.h +++ b/Kernel/KBuffer.h @@ -106,12 +106,12 @@ public: { } - [[nodiscard]] static OwnPtr try_create_with_size(size_t size, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve) + static KResultOr> 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 try_create_with_bytes(ReadonlyBytes bytes, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve) 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 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 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> TCPSocket::try_create(int protocol, NonnullOwnPtr 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 TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags) diff --git a/Kernel/PerformanceEventBuffer.cpp b/Kernel/PerformanceEventBuffer.cpp index 7914d07294..09f01eb62f 100644 --- a/Kernel/PerformanceEventBuffer.cpp +++ b/Kernel/PerformanceEventBuffer.cpp @@ -274,10 +274,10 @@ bool PerformanceEventBuffer::to_json(KBufferBuilder& builder) const OwnPtr 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)