]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/memory_utils.h
rexec: use __do_close_prot_errno
[mirror_lxc.git] / src / lxc / memory_utils.h
CommitLineData
af1dc7cd
CB
1/* liblxcapi
2 *
3 * Copyright © 2019 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2019 Canonical Ltd.
5 *
cd4a865d
CB
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10
11 * This library is distributed in the hope that it will be useful,
af1dc7cd 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cd4a865d
CB
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
af1dc7cd
CB
19 */
20
21#ifndef __LXC_MEMORY_UTILS_H
22#define __LXC_MEMORY_UTILS_H
23
d97919ab
CB
24#include <dirent.h>
25#include <errno.h>
26#include <stdio.h>
af1dc7cd 27#include <stdlib.h>
d97919ab 28#include <sys/types.h>
e236fe05 29#include <unistd.h>
af1dc7cd
CB
30
31static inline void __auto_free__(void *p)
32{
33 free(*(void **)p);
34}
35
d97919ab
CB
36static inline void __auto_fclose__(FILE **f)
37{
38 if (*f)
39 fclose(*f);
40}
41
42static inline void __auto_closedir__(DIR **d)
43{
44 if (*d)
45 closedir(*d);
46}
47
e236fe05
CB
48static inline void __auto_close__(int *fd)
49{
50 if (*fd >= 0) {
51 int e = errno;
52 close(*fd);
53 errno = e;
54 }
55}
56
57#define __do_close_prot_errno __attribute__((__cleanup__(__auto_close__)))
af1dc7cd 58#define __do_free __attribute__((__cleanup__(__auto_free__)))
d97919ab
CB
59#define __do_fclose __attribute__((__cleanup__(__auto_fclose__)))
60#define __do_closedir __attribute__((__cleanup__(__auto_closedir__)))
af1dc7cd
CB
61
62#endif /* __LXC_MEMORY_UTILS_H */