mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:28:10 +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:
parent
c69035c630
commit
899cee8185
10 changed files with 31 additions and 72 deletions
|
@ -19,11 +19,8 @@ inline void DoubleBuffer::compute_lockfree_metadata()
|
||||||
|
|
||||||
KResultOr<NonnullOwnPtr<DoubleBuffer>> DoubleBuffer::try_create(size_t capacity)
|
KResultOr<NonnullOwnPtr<DoubleBuffer>> DoubleBuffer::try_create(size_t capacity)
|
||||||
{
|
{
|
||||||
auto storage = KBuffer::try_create_with_size(capacity * 2, Memory::Region::Access::ReadWrite, "DoubleBuffer");
|
auto storage = TRY(KBuffer::try_create_with_size(capacity * 2, Memory::Region::Access::ReadWrite, "DoubleBuffer"));
|
||||||
if (!storage)
|
return adopt_nonnull_own_or_enomem(new (nothrow) DoubleBuffer(capacity, move(storage)));
|
||||||
return ENOMEM;
|
|
||||||
|
|
||||||
return adopt_nonnull_own_or_enomem(new (nothrow) DoubleBuffer(capacity, storage.release_nonnull()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DoubleBuffer::DoubleBuffer(size_t capacity, NonnullOwnPtr<KBuffer> storage)
|
DoubleBuffer::DoubleBuffer(size_t capacity, NonnullOwnPtr<KBuffer> storage)
|
||||||
|
|
|
@ -117,22 +117,13 @@ BlockBasedFileSystem::~BlockBasedFileSystem()
|
||||||
KResult BlockBasedFileSystem::initialize()
|
KResult BlockBasedFileSystem::initialize()
|
||||||
{
|
{
|
||||||
VERIFY(block_size() != 0);
|
VERIFY(block_size() != 0);
|
||||||
auto cached_block_data = KBuffer::try_create_with_size(DiskCache::EntryCount * block_size());
|
auto cached_block_data = TRY(KBuffer::try_create_with_size(DiskCache::EntryCount * block_size()));
|
||||||
if (!cached_block_data)
|
auto entries_data = TRY(KBuffer::try_create_with_size(DiskCache::EntryCount * sizeof(CacheEntry)));
|
||||||
return ENOMEM;
|
auto disk_cache = TRY(adopt_nonnull_own_or_enomem(new (nothrow) DiskCache(*this, move(cached_block_data), move(entries_data))));
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
m_cache.with_exclusive([&](auto& cache) {
|
m_cache.with_exclusive([&](auto& cache) {
|
||||||
cache = move(disk_cache);
|
cache = move(disk_cache);
|
||||||
});
|
});
|
||||||
|
|
||||||
return KSuccess;
|
return KSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -132,11 +132,7 @@ KResult Ext2FS::initialize()
|
||||||
|
|
||||||
auto blocks_to_read = ceil_div(m_block_group_count * sizeof(ext2_group_desc), block_size());
|
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;
|
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");
|
m_cached_group_descriptor_table = TRY(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;
|
|
||||||
}
|
|
||||||
auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_group_descriptor_table->data());
|
auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_group_descriptor_table->data());
|
||||||
TRY(read_blocks(first_block_of_bgdt, blocks_to_read, buffer));
|
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;
|
return cached_bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto block = KBuffer::try_create_with_size(block_size(), Memory::Region::Access::ReadWrite, "Ext2FS: Cached bitmap block");
|
auto block = TRY(KBuffer::try_create_with_size(block_size(), Memory::Region::Access::ReadWrite, "Ext2FS: Cached bitmap block"));
|
||||||
if (!block)
|
|
||||||
return ENOMEM;
|
|
||||||
auto buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
|
auto buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
|
||||||
if (auto result = read_block(bitmap_block_index, &buffer, block_size()); result.is_error()) {
|
if (auto result = read_block(bitmap_block_index, &buffer, block_size()); result.is_error()) {
|
||||||
dbgln("Ext2FS: Failed to load bitmap block {}", bitmap_block_index);
|
dbgln("Ext2FS: Failed to load bitmap block {}", bitmap_block_index);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
auto new_bitmap = adopt_own_if_nonnull(new (nothrow) CachedBitmap(bitmap_block_index, block.release_nonnull()));
|
auto new_bitmap = TRY(adopt_nonnull_own_or_enomem(new (nothrow) CachedBitmap(bitmap_block_index, move(block))));
|
||||||
if (!new_bitmap)
|
|
||||||
return ENOMEM;
|
|
||||||
if (!m_cached_bitmaps.try_append(move(new_bitmap)))
|
if (!m_cached_bitmaps.try_append(move(new_bitmap)))
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
return m_cached_bitmaps.last();
|
return m_cached_bitmaps.last();
|
||||||
|
|
|
@ -232,11 +232,7 @@ KResult ISO9660FS::parse_volume_set()
|
||||||
{
|
{
|
||||||
VERIFY(!m_primary_volume);
|
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");
|
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"));
|
||||||
if (!block) {
|
|
||||||
return ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto block_buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
|
auto block_buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
|
||||||
|
|
||||||
auto current_block_index = first_data_area_block;
|
auto current_block_index = first_data_area_block;
|
||||||
|
@ -392,11 +388,7 @@ KResultOr<NonnullRefPtr<ISO9660FS::DirectoryEntry>> ISO9660FS::directory_entry_f
|
||||||
return EIO;
|
return EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto blocks = KBuffer::try_create_with_size(data_length, Memory::Region::Access::Read | Memory::Region::Access::Write, "ISO9660FS: Directory traversal buffer");
|
auto blocks = TRY(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_buffer = UserOrKernelBuffer::for_kernel_buffer(blocks->data());
|
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);
|
auto did_read = raw_read_blocks(BlockBasedFileSystem::BlockIndex { extent_location }, data_length / logical_block_size(), blocks_buffer);
|
||||||
if (!did_read) {
|
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)
|
if (static_cast<u64>(offset) >= data_length)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
auto block = KBuffer::try_create_with_size(fs().m_logical_block_size);
|
auto block = TRY(KBuffer::try_create_with_size(fs().m_logical_block_size));
|
||||||
if (!block) {
|
|
||||||
return ENOMEM;
|
|
||||||
}
|
|
||||||
auto block_buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
|
auto block_buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
|
||||||
|
|
||||||
size_t total_bytes = min(size, data_length - offset);
|
size_t total_bytes = min(size, data_length - offset);
|
||||||
|
|
|
@ -553,9 +553,7 @@ KResult Plan9FS::read_and_dispatch_one_message()
|
||||||
Header header;
|
Header header;
|
||||||
TRY(do_read(reinterpret_cast<u8*>(&header), sizeof(header)));
|
TRY(do_read(reinterpret_cast<u8*>(&header), sizeof(header)));
|
||||||
|
|
||||||
auto buffer = KBuffer::try_create_with_size(header.size, Memory::Region::Access::ReadWrite);
|
auto buffer = TRY(KBuffer::try_create_with_size(header.size, Memory::Region::Access::ReadWrite));
|
||||||
if (!buffer)
|
|
||||||
return ENOMEM;
|
|
||||||
// Copy the already read header into the buffer.
|
// Copy the already read header into the buffer.
|
||||||
memcpy(buffer->data(), &header, sizeof(header));
|
memcpy(buffer->data(), &header, sizeof(header));
|
||||||
TRY(do_read(buffer->data() + sizeof(header), header.size - 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();
|
auto completion = optional_completion.value();
|
||||||
SpinlockLocker lock(completion->lock);
|
SpinlockLocker lock(completion->lock);
|
||||||
completion->result = KSuccess;
|
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;
|
completion->completed = true;
|
||||||
|
|
||||||
m_completions.remove(header.tag);
|
m_completions.remove(header.tag);
|
||||||
|
|
|
@ -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
|
// 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
|
// KBuffer and it will add physical pages as needed while keeping the
|
||||||
// existing ones.
|
// existing ones.
|
||||||
auto tmp = KBuffer::try_create_with_size(new_size * 2);
|
auto tmp = TRY(KBuffer::try_create_with_size(new_size * 2));
|
||||||
if (!tmp)
|
|
||||||
return ENOMEM;
|
|
||||||
tmp->set_size(new_size);
|
tmp->set_size(new_size);
|
||||||
if (m_content)
|
if (m_content)
|
||||||
memcpy(tmp->data(), m_content->data(), old_size);
|
memcpy(tmp->data(), m_content->data(), old_size);
|
||||||
|
@ -316,9 +314,7 @@ KResult TmpFSInode::truncate(u64 size)
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
m_content.clear();
|
m_content.clear();
|
||||||
else if (!m_content) {
|
else if (!m_content) {
|
||||||
m_content = KBuffer::try_create_with_size(size);
|
m_content = TRY(KBuffer::try_create_with_size(size));
|
||||||
if (!m_content)
|
|
||||||
return ENOMEM;
|
|
||||||
} else if (static_cast<size_t>(size) < m_content->capacity()) {
|
} else if (static_cast<size_t>(size) < m_content->capacity()) {
|
||||||
size_t prev_size = m_metadata.size;
|
size_t prev_size = m_metadata.size;
|
||||||
m_content->set_size(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);
|
memset(m_content->data() + prev_size, 0, size - prev_size);
|
||||||
} else {
|
} else {
|
||||||
size_t prev_size = m_metadata.size;
|
size_t prev_size = m_metadata.size;
|
||||||
auto tmp = KBuffer::try_create_with_size(size);
|
auto tmp = TRY(KBuffer::try_create_with_size(size));
|
||||||
if (!tmp)
|
|
||||||
return ENOMEM;
|
|
||||||
memcpy(tmp->data(), m_content->data(), prev_size);
|
memcpy(tmp->data(), m_content->data(), prev_size);
|
||||||
m_content = move(tmp);
|
m_content = move(tmp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
auto impl = KBufferImpl::try_create_with_size(size, access, name, strategy);
|
||||||
if (!impl)
|
if (!impl)
|
||||||
return {};
|
return ENOMEM;
|
||||||
return adopt_own_if_nonnull(new (nothrow) KBuffer(impl.release_nonnull()));
|
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)
|
[[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)
|
||||||
|
|
|
@ -116,10 +116,11 @@ RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
|
||||||
{
|
{
|
||||||
InterruptDisabler disabler;
|
InterruptDisabler disabler;
|
||||||
if (m_unused_packets.is_empty()) {
|
if (m_unused_packets.is_empty()) {
|
||||||
auto buffer = KBuffer::try_create_with_size(size, Memory::Region::Access::ReadWrite, "Packet Buffer", AllocationStrategy::AllocateNow);
|
auto buffer_or_error = KBuffer::try_create_with_size(size, Memory::Region::Access::ReadWrite, "Packet Buffer", AllocationStrategy::AllocateNow);
|
||||||
if (!buffer)
|
if (buffer_or_error.is_error())
|
||||||
return {};
|
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)
|
if (!packet)
|
||||||
return {};
|
return {};
|
||||||
packet->buffer->set_size(size);
|
packet->buffer->set_size(size);
|
||||||
|
@ -133,10 +134,10 @@ RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto buffer = KBuffer::try_create_with_size(size, Memory::Region::Access::ReadWrite, "Packet Buffer", AllocationStrategy::AllocateNow);
|
auto buffer_or_error = KBuffer::try_create_with_size(size, Memory::Region::Access::ReadWrite, "Packet Buffer", AllocationStrategy::AllocateNow);
|
||||||
if (!buffer)
|
if (buffer_or_error.is_error())
|
||||||
return {};
|
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)
|
if (!packet)
|
||||||
return {};
|
return {};
|
||||||
packet->buffer->set_size(size);
|
packet->buffer->set_size(size);
|
||||||
|
|
|
@ -150,11 +150,8 @@ TCPSocket::~TCPSocket()
|
||||||
KResultOr<NonnullRefPtr<TCPSocket>> TCPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer)
|
KResultOr<NonnullRefPtr<TCPSocket>> TCPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer)
|
||||||
{
|
{
|
||||||
// Note: Scratch buffer is only used for SOCK_STREAM sockets.
|
// Note: Scratch buffer is only used for SOCK_STREAM sockets.
|
||||||
auto scratch_buffer = KBuffer::try_create_with_size(65536);
|
auto scratch_buffer = TRY(KBuffer::try_create_with_size(65536));
|
||||||
if (!scratch_buffer)
|
return adopt_nonnull_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer)));
|
||||||
return ENOMEM;
|
|
||||||
|
|
||||||
return adopt_nonnull_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), scratch_buffer.release_nonnull()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
KResultOr<size_t> TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags)
|
KResultOr<size_t> TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags)
|
||||||
|
|
|
@ -274,10 +274,10 @@ bool PerformanceEventBuffer::to_json(KBufferBuilder& builder) const
|
||||||
|
|
||||||
OwnPtr<PerformanceEventBuffer> PerformanceEventBuffer::try_create_with_size(size_t buffer_size)
|
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);
|
auto buffer_or_error = KBuffer::try_create_with_size(buffer_size, Memory::Region::Access::ReadWrite, "Performance events", AllocationStrategy::AllocateNow);
|
||||||
if (!buffer)
|
if (buffer_or_error.is_error())
|
||||||
return {};
|
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)
|
void PerformanceEventBuffer::add_process(const Process& process, ProcessEventType event_type)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue