ATTRIBUTE(3)
π ATTRIBUTE(3) β non-standard compiler attribute extensions β¨
okay so this is ATTRIBUTE(3) from the Library Functions Manual, and it's basically the group chat where all your fave __-prefixed macros live. buckle up bestie, there's a lot of them and they're all iconic in their own way π
β¨ SYNOPSIS β¨
#include <sys/cdefs.h>
__dead
__pure
__constfunc
__noinline
__unused
__used
__diagused
__debugused
__packed
__aligned(x);
__section(section);
__read_mostly
__cacheline_aligned
__predict_true(exp);
__predict_false(exp);
__printflike(fmtarg, firstvararg);
__sysloglike(fmtarg, firstvararg);
π¦ DESCRIPTION π¦
so like, as an extension to the C standard, some compilers are like "yes queen" and allow non-standard attributes to be attached to functions, variables, or types, so you can tweak some aspect of how the compiler treats that item. ngl it's giving main character energy but for your code π
The GNU Compiler Collection (GCC), and LLVM/Clang, use the __attribute__ syntax for these attributes β but plot twist π different versions of the compilers support different attributes, and other compilers may use entirely different syntax. messy, right?
NetBSD code should usually avoid direct use of __attribute__ or similar compiler-specific syntax. instead, NetBSD's <sys/cdefs.h> header file hands you several attribute macros in a namespace reserved for the implementation (they all start with β__β, iykyk), and those macros expand to the appropriate syntax for whatever compiler is actually in use. slay π«Ά
π ATTRIBUTES π
__deadβ Some functions, likeabort(3)andexit(3), literally can never return. when one of those is declared with__dead, certain optimizations become possible and warnings that are sensitive to the code flow graph may get pruned. and obviously (emphasis on obviously) a__deadfunction can never have a return type other thanvoid. periodt.__pureβ a__purefunction is defined as one that has no effects except the return value, which is assumed to depend only on the function parameters and/or global variables. any access to parameters and/or global variables must also be read-only. a function that depends on volatile memory, or any other comparable system resource that can change between two consecutive calls, can never be__pure. a bunch ofmath(3)functions satisfy the definition of a__purefunction β at least in theory π. other examples includestrlen(3)andstrcmp(3).__constfuncβ a "const function" is a stricter variant of "pure functions". on top of everything pure functions already restrict, a function declared with__constfunccan never access global variables and can't take pointers as parameters. its return value must depend only on the passed-by-value parameters. also note: a function that calls non-const functions can't be__constfunc. the canonical example of a const function isabs(3). and just like with pure functions, certain micro-optimizations are possible for functions declared with__constfunc. two for one π__noinlineβ sometimes you just know inlining is undesirable, or that a function will perform incorrectly when inlined. the__noinlinemacro expands to a function attribute that stops the compiler from inlining the function, no matter whether the function was declared with theinlinekeyword or not. this attribute takes precedence over all other compiler options related to inlining. it said "not today" π__unusedβ marking an unused function with__unusedinhibits warnings that a function is defined but not used. marking a variable with__unusedinhibits warnings that the variable is unused, or that it's set but never read. it's the "leave her alone" macro fr fr.__usedβ the__usedmacro expands to an attribute that tells the compiler a static variable or function is to be always retained in the object file even if nothing references it. it stays. no cap.__diagusedβ the__diagusedmacro expands to an attribute that tells the compiler a variable or function is used only in diagnostic code, and may be unused in non-diagnostic code.- In the kernel: variables that are used when
DIAGNOSTICis defined, but unused whenDIAGNOSTICis not defined, may be declared with__diagused. - In userland: variables that are used when
NDEBUGis not defined, but unused whenNDEBUGis defined, may be declared with__diagused. - Variables used only in
assert(3)orKASSERT(9)macros are likely candidates for being declared with__diagused. π
- In the kernel: variables that are used when
__debugusedβ the__debugusedmacro expands to an attribute that tells the compiler a variable or function is used only in debug code, and may be unused in non-debug code.- In either the kernel or userland: variables that are used when
DEBUGis defined, but unused whenDEBUGis not defined, may be declared with__debugused. - In the kernel: variables used only in
KDASSERT(9)macros are likely candidates for being declared with__debugused. There is no established convention for the use ofDEBUGin userland code. π
- In either the kernel or userland: variables that are used when
__packedβ the__packedmacro expands to an attribute that forces a variable or structure field to have the smallest possible alignment, potentially disregarding architecture-specific alignment requirements. smallest possible alignment is effectively one byte for variables and one bit for fields. if specified on astructorunion, all variables therein are also packed.__packedis often useful when dealing with data that's in a particular static format on the disk, wire, or memory. lowkey the β¨compactβ¨ queen.__aligned(x)β the__aligned()macro expands to an attribute that specifies the minimum alignment in bytes for a variable, structure field, or function. in other words, the specified object should have an alignment of at least x bytes, as opposed to the minimum alignment requirements dictated by the architecture and the ABI. possible use cases include:- Mixing assembly and C code.
- Dealing with hardware that may impose alignment requirements greater than the architecture itself.
- Using instructions that may impose special alignment requirements. a typical example would be alignment of frequently used objects along processor cache lines.
β οΈ note that when used with functions, structures, or structure members,__aligned()can only be used to increase the alignment. if the macro is however used as part of atypedef, the alignment can both increase and decrease. otherwise it's only possible to decrease the alignment for variables and fields by using the__packedmacro. the effectiveness of__aligned()is largely dependent on the linker. the__alignof__(3)operator can be used to examine the alignment. bestie, read that twice π
__section(section)β the__section()macro expands to an attribute that specifies a particular section to which a variable or function should be placed. normally the compiler places the generated objects into sections such asβdataβorβtextβ. by using__section(), it's possible to override this behavior β maybe so you can place some variables into particular sections specific to unique hardware. custom real estate π__read_mostlyβ the__read_mostlymacro uses__section()to place a variable or function into theβ.data.read_mostlyβsection of the (kernel)elf(5). using__read_mostlylets infrequently modified data be grouped together; it's expected that the cachelines of rarely and frequently modified data structures get separated this way. candidates for__read_mostlyinclude variables that are initialized once, read very often, and seldom written to. read-only icon behavior π__cacheline_alignedβ the__cacheline_alignedmacro behaves like__read_mostly, but the section used isβ.data.cacheline_alignedβinstead. it also uses__aligned()to set the minimum alignment into a predefined coherency unit. this should ensure frequently used data structures are aligned on cacheline boundaries. both__cacheline_alignedand__read_mostlyare only available for the kernel.__predict_trueβ okay so a branch is generally defined as a conditional execution of a program depending on whether a certain flow control mechanism is altered. typical example would be anβif-then-elseβsequence used in high-level languages, or a jump instruction used in machine-level code. a branch prediction would then be defined as an attempt to guess whether a conditional branch will be taken. The macros__predict_true()and__predict_false()annotate the likelihood of whether a branch will evaluate to true or false. the rationale? improving instruction pipelining. semantically,__predict_trueexpects that the integral expression exp yields nonzero. π₯__predict_falseβ the__predict_falseexpands to an attribute that instructs the compiler to predict that a given branch will be likely false. and because programmers are notoriously bad at predicting the likely behavior of their own code π, profiling and empirical evidence should precede the use of__predict_falseand__predict_true. do not just vibe-guess this one.__printflike(fmtarg, firstvararg)β marks a function as takingprintf-like arguments. fmtarg is the index of the format string in the argument list, and firstvararg is the index of the first item of the vararg list.__sysloglike(fmtarg, firstvararg)β marks a function as takingsyslog-like arguments. allows use of the%mformatting code. fmtarg is the index of the format string in the argument list, and firstvararg is the index of the first item of the vararg list, or0if the argument is ava_list. don't forget the zero case bestie π§
π SEE ALSO π
clang(1), gcc(1), __builtin_object_size(3), cdefs(3), c(7)
π CAVEATS π
it goes without saying (but we're saying it anyway) that portable applications should steer clear from non-standard extensions specific to any given compiler. and even when portability is not a concern β use these macros sparsely and wisely. no overdoing it, queen. moderation is the vibe check.
πΈ that's the whole page, bestie β written by the Debian crew and dated September 14, 2018. go forth and annotate responsibly β¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2010 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms