开发者

When is posix_memalign/memalign used? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Dupl开发者_如何学运维icate:

When do we need to use posix_memalign instead of malloc ?

When would you need "size bytes of memory such that the allocation's base address is an exact multiple of alignment" over just a simple malloc()?


I believe it is memalign() under Linux and posix_memalign() under Mac OS X/BSD.


One of the most common use cases is when working with SIMD code (SSE, AltiVec, AVX, Neon, etc) where you typically need all buffers to be aligned on a SIMD vector boundary (16 byte or 32 byte alignment).

Another common alignment requirement is virtual memory page alignment (4 kB typically).


One reason that popped up recently is to ensure that data buffer structs/objects do not share cache lines. This reduces the need for cache flushes.

Rgds, Martin


if you need strict memory alignment than what malloc offers.

malloc usually would return a pointer aligned in such away that it may be used with any of the primitive types (usually 8 bytes).

However, sometimes you need memory aligned on other boundaries, In this case, you would need memalign.


Some special instructions (eg. SSE instructions) require correct alignment of data in order to work. If you're developing in kernel mode, some DMA controllers also require correct alignment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜