Skip to content

Commit

Permalink
Kernel: Replace KResult and KResultOr<T> with Error and ErrorOr<T>
Browse files Browse the repository at this point in the history
We now use AK::Error and AK::ErrorOr<T> in both kernel and userspace!
This was a slightly tedious refactoring that took a long time, so it's
not unlikely that some bugs crept in.

Nevertheless, it does pass basic functionality testing, and it's just
real nice to finally see the same pattern in all contexts. :^)
  • Loading branch information
awesomekling committed Nov 8, 2021
1 parent 7ee10c6 commit 79fa976
Show file tree
Hide file tree
Showing 262 changed files with 2,422 additions and 2,607 deletions.
17 changes: 1 addition & 16 deletions AK/OwnPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@

#pragma once

#include <AK/Error.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/RefCounted.h>
#ifdef KERNEL
# include <Kernel/API/KResult.h>
#else
# include <AK/Error.h>
#endif

#define OWNPTR_SCRUB_BYTE 0xf0

Expand Down Expand Up @@ -209,16 +205,6 @@ inline OwnPtr<T> adopt_own_if_nonnull(T* object)
return {};
}

#ifdef KERNEL
template<typename T>
inline Kernel::KResultOr<NonnullOwnPtr<T>> adopt_nonnull_own_or_enomem(T* object)
{
auto result = adopt_own_if_nonnull(object);
if (!result)
return ENOMEM;
return result.release_nonnull();
}
#else
template<typename T>
inline ErrorOr<NonnullOwnPtr<T>> adopt_nonnull_own_or_enomem(T* object)
{
Expand All @@ -227,7 +213,6 @@ inline ErrorOr<NonnullOwnPtr<T>> adopt_nonnull_own_or_enomem(T* object)
return ENOMEM;
return result.release_nonnull();
}
#endif

template<typename T, class... Args>
requires(IsConstructible<T, Args...>) inline OwnPtr<T> try_make(Args&&... args)
Expand Down
2 changes: 1 addition & 1 deletion AK/Try.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#pragma once

// NOTE: This macro works with any result type that has the expected APIs.
// It's designed with AK::Result and Kernel::KResult in mind.
// It's designed with AK::Result and AK::Error in mind.

#define TRY(expression) \
({ \
Expand Down
199 changes: 0 additions & 199 deletions Kernel/API/KResult.h

This file was deleted.

2 changes: 1 addition & 1 deletion Kernel/Arch/aarch64/dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Kernel {
void dump_backtrace(PrintToScreen) { }

// KString.cpp
KResultOr<NonnullOwnPtr<KString>> KString::try_create_uninitialized(size_t, char*&) { return ENOMEM; }
ErrorOr<NonnullOwnPtr<KString>> KString::try_create_uninitialized(size_t, char*&) { return ENOMEM; }
void KString::operator delete(void*) { }

// SafeMem.h
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Bus/PCI/Access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

#include <AK/ByteReader.h>
#include <AK/Error.h>
#include <AK/HashTable.h>
#include <Kernel/API/KResult.h>
#include <Kernel/Arch/x86/IO.h>
#include <Kernel/Bus/PCI/Access.h>
#include <Kernel/Debug.h>
Expand Down
6 changes: 3 additions & 3 deletions Kernel/Bus/PCI/SysFSPCI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ PCIDeviceAttributeSysFSComponent::PCIDeviceAttributeSysFSComponent(String name,
{
}

KResultOr<size_t> PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
ErrorOr<size_t> PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{
auto blob = TRY(try_to_generate_buffer());

if ((size_t)offset >= blob->size())
return KSuccess;
return 0;

ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count));
TRY(buffer.write(blob->data() + offset, nread));
return nread;
}

KResultOr<NonnullOwnPtr<KBuffer>> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const
ErrorOr<NonnullOwnPtr<KBuffer>> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const
{
String value;
switch (m_field_bytes_width) {
Expand Down
4 changes: 2 additions & 2 deletions Kernel/Bus/PCI/SysFSPCI.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class PCIDeviceAttributeSysFSComponent : public SysFSComponent {
public:
static NonnullRefPtr<PCIDeviceAttributeSysFSComponent> create(String name, const PCIDeviceSysFSDirectory& device, PCI::RegisterOffset offset, size_t field_bytes_width);

virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override;
virtual ErrorOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override;
virtual ~PCIDeviceAttributeSysFSComponent() {};

protected:
KResultOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const;
ErrorOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const;
PCIDeviceAttributeSysFSComponent(String name, const PCIDeviceSysFSDirectory& device, PCI::RegisterOffset offset, size_t field_bytes_width);
NonnullRefPtr<PCIDeviceSysFSDirectory> m_device;
PCI::RegisterOffset m_offset;
Expand Down
18 changes: 9 additions & 9 deletions Kernel/Bus/USB/SysFSUSB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SysFSUSBDeviceInformation::~SysFSUSBDeviceInformation()
{
}

KResult SysFSUSBDeviceInformation::try_generate(KBufferBuilder& builder)
ErrorOr<void> SysFSUSBDeviceInformation::try_generate(KBufferBuilder& builder)
{
VERIFY(m_lock.is_locked());
JsonArraySerializer array { builder };
Expand All @@ -44,10 +44,10 @@ KResult SysFSUSBDeviceInformation::try_generate(KBufferBuilder& builder)
obj.add("num_configurations", m_device->device_descriptor().num_configurations);
obj.finish();
array.finish();
return KSuccess;
return {};
}

KResult SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description) const
ErrorOr<void> SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description) const
{
MutexLocker lock(m_lock);
auto& cached_data = description.data();
Expand All @@ -60,24 +60,24 @@ KResult SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description
typed_cached_data.buffer = builder.build();
if (!typed_cached_data.buffer)
return ENOMEM;
return KSuccess;
return {};
}

KResultOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
ErrorOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
{
dbgln_if(PROCFS_DEBUG, "SysFSUSBDeviceInformation @ {}: read_bytes offset: {} count: {}", name(), offset, count);

VERIFY(offset >= 0);
VERIFY(buffer.user_or_kernel_ptr());

if (!description)
return KResult(EIO);
return Error::from_errno(EIO);

MutexLocker locker(m_lock);

if (!description->data()) {
dbgln("SysFSUSBDeviceInformation: Do not have cached data!");
return KResult(EIO);
return Error::from_errno(EIO);
}

auto& typed_cached_data = static_cast<SysFSInodeData&>(*description->data());
Expand All @@ -91,7 +91,7 @@ KResultOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t cou
return nread;
}

KResult SysFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
ErrorOr<void> SysFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
{
SpinlockLocker lock(m_lock);
// Note: if the parent directory is null, it means something bad happened as this should not happen for the USB directory.
Expand All @@ -103,7 +103,7 @@ KResult SysFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function<bool
InodeIdentifier identifier = { fsid, device_node.component_index() };
callback({ device_node.name(), identifier, 0 });
}
return KSuccess;
return {};
}

RefPtr<SysFSComponent> SysFSUSBBusDirectory::lookup(StringView name)
Expand Down
Loading

0 comments on commit 79fa976

Please sign in to comment.