Sometimes some architectures in the Linux kernel don't provide certain functions, such as arch_atomic_sub_and_test
. This allows these functions to be conditionally provided without breaking other architectures.
The #define
allows you to test for the existence of the function with #ifdef
:
#ifdef arch_atomic_sub_and_test
// use arch_atomic_sub_and_test
#else
// some other equivalent code
#endif
or it can be used to error out if the function is not available:
#ifndef arch_atomic_sub_and_test
# error "arch_atomic_sub_and_test not available"
#endif
For example, this is how it's used in the Linux kernel (from include/asm-generic/atomic-instrumented.h
):
#if defined(arch_atomic_sub_and_test)
static inline bool
atomic_sub_and_test(int i, atomic_t *v)
{
kasan_check_write(v, sizeof(*v));
return arch_atomic_sub_and_test(i, v);
}
#define atomic_sub_and_test atomic_sub_and_test
#endif
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…