5 #ifndef SPA_UTILS_CLEANUP_H 6 #define SPA_UTILS_CLEANUP_H 8 #define spa_exchange(var, new_value) \ 10 __typeof__(var) *_ptr = &(var); \ 11 __typeof__(var) _old_value = *_ptr; \ 12 *_ptr = (new_value); \ 18 #if __GNUC__ >= 10 || defined(__clang__) 19 #define spa_steal_ptr(ptr) ((__typeof__(*(ptr)) *) spa_exchange((ptr), NULL)) 21 #define spa_steal_ptr(ptr) spa_exchange((ptr), NULL) 24 #define spa_clear_ptr(ptr, destructor) \ 26 __typeof__(ptr) _old_value = spa_steal_ptr(ptr); \ 28 destructor(_old_value); \ 36 #define spa_steal_fd(fd) spa_exchange((fd), -1) 38 #define spa_clear_fd(fd) \ 40 int _old_value = spa_steal_fd(fd), _res = 0; \ 41 if (_old_value >= 0) \ 42 _res = close(_old_value); \ 48 #if defined(__has_attribute) && __has_attribute(__cleanup__) 50 #define spa_cleanup(func) __attribute__((__cleanup__(func))) 52 #define SPA_DEFINE_AUTO_CLEANUP(name, type, ...) \ 53 typedef __typeof__(type) _spa_auto_cleanup_type_ ## name; \ 54 static inline void _spa_auto_cleanup_func_ ## name (__typeof__(type) *thing) \ 59 #define spa_auto(name) \ 60 spa_cleanup(_spa_auto_cleanup_func_ ## name) \ 61 _spa_auto_cleanup_type_ ## name 63 #define SPA_DEFINE_AUTOPTR_CLEANUP(name, type, ...) \ 64 typedef __typeof__(type) * _spa_autoptr_cleanup_type_ ## name; \ 65 static inline void _spa_autoptr_cleanup_func_ ## name (__typeof__(type) **thing) \ 70 #define spa_autoptr(name) \ 71 spa_cleanup(_spa_autoptr_cleanup_func_ ## name) \ 72 _spa_autoptr_cleanup_type_ ## name 78 static inline void _spa_autofree_cleanup_func(
void *p)
82 #define spa_autofree spa_cleanup(_spa_autofree_cleanup_func) 86 static inline void _spa_autoclose_cleanup_func(
int *fd)
90 #define spa_autoclose spa_cleanup(_spa_autoclose_cleanup_func) 96 SPA_DEFINE_AUTOPTR_CLEANUP(FILE, FILE, {
97 spa_clear_ptr(*thing, fclose);
104 SPA_DEFINE_AUTOPTR_CLEANUP(DIR, DIR, {
105 spa_clear_ptr(*thing, closedir);
110 #define SPA_DEFINE_AUTO_CLEANUP(name, type, ...)
111 #define SPA_DEFINE_AUTOPTR_CLEANUP(name, type, ...)