diff options
author | Liav A <liavalb@gmail.com> | 2021-08-27 12:32:05 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-31 16:51:13 +0200 |
commit | 8e90a4fd1c4162f26d7e276db6d83005d165abde (patch) | |
tree | 0b38561796c08c25c4b47dff0f4387315b79eff7 /Kernel | |
parent | 01ae6147274e210a157f83856d290e8b1cfbb73f (diff) | |
download | serenity-8e90a4fd1c4162f26d7e276db6d83005d165abde.zip |
Kernel/VirtIO: Don't expose constructors as public method
This leads to a bad pattern where anyone could create an RNG or a
Console object. Instead, let's just use the common pattern of a static
method to instantiate a new object and return it wrapped by a
NonnullRefPtr.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Bus/VirtIO/Console.cpp | 5 | ||||
-rw-r--r-- | Kernel/Bus/VirtIO/Console.h | 3 | ||||
-rw-r--r-- | Kernel/Bus/VirtIO/Device.cpp | 4 | ||||
-rw-r--r-- | Kernel/Bus/VirtIO/RNG.cpp | 5 | ||||
-rw-r--r-- | Kernel/Bus/VirtIO/RNG.h | 4 |
5 files changed, 16 insertions, 5 deletions
diff --git a/Kernel/Bus/VirtIO/Console.cpp b/Kernel/Bus/VirtIO/Console.cpp index ab6d3b02ef..6902214ca9 100644 --- a/Kernel/Bus/VirtIO/Console.cpp +++ b/Kernel/Bus/VirtIO/Console.cpp @@ -12,6 +12,11 @@ namespace Kernel::VirtIO { unsigned Console::next_device_id = 0; +UNMAP_AFTER_INIT NonnullRefPtr<Console> Console::must_create(PCI::Address address) +{ + return adopt_ref_if_nonnull(new Console(address)).release_nonnull(); +} + UNMAP_AFTER_INIT Console::Console(PCI::Address address) : VirtIO::Device(address) , m_device_id(next_device_id++) diff --git a/Kernel/Bus/VirtIO/Console.h b/Kernel/Bus/VirtIO/Console.h index a9bb01cb50..3a303166ff 100644 --- a/Kernel/Bus/VirtIO/Console.h +++ b/Kernel/Bus/VirtIO/Console.h @@ -18,7 +18,7 @@ class Console friend VirtIO::ConsolePort; public: - Console(PCI::Address); + static NonnullRefPtr<Console> must_create(PCI::Address address); virtual ~Console() override = default; virtual StringView purpose() const override { return class_name(); } @@ -30,6 +30,7 @@ public: private: virtual StringView class_name() const override { return "VirtIOConsole"; } + explicit Console(PCI::Address); enum class ControlEvent : u16 { DeviceReady = 0, DeviceAdd = 1, diff --git a/Kernel/Bus/VirtIO/Device.cpp b/Kernel/Bus/VirtIO/Device.cpp index dbf78073bb..320c3e5566 100644 --- a/Kernel/Bus/VirtIO/Device.cpp +++ b/Kernel/Bus/VirtIO/Device.cpp @@ -25,11 +25,11 @@ UNMAP_AFTER_INIT void detect() return; switch (id.device_id) { case PCI::DeviceID::VirtIOConsole: { - [[maybe_unused]] auto& unused = adopt_ref(*new Console(address)).leak_ref(); + [[maybe_unused]] auto& unused = Console::must_create(address).leak_ref(); break; } case PCI::DeviceID::VirtIOEntropy: { - [[maybe_unused]] auto& unused = adopt_ref(*new RNG(address)).leak_ref(); + [[maybe_unused]] auto& unused = RNG::must_create(address).leak_ref(); break; } case PCI::DeviceID::VirtIOGPU: { diff --git a/Kernel/Bus/VirtIO/RNG.cpp b/Kernel/Bus/VirtIO/RNG.cpp index b4db822d9b..5bdaedc449 100644 --- a/Kernel/Bus/VirtIO/RNG.cpp +++ b/Kernel/Bus/VirtIO/RNG.cpp @@ -9,6 +9,11 @@ namespace Kernel::VirtIO { +UNMAP_AFTER_INIT NonnullRefPtr<RNG> RNG::must_create(PCI::Address address) +{ + return adopt_ref_if_nonnull(new RNG(address)).release_nonnull(); +} + UNMAP_AFTER_INIT RNG::RNG(PCI::Address address) : VirtIO::Device(address) { diff --git a/Kernel/Bus/VirtIO/RNG.h b/Kernel/Bus/VirtIO/RNG.h index ea4e8c775d..bc7947b0e0 100644 --- a/Kernel/Bus/VirtIO/RNG.h +++ b/Kernel/Bus/VirtIO/RNG.h @@ -19,13 +19,13 @@ class RNG final : public RefCounted<RNG> , public VirtIO::Device { public: + static NonnullRefPtr<RNG> must_create(PCI::Address address); virtual StringView purpose() const override { return class_name(); } - - RNG(PCI::Address); virtual ~RNG() override; private: virtual StringView class_name() const override { return "VirtIOConsole"; } + explicit RNG(PCI::Address); virtual bool handle_device_config_change() override; virtual void handle_queue_update(u16 queue_index) override; void request_entropy_from_host(); |