From fb9f798b16dc70a3b179f644c088318e315953e2 Mon Sep 17 00:00:00 2001 From: Simon McVittie <smcv@collabora.com> Date: Mon, 31 Mar 2025 12:58:45 +0100 Subject: [PATCH] libc-utils: Factor out autoclear attribute Signed-off-by: Simon McVittie <smcv@collabora.com> --- steam-runtime-tools/libc-utils-internal.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/steam-runtime-tools/libc-utils-internal.h b/steam-runtime-tools/libc-utils-internal.h index e236fdde7..4d770cb7f 100644 --- a/steam-runtime-tools/libc-utils-internal.h +++ b/steam-runtime-tools/libc-utils-internal.h @@ -21,13 +21,23 @@ int steal_fd (int *fdp); void clear_with_free (void *pp); void clear_with_fclose (void *pp); +/* + * autoclear: + * @clear: A function that can clear a variable of the type with this attribute + * + * A type attribute marking a variable to be cleared with the given + * function when it goes out of scope: + * `autoclear(clear_with_fclose) FILE *fh = fopen (...);`. + */ +#define autoclear(clear) __attribute__((__cleanup__(clear))) + /* * autofclose: * * A type attribute marking a `FILE *` variable to be closed with `fclose()` * when it goes out of scope: `autofclose FILE *fh = fopen (...);`. */ -#define autofclose __attribute__((__cleanup__(clear_with_fclose))) +#define autofclose autoclear(clear_with_fclose) /* * autofree: @@ -35,7 +45,7 @@ void clear_with_fclose (void *pp); * A type attribute marking a pointer variable to be freed with `free()` * when it goes out of scope: `autofree char *str = strdup (...);`. */ -#define autofree __attribute__((__cleanup__(clear_with_free))) +#define autofree autoclear(clear_with_free) /* * clear_pointer: -- GitLab