From: mwilck@suse.com
To: Keith Busch <kbusch@kernel.org>, Sagi Grimberg <sagi@grimberg.me>,
Chaitanya Kulkarni <Chaitanya.Kulkarni@wdc.com>,
Hannes Reinecke <hare@suse.de>
Cc: linux-nvme@lists.infradead.org,
Enzo Matsumiya <ematsumiya@suse.de>,
Martin Wilck <mwilck@suse.com>,
Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
Subject: [PATCH v2 4/9] nvme: add some simplifying macros for __attribute__((cleanup()))
Date: Tue, 30 Mar 2021 17:57:06 +0200 [thread overview]
Message-ID: <20210330155711.8436-5-mwilck@suse.com> (raw)
In-Reply-To: <20210330155711.8436-1-mwilck@suse.com>
From: Martin Wilck <mwilck@suse.com>
Using __attribute__((cleanup())) is very helpful for writing leak-free
code, but it requires lots of awkward boiler plate code. Add some
small helpers to make its use more comfortable.
Reviewed-by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
Signed-off-by: Martin Wilck <mwilck@suse.com>
---
Makefile | 2 +-
util/cleanup.c | 4 ++++
util/cleanup.h | 18 ++++++++++++++++++
3 files changed, 23 insertions(+), 1 deletion(-)
create mode 100644 util/cleanup.c
create mode 100644 util/cleanup.h
diff --git a/Makefile b/Makefile
index 3ea17b5..3412e5d 100644
--- a/Makefile
+++ b/Makefile
@@ -62,7 +62,7 @@ OBJS := nvme-print.o nvme-ioctl.o nvme-rpmb.o \
nvme-lightnvm.o fabrics.o nvme-models.o plugin.o \
nvme-status.o nvme-filters.o nvme-topology.o
-UTIL_OBJS := util/argconfig.o util/suffix.o util/json.o util/parser.o
+UTIL_OBJS := util/argconfig.o util/suffix.o util/json.o util/parser.o util/cleanup.o
PLUGIN_OBJS := \
plugins/intel/intel-nvme.o \
diff --git a/util/cleanup.c b/util/cleanup.c
new file mode 100644
index 0000000..0d5d910
--- /dev/null
+++ b/util/cleanup.c
@@ -0,0 +1,4 @@
+#include <stdlib.h>
+#include "cleanup.h"
+
+DEFINE_CLEANUP_FUNC(cleanup_charp, char *, free);
diff --git a/util/cleanup.h b/util/cleanup.h
new file mode 100644
index 0000000..89a4984
--- /dev/null
+++ b/util/cleanup.h
@@ -0,0 +1,18 @@
+#ifndef __CLEANUP_H
+#define __CLEANUP_H
+
+#define __cleanup__(fn) __attribute__((cleanup(fn)))
+
+#define DECLARE_CLEANUP_FUNC(name, type) \
+ void name(type *__p)
+
+#define DEFINE_CLEANUP_FUNC(name, type, free_fn)\
+DECLARE_CLEANUP_FUNC(name, type) \
+{ \
+ if (*__p) \
+ free_fn(*__p); \
+}
+
+DECLARE_CLEANUP_FUNC(cleanup_charp, char *);
+
+#endif
--
2.30.1
_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme
next prev parent reply other threads:[~2021-03-30 16:00 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-30 15:57 [PATCH v2 0/9] Some minor fixes/additions for nvme-cli mwilck
2021-03-30 15:57 ` [PATCH v2 1/9] nvme-discover: lookup existing persistent controllers mwilck
2021-03-30 15:57 ` [PATCH v2 2/9] do_discover: free cfg.device when resetting it mwilck
2021-03-30 15:57 ` [PATCH v2 3/9] nvme-connect-all(1): fix documentation for --quiet/-S mwilck
2021-03-30 15:57 ` mwilck [this message]
2021-03-30 15:57 ` [PATCH v2 5/9] nvme-cli: add generic logging functionality mwilck
2021-03-30 15:57 ` [PATCH v2 6/9] nvme: convert some function arguments from "char *" to "const char *" mwilck
2021-03-30 15:57 ` [PATCH v2 7/9] fabrics: use "const char *" in struct config mwilck
2021-03-30 15:57 ` [PATCH v2 8/9] fabrics: fix some memory leaks mwilck
2021-03-30 15:57 ` [PATCH v2 9/9] fabrics: fix invalid memory access in discover_from_conf_file() mwilck
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210330155711.8436-5-mwilck@suse.com \
--to=mwilck@suse.com \
--cc=Chaitanya.Kulkarni@wdc.com \
--cc=ematsumiya@suse.de \
--cc=hare@suse.de \
--cc=kbusch@kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=sagi@grimberg.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox