]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/memory_utils.h
Merge pull request #3059 from brauner/2019-06-21/seccomp_notify
[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 30
4aa90f60
CB
31#include "macro.h"
32
af1dc7cd
CB
33static inline void __auto_free__(void *p)
34{
35 free(*(void **)p);
36}
37
d97919ab
CB
38static inline void __auto_fclose__(FILE **f)
39{
40 if (*f)
41 fclose(*f);
42}
43
44static inline void __auto_closedir__(DIR **d)
45{
46 if (*d)
47 closedir(*d);
48}
49
4aa90f60
CB
50#define close_prot_errno_disarm(fd) \
51 if (fd >= 0) { \
52 int _e_ = errno; \
53 close(fd); \
54 errno = _e_; \
55 fd = -EBADF; \
56 }
57
c3e3c21a
CB
58#define free_disarm(ptr) \
59 ({ \
60 free(ptr); \
61 move_ptr(ptr); \
62 })
63
e236fe05
CB
64static inline void __auto_close__(int *fd)
65{
4aa90f60 66 close_prot_errno_disarm(*fd);
e236fe05
CB
67}
68
69#define __do_close_prot_errno __attribute__((__cleanup__(__auto_close__)))
af1dc7cd 70#define __do_free __attribute__((__cleanup__(__auto_free__)))
d97919ab
CB
71#define __do_fclose __attribute__((__cleanup__(__auto_fclose__)))
72#define __do_closedir __attribute__((__cleanup__(__auto_closedir__)))
af1dc7cd
CB
73
74#endif /* __LXC_MEMORY_UTILS_H */