summaryrefslogtreecommitdiff
path: root/AK/RefCounted.h
AgeCommit message (Collapse)Author
2023-01-02Everywhere: Remove unused includes of AK/StdLibExtras.hBen Wiederhake
These instances were detected by searching for files that include AK/StdLibExtras.h, but don't match the regex: \\b(abs|AK_REPLACED_STD_NAMESPACE|array_size|ceil_div|clamp|exchange|for ward|is_constant_evaluated|is_power_of_two|max|min|mix|move|_RawPtr|RawP tr|round_up_to_power_of_two|swap|to_underlying)\\b (Without the linebreaks.) This regex is pessimistic, so there might be more files that don't actually use any "extra stdlib" functions. In theory, one might use LibCPP to detect things like this automatically, but let's do this one step after another.
2022-11-26AK: Make it possible to not `using` AK classes into the global namespaceAndreas Kling
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-08-20AK+Kernel: Add AK::AtomicRefCounted and use everywhere in the kernelAndreas Kling
Instead of having two separate implementations of AK::RefCounted, one for userspace and one for kernelspace, there is now RefCounted and AtomicRefCounted.
2022-01-11AK+Kernel: Remove one_ref_left() footgunAndreas Kling
This mechanism was unsafe to use in any multithreaded context, since the hook function was invoked on a raw pointer *after* decrementing the local ref count. Since we don't use it for anything anymore, let's just get rid of it.
2022-01-08AK: Unbreak ref counting hooks in RefCountedAndreas Kling
Same fix as 5871072ed3f7b7f4c2492828e1ad7fe167cd16bd, but for userspace this time. Regressed in c4a0f01b02d84117e644f2aff4396bdac1bcf40d.
2022-01-08AK+Kernel: Use requires expression when invoking ref counting hooksAndreas Kling
Replace some old-school template trickery with C++20 requires. :^)
2021-11-14AK+Kernel: Suppress clang-tidy warnings from the cert-* categoryAndrew Kaster
cert-dcl50-cpp: No variadic functions, suppressed in RefCounted and ThreadSafeRefCounted for implementing the magic one_ref_left and will_be_destroyed functions. cert-dcl58-cpp: No opening ::std, suppressed in the places we put names in ::std to aid tools (move, forward, nullptr_t, align_val_t, etc).
2021-10-08AK: Always inline the RefCountedBase functionsAndreas Kling
2021-10-07AK+Kernel: Make automatically locking RefPtr & co a kernel-only thingAndreas Kling
Some time ago, automatic locking was added to the AK smart pointers to paper over various race conditions in the kernel. Until we've actually solved the issues in the kernel, we're stuck with the locking. However, we don't need to punish single-threaded userspace programs with the high cost of locking. This patch moves the thread-safe variants of RefPtr, NonnullRefPtr, WeakPtr and RefCounted into Kernel/Library/.
2021-08-15AK: Pull RefCountedBase into the global namespaceAndreas Kling
2021-06-03AK: Allow inlining ref-count functionalityGunnar Beutner
Previously we'd incur the costs for a function call via the PLT even for the most trivial ref-count actions like increasing/decreasing the reference count. By moving the code to the header file we allow the compiler to inline this code into the caller's function.
2021-05-29AK: Move RefCountedBase definitions out-of-lineAndreas Kling
This dramatically reduces code size since we no longer inline all these VERIFY() checks everywhere. Appears to be performance neutral.
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-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.
2021-01-04AK: Decorate RefCountedBase::try_ref with nodiscardTom
Because try_ref only increments the ref count if it returned true, it is important that any caller properly acts upon the return value.
2020-12-31AK: Fix a race condition with WeakPtr<T>::strong_ref and destructionTom
Since RefPtr<T> decrements the ref counter to 0 and after that starts destructing the object, there is a window where the ref count is 0 and the weak references have not been revoked. Also change WeakLink to be able to obtain a strong reference concurrently and block revoking instead, which should happen a lot less often. Fixes a problem observed in #4621
2020-12-06AK: Add test for the will_be_destroyed and one_ref_left magic functionsAndrew Kaster
Fixes a regression introduced by 5c1b3ce. The commit description there asserts that the changes allow calling will_be_destroyed and one_ref_left, which are not required to be const qualified. The implementation in fact does require the methods to be const qualified, because we forgot to add the const_cast inside the decltypes :^)
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-03AK: Really disallow making OwnPtrs from refcounted typesAnotherTest
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-08-27Meta: Force semi-colon after MAKE_AK_NONXXXABLE()Ben Wiederhake
Before, we had about these occurrence counts: COPY: 13 without, 33 with MOVE: 12 without, 28 with Clearly, 'with' was the preferred way. However, this introduced double-semicolons all over the place, and caused some warnings to trigger. This patch *forces* the usage of a semi-colon when calling the macro, by removing the semi-colon within the macro. (And thus also gets rid of the double-semicolon.)
2020-06-12AK: Ensure RefCounted types are never copied or movedSergey Bugaev
Before this, it has been possible to assign a RefCounted object to another RefCounted object. Hilariosly (or sadly), that copied the refcount among the other fields, meaning the target value ended up with a wrong refcount. Ensure this never happens by disallowing copies and moves for RefCounted types.
2020-06-12AK: Assert refcount doesn't overflowSergey Bugaev
We don't really have a good way to prevent this kind of overflow, but let's at least immediately panic in this case.
2020-06-12AK: Switch RefCounted to atomic refcountingSergey Bugaev
This fixes all sorts of race conditions, primarily in the kernel, where till now it's been possible to obtain either double free or use-after-free by exploiting refcounting races.
2020-06-12AK: Use unsigned int for refcountSergey Bugaev
And while fixing all the tests that look at ref_count(), sneak in a fix for the test suite name.
2020-06-12AK: Ensure we never use OwnPtr<> with RefCounted typesSergey Bugaev
2020-05-20AK: Include Platform.h in RefCounted.h so we have ALWAYS_INLINEAndreas Kling
Otherwise Lagom doesn't build on my host machine.
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-20AK: Allow having ref counted pointers to const objectItamar
We allow the ref-counting parts of an object to be mutated even when the object itself is a const. An important detail is that we allow invoking 'will_be_destroyed' and 'one_ref_left', which are not required to be const qualified, on const objects.
2020-02-09AK: Apply changes for the Bootstrapper environmentLiav A
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-06-21AK: Rename Retainable.h => RefCounted.h.Andreas Kling