[RFC] Define/use pad() and pad_mask() instead of open coding it

Peter Hutterer peter.hutterer at who-t.net
Thu Mar 11 17:37:15 PST 2010


On Thu, Mar 11, 2010 at 08:19:04PM -0500, Matt Turner wrote:
> On Thu, Mar 11, 2010 at 8:05 PM, Peter Hutterer
> <peter.hutterer at who-t.net> wrote:
> > On Thu, Mar 11, 2010 at 07:33:30PM -0500, Matt Turner wrote:
> >> à la the Linux Kernel's ALIGN and __ALIGN_MASK macros.
> >>
> >> Not even compile tested, some files might need to include "misc.h".
> >>
> >> CC: Peter Hutterer <peter.hutterer at who-t.net>
> >> Signed-off-by: Matt Turner <mattst88 at gmail.com>
> >
> > ACK in principle, not tested either. :)
> >
> >> diff --git a/include/misc.h b/include/misc.h
> >> index 62d813e..98d39bd 100644
> >> --- a/include/misc.h
> >> +++ b/include/misc.h
> >> @@ -200,6 +200,16 @@ bytes_to_int32(const int bytes) {
> >>      return (((bytes) + 3) >> 2);
> >>  }
> >
> > + /**
> > +  * please comment on what I'm supposed to be doing
> > +  */
> 
> Got any suggested text?

nothing special, just something like this:

pad(): return the number of bytes needed to align "bytes" with a multiple of
"alignment"
(assuming the second param was renamed of course). that should do, plus the
param/return description for completeness' sake.

pad_mask(): return the number of bytes needed to align "bytes" with the bit
mask.

it might be worth checking the mask for completeness as well. Or at least
document what happens when you call a pad_mask(2, 3)?

also, looks like by adding these you can replace the body of bits_to_bytes
and friends with pad().

Cheers,
  Peter

> >>
> >> +static inline int
> >> +pad_mask(const int bytes, const int mask) {
> >> +     return (((bytes) + mask) & ~mask);
> >
> > you don't need the () around bytes, this isn't a macro.
> > which, looking at the context goes for bytes_to_int32 as well :)
> >
> >> +}
> >> +
> >> +static inline int
> >> +pad(const int bytes, const int mask) {
> >
> > shouldn't the second arg be called alignment or something, not mask?
> > otherwise, it's a bit confusing why both pad() and pad_mask() take a mask
> > argument.
> 
> Good point.
> 
> >> +     return pad_mask(bytes, mask - 1);
> >> +}
> >> +
> >
> > Cheers,
> >  Peter
> 
> I've found some more too--((x + bytes - 1) / bytes) * bytes. Ugh, how nasty..
> 
> Matt


More information about the xorg-devel mailing list