From: Bastien Nocera <hadess@hadess.net>
To: linux-bluetooth@vger.kernel.org
Subject: [BlueZ v2 2/5] shared/util: Add helper for "cleanup" variable attribute
Date: Wed, 6 May 2026 16:30:56 +0200 [thread overview]
Message-ID: <20260506143220.3076135-3-hadess@hadess.net> (raw)
In-Reply-To: <20260506143220.3076135-1-hadess@hadess.net>
Use the widespread "cleanup" variable attribute:
https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-cleanup
It is implemented by both GCC and clang on platforms where bluez is
used, and can help reduce memory leaks, while improving readability.
See also:
https://systemd.io/CODING_STYLE/#memory-allocation
https://docs.gtk.org/glib/auto-cleanup.html
---
src/shared/util.h | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/src/shared/util.h b/src/shared/util.h
index 67629dddfaa9..1660b6ea5149 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -93,6 +93,14 @@ do { \
#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
#define malloc0(n) (calloc(1, (n)))
+static inline void freep(void *p)
+{
+ free(*(void **) p);
+}
+
+#define _cleanup_(f) __attribute__((cleanup(f)))
+#define _cleanup_free_ _cleanup_(freep)
+
char *strdelimit(char *str, char *del, char c);
int strsuffix(const char *str, const char *suffix);
char *strstrip(char *str);
--
2.54.0
next prev parent reply other threads:[~2026-05-06 14:32 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-06 14:30 [BlueZ v2 0/5] Add helper for "cleanup" variable attribute Bastien Nocera
2026-05-06 14:30 ` [BlueZ v2 1/5] all: Remove more unneeded MIN/MAX macro definition Bastien Nocera
2026-05-06 21:28 ` Add helper for "cleanup" variable attribute bluez.test.bot
2026-05-06 14:30 ` Bastien Nocera [this message]
2026-05-06 14:30 ` [BlueZ v2 3/5] doc: Recommend using _cleanup_ and friends Bastien Nocera
2026-05-06 14:30 ` [BlueZ v2 4/5] main: Use _cleanup_() to simplify configuration parsing Bastien Nocera
2026-05-06 14:30 ` [BlueZ v2 5/5] main: Use _cleanup_() to simplify GError-handling Bastien Nocera
2026-05-07 14:42 ` [BlueZ v2 0/5] Add helper for "cleanup" variable attribute Bastien Nocera
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=20260506143220.3076135-3-hadess@hadess.net \
--to=hadess@hadess.net \
--cc=linux-bluetooth@vger.kernel.org \
/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