ao_align.h
Memory alignment

Notes

Reading from or writing to memory usually requires scalar data, such as an integer or a pointer, to be aligned, that is, its address must be a multiple of its size. This is, because such reads and writes are implemented efficiently on most CPUs, while unaligned accesses impose a penalty on the execution speed.

Normally, the compiler takes care of the correct alignment of variables automatically. However, there are situations, when this is not the case, for example, when dealing with dynamic memory management functions or stack pointers.

Include

stdbool.h

Functions

AO_ALIGN_DOWN

#define AO_ALIGN_DOWN(x, b) \
(                           \
    ((x) / (b)) * (b)       \
)

Aligns value x down to boundary b.

AO_ALIGN_UP

#define AO_ALIGN_UP(x, b)           \
(                                   \
    (((x) + ((b) - 1)) / (b)) * (b) \
)

Aligns value x up to boundary b.

AO_IS_ALIGNED

#define AO_IS_ALIGNED(x, b) \
(                           \
    (x) % (b)               \
    ? false                 \
    : true                  \
)

Checks whether value x is aligned to boundary b.

External Links