From a3f39fe789380ff0a577083993bbd0cda0e9e55e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 8 Feb 2020 00:19:46 +0100 Subject: Net: Make NetworkAdapter reference-counted The idea behind WeakPtr was to support hot-pluggable network adapters, but on closer thought, that's super impractical so let's not go down that road. --- Kernel/Net/E1000NetworkAdapter.cpp | 4 ++-- Kernel/Net/NetworkAdapter.cpp | 10 +++++----- Kernel/Net/NetworkAdapter.h | 6 +++--- Kernel/Net/RTL8139NetworkAdapter.cpp | 2 +- Kernel/Net/Routing.cpp | 12 ++++++------ Kernel/Net/Routing.h | 2 +- Kernel/Net/TCPSocket.h | 2 +- 7 files changed, 19 insertions(+), 19 deletions(-) (limited to 'Kernel') diff --git a/Kernel/Net/E1000NetworkAdapter.cpp b/Kernel/Net/E1000NetworkAdapter.cpp index a62fe622c3..d4f05871c3 100644 --- a/Kernel/Net/E1000NetworkAdapter.cpp +++ b/Kernel/Net/E1000NetworkAdapter.cpp @@ -128,7 +128,7 @@ void E1000NetworkAdapter::detect(const PCI::Address& address) if (id != qemu_bochs_vbox_id) return; u8 irq = PCI::get_interrupt_line(address); - new E1000NetworkAdapter(address, irq); + (void)adopt(*new E1000NetworkAdapter(address, irq)).leak_ref(); } E1000NetworkAdapter::E1000NetworkAdapter(PCI::Address pci_address, u8 irq) @@ -376,7 +376,7 @@ void E1000NetworkAdapter::send_raw(const u8* data, size_t length) #endif auto& descriptor = m_tx_descriptors[tx_current]; ASSERT(length <= 8192); - auto *vptr = (void*)(descriptor.addr + 0xc0000000); + auto* vptr = (void*)(descriptor.addr + 0xc0000000); memcpy(vptr, data, length); descriptor.length = length; descriptor.status = 0; diff --git a/Kernel/Net/NetworkAdapter.cpp b/Kernel/Net/NetworkAdapter.cpp index 62e91714b3..a82f7edb84 100644 --- a/Kernel/Net/NetworkAdapter.cpp +++ b/Kernel/Net/NetworkAdapter.cpp @@ -49,26 +49,26 @@ void NetworkAdapter::for_each(Function callback) callback(*it); } -WeakPtr NetworkAdapter::from_ipv4_address(const IPv4Address& address) +RefPtr NetworkAdapter::from_ipv4_address(const IPv4Address& address) { LOCKER(all_adapters().lock()); for (auto* adapter : all_adapters().resource()) { if (adapter->ipv4_address() == address) - return adapter->make_weak_ptr(); + return adapter; } if (address[0] == 127) - return LoopbackAdapter::the().make_weak_ptr(); + return LoopbackAdapter::the(); return nullptr; } -WeakPtr NetworkAdapter::lookup_by_name(const StringView& name) +RefPtr NetworkAdapter::lookup_by_name(const StringView& name) { NetworkAdapter* found_adapter = nullptr; for_each([&](auto& adapter) { if (adapter.name() == name) found_adapter = &adapter; }); - return found_adapter ? found_adapter->make_weak_ptr() : nullptr; + return found_adapter; } NetworkAdapter::NetworkAdapter() diff --git a/Kernel/Net/NetworkAdapter.h b/Kernel/Net/NetworkAdapter.h index f2648c2a6b..12bac4207e 100644 --- a/Kernel/Net/NetworkAdapter.h +++ b/Kernel/Net/NetworkAdapter.h @@ -40,11 +40,11 @@ class NetworkAdapter; -class NetworkAdapter : public Weakable { +class NetworkAdapter : public RefCounted { public: static void for_each(Function); - static WeakPtr from_ipv4_address(const IPv4Address&); - static WeakPtr lookup_by_name(const StringView&); + static RefPtr from_ipv4_address(const IPv4Address&); + static RefPtr lookup_by_name(const StringView&); virtual ~NetworkAdapter(); virtual const char* class_name() const = 0; diff --git a/Kernel/Net/RTL8139NetworkAdapter.cpp b/Kernel/Net/RTL8139NetworkAdapter.cpp index 4ea4a6d4e4..d4686e7dcc 100644 --- a/Kernel/Net/RTL8139NetworkAdapter.cpp +++ b/Kernel/Net/RTL8139NetworkAdapter.cpp @@ -132,7 +132,7 @@ void RTL8139NetworkAdapter::detect(const PCI::Address& address) if (id != rtl8139_id) return; u8 irq = PCI::get_interrupt_line(address); - new RTL8139NetworkAdapter(address, irq); + (void)adopt(*new RTL8139NetworkAdapter(address, irq)).leak_ref(); } RTL8139NetworkAdapter::RTL8139NetworkAdapter(PCI::Address pci_address, u8 irq) diff --git a/Kernel/Net/Routing.cpp b/Kernel/Net/Routing.cpp index 0f6c12c926..17149c7aff 100644 --- a/Kernel/Net/Routing.cpp +++ b/Kernel/Net/Routing.cpp @@ -46,13 +46,13 @@ bool RoutingDecision::is_zero() const RoutingDecision route_to(const IPv4Address& target, const IPv4Address& source) { if (target[0] == 127) - return { LoopbackAdapter::the().make_weak_ptr(), {} }; + return { LoopbackAdapter::the(), {} }; auto target_addr = target.to_u32(); auto source_addr = source.to_u32(); - WeakPtr local_adapter = nullptr; - WeakPtr gateway_adapter = nullptr; + RefPtr local_adapter = nullptr; + RefPtr gateway_adapter = nullptr; NetworkAdapter::for_each([source_addr, &target_addr, &local_adapter, &gateway_adapter](auto& adapter) { auto adapter_addr = adapter.ipv4_address().to_u32(); @@ -62,10 +62,10 @@ RoutingDecision route_to(const IPv4Address& target, const IPv4Address& source) return; if ((target_addr & adapter_mask) == (adapter_addr & adapter_mask)) - local_adapter = adapter.make_weak_ptr(); + local_adapter = adapter; if (adapter.ipv4_gateway().to_u32() != 0) - gateway_adapter = adapter.make_weak_ptr(); + gateway_adapter = adapter; }); if (!local_adapter && !gateway_adapter) { @@ -76,7 +76,7 @@ RoutingDecision route_to(const IPv4Address& target, const IPv4Address& source) return { nullptr, {} }; } - WeakPtr adapter = nullptr; + RefPtr adapter = nullptr; IPv4Address next_hop_ip; if (local_adapter) { diff --git a/Kernel/Net/Routing.h b/Kernel/Net/Routing.h index 3a087bda88..11d661420d 100644 --- a/Kernel/Net/Routing.h +++ b/Kernel/Net/Routing.h @@ -30,7 +30,7 @@ struct RoutingDecision { - WeakPtr adapter; + RefPtr adapter; MACAddress next_hop; bool is_zero() const; diff --git a/Kernel/Net/TCPSocket.h b/Kernel/Net/TCPSocket.h index 24ff66218e..87f5871d40 100644 --- a/Kernel/Net/TCPSocket.h +++ b/Kernel/Net/TCPSocket.h @@ -182,7 +182,7 @@ private: HashMap> m_pending_release_for_accept; Direction m_direction { Direction::Unspecified }; Error m_error { Error::None }; - WeakPtr m_adapter; + RefPtr m_adapter; u32 m_sequence_number { 0 }; u32 m_ack_number { 0 }; State m_state { State::Closed }; -- cgit v1.2.3