The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=63ffcf6971a36e00efc138c3261b499aa48e102f
commit 63ffcf6971a36e00efc138c3261b499aa48e102f Author: John Baldwin <j...@freebsd.org> AuthorDate: 2025-08-04 19:38:07 +0000 Commit: John Baldwin <j...@freebsd.org> CommitDate: 2025-08-04 19:38:07 +0000 libutil++: Add freebsd::nvlist_up wrapper class for std::unique_ptr<> This class uses a custom deleter that calls nvlist_destroy(). Sponsored by: Chelsio Communications Pull Request: https://github.com/freebsd/freebsd-src/pull/1794 --- lib/libutil++/Makefile | 1 + lib/libutil++/freebsd::nvlist_up.3 | 37 +++++++++++++++++++++++++++++++++++++ lib/libutil++/libutil++.hh | 14 ++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/lib/libutil++/Makefile b/lib/libutil++/Makefile index e64c9fe6cb75..3389e7e21b11 100644 --- a/lib/libutil++/Makefile +++ b/lib/libutil++/Makefile @@ -8,6 +8,7 @@ MAN+= freebsd::FILE_up.3 \ freebsd::addrinfo_up.3 \ freebsd::fd_up.3 \ freebsd::malloc_up.3 \ + freebsd::nvlist_up.3 \ freebsd::stringf.3 .include <src.opts.mk> diff --git a/lib/libutil++/freebsd::nvlist_up.3 b/lib/libutil++/freebsd::nvlist_up.3 new file mode 100644 index 000000000000..43f76cf3ead3 --- /dev/null +++ b/lib/libutil++/freebsd::nvlist_up.3 @@ -0,0 +1,37 @@ +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.\" Copyright (c) 2025 Chelsio Communications, Inc. +.\" Written by: John Baldwin <j...@freebsd.org> +.\" +.Dd July 31, 2025 +.Dt FREEBSD::NVLIST_UP 3 +.Os +.Sh NAME +.Nm freebsd::nvlist_up +.Nd std::unique_ptr specialization for name/value pairs +.Sh LIBRARY +.Lb libutil++ +.Sh SYNOPSIS +.In libutil++.hh +.Ft using nvlist_up = std::unique_ptr<nvlist, nvlist_deleter>; +.Sh DESCRIPTION +This class is a specialization of +.Vt std::unique_ptr +for +.Vt nvlist_t +objects. +When an +.Vt nvlist_t +object managed by an instance of this class is disposed, +.Xr nvlist_destroy 3 +is invoked to dispose of the object. +.Sh EXAMPLES +.Bd -literal -offset indent +freebsd::nvlist_up nvl(nvlist_create(0)); +nvlist_add_number(nvl.get(), "answer", 42); +nvlist_add_bool(nvl.get(), "valid", true); +// `nvl' is implicitly destroyed +.Ed +.Sh SEE ALSO +.Xr nvlist_destroy 3 diff --git a/lib/libutil++/libutil++.hh b/lib/libutil++/libutil++.hh index 616bbecd8a12..121633c4deea 100644 --- a/lib/libutil++/libutil++.hh +++ b/lib/libutil++/libutil++.hh @@ -8,6 +8,7 @@ #ifndef __LIBUTILPP_HH__ #define __LIBUTILPP_HH__ +#include <sys/nv.h> #include <netdb.h> #include <unistd.h> @@ -131,6 +132,19 @@ namespace freebsd { template <class T> using malloc_up = std::unique_ptr<T, free_deleter<T>>; + /* + * nvlist_up is a std::unique_ptr<> for nvlist_t objects which + * uses nvlist_destroy() to destroy the wrapped pointer. + */ + struct nvlist_deleter { + void operator() (nvlist_t *nvl) const + { + nvlist_destroy(nvl); + } + }; + + using nvlist_up = std::unique_ptr<nvlist_t, nvlist_deleter>; + /* * Returns a std::string containing the same output as * sprintf(). Throws std::bad_alloc if an error occurs.