]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
cleanup: add cleanup functions
authorCaleb Sander <csander@purestorage.com>
Tue, 28 Nov 2023 20:35:01 +0000 (13:35 -0700)
committerDaniel Wagner <wagi@monom.org>
Fri, 1 Dec 2023 09:46:56 +0000 (10:46 +0100)
Introduce _cleanup_free_ from nvme-cli to call free() on cleanup,
_cleanup_file_ to call fclose(), _cleanup_dir_ to call closedir(),
and _cleanup_fd_ to call close().

_cleanup_free_ generalizes __cleanup__(cleanup_charp),
so remove it along with cleanup.c.

Signed-off-by: Caleb Sander <csander@purestorage.com>
src/meson.build
src/nvme/cleanup.c [deleted file]
src/nvme/cleanup.h
src/nvme/log.c
test/test-util.c

index 92e39d44e57f20d02e6d6d4c6a52851743c4b895..811f0f82d24e0385079728ae202207302e583e31 100644 (file)
@@ -6,7 +6,6 @@
 # Authors: Martin Belanger <Martin.Belanger@dell.com>
 #
 sources = [
-    'nvme/cleanup.c',
     'nvme/nbft.c',
     'nvme/fabrics.c',
     'nvme/filters.c',
@@ -19,7 +18,6 @@ sources = [
 ]
 
 mi_sources = [
-    'nvme/cleanup.c',
     'nvme/log.c',
     'nvme/mi.c',
     'nvme/mi-mctp.c',
diff --git a/src/nvme/cleanup.c b/src/nvme/cleanup.c
deleted file mode 100644 (file)
index e652e33..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-// SPDX-License-Identifier: LGPL-2.1-or-later
-#include <stdlib.h>
-#include "cleanup.h"
-
-DEFINE_CLEANUP_FUNC(cleanup_charp, char *, free);
index b7e1533583032e2f3566532b106eeca0c489ec81..432760046c3154bc49c9469780c5d638c3f85a65 100644 (file)
@@ -2,6 +2,11 @@
 #ifndef __CLEANUP_H
 #define __CLEANUP_H
 
+#include <dirent.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
 #define __cleanup__(fn) __attribute__((cleanup(fn)))
 
 #define DECLARE_CLEANUP_FUNC(name, type) \
@@ -14,6 +19,23 @@ DECLARE_CLEANUP_FUNC(name, type)             \
                free_fn(*__p);                  \
 }
 
-DECLARE_CLEANUP_FUNC(cleanup_charp, char *);
+static inline void freep(void *p)
+{
+       free(*(void **)p);
+}
+#define _cleanup_free_ __cleanup__(freep)
+
+static inline DEFINE_CLEANUP_FUNC(cleanup_file, FILE *, fclose)
+#define _cleanup_file_ __cleanup__(cleanup_file)
+
+static inline DEFINE_CLEANUP_FUNC(cleanup_dir, DIR *, closedir)
+#define _cleanup_dir_ __cleanup__(cleanup_dir)
+
+static inline void cleanup_fd(int *fd)
+{
+       if (*fd >= 0)
+               close(*fd);
+}
+#define _cleanup_fd_ __cleanup__(cleanup_fd)
 
 #endif
index d4cef19fb0671c15aa75df552f07fe3a8f3934ff..2ffca3eb93f0963fdc3af75e46dcfcab0ca1faa5 100644 (file)
@@ -46,8 +46,8 @@ __nvme_msg(nvme_root_t r, int lvl,
                "[%s] <%s>%s ",
                "[%s] <%s> %s: ",
        };
-       char *header __cleanup__(cleanup_charp) = NULL;
-       char *message __cleanup__(cleanup_charp) = NULL;
+       _cleanup_free_ char *header = NULL;
+       _cleanup_free_ char *message = NULL;
        int idx = 0;
 
        if (!r)
index e32f030a02401a4e74ddd32569fd783061d93c63..88a3f42c4a88c6cea4397022b6eade233733e6ab 100644 (file)
@@ -20,7 +20,6 @@
 #include <netdb.h>
 #include <string.h>
 
-#include "nvme/cleanup.c"      /* to resolve cleanup_charp() */
 #include "nvme/log.c"          /* to resolve __nvme_msg() */
 #include "nvme/util.c"