git: 22ce7be762ea - main - libutil++: Add freebsd::addrinfo_up wrapper class for std::unique_ptr<>

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Mon, 04 Aug 2025 19:46:49 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=22ce7be762ea7b03a2c40ad52932023c69e1400f

commit 22ce7be762ea7b03a2c40ad52932023c69e1400f
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2025-08-04 19:38:07 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2025-08-04 19:38:07 +0000

    libutil++: Add freebsd::addrinfo_up wrapper class for std::unique_ptr<>
    
    This class uses a custom deleter that calls freeaddrinfo().
    
    Sponsored by:   Chelsio Communications
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1794
---
 lib/libutil++/Makefile               |  1 +
 lib/libutil++/freebsd::addrinfo_up.3 | 45 ++++++++++++++++++++++++++++++++++++
 lib/libutil++/libutil++.hh           | 16 +++++++++++++
 3 files changed, 62 insertions(+)

diff --git a/lib/libutil++/Makefile b/lib/libutil++/Makefile
index ec83a9ad35f9..d53629b2e4e0 100644
--- a/lib/libutil++/Makefile
+++ b/lib/libutil++/Makefile
@@ -5,6 +5,7 @@ SHLIB_MAJOR=	1
 SRCS=		stringf.cc
 
 MAN+=	freebsd::FILE_up.3 \
+	freebsd::addrinfo_up.3 \
 	freebsd::malloc_up.3 \
 	freebsd::stringf.3
 
diff --git a/lib/libutil++/freebsd::addrinfo_up.3 b/lib/libutil++/freebsd::addrinfo_up.3
new file mode 100644
index 000000000000..4845a76bfb61
--- /dev/null
+++ b/lib/libutil++/freebsd::addrinfo_up.3
@@ -0,0 +1,45 @@
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2025 Chelsio Communications, Inc.
+.\" Written by: John Baldwin <jhb@FreeBSD.org>
+.\"
+.Dd July 31, 2025
+.Dt FREEBSD::ADDRINFO_UP 3
+.Os
+.Sh NAME
+.Nm freebsd::addrinfo_up
+.Nd std::unique_ptr specialization for lists of socket addresses
+.Sh LIBRARY
+.Lb libutil++
+.Sh SYNOPSIS
+.In libutil++.hh
+.Ft using addrinfo_up = std::unique_ptr<addrinfo, freeaddrinfo_deleter>;
+.Sh DESCRIPTION
+This class is a specialization of
+.Vt std::unique_ptr
+for socket addresses returned by
+.Xr getaddrinfo 3 .
+When a list of socket addresses managed by an instance of this class is
+disposed,
+.Xr freeaddrinfo 3
+is invoked to dispose of the list.
+.Sh EXAMPLES
+.Bd -literal -offset indent
+freebsd::addrinfo_up
+resolve_address(const char *address, const char *port)
+{
+	struct addrinfo hints, *ai;
+	int error;
+
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_socktype = SOCK_STREAM;
+	error = getaddrinfo(address, port, &hints, &ai);
+	if (error != 0)
+		return {};
+	return freebsd::addrinfo_up(ai);
+}
+.Ed
+.Sh SEE ALSO
+.Xr getaddrinfo 3
diff --git a/lib/libutil++/libutil++.hh b/lib/libutil++/libutil++.hh
index 93cc2d9e6650..e9269b482a5c 100644
--- a/lib/libutil++/libutil++.hh
+++ b/lib/libutil++/libutil++.hh
@@ -8,6 +8,8 @@
 #ifndef __LIBUTILPP_HH__
 #define	__LIBUTILPP_HH__
 
+#include <netdb.h>
+
 #include <cstdarg>
 #include <cstdio>
 #include <cstdlib>
@@ -27,6 +29,20 @@ namespace freebsd {
 
 	using FILE_up = std::unique_ptr<std::FILE, fclose_deleter>;
 
+	/*
+	 * addrinfo_up is a std::unique_ptr<> which uses
+	 * freeaddrinfo() to destroy the wrapped pointer.  It is
+	 * intended to wrap arrays allocated by getaddrinfo().
+	 */
+	struct freeaddrinfo_deleter {
+		void operator() (struct addrinfo *ai) const
+		{
+			freeaddrinfo(ai);
+		}
+	};
+
+	using addrinfo_up = std::unique_ptr<addrinfo, freeaddrinfo_deleter>;
+
 	/*
 	 * malloc_up<T> is a std::unique_ptr<> which uses free() to
 	 * destroy the wrapped pointer.  This can be used to wrap