我们什么时候需要使用代替的malloc posix_memalign? [英] When do we need to use posix_memalign instead of malloc?

查看:2549
本文介绍了我们什么时候需要使用代替的malloc posix_memalign?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像 posix_memalign 让你选择一个自定义的调整,但有此必要?

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

的malloc 已经做好内部的调整工作。

malloc has already done the alignment work internally.

更新

确切的原因,我问这个是因为我看到nginx的做到这一点, ngx_memalign(NGX_POOL_ALIGNMENT,大小,日志); ,这里 NGX_POOL_ALIGNMENT 定义为 16 ,<一个href="http://nginxs.google$c$c.com/svn-history/trunk/src/core/ngx_palloc.c">nginxs.google$c$c.com/svn-history/trunk/src/core/ngx_palloc.c

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

推荐答案

基本上,如果你需要比malloc的强硬对准会给你。 malloc的一般返回一个指针对准这样的,它可与任何原始类型(通常,8个字节常见的台式机)。

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).

不过,有时你需要存储在其他边界上对齐,例如4K对齐,等等。在这种情况下,你需要 memalign可

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,

  • 写入内存管理器时(如垃圾收集器)。在这种情况下,有时方便的与存储器上较大的块对准工作。这种方式,可以在所分配的区域的底部在给定块中存储的元数据共同所有的对象,并简单地通过屏蔽对象指针的至少显著位访问此
  • 当与硬件接口(从来没有这样做我自己,但IIRC,某些种类的块设备需要对齐的内存)。详情请参见N.M.的答案。

这篇关于我们什么时候需要使用代替的malloc posix_memalign?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆