summaryrefslogtreecommitdiff
path: root/Kernel/Bus/USB
diff options
context:
space:
mode:
authorLiav A <liavalb@gmail.com>2021-12-12 16:33:08 +0200
committerAndreas Kling <kling@serenityos.org>2021-12-14 09:01:33 +0100
commit478f54389903d34a0a5b670d6606c5cc0db1dd4d (patch)
tree854c0afe4bb7ec267a592f8039db33ff2eb9f4d4 /Kernel/Bus/USB
parent4daf07e69fe8ef641bcb39909f17df7cbfe88a01 (diff)
downloadserenity-478f54389903d34a0a5b670d6606c5cc0db1dd4d.zip
Kernel/SysFS: Prevent allocation for component name during construction
Instead, allocate before constructing the object and pass NonnullOwnPtr of KString to the object if needed. Some classes can determine their names as they have a known attribute to look for or have a static name.
Diffstat (limited to 'Kernel/Bus/USB')
-rw-r--r--Kernel/Bus/USB/SysFSUSB.cpp10
-rw-r--r--Kernel/Bus/USB/SysFSUSB.h6
2 files changed, 11 insertions, 5 deletions
diff --git a/Kernel/Bus/USB/SysFSUSB.cpp b/Kernel/Bus/USB/SysFSUSB.cpp
index 67c8995744..e4fc0a67ef 100644
--- a/Kernel/Bus/USB/SysFSUSB.cpp
+++ b/Kernel/Bus/USB/SysFSUSB.cpp
@@ -13,9 +13,10 @@ namespace Kernel::USB {
static SysFSUSBBusDirectory* s_procfs_usb_bus_directory;
-SysFSUSBDeviceInformation::SysFSUSBDeviceInformation(USB::Device& device)
- : SysFSComponent(String::number(device.address()))
+SysFSUSBDeviceInformation::SysFSUSBDeviceInformation(NonnullOwnPtr<KString> device_name, USB::Device& device)
+ : SysFSComponent()
, m_device(device)
+ , m_device_name(move(device_name))
{
}
@@ -150,7 +151,7 @@ SysFSUSBBusDirectory& SysFSUSBBusDirectory::the()
}
UNMAP_AFTER_INIT SysFSUSBBusDirectory::SysFSUSBBusDirectory(SysFSBusDirectory& buses_directory)
- : SysFSDirectory("usb"sv, buses_directory)
+ : SysFSDirectory(buses_directory)
{
}
@@ -163,7 +164,8 @@ UNMAP_AFTER_INIT void SysFSUSBBusDirectory::initialize()
NonnullRefPtr<SysFSUSBDeviceInformation> SysFSUSBDeviceInformation::create(USB::Device& device)
{
- return adopt_ref(*new SysFSUSBDeviceInformation(device));
+ auto device_name = KString::must_create(String::number(device.address()));
+ return adopt_ref(*new SysFSUSBDeviceInformation(move(device_name), device));
}
}
diff --git a/Kernel/Bus/USB/SysFSUSB.h b/Kernel/Bus/USB/SysFSUSB.h
index 066254dcc9..87a5b8857e 100644
--- a/Kernel/Bus/USB/SysFSUSB.h
+++ b/Kernel/Bus/USB/SysFSUSB.h
@@ -20,11 +20,12 @@ public:
virtual ~SysFSUSBDeviceInformation() override;
static NonnullRefPtr<SysFSUSBDeviceInformation> create(USB::Device&);
+ virtual StringView name() const override { return m_device_name->view(); }
RefPtr<USB::Device> device() const { return m_device; }
protected:
- explicit SysFSUSBDeviceInformation(USB::Device& device);
+ SysFSUSBDeviceInformation(NonnullOwnPtr<KString> device_name, USB::Device& device);
virtual ErrorOr<size_t> read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const override;
@@ -36,6 +37,7 @@ private:
ErrorOr<void> try_generate(KBufferBuilder&);
virtual ErrorOr<void> refresh_data(OpenFileDescription& description) const override;
mutable Mutex m_lock { "SysFSUSBDeviceInformation" };
+ NonnullOwnPtr<KString> m_device_name;
};
class SysFSUSBBusDirectory final : public SysFSDirectory {
@@ -43,6 +45,8 @@ public:
static void initialize();
static SysFSUSBBusDirectory& the();
+ virtual StringView name() const override { return "usb"sv; }
+
void plug(USB::Device&);
void unplug(USB::Device&);