summaryrefslogtreecommitdiff
path: root/AK/WeakPtr.h
AgeCommit message (Collapse)Author
2021-12-05AK: Mark smart pointer classes as [[nodiscard]]Sam Atkins
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-11-17AK: Convert AK::Format formatting helpers to returning ErrorOr<void>Andreas Kling
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-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-04-23AK: Rename adopt() to adopt_ref()Andreas Kling
This makes it more symmetrical with adopt_own() (which is used to create a NonnullOwnPtr from the result of a naked new.)
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-11AK: Annotate WeakPtr functions as [[nodiscard]]Brian Gianforcaro
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-12Everywhere: Remove klog(), dbg() and purge all LogStream usage :^)Andreas Kling
Good-bye LogStream. Long live AK::Format!
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-12-31AK: Fix some WeakPtr copy constructor variants not copying the linkTom
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-30AK+Format: Remove TypeErasedFormatParams& from format function.asynts
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-10-17AK: Add formatters for NonnullOwnPtr and WeakPtr.asynts
2020-09-25Meta+AK: Make clang-format-10 cleanBen Wiederhake
2020-01-25AK: Assert if trying to create a WeakPtr to an object being destroyedAndreas Kling
Trying to make_weak_ptr() on something that has begun destruction is very unlikely to be what you want. Let's assert if that scenario comes up so we can catch it immediately.
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-11-15AK: Fix leak in WeakPtr(WeakPtr&&) and WeakPtr::operator=(WeakPtr&&)Andreas Kling
We were forgetting to adopt the WeakLink, causing a reference leak. This ended up costing us one allocation per exec(), with this stack: kmalloc_impl() Inode::set_vmo() InodeVMObject::create_with_inode() Process::do_exec() Process::exec() Process::sys$execve() This was a pain to track down, in the end I caught it by dumping out every live kmalloc pointer between runs and diffing the sets. Then it was just a matter of matching the pointer to a call stack and looking at what went wrong. :^)
2019-07-11AK: Remove use of copy_ref().Andreas Kling
2019-07-04AK: Move some of LogStream out of line & add overloads for smart pointers.Andreas Kling
2019-06-21AK: Rename RetainPtr => RefPtr and Retained => NonnullRefPtr.Andreas Kling
2019-05-28Add clang-format fileRobin Burchell
Also run it across the whole tree to get everything using the One True Style. We don't yet run this in an automated fashion as it's a little slow, but there is a snippet to do so in makeall.sh.
2019-04-17WindowServer: Mouse switching between system menu and app menu was broken.Andreas Kling
2019-04-14AK: Add WeakPtr::operator T*() for ergonomy.Andreas Kling
2019-01-31Big, possibly complete sweep of naming changes.Andreas Kling
2019-01-13Add basic GUI API for creating labels and buttons.Andreas Kling
2019-01-09Start refactoring graphics system to have per-window backing stores.Andreas Kling
It was fun for everyone to share a single framebuffer but it was also kinda really awful. Let's move towards having a "GraphicsBitmap" as the backing store for each Window. This is going to need a lot of refactoring so let's get started.
2018-12-21Yet another pass of style fixes.Andreas Kling
2018-10-13Start using WeakPtr for some of the WindowManager window pointers.Andreas Kling
2018-10-13Add WeakPtr/Weakable templates.Andreas Kling