summaryrefslogtreecommitdiffstats
path: root/contrib/cleanup.h
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/cleanup.h')
-rw-r--r--contrib/cleanup.h25
1 files changed, 25 insertions, 0 deletions
diff --git a/contrib/cleanup.h b/contrib/cleanup.h
new file mode 100644
index 0000000..c9d170a
--- /dev/null
+++ b/contrib/cleanup.h
@@ -0,0 +1,25 @@
+/* Copyright (C) CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+*/
+/**
+ * Cleanup attributes.
+ * @cond internal
+ */
+#pragma once
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#define auto_free __attribute__((cleanup(_cleanup_free)))
+static inline void _cleanup_free(const void *p) {
+ free(*(char **)p);
+}
+#define auto_close __attribute__((cleanup(_cleanup_close)))
+static inline void _cleanup_close(int *p) {
+ if (*p != -1) close(*p);
+}
+#define auto_fclose __attribute__((cleanup(_cleanup_fclose)))
+static inline void _cleanup_fclose(FILE **p) {
+ if (*p) fclose(*p);
+}
+/* @endcond */