Re: [RFC PATCH 01/20] Create generic alignment API (v8)

Steven Rostedt
2010-08-18T01:25:36Z

On Tue, 2010-08-17 at 19:16 -0400, Mathieu Desnoyers wrote:

> +/*
> + * Align pointer on natural object alignment. Object size must be power of two.
> + */

Hmm, I wonder if we should add a compiler bug here too.

extern void __bug_obj_not_power_of_two(void);

({
if ((sizeof(*obj) - 1) & sizeof(*obj))
__bug_obj_not_power_of_two();
PTR_ALIGN((obj), __alignof__(*(obj)));
})

> +#define object_align(obj) PTR_ALIGN((obj), __alignof__(*(obj)))
> +#define object_align_floor(obj) PTR_ALIGN_FLOOR((obj), __alignof__(*(obj)))
> +
> +/**
> + * offset_align - Calculate the offset needed to align an object on its natural
> + * alignment towards higher addresses.
> + * @align_drift: object offset from an "alignment"-aligned address.
> + * @alignment: natural object alignment. Must be non-zero, power of 2.
> + *
> + * Returns the offset that must be added to align towards higher
> + * addresses.
> + */
> +static inline size_t offset_align(size_t align_drift, size_t alignment)
> +{
> + return (alignment - align_drift) & (alignment - 1);
> +}
> +
> +/**
> + * offset_align_floor - Calculate the offset needed to align an object
> + * on its natural alignment towards lower addresses.
> + * @align_drift: object offset from an "alignment"-aligned address.
> + * @alignment: natural object alignment. Must be non-zero, power of 2.
> + *
> + * Returns the offset that must be substracted to align towards lower addresses.
> + */
> +static inline size_t offset_align_floor(size_t align_drift, size_t alignment)
> +{
> + return (align_drift - alignment) & (alignment - 1);
> +}

I take it that theses functions can have variables passed to it for
alignment, thus a check wont help. Although, we could add a test for the
constant case.

-- Steve

> +