Skip to content

Commit 2d22ef0

Browse files
committed
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.
1 parent cdc81e8 commit 2d22ef0

File tree

4 files changed

+71
-30
lines changed

4 files changed

+71
-30
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2022, Lucas Chollet <lucas.chollet@free.fr>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibCore/AddressInfoVector.h>
8+
9+
#if defined(AK_OS_WINDOWS)
10+
# include <ws2tcpip.h>
11+
#else
12+
# include <netdb.h>
13+
# include <sys/socket.h>
14+
# include <sys/types.h>
15+
#endif
16+
17+
namespace Core::System {
18+
19+
AddressInfoVector::AddressInfoVector(Vector<struct addrinfo> addresses, struct addrinfo* ptr)
20+
: m_addresses(move(addresses))
21+
, m_ptr(adopt_own_if_nonnull(ptr))
22+
{
23+
}
24+
25+
AddressInfoVector::~AddressInfoVector() = default;
26+
27+
void AddressInfoVector::AddrInfoDeleter::operator()(struct addrinfo* ptr)
28+
{
29+
if (ptr)
30+
::freeaddrinfo(ptr);
31+
}
32+
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2022, Lucas Chollet <lucas.chollet@free.fr>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <AK/Noncopyable.h>
10+
#include <AK/OwnPtr.h>
11+
#include <AK/Vector.h>
12+
13+
struct addrinfo;
14+
15+
namespace Core::System {
16+
17+
class AddressInfoVector {
18+
AK_MAKE_NONCOPYABLE(AddressInfoVector);
19+
AK_MAKE_DEFAULT_MOVABLE(AddressInfoVector);
20+
21+
public:
22+
AddressInfoVector(Vector<struct addrinfo> addresses, struct addrinfo* ptr);
23+
~AddressInfoVector();
24+
25+
ReadonlySpan<struct addrinfo> addresses() const { return m_addresses; }
26+
27+
private:
28+
struct AddrInfoDeleter {
29+
void operator()(struct addrinfo*);
30+
};
31+
32+
Vector<struct addrinfo> m_addresses;
33+
OwnPtr<struct addrinfo, AddrInfoDeleter> m_ptr;
34+
};
35+
36+
}

Libraries/LibCore/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# These are the minimal set of sources needed to build the code generators. We separate them to allow
22
# LibCore to depend on generated sources.
33
set(SOURCES
4+
AddressInfoVector.cpp
45
ArgsParser.cpp
56
Directory.cpp
67
DirectoryEntry.cpp

Libraries/LibCore/System.h

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <AK/OwnPtr.h>
1414
#include <AK/StringView.h>
1515
#include <AK/Vector.h>
16+
#include <LibCore/AddressInfoVector.h>
1617
#include <fcntl.h>
1718
#include <signal.h>
1819
#include <sys/stat.h>
@@ -141,36 +142,6 @@ ErrorOr<WaitPidResult> waitpid(pid_t waitee, int options = 0);
141142
ErrorOr<void> fchown(int fd, uid_t, gid_t);
142143
#endif
143144

144-
class AddressInfoVector {
145-
AK_MAKE_NONCOPYABLE(AddressInfoVector);
146-
AK_MAKE_DEFAULT_MOVABLE(AddressInfoVector);
147-
148-
public:
149-
~AddressInfoVector() = default;
150-
151-
ReadonlySpan<struct addrinfo> addresses() const { return m_addresses; }
152-
153-
private:
154-
friend ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints);
155-
156-
AddressInfoVector(Vector<struct addrinfo>&& addresses, struct addrinfo* ptr)
157-
: m_addresses(move(addresses))
158-
, m_ptr(adopt_own_if_nonnull(ptr))
159-
{
160-
}
161-
162-
struct AddrInfoDeleter {
163-
void operator()(struct addrinfo* ptr)
164-
{
165-
if (ptr)
166-
::freeaddrinfo(ptr);
167-
}
168-
};
169-
170-
Vector<struct addrinfo> m_addresses {};
171-
OwnPtr<struct addrinfo, AddrInfoDeleter> m_ptr {};
172-
};
173-
174145
ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints);
175146

176147
unsigned hardware_concurrency();

0 commit comments

Comments
 (0)