Commit Graph

48 Commits

Author SHA1 Message Date
Tim Schumacher
a2f60911fe AK: Rename GenericTraits to DefaultTraits
This feels like a more fitting name for something that provides the
default values for Traits.
2023-11-09 10:05:51 -05:00
Andreas Kling
b256e52586 AK: Make Formatter for NonnullOwnPtr<T> format the T
This mirrors the behavior of NonnullRefPtr<T>. If you want to format
the pointer address, call .ptr() on it.
2023-08-25 20:10:47 +02:00
Ali Mohammad Pur
d06057f88b AK: Don't refer to AK::swap() as ::swap()
While swap() is available in the global namespace in normal conditions,
!USING_AK_GLOBALLY will make this name unavailable in the global
namespace, making these calls fail to compile.
2023-04-28 05:56:04 +02:00
Andreas Kling
b7e847e58b AK: Fix crash during teardown of self-owning objects
We now null out smart pointers *before* calling unref on the pointee.
This ensures that the same smart pointer can't be used to acquire a new
reference to the pointee after its destruction has begun.

I ran into this when destroying a non-empty IntrusiveList of RefPtrs,
but the problem was more general so this fixes it for all of RefPtr,
NonnullRefPtr, OwnPtr and NonnullOwnPtr.
2023-04-21 18:15:00 +02:00
Sam Atkins
067d0689c5 AK: Replace C-style casts 2023-03-09 21:43:54 +01:00
Nico Weber
3c842d9d76 AK: Move try_make() to NonnullOwnPtr.h 2023-02-11 08:53:00 -05:00
Nico Weber
ca6090889a AK: Move adopt_nonnull_own_or_enomem() to NonnullOwnPtr.h
Rewrite the implementation to not depend on OwnPtr.h.

No intended behavior change.
2023-02-11 10:36:48 +01:00
Linus Groh
d26aabff04 Everywhere: Run clang-format 2022-12-03 23:52:23 +00:00
Andreas Kling
ae3ffdd521 AK: Make it possible to not using AK classes into the global namespace
This patch adds the `USING_AK_GLOBALLY` macro which is enabled by
default, but can be overridden by build flags.

This is a step towards integrating Jakt and AK types.
2022-11-26 15:51:34 +01:00
MacDue
3483407ddc AK: Return non-const types from Ptr class operators
Even if the pointer value is const, the value they point to is not
necessarily const, so these functions should not add the qualifier.

This also removes the redundant non-const implementations of these
operators.
2022-11-19 14:37:31 +00:00
Linus Groh
9e79856b43 Revert "AK: Add comparison operators to NonnullOwnPtr"
This reverts commit 50c88e5e3a.

The intention was to add them to NonnullRefPtr, not NonnullOwnPtr. That
is also what was advertised in the PR, but not actually done in the
reverted commit.
2022-06-18 13:17:49 +01:00
Allan Regush
50c88e5e3a AK: Add comparison operators to NonnullOwnPtr 2022-06-18 02:11:08 +01:00
Andreas Kling
75dca629df AK+Kernel: Remove RefPtrTraits template param in userspace code
Only the kernel actually uses RefPtrTraits, so let's not burden
userspace builds with the complexity.
2022-06-15 17:15:04 +02:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Idan Horowitz
ba0a2a3e2f AK: Hide the infallible make<T> factory function from the Kernel
This function has no users, nor should it ever be used in the kernel,
as all allocation failures in the Kernel should be explicitly checked.
2022-02-03 23:33:20 +01:00
Sam Atkins
5013a6480d AK: Mark smart pointer classes as [[nodiscard]]
This makes it an error to not do something with a returned smart
pointer, which should help prevent mistakes. In cases where you do need
to ignore the value, casting to void will placate the compiler.

I did have to add comments to disable clang-format on a couple of lines,
where it wanted to format the code like this:

```c++
private : NonnullRefPtr() = delete;
```
2021-12-05 15:31:03 +01:00
Andreas Kling
216e21a1fa AK: Convert AK::Format formatting helpers to returning ErrorOr<void>
This isn't a complete conversion to ErrorOr<void>, but a good chunk.
The end goal here is to propagate buffer allocation failures to the
caller, and allow the use of TRY() with formatting functions.
2021-11-17 00:21:13 +01:00
Luke Wilde
49259777ef Kernel: Note if the page fault address is a destroyed smart pointer
While I was working on LibWeb, I got a page fault at 0xe0e0e0e4.
This indicates a destroyed RefPtr if compiled with SANITIZE_PTRS
defined. However, the page fault handler didn't print out this
indication.

This makes the page fault handler print out a note if the faulting
address looks like a recently destroyed RefPtr, OwnPtr, NonnullRefPtr,
NonnullOwnPtr, ThreadSafeRefPtr or ThreadSafeNonnullRefPtr. It will
only do this if SANITIZE_PTRS is defined, as smart pointers don't get
scrubbed without it being defined.
2021-10-07 21:30:13 +02:00
Ali Mohammad Pur
a21a3c2620 AK: Make traits for NonnullOwnPtr use ptr_hash instead of int_hash
Otherwise they'd be truncating the pointer in 64-bit builds.
2021-09-13 14:38:53 +04:30
Hendiadyoin1
607bddac96 AK: Use explode_byte for pointer sanitization 2021-08-23 12:30:29 +04:30
Daniel Bertalan
3c6bdb8a61 AK: Make smart pointer factories work with aggregates
Aggregate initialization with brace-enclosed parameters is a
[C++20 feature][1] not yet implemented by Clang. This caused compile
errors if we tried to use the factory functions to create smart pointers
to aggregates.

As a (temporary) fix, [the LWG's previously proposed solution][2] is
implemented by this commit.

Now, wherever it's not possible to direct-initialize, aggregate
initialization is performed.

[1]:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0960r3.html
[2]: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#2089
2021-07-03 01:56:31 +04:30
Daniel Bertalan
3162c9e214 AK: Make (Nonnull)OwnPtr work with abstract classes
Clang produced a compile error at this requires statement if `T` was an
abstract class.
2021-07-03 01:56:31 +04:30
Daniel Bertalan
23d66fe719 AK: Add RETURNS_NONNULL attribute and use it
This attribute tells compilers that the pointer returned by a function
is never null, which lets it optimize away null checks in some places.
This seems like a nice addition to `NonnullOwnPtr` and `NonnullRefPtr`.

Using this attribute causes extra UBSan checks to be emitted. To offset
its performance loss, some additional methods were marked ALWAYS_INLINE,
which lets the compiler optimize duplicate checks
2021-06-29 22:57:52 +04:30
Daniel Bertalan
00915e8948 AK: Add factory methods for creating smart pointers
These functions abstract away the need to call the proper new operator
("throwing" or "non-throwing") and manually adopt the resulting raw
pointer. Modelled after the existing `NonnullOwnPtr<T> make()`
functions, these forward their parameters to the object's constructor.

Note: These can't be used in the common "factory method" idiom, as
private constructors can't be called from a standalone function.

The naming is consistent with AK's and Shell's previous implementation
of these:
- `make` creates a `NonnullOwnPtr<T>` and aborts if the allocation could
  not be performed.
- `try_make` creates an `OwnPtr<T>`, which may be null if the allocation
  failed.
- `create` creates a `NonnullRefPtr<T>`, and aborts on allocation
  failure.
- `try_create` creates a `RefPtr<T>`, which may be null if the
  allocation was not successful.
2021-06-24 17:35:49 +04:30
Brian Gianforcaro
99e23a711d AK+Kernel: Hide AK::adopt_own from usage in the Kernel
We want to discourage folks from using APIs which lull you into a sense
of false safety in terms of OOM. There are cases where you want to force
allocations to succeed or crash, but those should use a more explicit
API than `AK::adopt_own(.)`.
2021-05-29 09:04:05 +02:00
Itamar
b816bd0806 AK: Add ConstPeekType to Traits
Also, the PeekType of smart pointers is now T* instead of const T*.

Note: This commit doesn't compile, it breaks HashMap::get() for some
types. Fixed in the next commit.
2021-05-08 18:10:56 +02:00
Brian Gianforcaro
1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Andreas Kling
ef1e5db1d0 Everywhere: Remove klog(), dbg() and purge all LogStream usage :^)
Good-bye LogStream. Long live AK::Format!
2021-03-12 17:29:37 +01:00
Andreas Kling
5d180d1f99 Everywhere: Rename ASSERT => VERIFY
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED)

Since all of these checks are done in release builds as well,
let's rename them to VERIFY to prevent confusion, as everyone is
used to assertions being compiled out in release.

We can introduce a new ASSERT macro that is specifically for debug
checks, but I'm doing this wholesale conversion first since we've
accumulated thousands of these already, and it's not immediately
obvious which ones are suitable for ASSERT.
2021-02-23 20:56:54 +01:00
asynts
7e62ffbc6e AK+Format: Remove TypeErasedFormatParams& from format function. 2020-12-30 20:33:53 +01:00
Lenny Maiorani
f5ced347e6 AK: Prefer using instead of typedef
Problem:
- `typedef` is a keyword which comes from C and carries with it old
  syntax that is hard to read.
- Creating type aliases with the `using` keyword allows for easier
  future maintenance because it supports template syntax.
- There is inconsistent use of `typedef` vs `using`.

Solution:
- Use `clang-tidy`'s checker called `modernize-use-using` to update
  the syntax to use the newer syntax.
- Remove unused functions to make `clang-tidy` happy.
- This results in consistency within the codebase.
2020-11-12 10:19:04 +01:00
Tom
3c1ef744f6 AK: Add RefPtrTraits to allow implementing custom null pointers
This adds the ability to implement custom null states that allow
storing state in null pointers.
2020-11-10 19:11:52 +01:00
AnotherTest
060ddd2a7a AK: Really disallow making OwnPtrs from refcounted types
This looks at three things:
- if the type has a typedef `AllowOwnPtr', respect that
- if not, disallow construction if both of `ref()' and `unref()' are
  present.
Note that in the second case, if a type only defines `ref()' or only
defines `unref()', an OwnPtr can be created, as a RefPtr of that type
would be ill-formed.

Also marks a `Performance' to explicitly allow OwnPtrs.
2020-11-03 19:14:34 +01:00
asynts
235622dc7f AK: Add formatters for NonnullOwnPtr and WeakPtr. 2020-10-17 23:20:31 +02:00
Brian Gianforcaro
f34fcdedef AK: Decorate AK::NonnullOwnPtr::leak_ptr() with [[nodiscard]] 2020-08-05 16:12:00 +02:00
Sergey Bugaev
0466810638 AK: Ensure we never use OwnPtr<> with RefCounted types 2020-06-12 16:08:45 +02:00
Andreas Kling
76bcd284f9 AK: Remove experimental clang -Wconsumed stuff
This stopped working quite some time ago due to Clang losing track of
typestates for some reason and everything becoming "unknown".

Since we're primarily using GCC anyway, it doesn't seem worth it to try
and maintain this non-working experiment for a secondary compiler.

Also it doesn't look like the Clang team is actively maintaining this
flag anyway. So good-bye, -Wconsumed. :/
2020-05-16 10:55:54 +02:00
Andreas Kling
d69fd87b50 AK: Add templated NonnullOwnPtr::release_nonnull()
This allows you to release a NonnullOwnPtr<T> into a NonnullOwnPtr<U>
2020-05-08 21:38:47 +02:00
Andreas Kling
4f200def9c AK: Stop allowing implicit downcast with OwnPtr and NonnullOwnPtr
Same issue here as we had with RefPtr and NonnullRefPtr.

Since we can't make copies of an owning pointer, we don't get quite the
same static_ptr_cast<T> here. Instead I've only added a new templated
version of OwnPtr::release_nonnull() in this patch, to solve the only
issue that popped up.

I'm not sure what the best solution here is, but this works for now.
2020-04-05 11:32:30 +02:00
Andreas Kling
806d3d8e79 AK: Add adopt_own() to create a NonnullOwnPtr<T> from a T& 2020-04-01 21:05:35 +02:00
Andreas Kling
6cbd72f54f AK: Remove bitrotted Traits::dump() mechanism
This was only used by HashTable::dump() which I used when doing the
first HashTable implementation. Removing this allows us to also remove
most includes of <AK/kstdio.h>.
2020-02-10 11:55:34 +01:00
Andreas Kling
d394267f50 AK: Add NonnullOwnPtr::swap() as well for symmetry 2020-01-19 16:03:57 +01:00
Andreas Kling
94ca55cefd Meta: Add license header to source files
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.

For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.

Going forward, all new source files should include a license header.
2020-01-18 09:45:54 +01:00
Andreas Kling
68e23bca3f AK: Delete operator!() and operator bool() from the Nonnull pointers
Since NonnullRefPtr and NonnullOwnPtr cannot be null, it is pointless
to convert them to a bool, since it would always be true.

This patch makes it an error to null-check one of these pointers.
2019-11-07 18:00:05 +01:00
Andreas Kling
eaa9cf58f5 AK: Use int_hash() to generate less idiotic hashes for {Nonnull,}OwnPtr 2019-08-14 21:21:15 +02:00
Andreas Kling
fdcff7d15e AK: Make it possible to use HashMap<K, NonnullOwnPtr>::get()
Add the concept of a PeekType to Traits<T>. This is the type we'll
return (wrapped in an Optional) from HashMap::get().

The PeekType for OwnPtr<T> and NonnullOwnPtr<T> is const T*,
which means that HashMap::get() will return an Optional<const T*> for
maps-of-those.
2019-08-14 11:47:38 +02:00
Andreas Kling
6a5446d6dd AK: Simplify NonnullPtrVector template a bit.
Add an "ElementType" typedef to NonnullOwnPtr and NonnullRefPtr to allow
clients to easily find the pointee type. Then use this to remove a template
argument from NonnullPtrVector. :^)
2019-07-25 11:10:28 +02:00
Andreas Kling
28da5b002f AK: Add NonnullOwnPtr.
This is just like OwnPtr (also single-owner), except it cannot be null.

NonnullOwnPtr is perfect as the return type of functions that never need to
return nullptr.

It's also useful as an argument type to encode the fact that the argument
must not be nullptr.

The make<Foo>() helper is changed to return NonnullOwnPtr<Foo>.

Note: You can move() out of a NonnullOwnPtr, and after that the object is
in an invalid state. Internally it will be a nullptr at this point, so we'll
still catch misuse, but the only thing that should be done in this state
is running the destructor. I've used consumable annotations to generate some
warnings when using a NonnullOwnPtr after moving from it, but these only
work when compiling with clang, so be aware of that.
2019-07-24 08:32:11 +02:00