LinuxKPI: cleanup: implement __free() and DEFINE_FREE(); use for kfree()

A wifi driver update needs this.

Sponsored by:	The FreeBSD Foundation (initially)
Reviewed by:	dumbbell
Differential Revision:	https://reviews.freebsd.org/D52075

(cherry picked from commit 39e9290d89)
This commit is contained in:
Bjoern A. Zeeb 2025-09-04 20:24:55 +00:00
parent 88198564db
commit 830cc677d9
2 changed files with 17 additions and 1 deletions

View file

@ -1,7 +1,7 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 The FreeBSD Foundation
* Copyright (c) 2024-2025 The FreeBSD Foundation
*
* This software was developed by Björn Zeeb under sponsorship from
* the FreeBSD Foundation.
@ -43,4 +43,16 @@
guard_ ## _n ## _t guard_ ## _n ## _ ## __COUNTER__ \
__cleanup(guard_ ## _n ## _destroy) = guard_ ## _n ## _create
#define DEFINE_FREE(_n, _t, _f) \
static inline void \
__free_ ## _n(void *p) \
{ \
_t _T; \
\
_T = *(_t *)p; \
_f; \
}
#define __free(_n) __cleanup(__free_##_n)
#endif /* _LINUXKPI_LINUX_CLEANUP_H */

View file

@ -40,8 +40,10 @@
#include <linux/compat.h>
#include <linux/types.h>
#include <linux/gfp.h>
#include <linux/err.h>
#include <linux/llist.h>
#include <linux/overflow.h>
#include <linux/cleanup.h>
MALLOC_DECLARE(M_KMALLOC);
@ -153,6 +155,8 @@ kfree(const void *ptr)
lkpi_kfree(ptr);
}
DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T))
/*
* Other k*alloc() funtions using the above as underlying allocator.
*/