Re: [PATCH 12/35] monitor: add helpers for __attribute__((cleanup))
From: Hannes Reinecke <hare@suse.de>
Date: 2021-02-04 07:14:30
On 1/26/21 9:33 PM, mwilck@suse.com wrote:
quoted hunk ↗ jump to hunk
From: Martin Wilck <redacted> __attribute__((cleanup)) is very helpful but ugly. Try to avoid defining lots of cleanup functions with these macros. Usage: to declare an auto-cleanup variable of type (some_type *), write CLEANUP_FUNC(some_type) void some_func(void) { CLEANUP(some_type, varname) = NULL; ... } Signed-off-by: Martin Wilck <redacted> --- common.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)diff --git a/common.h b/common.h index 1c214a4..9386ec5 100644 --- a/common.h +++ b/common.h@@ -12,4 +12,16 @@ #define __stringify_1(x...) #x #define __stringify(x...) __stringify_1(x) +#define CLEANUP_FUNC(type) \ +static void __cleanup_ ## type ##_p(type ** __p) \ +{ \ + if (*__p) { \ + free(*__p); \ + *__p = NULL; \ + } \ +} + +#define CLEANUP(__t, __v) \ + __t *__v __attribute__((cleanup(__cleanup_ ## __t ## _p))) + #endif
And this is used where? Can't you merge it with the patch usig the __cleanup functionality? Cheers, Hannes -- Dr. Hannes Reinecke Kernel Storage Architect hare@suse.de +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer _______________________________________________ Linux-nvme mailing list Linux-nvme@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-nvme