LibCore: Move AddressInfoVector to its own file

By defining this class entirely in the System.h header, we are relying
on ::freeaddrinfo being available. This has led to us polluting the
System.h header with system-level definitions on Windows by way of
SocketAddressWindows.h.
This commit is contained in:
Timothy Flynn 2025-11-30 10:18:43 -05:00 committed by Tim Flynn
parent cdc81e8921
commit 2d22ef0db5
Notes: github-actions[bot] 2025-12-01 11:35:48 +00:00
4 changed files with 71 additions and 30 deletions

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2022, Lucas Chollet <lucas.chollet@free.fr>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/AddressInfoVector.h>
#if defined(AK_OS_WINDOWS)
# include <ws2tcpip.h>
#else
# include <netdb.h>
# include <sys/socket.h>
# include <sys/types.h>
#endif
namespace Core::System {
AddressInfoVector::AddressInfoVector(Vector<struct addrinfo> addresses, struct addrinfo* ptr)
: m_addresses(move(addresses))
, m_ptr(adopt_own_if_nonnull(ptr))
{
}
AddressInfoVector::~AddressInfoVector() = default;
void AddressInfoVector::AddrInfoDeleter::operator()(struct addrinfo* ptr)
{
if (ptr)
::freeaddrinfo(ptr);
}
}

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2022, Lucas Chollet <lucas.chollet@free.fr>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Noncopyable.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
struct addrinfo;
namespace Core::System {
class AddressInfoVector {
AK_MAKE_NONCOPYABLE(AddressInfoVector);
AK_MAKE_DEFAULT_MOVABLE(AddressInfoVector);
public:
AddressInfoVector(Vector<struct addrinfo> addresses, struct addrinfo* ptr);
~AddressInfoVector();
ReadonlySpan<struct addrinfo> addresses() const { return m_addresses; }
private:
struct AddrInfoDeleter {
void operator()(struct addrinfo*);
};
Vector<struct addrinfo> m_addresses;
OwnPtr<struct addrinfo, AddrInfoDeleter> m_ptr;
};
}

View file

@ -1,6 +1,7 @@
# These are the minimal set of sources needed to build the code generators. We separate them to allow # These are the minimal set of sources needed to build the code generators. We separate them to allow
# LibCore to depend on generated sources. # LibCore to depend on generated sources.
set(SOURCES set(SOURCES
AddressInfoVector.cpp
ArgsParser.cpp ArgsParser.cpp
Directory.cpp Directory.cpp
DirectoryEntry.cpp DirectoryEntry.cpp

View file

@ -13,6 +13,7 @@
#include <AK/OwnPtr.h> #include <AK/OwnPtr.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/AddressInfoVector.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -141,36 +142,6 @@ ErrorOr<WaitPidResult> waitpid(pid_t waitee, int options = 0);
ErrorOr<void> fchown(int fd, uid_t, gid_t); ErrorOr<void> fchown(int fd, uid_t, gid_t);
#endif #endif
class AddressInfoVector {
AK_MAKE_NONCOPYABLE(AddressInfoVector);
AK_MAKE_DEFAULT_MOVABLE(AddressInfoVector);
public:
~AddressInfoVector() = default;
ReadonlySpan<struct addrinfo> addresses() const { return m_addresses; }
private:
friend ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints);
AddressInfoVector(Vector<struct addrinfo>&& addresses, struct addrinfo* ptr)
: m_addresses(move(addresses))
, m_ptr(adopt_own_if_nonnull(ptr))
{
}
struct AddrInfoDeleter {
void operator()(struct addrinfo* ptr)
{
if (ptr)
::freeaddrinfo(ptr);
}
};
Vector<struct addrinfo> m_addresses {};
OwnPtr<struct addrinfo, AddrInfoDeleter> m_ptr {};
};
ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints); ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints);
unsigned hardware_concurrency(); unsigned hardware_concurrency();