PipeWire  1.4.2
cleanup.h
1 /* Simple Plugin API */
2 /* SPDX-FileCopyrightText: Copyright © 2023 PipeWire authors */
3 /* SPDX-License-Identifier: MIT */
4 
5 #ifndef SPA_UTILS_CLEANUP_H
6 #define SPA_UTILS_CLEANUP_H
7 
8 #define spa_exchange(var, new_value) \
9 __extension__ ({ \
10  __typeof__(var) *_ptr_ = &(var); \
11  __typeof__(var) _old_value_ = *_ptr_; \
12  *_ptr_ = (new_value); \
13  _old_value_; \
14 })
15 
16 /* ========================================================================== */
17 
18 #if __GNUC__ >= 10 || defined(__clang__)
19 #define spa_steal_ptr(ptr) ((__typeof__(*(ptr)) *) spa_exchange((ptr), NULL))
20 #else
21 #define spa_steal_ptr(ptr) spa_exchange((ptr), NULL)
22 #endif
23 
24 #define spa_clear_ptr(ptr, destructor) \
25 __extension__ ({ \
26  __typeof__(ptr) _old_value = spa_steal_ptr(ptr); \
27  if (_old_value) \
28  destructor(_old_value); \
29  (void) 0; \
30 })
31 
32 /* ========================================================================== */
33 
34 #include <errno.h>
35 #include <unistd.h>
36 
37 #define spa_steal_fd(fd) spa_exchange((fd), -1)
38 
39 #define spa_clear_fd(fd) \
40 __extension__ ({ \
41  int _old_value = spa_steal_fd(fd), _res = 0; \
42  if (_old_value >= 0) \
43  _res = close(_old_value); \
44  _res; \
45 })
46 
47 /* ========================================================================== */
48 
49 #if defined(__has_attribute) && __has_attribute(__cleanup__)
50 
51 #define spa_cleanup(func) __attribute__((__cleanup__(func)))
52 
53 #define SPA_DEFINE_AUTO_CLEANUP(name, type, ...) \
54 typedef __typeof__(type) _spa_auto_cleanup_type_ ## name; \
55 static inline void _spa_auto_cleanup_func_ ## name (__typeof__(type) *thing) \
56 { \
57  int _save_errno = errno; \
58  __VA_ARGS__ \
59  errno = _save_errno; \
60 }
61 
62 #define spa_auto(name) \
63  spa_cleanup(_spa_auto_cleanup_func_ ## name) \
64  _spa_auto_cleanup_type_ ## name
65 
66 #define SPA_DEFINE_AUTOPTR_CLEANUP(name, type, ...) \
67 typedef __typeof__(type) * _spa_autoptr_cleanup_type_ ## name; \
68 static inline void _spa_autoptr_cleanup_func_ ## name (__typeof__(type) **thing) \
69 { \
70  int _save_errno = errno; \
71  __VA_ARGS__ \
72  errno = _save_errno; \
73 }
74 
75 #define spa_autoptr(name) \
76  spa_cleanup(_spa_autoptr_cleanup_func_ ## name) \
77  _spa_autoptr_cleanup_type_ ## name
78 
79 /* ========================================================================== */
80 
81 #include <stdlib.h>
82 
83 static inline void _spa_autofree_cleanup_func(void *p)
84 {
85  int save_errno = errno;
86  free(*(void **) p);
87  errno = save_errno;
88 }
89 #define spa_autofree spa_cleanup(_spa_autofree_cleanup_func)
90 
91 /* ========================================================================== */
92 
93 static inline void _spa_autoclose_cleanup_func(int *fd)
94 {
95  int save_errno = errno;
96  spa_clear_fd(*fd);
97  errno = save_errno;
98 }
99 #define spa_autoclose spa_cleanup(_spa_autoclose_cleanup_func)
100 
101 /* ========================================================================== */
102 
103 #include <stdio.h>
104 
105 SPA_DEFINE_AUTOPTR_CLEANUP(FILE, FILE, {
106  spa_clear_ptr(*thing, fclose);
107 })
108 
109 /* ========================================================================== */
110 
111 #include <dirent.h>
112 
113 SPA_DEFINE_AUTOPTR_CLEANUP(DIR, DIR, {
114  spa_clear_ptr(*thing, closedir);
115 })
116 
117 #else
118 
119 #define SPA_DEFINE_AUTO_CLEANUP(name, type, ...)
120 #define SPA_DEFINE_AUTOPTR_CLEANUP(name, type, ...)
121 
122 #endif
123 
124 #endif /* SPA_UTILS_CLEANUP_H */