1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:47:46 +00:00

Kernel: Convert try_make_ref_counted to use ErrorOr

This allows more ergonomic memory allocation failure related error
checking using the TRY macro.
This commit is contained in:
Idan Horowitz 2022-02-03 16:43:34 +02:00 committed by Andreas Kling
parent 8289727fac
commit a65bbbdb71
4 changed files with 14 additions and 15 deletions

View file

@ -98,13 +98,14 @@ NonnullRefPtr<Bar> another_owner = our_object;
```
The `try_make_ref_counted<T>()` function constructs an object wrapped in `RefPtr<T>` 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<T>()` function constructs an object wrapped in `ErrorOr<NonnullRefPtr<T>>` 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<Bar> our_object = try_make_ref_counted<Bar>();
if (!our_object) {
auto our_object_or_error = try_make_ref_counted<Bar>();
if (our_object_or_error.is_error()) {
// handle allocation failure...
}
NonnullRefPtr<Bar> our_object = our_object_or_error.release_value();
RefPtr<Bar> another_owner = our_object;
```