realloc是否保留posix_memalign的内存对齐方式? [英] Does realloc keep the memory alignment of posix_memalign?

查看:153
本文介绍了realloc是否保留posix_memalign的内存对齐方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对齐 malloc posix_memalign ,没关系,但是如何对齐 realloc realloc 是否保留对齐方式或如何确保重新分配的内存具有相同的对齐方式?假设Linux和x86_64。

解决方案

不, realloc posix_memalign 不能由ISO或POSIX保证保持相同的对齐。一个 realloc may可以简单地扩展当前块在相同的地址,但它也可能将块移动到一个不同的地址,原始。



如果您想要相同的对齐方式,最好分配另一个块并复制数据。



<不幸的是,在Single UNIX Specification中没有 posix_memalign_realloc 函数。



如果你不希望每次复制数据的麻烦,你可以试试 realloc (a),if这种对齐不符合预期,然后调用 posix_memalign 来获得正确对齐的地址并将数据复制到那里,释放旧的地址完成。



这可能导致:


  • 零拷贝(如果当前块可以在原地扩展);
  • 一个副本(如果 realloc 副本但是h给你一个正确对齐的块);或

  • 两个副本(如果 realloc 副本,那么您也必须复制,因为未对齐)。


根据底层的内存管理实现,它可能会导致复制的数量少于指定的数量。例如,副本可能只是重新映射内存块,而不是物理地移动数据。

因此,您可能需要保留一些统计数据以查看该方案是否值得(a)请记住,POSIX和Linux手册页都不能指定是否或者甚至不能可以将这些指针传递给 realloc ,只有您可以将它们传递给 free p>

我的担心是它会正常分配内存(标准对齐)并返回一个偏移地址(即不是分配的实际地址,而是一个 N free> free 足够聪明,可以在编织它的魔法之前回到实际的地址。



这样做的一种方式是存储实际的添加即使在返回地址之前,尽管这当然会导致即使对于经常分配的浪费。



在这种情况下,空闲可能已经变得聪明了(因为规范说它必须能够处理由 posix_memalign 完成的分配),但 realloc

内存比需要的少,然后摆弄竞技场来释放前空间和后空间,以便返回的地址是一个真实地址,可由 free realloc



但是,如前所述,文档不能保证这一点。


Aligned malloc is posix_memalign, that's OK, but what about the aligned realloc? Does realloc retain the alignment or how to assure that reallocated memory has the same alignment? Assume Linux and x86_64.

解决方案

No, realloc on the memory returned from posix_memalign is not guaranteed by either ISO or POSIX to maintain the same alignment. A realloc may simply expand the current block at the same address but it may also move the block to a different address whose alignment is less strict than the original.

If you want the same alignment, it's probably best to allocate another block and copy the data over.

There is, unfortunately, no posix_memalign_realloc function in the Single UNIX Specification either.

If you don't want to go through the hassle of copying data every time, you could try the realloc (a) and, if the alignment of that was not as expected, then and only then call posix_memalign to get a correctly aligned address and copy the data in to there, freeing the old address when done.

This may result in:

  • zero copies (if the current block can be expanded in-place);
  • one copy (if realloc copies but happens to give you a correctly aligned block); or
  • two copies (if realloc copies and then you also have to copy due to misalignment).

It may also result in less copying than indicated depending on the underlying memory management implementation. For example, a "copy" may simply involve remapping memory blocks rather than physically moving the data.

So you may want to keep some statistics to see if this scheme is worthwhile.


(a) Just keep in mind that neither POSIX nor Linux man pages specify whether or not you even can pass these pointers to realloc, only that you can pass them to free.

However, based on the current GNU libc source code, it appears to work, although that's no guarantee it will continue to work in future :-)

My fear was that it would allocate memory normally (standard alignment) and pass back an offset address (ie, not the actaul address allocated, but one N bytes beyond that) which free was intelligent enough to turn back into the actual address before weaving its magic.

One way of doing that would be to store the actual address immediately before the returned address though this of course would lead to wastage even for regular allocations.

In that case, free may have been made intelligent (since the specs say it must be able to handle the allocations done by posix_memalign) but realloc may not have been given the same intelligence (since the docs are silent on that matter).

However, based on GNU glibc 2.14.1, it actually allocates more memory than needed then fiddles with the arena to free up the pre-space and post-space, so that the address returned is a "real" address, usable by free or realloc.

But, as stated, the documentation doesn't guarantee this.

这篇关于realloc是否保留posix_memalign的内存对齐方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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