summaryrefslogtreecommitdiff
path: root/Kernel/KString.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-09-06 19:24:54 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-06 19:25:36 +0200
commit56a2594de7fdcd1b0e8c2a20832ba4a14fa20501 (patch)
treec5da30fe55661c18aad37208fc073d5180793f8f /Kernel/KString.cpp
parent69b9b2888cea47393461799746e80a04bdcae4d1 (diff)
downloadserenity-56a2594de7fdcd1b0e8c2a20832ba4a14fa20501.zip
Kernel: Make KString factories return KResultOr + use TRY() everywhere
There are a number of places that don't have an error propagation path right now, so I've added FIXME's about that.
Diffstat (limited to 'Kernel/KString.cpp')
-rw-r--r--Kernel/KString.cpp26
1 files changed, 10 insertions, 16 deletions
diff --git a/Kernel/KString.cpp b/Kernel/KString.cpp
index da5c3bf76c..8a6582c7cd 100644
--- a/Kernel/KString.cpp
+++ b/Kernel/KString.cpp
@@ -10,49 +10,43 @@ extern bool g_in_early_boot;
namespace Kernel {
-OwnPtr<KString> KString::try_create(StringView const& string)
+KResultOr<NonnullOwnPtr<KString>> KString::try_create(StringView string)
{
char* characters = nullptr;
size_t length = string.length();
- auto new_string = KString::try_create_uninitialized(length, characters);
- if (!new_string)
- return {};
+ auto new_string = TRY(KString::try_create_uninitialized(length, characters));
if (!string.is_empty())
__builtin_memcpy(characters, string.characters_without_null_termination(), length);
characters[length] = '\0';
return new_string;
}
-NonnullOwnPtr<KString> KString::must_create(StringView const& string)
+NonnullOwnPtr<KString> KString::must_create(StringView string)
{
// We can only enforce success during early boot.
VERIFY(g_in_early_boot);
- auto kstring = KString::try_create(string);
- VERIFY(kstring != nullptr);
- return kstring.release_nonnull();
+ return KString::try_create(string).release_value();
}
-OwnPtr<KString> KString::try_create_uninitialized(size_t length, char*& characters)
+KResultOr<NonnullOwnPtr<KString>> KString::try_create_uninitialized(size_t length, char*& characters)
{
size_t allocation_size = sizeof(KString) + (sizeof(char) * length) + sizeof(char);
auto* slot = kmalloc(allocation_size);
if (!slot)
- return {};
- auto* new_string = new (slot) KString(length);
+ return ENOMEM;
+ auto new_string = TRY(adopt_nonnull_own_or_enomem(new (slot) KString(length)));
characters = new_string->m_characters;
- return adopt_own_if_nonnull(new_string);
+ return new_string;
}
NonnullOwnPtr<KString> KString::must_create_uninitialized(size_t length, char*& characters)
{
// We can only enforce success during early boot.
VERIFY(g_in_early_boot);
- auto kstring = KString::try_create_uninitialized(length, characters);
- VERIFY(kstring != nullptr);
- return kstring.release_nonnull();
+ return KString::try_create_uninitialized(length, characters).release_value();
}
-OwnPtr<KString> KString::try_clone() const
+KResultOr<NonnullOwnPtr<KString>> KString::try_clone() const
{
return try_create(view());
}