1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:17:44 +00:00

Kernel: Make KBuffer::try_create_with_size() return KResultOr

This allows us to use TRY() in a lot of new places.
This commit is contained in:
Andreas Kling 2021-09-07 15:15:08 +02:00
parent c69035c630
commit 899cee8185
10 changed files with 31 additions and 72 deletions

View file

@ -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;
}

View file

@ -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();

View file

@ -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);

View file

@ -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);

View file

@ -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);
}