summaryrefslogtreecommitdiffstats
path: root/contrib/cleanup.h
blob: c9d170a553af1dd7f120f89d8195179db307fa9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 */