Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do we need to use posix_memalign instead of malloc?

Tags:

c

alignment

Seems posix_memalign let you choose a customized alignment,but when is that necessary?

malloc has already done the alignment work internally.

UPDATE

The exact reason I ask this is because I see nginx does this,ngx_memalign(NGX_POOL_ALIGNMENT, size, log);,here NGX_POOL_ALIGNMENT is defined as 16, nginxs.googlecode.com/svn-history/trunk/src/core/ngx_palloc.c

like image 560
compile-fan Avatar asked Jun 20 '11 11:06

compile-fan


4 Answers

The only benefits of posix_memalign, as far as I can tell, are:

  1. Allocating page-aligned (typically 4096 or larger alignment) memory for hardware-specific purposes.
  2. Evil hacks where you keep the low N bits of a pointer zero so you can store an N-bit integer in the low bits. :-)
like image 26
R.. GitHub STOP HELPING ICE Avatar answered Nov 18 '22 17:11

R.. GitHub STOP HELPING ICE


Basically, if you need tougher alignment than malloc will give you. Malloc generally returns a pointer aligned such, that it may be used with any of the primitive types (often, 8 bytes on common desktop machines).

However, sometimes you need memory aligned on other boundaries, for example 4K-aligned, etc. In this case, you would need memalign.

You would need this, for example,

  • when writing a memory manager (such as a garbage collector). In this case, it is sometimes handy to work with memory aligned on larger block sizes. This way, you can store meta-data common to all objects in a given block at the bottom of the allocated area, and access this simply by masking the least significant bits of the object pointer.
  • when interfacing with hardware (never done this myself, but IIRC, certain kinds of block-devices require aligned memory). See n.m.'s answer for details.
like image 52
Dirk Avatar answered Nov 18 '22 15:11

Dirk


Various hardware may have alignment requirements which malloc cannot satisfy. The Linux man page gives one such example, I quote:

On many systems there are alignment restrictions, e.g. on buffers used for direct block device I/O. POSIX specifies the pathconf(path,_PC_REC_XFER_ALIGN) call that tells what alignment is needed.

like image 2
n. 1.8e9-where's-my-share m. Avatar answered Nov 18 '22 17:11

n. 1.8e9-where's-my-share m.


A couple of uses:

  • Some processors have instructions that will only work on data that is aligned on a power of two greater than or equal to the buffer size - for example bit reverse addressing instructions used in ffts (fast fourier transforms).

  • To align data to cache boundaries to optimize access in multiprocessing applications so that data in the same cache line isn't being accessed by two processors simultaneously.

Basically, if you don't need to do absurd levels of optimizations and/or your hardware doesn't demand that an array be on a particular boundary then you can forget about posix_memalign.

like image 1
Dipstick Avatar answered Nov 18 '22 15:11

Dipstick