Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ensure buffer memory is aligned?

I am using a hardware interface to send data that requires me to set up a DMA buffer, which needs to be aligned on 64 bits boundaries.

The DMA engine expects buffers to be aligned on at least 32 bits boundaries (4 bytes). For optimal performance the buffer should be aligned on 64 bits boundaries (8 bytes). The transfer size must be a multiple of 4 bytes.

I use posix_memalign to create a buffer like this...

posix_memalign ((void**)&pPattern, 0x1000, DmaBufferSizeinInt32s * sizeof(int) ) )

pPattern is a pointer to an int, and is the start of my buffer which is DmaBufferSizeinInt32s deep.

Is my buffer aligned on 64bits?

like image 665
Krakkos Avatar asked Oct 30 '09 12:10

Krakkos


2 Answers

Yes, your buffer IS aligned on 64-bits. It's ALSO aligned on a 4 KByte boundary (hence the 0x1000). If you don't want the 4 KB alignement then pass 0x8 instead of 0x1000 ...

Edit: I would also note that usually when writing DMA chains you are writing them through uncached memory or through some kind of non-cache based write queue. If this is the case you want to align your DMA chains to the cache line size as well to prevent a cache write-back overwriting the start or end of your DMA chain.

like image 106
Goz Avatar answered Oct 19 '22 04:10

Goz


As Goz pointed out, but (imo) a bit less clearly: you're asking for alignment by 0x1000 bytes (the second argument), which is much more than 64 bits.

You could change the call to just:

posix_memalign ((void**)&pPattern, 8, DmaBufferSizeinInt32s * sizeof(int)))

This might make the call cheaper (less wasted memory), and in any case is clearer, since you ask for something that more closely matches what you actually want.

like image 21
unwind Avatar answered Oct 19 '22 03:10

unwind