acncommon.h

Utility macros which are common across Acacian code

Macros like <ARRAYSIZE()> and <container_of()> adhere to syntax and definitions encountered in many systems and are only defined here if there is not a definition already in force.

Summary
acncommon.hMacros like <ARRAYSIZE()> and <container_of()> adhere to syntax and definitions encountered in many systems and are only defined here if there is not a definition already in force.
Macros
container_of(ptr, type, member)Find the containing structure of a member.
ARRAYSIZE(array)The number of elements in array.
STRINGIFY(s)Turn the expansion of a macro into a string
ZEROTOEND(pstruct, member)zero out a structure starting at member through to the end
nbits(x)Number of bits required to contain an integer.
clog2(x)log2 of integer x rounded up to nearest integer.
cpwr2(x)Round up to nearest power of 2 larger than or equal to x This is the same as (1 << clog2(x))
Avoiding compiler warnings.

Macros

container_of(ptr, type, member)

Find the containing structure of a member.

This may already be defined in your programming environment.  If not it is defined here.

Given a pointer to a member of a structure, this macro will return a pointer to the parent structure.

Args

ptrpointer to to the member
ptypethe type of the parent structure
memberthe name of the member

ARRAYSIZE(array)

The number of elements in array.

STRINGIFY(s)

Turn the expansion of a macro into a string

ZEROTOEND(pstruct, member)

zero out a structure starting at member through to the end

nbits(x)

Number of bits required to contain an integer.

This is not efficient for variables but works fine for literal constants.

For x = 0 returns 1

Example

nbits(1023) compiles to 10, nbits(1024) compiles to 11, nbits(1025) compiles to 11

clog2(x)

log2 of integer x rounded up to nearest integer.  Returns -1 if x <= 0

Example

clog2(1023) compiles to 10, clog2(1024) compiles to 10, clog2(1025) compiles to 11

cpwr2(x)

Round up to nearest power of 2 larger than or equal to x This is the same as (1 << clog2(x))

Example

cpwr2(1023) compiles to 1024, cpwr2(1024) compiles to 1024, cpwr2(1025) compiles to 2048

Avoiding compiler warnings.

UNUSEDexplicitly mark a variable or argument as unused to avoid “unused variable” compiler warnings.
INITIALIZEDexplicitly mark a variable to avoid “may be used un-initialized” compiler warnings.
Close