Kernel: Make KBuffer::try_create_with_bytes() return KResultOr

This commit is contained in:
Andreas Kling 2021-09-07 15:22:24 +02:00
parent 899cee8185
commit 250b52d6e5
Notes: sideshowbarker 2024-07-18 04:31:21 +09:00
8 changed files with 16 additions and 24 deletions

View file

@ -33,9 +33,7 @@ UNMAP_AFTER_INIT NonnullRefPtr<ACPISysFSComponent> ACPISysFSComponent::create(St
KResultOr<size_t> ACPISysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{
auto blob = try_to_generate_buffer();
if (!blob)
return KResult(EFAULT);
auto blob = TRY(try_to_generate_buffer());
if ((size_t)offset >= blob->size())
return KSuccess;
@ -45,7 +43,7 @@ KResultOr<size_t> ACPISysFSComponent::read_bytes(off_t offset, size_t count, Use
return nread;
}
OwnPtr<KBuffer> ACPISysFSComponent::try_to_generate_buffer() const
KResultOr<NonnullOwnPtr<KBuffer>> ACPISysFSComponent::try_to_generate_buffer() const
{
auto acpi_blob = Memory::map_typed<u8>((m_paddr), m_length);
return KBuffer::try_create_with_bytes(Span<u8> { acpi_blob.ptr(), m_length });

View file

@ -31,7 +31,7 @@ public:
virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override;
protected:
OwnPtr<KBuffer> try_to_generate_buffer() const;
KResultOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const;
ACPISysFSComponent(String name, PhysicalAddress, size_t table_size);
PhysicalAddress m_paddr;

View file

@ -31,9 +31,7 @@ UNMAP_AFTER_INIT BIOSSysFSComponent::BIOSSysFSComponent(String name)
KResultOr<size_t> BIOSSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{
auto blob = try_to_generate_buffer();
if (!blob)
return KResult(EFAULT);
auto blob = TRY(try_to_generate_buffer());
if ((size_t)offset >= blob->size())
return KSuccess;
@ -50,7 +48,7 @@ UNMAP_AFTER_INIT DMIEntryPointExposedBlob::DMIEntryPointExposedBlob(PhysicalAddr
{
}
OwnPtr<KBuffer> DMIEntryPointExposedBlob::try_to_generate_buffer() const
KResultOr<NonnullOwnPtr<KBuffer>> DMIEntryPointExposedBlob::try_to_generate_buffer() const
{
auto dmi_blob = Memory::map_typed<u8>((m_dmi_entry_point), m_dmi_entry_point_length);
return KBuffer::try_create_with_bytes(Span<u8> { dmi_blob.ptr(), m_dmi_entry_point_length });
@ -68,7 +66,7 @@ UNMAP_AFTER_INIT SMBIOSExposedTable::SMBIOSExposedTable(PhysicalAddress smbios_s
{
}
OwnPtr<KBuffer> SMBIOSExposedTable::try_to_generate_buffer() const
KResultOr<NonnullOwnPtr<KBuffer>> SMBIOSExposedTable::try_to_generate_buffer() const
{
auto dmi_blob = Memory::map_typed<u8>((m_smbios_structure_table), m_smbios_structure_table_length);
return KBuffer::try_create_with_bytes(Span<u8> { dmi_blob.ptr(), m_smbios_structure_table_length });

View file

@ -63,7 +63,7 @@ public:
virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override;
protected:
virtual OwnPtr<KBuffer> try_to_generate_buffer() const = 0;
virtual KResultOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const = 0;
explicit BIOSSysFSComponent(String name);
};
@ -73,7 +73,7 @@ public:
private:
DMIEntryPointExposedBlob(PhysicalAddress dmi_entry_point, size_t blob_size);
virtual OwnPtr<KBuffer> try_to_generate_buffer() const override;
virtual KResultOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const override;
PhysicalAddress m_dmi_entry_point;
size_t m_dmi_entry_point_length;
};
@ -84,7 +84,7 @@ public:
private:
SMBIOSExposedTable(PhysicalAddress dmi_entry_point, size_t blob_size);
virtual OwnPtr<KBuffer> try_to_generate_buffer() const override;
virtual KResultOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const override;
PhysicalAddress m_smbios_structure_table;
size_t m_smbios_structure_table_length;

View file

@ -62,9 +62,7 @@ PCIDeviceAttributeSysFSComponent::PCIDeviceAttributeSysFSComponent(String name,
KResultOr<size_t> PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{
auto blob = try_to_generate_buffer();
if (!blob)
return KResult(EFAULT);
auto blob = TRY(try_to_generate_buffer());
if ((size_t)offset >= blob->size())
return KSuccess;
@ -74,7 +72,7 @@ KResultOr<size_t> PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, siz
return nread;
}
OwnPtr<KBuffer> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const
KResultOr<NonnullOwnPtr<KBuffer>> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const
{
String value;
switch (m_field_bytes_width) {

View file

@ -41,7 +41,7 @@ public:
virtual ~PCIDeviceAttributeSysFSComponent() {};
protected:
virtual OwnPtr<KBuffer> try_to_generate_buffer() const;
KResultOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const;
PCIDeviceAttributeSysFSComponent(String name, const PCIDeviceSysFSDirectory& device, size_t offset, size_t field_bytes_width);
NonnullRefPtr<PCIDeviceSysFSDirectory> m_device;
size_t m_offset;

View file

@ -114,12 +114,12 @@ public:
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)
static KResultOr<NonnullOwnPtr<KBuffer>> try_create_with_bytes(ReadonlyBytes bytes, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)
{
auto impl = KBufferImpl::try_create_with_bytes(bytes, 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 KBuffer copy(const void* data, size_t size, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer")

View file

@ -27,9 +27,7 @@ KResultOr<FlatPtr> Process::sys$module_load(Userspace<const char*> user_path, si
auto description = TRY(VirtualFileSystem::the().open(path->view(), O_RDONLY, 0, current_directory()));
auto payload = TRY(description->read_entire_file());
auto storage = KBuffer::try_create_with_bytes(ReadonlyBytes { payload->data(), payload->size() });
if (!storage)
return ENOMEM;
auto storage = TRY(KBuffer::try_create_with_bytes(ReadonlyBytes { payload->data(), payload->size() }));
auto elf_image = try_make<ELF::Image>(storage->data(), storage->size());
if (!elf_image)