diff --git a/Kernel/Library/ListedRefCounted.h b/Kernel/Library/ListedRefCounted.h new file mode 100644 index 0000000000..e0fdae1edc --- /dev/null +++ b/Kernel/Library/ListedRefCounted.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Kernel { + +template +class ListedRefCounted : public RefCountedBase { +public: + bool unref() const + { + bool did_hit_zero = T::all_instances().with([&](auto& list) { + if (deref_base()) + return false; + list.remove(const_cast(static_cast(*this))); + return true; + }); + if (did_hit_zero) + delete const_cast(static_cast(this)); + return did_hit_zero; + } +}; + +}