为什么大多数平台上都没有'aligned_realloc'? [英] Why is there no 'aligned_realloc' on most platforms?

查看:278
本文介绍了为什么大多数平台上都没有'aligned_realloc'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSVC具有自己的非标准函数 _aligned_malloc _aligned_realloc _aligned_free

MSVC has its own non-standard functions _aligned_malloc, _aligned_realloc and _aligned_free.

C ++ 17和C11引入了(std::) aligned_alloc ,结果其中的 de 可以通过免费重新分配进行分配。但是 realloc 不能用于重新分配 aligned_alloc 返回的内存,因为它确实不使用 alignment 参数,因此不能保证返回的指针将正确对齐。

C++17 and C11 have introduced (std::)aligned_alloc, results of which can be deallocated with free or realloc. But realloc cannot be used to actually reallocate memory returned by aligned_alloc, since it does not take an alignment parameter and thus cannot guarantee that the returned pointer will be properly aligned.

我什至找不到任何非标准扩展可以在Microsoft Windows / Visual C ++以外的平台上重新分配对齐的内存(保留对齐)。

I can't even find any non-standard extensions that could reallocate aligned memory (preserving its alignment) on platforms other than Microsoft Windows / Visual C++.

我搜索错误,或者确实没有<$ POSIX和其他平台上的c $ c> _aligned_realloc 替代方案?

Am I searching for it wrong, or is there indeed no _aligned_realloc alternative on POSIX and other platforms?

如果是,


  1. 为什么?

  2. 在那些平台上可以代替使用什么?没有什么比使用新的对齐方式调用 aligned_alloc 然后执行 memcpy free更好的了获得成功的旧指针了?

  1. Why?
  2. What can be used instead on those platforms? Is there nothing better than calling aligned_alloc with the new alignment, and then doing memcpy and freeing the old pointer on success?


推荐答案

而POSIX(在大多数平台上往往充当最低公分母)没有 aligned_realloc ,但确实有 aligned_alloc memcpy 。因此,您可以非常容易地实现自己的 aligned_realloc ,该保证可在任何使用posix兼容的合理平台上使用。但是,请注意,没有posix标准方法来获取 malloc d内存区域的大小。

While POSIX (which tends to act as a lowest common denominator on most platforms) does not have an aligned_realloc, it does have aligned_alloc and memcpy. Therefore you can very easily implement your own aligned_realloc which is guaranteed to work on any reasonably posix compliant platform using these. However, note that there is not a posix standard method to get the size of a malloc'd region of memory. You'll have to track that yourself.

编辑:有一些空闲时间,所以我将其扩展为回答最常见的批评

have a bit of free time so I'm extending this to answer the most common criticism

正如精明的评论者所指出的,我所建议的是 realloc 在内部如何工作。

What I've proposed is, as the astute commenter will note, not how realloc works internally.

在后台,您的标准 realloc 实现将尽最大努力避免执行上述的mallocing和memcpying行为,然后免费提供。在求助于回退之前,它将尝试使用两种行为之一。
1)如果新大小小于旧大小,它将在适当的位置调整内存大小,避免分配,复制或释放内存。
2)如果新大小大于旧大小,它将(以简化的方式)查看是否存在足够大的空闲内存,如果是,它将吞噬该内存并适当调整大小。

Under the hood, your standard realloc implementation will do its damnedest to avoid preforming the above behavior of mallocing and memcpying with a free afterwards. It will try to use one of two behaviors before resorting to the fallback. 1) If the new size is smaller than the old size, it will resize the memory in place, avoiding having to allocate, copy, or free. 2) if the new size is greater than the old size, it will (in simplified terms) see if there is free memory of sufficient size adjacent, and if so it will gobble up that memory and resize in place. If not, it resorts to the fallback.

我提出了一种幼稚的方法,因为我认为大多数提出这个问题的人都不想实现自己的malloc实现。 。 (尽管我强烈建议这样做是出于教育目的)

I proposed a naive approach, because I figured most people asking this question wouldn't want to have to implement their own malloc implementation. (Though I highly suggest doing such for educational purposes)

希望这可以满足所有投诉!

Hope this satisfies any complaints!

这篇关于为什么大多数平台上都没有'aligned_realloc'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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