From de719a105a7e6a38e348d5365fbb2b0e692dde80 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Sat, 28 May 2022 10:36:08 -0400 Subject: [PATCH] cpp-common: add GCharUP Add GCharUP, a unique_ptr type that wraps a g_char pointer and has a deleter that calls g_free. It can be used to provide automatic memory management of buffers returned by the GLib API. Change-Id: I9d98be2f972cc008877b55a20eceeb3dce256cdb Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/8170 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/10842 Tested-by: jenkins CI-Build: Philippe Proulx --- src/cpp-common/Makefile.am | 3 ++- src/cpp-common/glib-up.hpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/cpp-common/glib-up.hpp diff --git a/src/cpp-common/Makefile.am b/src/cpp-common/Makefile.am index 534e6388..62e5aff4 100644 --- a/src/cpp-common/Makefile.am +++ b/src/cpp-common/Makefile.am @@ -20,4 +20,5 @@ EXTRA_DIST = bt2 \ uuid.hpp \ vector.hpp \ std-int.hpp \ - read-fixed-len-int.hpp + read-fixed-len-int.hpp \ + glib-up.hpp diff --git a/src/cpp-common/glib-up.hpp b/src/cpp-common/glib-up.hpp new file mode 100644 index 00000000..92bbde09 --- /dev/null +++ b/src/cpp-common/glib-up.hpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 EfficiOS, inc. + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_GLIB_UP_HPP +#define BABELTRACE_CPP_COMMON_GLIB_UP_HPP + +#include +#include + +namespace bt2_common { +namespace internal { + +struct GCharDeleter final +{ + void operator()(gchar * const p) noexcept + { + g_free(p); + } +}; + +} /* namespace internal */ + +using GCharUP = std::unique_ptr; + +} /* namespace bt2_common */ + +#endif /* BABELTRACE_CPP_COMMON_GLIB_UP_HPP */ -- 2.34.1