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

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
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 KResultOr<size_t> ACPISysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{ {
auto blob = try_to_generate_buffer(); auto blob = TRY(try_to_generate_buffer());
if (!blob)
return KResult(EFAULT);
if ((size_t)offset >= blob->size()) if ((size_t)offset >= blob->size())
return KSuccess; return KSuccess;
@ -45,7 +43,7 @@ KResultOr<size_t> ACPISysFSComponent::read_bytes(off_t offset, size_t count, Use
return nread; 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); auto acpi_blob = Memory::map_typed<u8>((m_paddr), m_length);
return KBuffer::try_create_with_bytes(Span<u8> { acpi_blob.ptr(), 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; virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override;
protected: protected:
OwnPtr<KBuffer> try_to_generate_buffer() const; KResultOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const;
ACPISysFSComponent(String name, PhysicalAddress, size_t table_size); ACPISysFSComponent(String name, PhysicalAddress, size_t table_size);
PhysicalAddress m_paddr; 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 KResultOr<size_t> BIOSSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{ {
auto blob = try_to_generate_buffer(); auto blob = TRY(try_to_generate_buffer());
if (!blob)
return KResult(EFAULT);
if ((size_t)offset >= blob->size()) if ((size_t)offset >= blob->size())
return KSuccess; 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); 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 }); 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); 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 }); 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; virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override;
protected: protected:
virtual OwnPtr<KBuffer> try_to_generate_buffer() const = 0; virtual KResultOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const = 0;
explicit BIOSSysFSComponent(String name); explicit BIOSSysFSComponent(String name);
}; };
@ -73,7 +73,7 @@ public:
private: private:
DMIEntryPointExposedBlob(PhysicalAddress dmi_entry_point, size_t blob_size); 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; PhysicalAddress m_dmi_entry_point;
size_t m_dmi_entry_point_length; size_t m_dmi_entry_point_length;
}; };
@ -84,7 +84,7 @@ public:
private: private:
SMBIOSExposedTable(PhysicalAddress dmi_entry_point, size_t blob_size); 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; PhysicalAddress m_smbios_structure_table;
size_t m_smbios_structure_table_length; 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 KResultOr<size_t> PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{ {
auto blob = try_to_generate_buffer(); auto blob = TRY(try_to_generate_buffer());
if (!blob)
return KResult(EFAULT);
if ((size_t)offset >= blob->size()) if ((size_t)offset >= blob->size())
return KSuccess; return KSuccess;
@ -74,7 +72,7 @@ KResultOr<size_t> PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, siz
return nread; return nread;
} }
OwnPtr<KBuffer> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const KResultOr<NonnullOwnPtr<KBuffer>> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const
{ {
String value; String value;
switch (m_field_bytes_width) { switch (m_field_bytes_width) {

View file

@ -41,7 +41,7 @@ public:
virtual ~PCIDeviceAttributeSysFSComponent() {}; virtual ~PCIDeviceAttributeSysFSComponent() {};
protected: 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); PCIDeviceAttributeSysFSComponent(String name, const PCIDeviceSysFSDirectory& device, size_t offset, size_t field_bytes_width);
NonnullRefPtr<PCIDeviceSysFSDirectory> m_device; NonnullRefPtr<PCIDeviceSysFSDirectory> m_device;
size_t m_offset; size_t m_offset;

View file

@ -114,12 +114,12 @@ public:
return adopt_nonnull_own_or_enomem(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) 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); auto impl = KBufferImpl::try_create_with_bytes(bytes, 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 KBuffer copy(const void* data, size_t size, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer") [[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 description = TRY(VirtualFileSystem::the().open(path->view(), O_RDONLY, 0, current_directory()));
auto payload = TRY(description->read_entire_file()); auto payload = TRY(description->read_entire_file());
auto storage = KBuffer::try_create_with_bytes(ReadonlyBytes { payload->data(), payload->size() }); auto storage = TRY(KBuffer::try_create_with_bytes(ReadonlyBytes { payload->data(), payload->size() }));
if (!storage)
return ENOMEM;
auto elf_image = try_make<ELF::Image>(storage->data(), storage->size()); auto elf_image = try_make<ELF::Image>(storage->data(), storage->size());
if (!elf_image) if (!elf_image)