summaryrefslogtreecommitdiff
path: root/AK/RefPtr.h
AgeCommit message (Collapse)Author
2021-07-03AK: Make smart pointer factories work with aggregatesDaniel Bertalan
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-06-24AK: Add factory methods for creating smart pointersDaniel Bertalan
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-24Kernel: Pull apart CPU.hHendiadyoin1
This does not add any functional changes
2021-06-08LibC+AK: Remove our custom macros from <assert.h>Gunnar Beutner
Other software might not expect these to be defined and behave differently if they _are_ defined, e.g. scummvm which checks if the TODO macro is defined and fails to build if it is.
2021-05-13AK: Introduce adopt_ref_if_nonnull(..) to aid in Kernel OOM hardeningBrian Gianforcaro
Unfortunately adopt_ref requires a reference, which obviously does not work well with when attempting to harden against allocation failure. The adopt_ref_if_nonnull() variant will allow you to avoid using bare pointers, while still allowing you to handle allocation failure.
2021-05-08AK: Add ConstPeekType to TraitsItamar
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-04-22Everything: Move to SPDX license identifiers in all files.Brian Gianforcaro
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-10AK+Everywhere: Make StdLibExtras templates less wrapper-yAnotherTest
This commit makes the user-facing StdLibExtras templates and utilities arguably more nice-looking by removing the need to reach into the wrapper structs generated by them to get the value/type needed. The C++ standard library had to invent `_v` and `_t` variants (likely because of backwards compat), but we don't need to cater to any codebase except our own, so might as well have good things for free. :^)
2021-03-21Kernel::CPU: Move headers into common directoryHendiadyoin1
Alot of code is shared between i386/i686/x86 and x86_64 and a lot probably will be used for compatability modes. So we start by moving the headers into one Directory. We will probalby be able to move some cpp files aswell.
2021-03-12Everywhere: Remove klog(), dbg() and purge all LogStream usage :^)Andreas Kling
Good-bye LogStream. Long live AK::Format!
2021-03-09AK: Add Formatter for RefPtrAndreas Kling
2021-02-23Everywhere: Rename ASSERT => VERIFYAndreas Kling
(...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-01-12AK: Simplify constructors and conversions from nullptr_tLenny Maiorani
Problem: - Many constructors are defined as `{}` rather than using the ` = default` compiler-provided constructor. - Some types provide an implicit conversion operator from `nullptr_t` instead of requiring the caller to default construct. This violates the C++ Core Guidelines suggestion to declare single-argument constructors explicit (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit). Solution: - Change default constructors to use the compiler-provided default constructor. - Remove implicit conversion operators from `nullptr_t` and change usage to enforce type consistency without conversion.
2020-11-25AK: Always inline RefPtr::operator bool() and RefPtr::is_null()Andreas Kling
2020-11-12AK: Prefer using instead of typedefLenny Maiorani
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-10AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safeTom
This makes most operations thread safe, especially so that they can safely be used in the Kernel. This includes obtaining a strong reference from a weak reference, which now requires an explicit call to WeakPtr::strong_ref(). Another major change is that Weakable::make_weak_ref() may require the explicit target type. Previously we used reinterpret_cast in WeakPtr, assuming that it can be properly converted. But WeakPtr does not necessarily have the knowledge to be able to do this. Instead, we now ask the class itself to deliver a WeakPtr to the type that we want. Also, WeakLink is no longer specific to a target type. The reason for this is that we want to be able to safely convert e.g. WeakPtr<T> to WeakPtr<U>, and before this we just reinterpret_cast the internal WeakLink<T> to WeakLink<U>, which is a bold assumption that it would actually produce the correct code. Instead, WeakLink now operates on just a raw pointer and we only make those constructors/operators available if we can verify that it can be safely cast. In order to guarantee thread safety, we now use the least significant bit in the pointer for locking purposes. This also means that only properly aligned pointers can be used.
2020-11-10AK: Add RefPtrTraits to allow implementing custom null pointersTom
This adds the ability to implement custom null states that allow storing state in null pointers.
2020-09-25Meta+AK: Make clang-format-10 cleanBen Wiederhake
2020-05-20AK+Kernel: Help the compiler inline a bunch of trivial methodsSergey Bugaev
If these methods get inlined, the compiler is able to statically eliminate most of the assertions. Alas, it doesn't realize this, and believes inlining them to be too expensive. So give it a strong hint that it's not the case. This *decreases* the kernel binary size.
2020-04-05AK: Stop allowing implicit downcast with RefPtr and NonnullRefPtrAndreas Kling
We were allowing this dangerous kind of thing: RefPtr<Base> base; RefPtr<Derived> derived = base; This patch changes the {Nonnull,}RefPtr constructors so this is no longer possible. To downcast one of these pointers, there is now static_ptr_cast<T>: RefPtr<Derived> derived = static_ptr_cast<Derived>(base); Fixing this exposed a ton of cowboy-downcasts in various places, which we're now forced to fix. :^)
2020-02-25AK: Add ptr_hash to use int_hash or u64_hash depending on pointer sizejoshua stein
2020-02-16AK: Add basic Traits for RefPtrAndreas Kling
This allows RefPtr to be stored in a HashTable<RefPtr<T>> :^) It's unfortunate about the const_casts. We'll need to fix HashMap::get to play nice with non-const Traits<T>::PeekType at some point.
2020-01-23AK: Let's call decrementing reference counts "unref" instead of "deref"Andreas Kling
It always bothered me that we're using the overloaded "dereference" term for this. Let's call it "unreference" instead. :^)
2020-01-18Meta: Add license header to source filesAndreas Kling
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.
2019-08-14AK: Make it possible to use HashMap<K, NonnullOwnPtr>::get()Andreas Kling
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-03AK: Remove two redundant RefPtr constructors.Andreas Kling
We already have constructors for "const T*" and "const T&" so we don't need to have non-const variants.
2019-08-02AK: Simplify RefPtr and NonnullRefPtr's leak_ref() functionsAndreas Kling
Use AK::exchange() to switch out the internal storage. Also mark these functions with [[nodiscard]] to provoke an compile-time error if they are called without using the return value.
2019-08-02AK: Add anti-null assertions in RefPtr.Andreas Kling
This gives us better error messages when dereferencing null RefPtrs.
2019-08-02AK: Fix ref leaks in RefPtr assignment operators.Andreas Kling
Many of the RefPtr assignment operators would cause ref leaks when we call them to assign a pointer that's already the one kept.
2019-07-21AK: RefPtr::operator=(RefPtr<U>&&) needs to cast the incoming pointer.Andreas Kling
Otherwise it's not possible to assign a RefPtr<Derived>&& to a RefPtr<Base>.
2019-07-11AK: Delete bad pointer assignment operators and constructors.Andreas Kling
We shouldn't allow constructing e.g an OwnPtr from a RefPtr, and similar conversions. Instead just delete those functions so the compiler whines loudly if you try to use them. This patch also deletes constructing OwnPtr from a WeakPtr, even though that *may* be a valid thing to do, it's sufficiently weird that we can make the client jump through some hoops if he really wants it. :^)
2019-07-11AK: Remove copy_ref().Andreas Kling
This patch removes copy_ref() from RefPtr and NonnullRefPtr. This means that it's now okay to simply copy these smart pointers instead: - RefPtr = RefPtr // Okay! - RefPtr = NonnullRefPtr // Okay! - NonnullRefPtr = NonnullRefPtr // Okay! - NonnullRefPtr = RefPtr // Not okay, since RefPtr can be null.
2019-07-11AK: Remove use of copy_ref().Andreas Kling
2019-07-11AK: Make it more more pleasant to copy RefPtr's.Andreas Kling
I had a silly ambition that we would avoid unnecessary ref count churn by forcing explicit use of "copy_ref()" wherever a copy was actually needed. This was making RefPtr a bit clunky to work with, for no real benefit. This patch adds the missing copy construction/assignment stuff to RefPtr.
2019-07-11AK: Remove weird RefPtr(RefPtr&) constructor.Andreas Kling
2019-07-04AK: Move some of LogStream out of line & add overloads for smart pointers.Andreas Kling
2019-06-21AK: Rename RetainPtr.h => RefPtr.h, Retained.h => NonnullRefPtr.h.Andreas Kling