tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Address alignment



On Sat, Jan 09, 2010 at 05:46:05PM +0000, Sad Clouds wrote:
> Hi, I have a requirement where I'm writing N bytes of data to a buffer. If 
> the 
> last byte after data is not aligned on 8-byte boundary, I need to add some 
> padding after data.
> 
> So assume 'buffer' is a pointer to a buffer, after putting N bytes of data in 
> the buffer the pointer will be incremented by N. Is the following code 
> correct 
> for aligning a pointer on 8-byte boundary? Will it work correctly with 64-bit 
> addresses, or do I need to cast -8 to 64-bit value in that case?
> 
> buffer & -8

You can't use '&' on pointers, and that isn't quite the right expression.
The one you want is: x = (x + 7) & ~7;

With a pointer there are varios options, casting to uintptr_t
is one one the easiest.

        David

-- 
David Laight: david%l8s.co.uk@localhost


Home | Main Index | Thread Index | Old Index