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

Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr

Apologies for the enormous commit, but I don't see a way to split this
up nicely. In the vast majority of cases it's a simple change. A few
extra places can use TRY instead of manual error checking though. :^)
This commit is contained in:
Sam Atkins 2022-01-20 17:47:39 +00:00 committed by Andreas Kling
parent 140f1d9e55
commit 45cf40653a
79 changed files with 202 additions and 274 deletions

View file

@ -140,10 +140,7 @@ ErrorOr<void> BlockBasedFileSystem::write_block(BlockIndex index, const UserOrKe
// NOTE: We copy the `data` to write into a local buffer before taking the cache lock.
// This makes sure any page faults caused by accessing the data will occur before
// we tie down the cache.
auto buffered_data_or_error = ByteBuffer::create_uninitialized(count);
if (!buffered_data_or_error.has_value())
return ENOMEM;
auto buffered_data = buffered_data_or_error.release_value();
auto buffered_data = TRY(ByteBuffer::create_uninitialized(count));
TRY(data.read(buffered_data.bytes()));

View file

@ -201,10 +201,7 @@ ErrorOr<void> Ext2FSInode::write_indirect_block(BlockBasedFileSystem::BlockIndex
const auto entries_per_block = EXT2_ADDR_PER_BLOCK(&fs().super_block());
VERIFY(blocks_indices.size() <= entries_per_block);
auto block_contents_result = ByteBuffer::create_uninitialized(fs().block_size());
if (!block_contents_result.has_value())
return ENOMEM;
auto block_contents = block_contents_result.release_value();
auto block_contents = TRY(ByteBuffer::create_uninitialized(fs().block_size()));
OutputMemoryStream stream { block_contents };
auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
@ -226,10 +223,7 @@ ErrorOr<void> Ext2FSInode::grow_doubly_indirect_block(BlockBasedFileSystem::Bloc
VERIFY(blocks_indices.size() > old_blocks_length);
VERIFY(blocks_indices.size() <= entries_per_doubly_indirect_block);
auto block_contents_result = ByteBuffer::create_uninitialized(fs().block_size());
if (!block_contents_result.has_value())
return ENOMEM;
auto block_contents = block_contents_result.release_value();
auto block_contents = TRY(ByteBuffer::create_uninitialized(fs().block_size()));
auto* block_as_pointers = (unsigned*)block_contents.data();
OutputMemoryStream stream { block_contents };
auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
@ -269,10 +263,7 @@ ErrorOr<void> Ext2FSInode::shrink_doubly_indirect_block(BlockBasedFileSystem::Bl
VERIFY(old_blocks_length >= new_blocks_length);
VERIFY(new_blocks_length <= entries_per_doubly_indirect_block);
auto block_contents_result = ByteBuffer::create_uninitialized(fs().block_size());
if (!block_contents_result.has_value())
return ENOMEM;
auto block_contents = block_contents_result.release_value();
auto block_contents = TRY(ByteBuffer::create_uninitialized(fs().block_size()));
auto* block_as_pointers = (unsigned*)block_contents.data();
auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(block_as_pointers));
TRY(fs().read_block(block, &buffer, fs().block_size()));
@ -305,10 +296,7 @@ ErrorOr<void> Ext2FSInode::grow_triply_indirect_block(BlockBasedFileSystem::Bloc
VERIFY(blocks_indices.size() > old_blocks_length);
VERIFY(blocks_indices.size() <= entries_per_triply_indirect_block);
auto block_contents_result = ByteBuffer::create_uninitialized(fs().block_size());
if (!block_contents_result.has_value())
return ENOMEM;
auto block_contents = block_contents_result.release_value();
auto block_contents = TRY(ByteBuffer::create_uninitialized(fs().block_size()));
auto* block_as_pointers = (unsigned*)block_contents.data();
OutputMemoryStream stream { block_contents };
auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
@ -351,10 +339,7 @@ ErrorOr<void> Ext2FSInode::shrink_triply_indirect_block(BlockBasedFileSystem::Bl
VERIFY(old_blocks_length >= new_blocks_length);
VERIFY(new_blocks_length <= entries_per_triply_indirect_block);
auto block_contents_result = ByteBuffer::create_uninitialized(fs().block_size());
if (!block_contents_result.has_value())
return ENOMEM;
auto block_contents = block_contents_result.release_value();
auto block_contents = TRY(ByteBuffer::create_uninitialized(fs().block_size()));
auto* block_as_pointers = (unsigned*)block_contents.data();
auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(block_as_pointers));
TRY(fs().read_block(block, &buffer, fs().block_size()));
@ -587,10 +572,7 @@ ErrorOr<Vector<Ext2FS::BlockIndex>> Ext2FSInode::compute_block_list_impl_interna
if (!count)
return {};
size_t read_size = count * sizeof(u32);
auto maybe_array_storage = ByteBuffer::create_uninitialized(read_size);
if (!maybe_array_storage.has_value())
return ENOMEM;
auto array_storage = maybe_array_storage.release_value();
auto array_storage = TRY(ByteBuffer::create_uninitialized(read_size));
auto* array = (u32*)array_storage.data();
auto buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)array);
TRY(fs().read_block(array_block_index, &buffer, read_size, 0));
@ -1111,10 +1093,7 @@ ErrorOr<void> Ext2FSInode::write_directory(Vector<Ext2FSDirectoryEntry>& entries
dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]::write_directory(): New directory contents to write (size {}):", identifier(), directory_size);
auto directory_data_result = ByteBuffer::create_uninitialized(directory_size);
if (!directory_data_result.has_value())
return ENOMEM;
auto directory_data = directory_data_result.release_value();
auto directory_data = TRY(ByteBuffer::create_uninitialized(directory_size));
OutputMemoryStream stream { directory_data };
for (auto& entry : entries) {

View file

@ -281,10 +281,7 @@ ErrorOr<ByteBuffer> BochsGraphicsAdapter::get_edid(size_t output_port_index) con
if (output_port_index != 0)
return Error::from_errno(ENODEV);
auto bytes = ByteBuffer::copy(const_cast<u8 const*>(m_registers->edid_data), sizeof(m_registers->edid_data));
if (!bytes.has_value())
return Error::from_errno(ENOMEM);
return bytes.release_value();
return ByteBuffer::copy(const_cast<u8 const*>(m_registers->edid_data), sizeof(m_registers->edid_data));
}
}

View file

@ -661,12 +661,8 @@ ErrorOr<ByteBuffer> IntelNativeGraphicsAdapter::get_edid(size_t output_port_inde
return Error::from_errno(ENODEV);
}
if (m_crt_edid.has_value()) {
auto bytes = ByteBuffer::copy(m_crt_edid_bytes, sizeof(m_crt_edid_bytes));
if (!bytes.has_value())
return Error::from_errno(ENOMEM);
return bytes.release_value();
}
if (m_crt_edid.has_value())
return ByteBuffer::copy(m_crt_edid_bytes, sizeof(m_crt_edid_bytes));
return ByteBuffer {};
}

View file

@ -214,12 +214,8 @@ ErrorOr<ByteBuffer> GraphicsAdapter::get_edid(size_t output_port_index) const
if (output_port_index >= VIRTIO_GPU_MAX_SCANOUTS)
return Error::from_errno(ENODEV);
auto& edid = m_scanouts[output_port_index].edid;
if (edid.has_value()) {
auto bytes = ByteBuffer::copy(edid.value().bytes());
if (!bytes.has_value())
return Error::from_errno(ENOMEM);
return bytes.release_value();
}
if (edid.has_value())
return ByteBuffer::copy(edid.value().bytes());
return ByteBuffer {};
}
@ -243,11 +239,8 @@ auto GraphicsAdapter::query_edid(u32 scanout_id) -> ErrorOr<Optional<EDID::Parse
if (response.size == 0)
return Error::from_string_literal("VirtIO::GraphicsAdapter: Failed to get EDID, empty buffer");
auto edid_buffer = ByteBuffer::copy(response.edid, response.size);
if (!edid_buffer.has_value())
return Error::from_errno(ENOMEM);
auto edid = TRY(EDID::Parser::from_bytes(edid_buffer.release_value()));
auto edid_buffer = TRY(ByteBuffer::copy(response.edid, response.size));
auto edid = TRY(EDID::Parser::from_bytes(move(edid_buffer)));
return edid;
}

View file

@ -421,11 +421,11 @@ void NE2000NetworkAdapter::receive()
u8 drop_buffer[NE2K_PAGE_SIZE];
Bytes buffer { drop_buffer, array_size(drop_buffer) };
bool will_drop { false };
if (!packet_result.has_value()) {
if (packet_result.is_error()) {
dbgln("NE2000NetworkAdapter: Not enough memory for packet with length = {}, dropping.", header.length);
will_drop = true;
} else {
buffer = packet_result->bytes();
buffer = packet_result.value().bytes();
}
int current_offset = 0;

View file

@ -34,11 +34,11 @@ void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet
{
size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
auto buffer_result = NetworkByteBuffer::create_zeroed(size_in_bytes);
if (!buffer_result.has_value()) {
if (buffer_result.is_error()) {
dbgln("Dropping ARP packet targeted at {} as there is not enough memory to buffer it", packet.target_hardware_address().to_string());
return;
}
auto* eth = (EthernetFrameHeader*)buffer_result->data();
auto* eth = (EthernetFrameHeader*)buffer_result.value().data();
eth->set_source(mac_address());
eth->set_destination(destination);
eth->set_ether_type(EtherType::ARP);

View file

@ -29,9 +29,9 @@ public:
using HashType = HashT;
using DigestType = typename HashT::DigestType;
// FIXME: Do something other than VERIFY()'ing inside Optional in case of OOM.
// FIXME: Do something other than VERIFY()'ing in case of OOM.
FortunaPRNG()
: m_counter(ByteBuffer::create_zeroed(BlockType::block_size()).release_value())
: m_counter(ByteBuffer::create_zeroed(BlockType::block_size()).release_value_but_fixme_should_propagate_errors())
{
}
@ -101,7 +101,7 @@ private:
} else {
auto buffer_result = ByteBuffer::copy(digest.immutable_data(), digest.data_length());
// If there's no memory left to copy this into, bail out.
if (!buffer_result.has_value())
if (buffer_result.is_error())
return;
m_key = buffer_result.release_value();

View file

@ -381,8 +381,8 @@ UNMAP_AFTER_INIT void IDEChannel::detect_disks()
}
// FIXME: Handle possible OOM situation here.
ByteBuffer wbuf = ByteBuffer::create_uninitialized(512).release_value();
ByteBuffer bbuf = ByteBuffer::create_uninitialized(512).release_value();
ByteBuffer wbuf = ByteBuffer::create_uninitialized(512).release_value_but_fixme_should_propagate_errors();
ByteBuffer bbuf = ByteBuffer::create_uninitialized(512).release_value_but_fixme_should_propagate_errors();
u8* b = bbuf.data();
u16* w = (u16*)wbuf.data();

View file

@ -151,7 +151,7 @@ UNMAP_AFTER_INIT ErrorOr<void> NVMeController::identify_and_init_namespaces()
RefPtr<Memory::PhysicalPage> prp_dma_buffer;
OwnPtr<Memory::Region> prp_dma_region;
auto namespace_data_struct = ByteBuffer::create_zeroed(NVMe_IDENTIFY_SIZE).release_value();
auto namespace_data_struct = TRY(ByteBuffer::create_zeroed(NVMe_IDENTIFY_SIZE));
u32 active_namespace_list[NVMe_IDENTIFY_SIZE / sizeof(u32)];
{

View file

@ -59,7 +59,7 @@ GUIDPartitionTable::GUIDPartitionTable(const StorageDevice& device)
: MBRPartitionTable(device)
{
// FIXME: Handle OOM failure here.
m_cached_header = ByteBuffer::create_zeroed(m_device->block_size()).release_value();
m_cached_header = ByteBuffer::create_zeroed(m_device->block_size()).release_value_but_fixme_should_propagate_errors();
VERIFY(partitions_count() == 0);
if (!initialize())
m_valid = false;
@ -89,7 +89,7 @@ bool GUIDPartitionTable::initialize()
}
auto entries_buffer_result = ByteBuffer::create_zeroed(m_device->block_size());
if (!entries_buffer_result.has_value()) {
if (entries_buffer_result.is_error()) {
dbgln("GUIPartitionTable: not enough memory for entries buffer");
return false;
}

View file

@ -47,7 +47,7 @@ bool MBRPartitionTable::read_boot_record()
MBRPartitionTable::MBRPartitionTable(const StorageDevice& device, u32 start_lba)
: PartitionTable(device)
, m_start_lba(start_lba)
, m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value()) // FIXME: Do something sensible if this fails because of OOM.
, m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value_but_fixme_should_propagate_errors()) // FIXME: Do something sensible if this fails because of OOM.
{
if (!read_boot_record() || !initialize())
return;
@ -68,7 +68,7 @@ MBRPartitionTable::MBRPartitionTable(const StorageDevice& device, u32 start_lba)
MBRPartitionTable::MBRPartitionTable(const StorageDevice& device)
: PartitionTable(device)
, m_start_lba(0)
, m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value()) // FIXME: Do something sensible if this fails because of OOM.
, m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value_but_fixme_should_propagate_errors()) // FIXME: Do something sensible if this fails because of OOM.
{
if (!read_boot_record() || contains_ebr() || is_protective_mbr() || !initialize())
return;

View file

@ -62,10 +62,7 @@ ErrorOr<size_t> StorageDevice::read(OpenFileDescription&, u64 offset, UserOrKern
off_t pos = whole_blocks * block_size();
if (remaining > 0) {
auto data_result = ByteBuffer::create_uninitialized(block_size());
if (!data_result.has_value())
return ENOMEM;
auto data = data_result.release_value();
auto data = TRY(ByteBuffer::create_uninitialized(block_size()));
auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
auto read_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size()));
auto result = read_request->wait();
@ -133,7 +130,7 @@ ErrorOr<size_t> StorageDevice::write(OpenFileDescription&, u64 offset, const Use
// then write the whole block back to the disk.
if (remaining > 0) {
// FIXME: Do something sensible with this OOM scenario.
auto data = ByteBuffer::create_zeroed(block_size()).release_value();
auto data = ByteBuffer::create_zeroed(block_size()).release_value_but_fixme_should_propagate_errors();
auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
{