diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 5d3acff198..3696082e14 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -336,16 +336,16 @@ inline RefPtr adopt_ref_if_nonnull(T* object) } template -requires(IsConstructible) inline RefPtr try_make_ref_counted(Args&&... args) +requires(IsConstructible) inline ErrorOr> try_make_ref_counted(Args&&... args) { - return adopt_ref_if_nonnull(new (nothrow) T(forward(args)...)); + return adopt_nonnull_ref_or_enomem(new (nothrow) T(forward(args)...)); } // FIXME: Remove once P0960R3 is available in Clang. template -inline RefPtr try_make_ref_counted(Args&&... args) +inline ErrorOr> try_make_ref_counted(Args&&... args) { - return adopt_ref_if_nonnull(new (nothrow) T { forward(args)... }); + return adopt_nonnull_ref_or_enomem(new (nothrow) T { forward(args)... }); } template diff --git a/Documentation/SmartPointers.md b/Documentation/SmartPointers.md index 23253980e2..9d63c2f335 100644 --- a/Documentation/SmartPointers.md +++ b/Documentation/SmartPointers.md @@ -98,13 +98,14 @@ NonnullRefPtr another_owner = our_object; ``` -The `try_make_ref_counted()` function constructs an object wrapped in `RefPtr` which may be null if the allocation does not succeed. This allows the calling code to handle allocation failure as it wishes. All arguments passed to it are forwarded to `T`'s constructor. +The `try_make_ref_counted()` function constructs an object wrapped in `ErrorOr>` which may be an error if the allocation does not succeed. This allows the calling code to handle allocation failure as it wishes. All arguments passed to it are forwarded to `T`'s constructor. ```cpp -RefPtr our_object = try_make_ref_counted(); -if (!our_object) { +auto our_object_or_error = try_make_ref_counted(); +if (our_object_or_error.is_error()) { // handle allocation failure... } +NonnullRefPtr our_object = our_object_or_error.release_value(); RefPtr another_owner = our_object; ``` diff --git a/Kernel/Library/ThreadSafeRefPtr.h b/Kernel/Library/ThreadSafeRefPtr.h index 14cda73d65..b8bda74833 100644 --- a/Kernel/Library/ThreadSafeRefPtr.h +++ b/Kernel/Library/ThreadSafeRefPtr.h @@ -486,16 +486,16 @@ inline RefPtr adopt_ref_if_nonnull(T* object) } template -requires(IsConstructible) inline RefPtr try_make_ref_counted(Args&&... args) +requires(IsConstructible) inline ErrorOr> try_make_ref_counted(Args&&... args) { - return adopt_ref_if_nonnull(new (nothrow) T(forward(args)...)); + return adopt_nonnull_ref_or_enomem(new (nothrow) T(forward(args)...)); } // FIXME: Remove once P0960R3 is available in Clang. template -inline RefPtr try_make_ref_counted(Args&&... args) +inline ErrorOr> try_make_ref_counted(Args&&... args) { - return adopt_ref_if_nonnull(new (nothrow) T { forward(args)... }); + return adopt_nonnull_ref_or_enomem(new (nothrow) T { forward(args)... }); } template diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index c5a945329d..f0551c28e6 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -45,12 +45,10 @@ ErrorOr> Thread::try_create(NonnullRefPtr process auto kernel_stack_region = TRY(MM.allocate_kernel_region(default_kernel_stack_size, {}, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow)); kernel_stack_region->set_stack(true); - auto block_timer = try_make_ref_counted(); - if (!block_timer) - return ENOMEM; + auto block_timer = TRY(try_make_ref_counted()); auto name = TRY(KString::try_create(process->name())); - return adopt_nonnull_ref_or_enomem(new (nothrow) Thread(move(process), move(kernel_stack_region), block_timer.release_nonnull(), move(name))); + return adopt_nonnull_ref_or_enomem(new (nothrow) Thread(move(process), move(kernel_stack_region), move(block_timer), move(name))); } Thread::Thread(NonnullRefPtr process, NonnullOwnPtr kernel_stack_region, NonnullRefPtr block_timer, NonnullOwnPtr name)