1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:48:11 +00:00

Kernel: Convert IPv4 socket list from HashTable to IntrusiveList

There was no reason whatsoever to use a HashTable here. IntrusiveList
removes all the heap allocations and does everything more efficiently.
This commit is contained in:
Andreas Kling 2021-08-15 15:46:35 +02:00
parent a154faebb7
commit 7063303022
3 changed files with 17 additions and 10 deletions

View file

@ -26,13 +26,13 @@
namespace Kernel { namespace Kernel {
static Singleton<ProtectedValue<HashTable<IPv4Socket*>>> s_table; static Singleton<ProtectedValue<IPv4Socket::List>> s_all_sockets;
using BlockFlags = Thread::FileDescriptionBlocker::BlockFlags; using BlockFlags = Thread::FileDescriptionBlocker::BlockFlags;
ProtectedValue<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets() ProtectedValue<IPv4Socket::List>& IPv4Socket::all_sockets()
{ {
return *s_table; return *s_all_sockets;
} }
OwnPtr<DoubleBuffer> IPv4Socket::create_receive_buffer() OwnPtr<DoubleBuffer> IPv4Socket::create_receive_buffer()
@ -79,14 +79,14 @@ IPv4Socket::IPv4Socket(int type, int protocol, NonnullOwnPtr<DoubleBuffer> recei
} }
all_sockets().with_exclusive([&](auto& table) { all_sockets().with_exclusive([&](auto& table) {
table.set(this); table.append(*this);
}); });
} }
IPv4Socket::~IPv4Socket() IPv4Socket::~IPv4Socket()
{ {
all_sockets().with_exclusive([&](auto& table) { all_sockets().with_exclusive([&](auto& table) {
table.remove(this); table.remove(*this);
}); });
} }

View file

@ -31,8 +31,6 @@ public:
static KResultOr<NonnullRefPtr<Socket>> create(int type, int protocol); static KResultOr<NonnullRefPtr<Socket>> create(int type, int protocol);
virtual ~IPv4Socket() override; virtual ~IPv4Socket() override;
static ProtectedValue<HashTable<IPv4Socket*>>& all_sockets();
virtual KResult close() override; virtual KResult close() override;
virtual KResult bind(Userspace<const sockaddr*>, socklen_t) override; virtual KResult bind(Userspace<const sockaddr*>, socklen_t) override;
virtual KResult connect(FileDescription&, Userspace<const sockaddr*>, socklen_t, ShouldBlock = ShouldBlock::Yes) override; virtual KResult connect(FileDescription&, Userspace<const sockaddr*>, socklen_t, ShouldBlock = ShouldBlock::Yes) override;
@ -131,6 +129,13 @@ private:
BufferMode m_buffer_mode { BufferMode::Packets }; BufferMode m_buffer_mode { BufferMode::Packets };
OwnPtr<KBuffer> m_scratch_buffer; OwnPtr<KBuffer> m_scratch_buffer;
IntrusiveListNode<IPv4Socket> m_list_node;
public:
using List = IntrusiveList<IPv4Socket, RawPtr<IPv4Socket>, &IPv4Socket::m_list_node>;
static ProtectedValue<IPv4Socket::List>& all_sockets();
}; };
} }

View file

@ -224,9 +224,11 @@ void handle_icmp(EthernetFrameHeader const& eth, IPv4Packet const& ipv4_packet,
{ {
NonnullRefPtrVector<IPv4Socket> icmp_sockets; NonnullRefPtrVector<IPv4Socket> icmp_sockets;
IPv4Socket::all_sockets().for_each_shared([&](const auto& socket) { IPv4Socket::all_sockets().with_exclusive([&](auto& sockets) {
if (socket->protocol() == (unsigned)IPv4Protocol::ICMP) for (auto& socket : sockets) {
icmp_sockets.append(*socket); if (socket.protocol() == (unsigned)IPv4Protocol::ICMP)
icmp_sockets.append(socket);
}
}); });
for (auto& socket : icmp_sockets) for (auto& socket : icmp_sockets)
socket.did_receive(ipv4_packet.source(), 0, { &ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size() }, packet_timestamp); socket.did_receive(ipv4_packet.source(), 0, { &ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size() }, packet_timestamp);