I\'m trying to understand what functions memalign()
and posix_memalign()
do. Reading the available documentation didn\'t help.
Can someone
As memalign is obsolete (ref: man page), only the difference between malloc() and posix_memalign() will be described here. malloc() is 8-byte aligned (eg, for RHEL 32-bit), but for posix_memalign(), alignment is user-definable. To know the use of this, perhaps one good example is setting memory attribute using mprotect(). To use mprotect(), the memory pointer must be PAGE aligned. And so if you call posix_memalign() with pagesize as the alignment, then the returned pointer can easily submit to mprotect() to set the read-write-executable attributes. (for example, after you copy the data into the memory pointer, you can set it to read-only attribute to protect it from being modified). "malloc()"'s returned pointer cannot be use here.
How does it work is implementation dependent. The purpose of the function is to give you an n-bytes aligned memory block (the start address of the block is a multiply of n).
Deffault malloc return pointer that are multiple of 8, It's mean malloc split memory to chunks have 8 bytes and check free memory at start of each chunk. There are 2 faces of problem.
Larger chunk will waste more memory, but larger chunk help C find free chunk memory faster. memalign can change how big those chunk is. If you want to save memory, decrease chunk's size to 2 or 4. If you want to make your application faster, increase chunk's size to power of 2.
In addition to Oli's answer I would like to point you to an even more important issue.
On recent x86 architectures a cache-line, which is the smallest amount of data that can fetched from memory to cache, is 64 bytes. Suppose your structure size is 56 bytes, you have a large array of them. When you lookup one element, the CPU will need to issue 2 memory requests (it might issue 2 requests even if it is in the middle of the cacheline). That is bad for performance, as you have to wait for memory, and you use more cache, which ultimately gives a higher cache-miss ratio. In this case it is not enough to just use posix_memalign, but you should pad or compact your structure to be on 64byte boundaries.
Having 40 byte struct is just bad luck :)
When you use posix_memalign in GNU C, you should be careful that second parameter should not only be a power of two, but also be a multiple of sizeof (void*). Note that this requirement is different from the one in the memalign function, which requires only power of two.
int
__posix_memalign (void **memptr, size_t alignment, size_t size)
{
void *mem;
/* Test whether the SIZE argument is valid. It must be a power of
two multiple of sizeof (void *). */
if (alignment % sizeof (void *) != 0
|| !powerof2 (alignment / sizeof (void *))
|| alignment == 0)
return EINVAL;
void *address = RETURN_ADDRESS (0);
mem = _mid_memalign (alignment, size, address);
if (mem != NULL)
{
*memptr = mem;
return 0;
}
return ENOMEM;
}
weak_alias (__posix_memalign, posix_memalign)
When we look at the first if condition in the posix_memalign implementation, it checks whether alignment is multiple of sizeof(void*).
Whereas malloc
gives you a chunk of memory that could have any alignment (the only requirement is that it must be aligned for the largest primitive type that the implementation supports), posix_memalign
gives you a chunk of memory that is guaranteed to have the requested alignment.
So the result of e.g. posix_memalign(&p, 32, 128)
will be a 128-byte chunk of memory whose start address is guaranteed to be a multiple of 32.
This is useful for various low-level operations (such as using SSE instructions, or DMA), that require memory that obeys a particular alignment.