PTR_IF(9)
π PTR_IF(9) β from the Kernel API Manual, bestie
okay so picture this: a tiny macro in PTR_IF(9) that's literally just doing a vibe check on your conditional and handing you back either a pointer or NULL. that's it, that's the whole girlie. she's small, she's iconic, she's the main character of conditional compilation. π
β¨ NAME β¨
PTR_IF β evaluates to @ptr if @cond is true, or to NULL otherwise. no cap, that's exactly what she does. π
πΈ SYNOPSIS πΈ
PTR_IF (cond , ptr );
π¦ ARGUMENTS π¦
condβ a conditional, usually in a form ofIS_ENABLED(CONFIG_FOO). like, that's the typical glow-up situation she finds herself in. β¨ptrβ a pointer to assign ifcondis true.
π DESCRIPTION π
so PTR_IF(IS_ENABLED(CONFIG_FOO), ptr) evaluates to ptr if CONFIG_FOO is set to 'y' or 'm', or to NULL otherwise. and heads up bestie: the ptr argument must be a pointer. that's non-negotiable, it's giving strict. π
the macro can be very useful to help the compiler drop dead code. like, genuinely obsessed with how much she eats in that department. π₯
for instance, consider the following:
#ifdef CONFIG_FOO_SUSPEND
static int foo_suspend(struct device *dev)
{
...
}
#endif
static struct pm_ops foo_ops = {
#ifdef CONFIG_FOO_SUSPEND
.suspend = foo_suspend,
#endif
};
while this works, the foo_suspend macro is compiled conditionally, only when CONFIG_FOO_SUSPEND is set. and that's... kind of problematic, ngl π because there could be a build bug in this function, and we wouldn't have a way to know unless the configuration option is set. like the bug is just sitting there, unbothered, invisible to CI. π
an alternative is to declare foo_suspend always, but mark it as __maybe_unused. this works, but the __maybe_unused attribute is required to instruct the compiler that the function may not be referenced anywhere, and is safe to remove without making a fuss about it. so basically it makes the programmer responsible for tagging the functions that can be garbage-collected. you're the one doing the labelling bestie, the compiler isn't gonna guess. π«Ά
with the macro it is possible to write the following:
static int foo_suspend(struct device *dev)
{
...
}
static struct pm_ops foo_ops = {
.suspend = PTR_IF(IS_ENABLED(CONFIG_FOO_SUSPEND), foo_suspend),
};
and the foo_suspend function will now be automatically dropped by the compiler, and it does not require any specific attribute. π§ββοΈβ¨ she just handles it. no manual tagging, no fuss, no crumbs left behind. periodt. π
π SEE ALSO π
- Kernel file
./include/linux/util_macros.hβ the whole squad lives in there:
go read them, they're all lowkey serving. iykyk. π
okay that's the whole page!! π this little love letter lives in ./include/linux and it's dated October 2026 β so like, future icon behavior honestly. byeee, go build something and let the compiler eat the dead code for you π
β¨π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms