Skip to content

Commit

Permalink
Kernel: Make KBuffer::try_create_with_bytes() return KResultOr
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Sep 7, 2021
1 parent 899cee8 commit 250b52d
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 24 deletions.
6 changes: 2 additions & 4 deletions Kernel/ACPI/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 });
Expand Down
2 changes: 1 addition & 1 deletion Kernel/ACPI/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ACPISysFSComponent : public SysFSComponent {
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;
Expand Down
8 changes: 3 additions & 5 deletions Kernel/Arch/PC/BIOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 });
Expand All @@ -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 });
Expand Down
6 changes: 3 additions & 3 deletions Kernel/Arch/PC/BIOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BIOSSysFSComponent : public SysFSComponent {
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);
};

Expand All @@ -73,7 +73,7 @@ class DMIEntryPointExposedBlob : public BIOSSysFSComponent {

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;
};
Expand All @@ -84,7 +84,7 @@ class SMBIOSExposedTable : public BIOSSysFSComponent {

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;
Expand Down
6 changes: 2 additions & 4 deletions Kernel/Bus/PCI/SysFSPCI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Bus/PCI/SysFSPCI.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class PCIDeviceAttributeSysFSComponent : public SysFSComponent {
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;
Expand Down
6 changes: 3 additions & 3 deletions Kernel/KBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ class [[nodiscard]] KBuffer {
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")
Expand Down
4 changes: 1 addition & 3 deletions Kernel/Syscalls/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 250b52d

Please sign in to comment.