diff options
author | Andreas Kling <kling@serenityos.org> | 2020-02-08 00:19:46 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-02-08 00:19:46 +0100 |
commit | a3f39fe789380ff0a577083993bbd0cda0e9e55e (patch) | |
tree | deb5042ac0b62913668ab4dc2f727ead2d0dd142 /Kernel | |
parent | f3a5985bb2ffef9b1094d3672a51df81bdf80431 (diff) | |
download | serenity-a3f39fe789380ff0a577083993bbd0cda0e9e55e.zip |
Net: Make NetworkAdapter reference-counted
The idea behind WeakPtr<NetworkAdapter> was to support hot-pluggable
network adapters, but on closer thought, that's super impractical so
let's not go down that road.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Net/E1000NetworkAdapter.cpp | 4 | ||||
-rw-r--r-- | Kernel/Net/NetworkAdapter.cpp | 10 | ||||
-rw-r--r-- | Kernel/Net/NetworkAdapter.h | 6 | ||||
-rw-r--r-- | Kernel/Net/RTL8139NetworkAdapter.cpp | 2 | ||||
-rw-r--r-- | Kernel/Net/Routing.cpp | 12 | ||||
-rw-r--r-- | Kernel/Net/Routing.h | 2 | ||||
-rw-r--r-- | Kernel/Net/TCPSocket.h | 2 |
7 files changed, 19 insertions, 19 deletions
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<void(NetworkAdapter&)> callback) callback(*it); } -WeakPtr<NetworkAdapter> NetworkAdapter::from_ipv4_address(const IPv4Address& address) +RefPtr<NetworkAdapter> 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> NetworkAdapter::lookup_by_name(const StringView& name) +RefPtr<NetworkAdapter> 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<NetworkAdapter> { +class NetworkAdapter : public RefCounted<NetworkAdapter> { public: static void for_each(Function<void(NetworkAdapter&)>); - static WeakPtr<NetworkAdapter> from_ipv4_address(const IPv4Address&); - static WeakPtr<NetworkAdapter> lookup_by_name(const StringView&); + static RefPtr<NetworkAdapter> from_ipv4_address(const IPv4Address&); + static RefPtr<NetworkAdapter> 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<NetworkAdapter> local_adapter = nullptr; - WeakPtr<NetworkAdapter> gateway_adapter = nullptr; + RefPtr<NetworkAdapter> local_adapter = nullptr; + RefPtr<NetworkAdapter> 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<NetworkAdapter> adapter = nullptr; + RefPtr<NetworkAdapter> 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<NetworkAdapter> adapter; + RefPtr<NetworkAdapter> 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<IPv4SocketTuple, NonnullRefPtr<TCPSocket>> m_pending_release_for_accept; Direction m_direction { Direction::Unspecified }; Error m_error { Error::None }; - WeakPtr<NetworkAdapter> m_adapter; + RefPtr<NetworkAdapter> m_adapter; u32 m_sequence_number { 0 }; u32 m_ack_number { 0 }; State m_state { State::Closed }; |