Path: blob/main/sys/compat/linuxkpi/common/include/linux/cleanup.h
39604 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2024-2025 The FreeBSD Foundation4*5* This software was developed by Björn Zeeb under sponsorship from6* the FreeBSD Foundation.7*/89#ifndef _LINUXKPI_LINUX_CLEANUP_H10#define _LINUXKPI_LINUX_CLEANUP_H1112#define __cleanup(_f) __attribute__((__cleanup__(_f)))1314/*15* Note: "_T" are special as they are exposed into common code for16* statements. Extra care should be taken when changing the code.17*/18#define DEFINE_GUARD(_n, _dt, _lock, _unlock) \19\20typedef _dt guard_ ## _n ## _t; \21\22static inline _dt \23guard_ ## _n ## _create( _dt _T) \24{ \25_dt c; \26\27c = ({ _lock; _T; }); \28return (c); \29} \30\31static inline void \32guard_ ## _n ## _destroy(_dt *t) \33{ \34_dt _T; \35\36_T = *t; \37if (_T) { _unlock; }; \38}3940/* We need to keep these calls unique. */41#define guard(_n) \42guard_ ## _n ## _t guard_ ## _n ## _ ## __COUNTER__ \43__cleanup(guard_ ## _n ## _destroy) = guard_ ## _n ## _create4445#define DEFINE_FREE(_n, _t, _f) \46static inline void \47__free_ ## _n(void *p) \48{ \49_t _T; \50\51_T = *(_t *)p; \52_f; \53}5455#define __free(_n) __cleanup(__free_##_n)5657/*58* Given this is a _0 version it should likely be broken up into parts.59* But we have no idead what a _1, _2, ... version would do different60* until we see a call.61* This is used for a not-real-type (rcu). We use a bool to "simulate"62* the lock held. Also _T still special, may not always be used, so tag63* with __unused (or better the LinuxKPI __maybe_unused).64*/65#define DEFINE_LOCK_GUARD_0(_n, _lock, _unlock, ...) \66\67typedef struct { \68bool lock; \69__VA_ARGS__; \70} guard_ ## _n ## _t; \71\72static inline void \73guard_ ## _n ## _destroy(guard_ ## _n ## _t *_T) \74{ \75if (_T->lock) { \76_unlock; \77} \78} \79\80static inline guard_ ## _n ## _t \81guard_ ## _n ## _create(void) \82{ \83guard_ ## _n ## _t _tmp; \84guard_ ## _n ## _t *_T __maybe_unused; \85\86_tmp.lock = true; \87_T = &_tmp; \88_lock; \89return (_tmp); \90}9192#endif /* _LINUXKPI_LINUX_CLEANUP_H */939495