cpp-common: add GCharUP
authorSimon Marchi <simon.marchi@efficios.com>
Sat, 28 May 2022 14:36:08 +0000 (10:36 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8170
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10842
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Philippe Proulx <eeppeliteloop@gmail.com>

src/cpp-common/Makefile.am
src/cpp-common/glib-up.hpp [new file with mode: 0644]

index 534e63888639fd9ef7f2c5b028ec8d1bb78560ee..62e5aff46d954db3ddcfa0a6d8e4c34e1e6a0021 100644 (file)
@@ -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 (file)
index 0000000..92bbde0
--- /dev/null
@@ -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 <glib.h>
+#include <memory>
+
+namespace bt2_common {
+namespace internal {
+
+struct GCharDeleter final
+{
+    void operator()(gchar * const p) noexcept
+    {
+        g_free(p);
+    }
+};
+
+} /* namespace internal */
+
+using GCharUP = std::unique_ptr<gchar, internal::GCharDeleter>;
+
+} /* namespace bt2_common */
+
+#endif /* BABELTRACE_CPP_COMMON_GLIB_UP_HPP */
This page took 0.024484 seconds and 4 git commands to generate.