From 641083f3b89b7a7a5928b0eedb1a236949dcdc42 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 16 Aug 2021 20:58:23 +0200 Subject: [PATCH] Kernel: Customize File::unref() and make it virtual Make File inherit from RefCountedBase and provide a custom unref() implementation. This will allow subclasses that participate in lists to remove themselves in a safe way when being destroyed. --- Kernel/FileSystem/File.cpp | 8 ++++++++ Kernel/FileSystem/File.h | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Kernel/FileSystem/File.cpp b/Kernel/FileSystem/File.cpp index 0551f1a390..91311f639e 100644 --- a/Kernel/FileSystem/File.cpp +++ b/Kernel/FileSystem/File.cpp @@ -20,6 +20,14 @@ File::~File() { } +bool File::unref() const +{ + if (deref_base()) + return false; + delete this; + return true; +} + KResultOr> File::open(int options) { auto description = FileDescription::create(*this); diff --git a/Kernel/FileSystem/File.h b/Kernel/FileSystem/File.h index cd29de6e5c..2914d4c088 100644 --- a/Kernel/FileSystem/File.h +++ b/Kernel/FileSystem/File.h @@ -71,9 +71,10 @@ public: // - Should create a Region in the Process and return it if successful. class File - : public RefCounted + : public RefCountedBase , public Weakable { public: + virtual bool unref() const; virtual ~File(); virtual KResultOr> open(int options);