1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2024 The FreeBSD Foundation 5 * 6 * This software was developed by Björn Zeeb under sponsorship from 7 * the FreeBSD Foundation. 8 */ 9 10 #ifndef _LINUXKPI_LINUX_CLEANUP_H 11 #define _LINUXKPI_LINUX_CLEANUP_H 12 13 #define __cleanup(_f) __attribute__((__cleanup__(_f))) 14 15 /* 16 * Note: "_T" are special as they are exposed into common code for 17 * statements. Extra care should be taken when changing the code. 18 */ 19 #define DEFINE_GUARD(_n, _dt, _lock, _unlock) \ 20 \ 21 typedef _dt guard_ ## _n ## _t; \ 22 \ 23 static inline _dt \ 24 guard_ ## _n ## _create( _dt _T) \ 25 { \ 26 _dt c; \ 27 \ 28 c = ({ _lock; _T; }); \ 29 return (c); \ 30 } \ 31 \ 32 static inline void \ 33 guard_ ## _n ## _destroy(_dt *t) \ 34 { \ 35 _dt _T; \ 36 \ 37 _T = *t; \ 38 if (_T) { _unlock; }; \ 39 } 40 41 /* We need to keep these calls unique. */ 42 #define guard(_n) \ 43 guard_ ## _n ## _t guard_ ## _n ## _ ## __COUNTER__ \ 44 __cleanup(guard_ ## _n ## _destroy) = guard_ ## _n ## _create 45 46 #endif /* _LINUXKPI_LINUX_CLEANUP_H */ 47