5 #ifndef SPA_UTILS_CLEANUP_H
6 #define SPA_UTILS_CLEANUP_H
8 #if !defined(__has_attribute) || !__has_attribute(__cleanup__)
9 #error "attribute `cleanup` is required"
12 #define spa_cleanup(func) __attribute__((__cleanup__(func)))
14 #define SPA_DEFINE_AUTO_CLEANUP(name, type, ...) \
15 typedef __typeof__(type) _spa_auto_cleanup_type_ ## name; \
16 static inline void _spa_auto_cleanup_func_ ## name (__typeof__(type) *thing) \
21 #define spa_auto(name) \
22 spa_cleanup(_spa_auto_cleanup_func_ ## name) \
23 _spa_auto_cleanup_type_ ## name
25 #define SPA_DEFINE_AUTOPTR_CLEANUP(name, type, ...) \
26 typedef __typeof__(type) * _spa_autoptr_cleanup_type_ ## name; \
27 static inline void _spa_autoptr_cleanup_func_ ## name (__typeof__(type) **thing) \
32 #define spa_autoptr(name) \
33 spa_cleanup(_spa_autoptr_cleanup_func_ ## name) \
34 _spa_autoptr_cleanup_type_ ## name
36 #define spa_exchange(var, new_value) \
38 __typeof__(var) _old_value = (var); \
39 (var) = (new_value); \
43 #define spa_steal_ptr(ptr) ((__typeof__(*(ptr)) *) spa_exchange((ptr), NULL))
44 #define spa_steal_fd(fd) spa_exchange((fd), -1)
50 #define spa_clear_ptr(ptr, destructor) \
52 __typeof__(*(ptr)) *_old_value = spa_steal_ptr(ptr); \
54 destructor(_old_value); \
58 static inline void _spa_autofree_cleanup_func(
void *p)
62 #define spa_autofree spa_cleanup(_spa_autofree_cleanup_func)
68 #define spa_clear_fd(fd) \
70 int _old_value = spa_steal_fd(fd), _res = 0; \
71 if (_old_value >= 0) \
72 _res = close(_old_value); \
76 static inline void _spa_autoclose_cleanup_func(
int *fd)
80 #define spa_autoclose spa_cleanup(_spa_autoclose_cleanup_func)
86 SPA_DEFINE_AUTOPTR_CLEANUP(FILE, FILE, {
87 spa_clear_ptr(*thing, fclose);
94 SPA_DEFINE_AUTOPTR_CLEANUP(DIR, DIR, {
95 spa_clear_ptr(*thing, closedir);