macos OSX 缺少 memalign
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/196329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OSX lacks memalign
提问by user17925
I'm working on a project in C and it requires memalign(). Really, posix_memalign() would do as well, but darwin/OSX lacks both of them.
我正在用 C 做一个项目,它需要 memalign()。确实, posix_memalign() 也可以,但是 darwin/OSX 两者都没有。
What is a good solution to shoehorn-in memalign? I don't understand the licensing for posix-C code if I were to rip off memalign.c and put it in my project- I don't want any viral-type licensing LGPL-ing my whole project.
什么是鞋拔式 memalign 的好解决方案?如果我要撕掉 memalign.c 并将其放入我的项目中,我不了解 posix-C 代码的许可 - 我不希望我的整个项目获得任何病毒式许可 LGPL。
采纳答案by paxos1977
Mac OS X appears to be 16-byte mem aligned.
Mac OS X 似乎是16 字节内存对齐的。
Quote from the website:
来自网站的报价:
I had a hard time finding a definitive statement on MacOS X memory alignment so I did my own tests. On 10.4/intel, both stack and heap memory is 16 byte aligned. So people porting software can stop looking for memalign() and posix_memalign(). It's not needed.
我很难找到关于 MacOS X 内存对齐的明确声明,所以我做了自己的测试。在 10.4/intel 上,堆栈和堆内存都是 16 字节对齐的。所以人们移植软件可以停止寻找 memalign() 和 posix_memalign()。不需要。
回答by fearless_fool
update: OSX now has posix_memalign()
更新:OSX 现在有 posix_memalign()
Late to the party, but newer versions of OSX dohave posix_memalign()
. You might want this when aligning to page boundaries. For example:
迟到了,但较新版本的 OSX确实有posix_memalign()
. 在对齐页面边界时,您可能需要这样做。例如:
#include <stdlib.h>
char *buffer;
int pagesize;
pagesize = sysconf(_SC_PAGE_SIZE);
if (pagesize == -1) handle_error("sysconf");
if (posix_memalign((void **)&buffer, pagesize, 4 * pagesize) != 0) {
handle_error("posix_memalign");
}
One thing to note is that, unlike memalign()
, posix_memalign()
takes **buffer
as an argument and returns an integer error code.
需要注意的一件事是,与 不同memalign()
,posix_memalign()
它接受**buffer
一个参数并返回一个整数错误代码。
回答by fearless_fool
Should be easy enough to do yourself, no? Something like the following (not tested):
自己做应该很容易吧?类似于以下内容(未测试):
void *aligned_malloc( size_t size, int align )
{
void *mem = malloc( size + (align-1) + sizeof(void*) );
char *amem = ((char*)mem) + sizeof(void*);
amem += align - ((uintptr)amem & (align - 1));
((void**)amem)[-1] = mem;
return amem;
}
void aligned_free( void *mem )
{
free( ((void**)mem)[-1] );
}
(thanks Jonathan Leffler)
(感谢乔纳森莱夫勒)
Edit:Regarding ripping off another memalign implementation, the problem with that is not licensing. Rather, you'd run into the difficulty that any good memalign implementation will be an integral part of the heap-manager codebase, not simply layered on top of malloc/free. So you'd have serious trouble transplanting it to a different heap-manager, especially when you have no access to it's internals.
编辑:关于窃取另一个 memalign 实现,问题不在于许可。相反,您会遇到困难,即任何好的 memalign 实现都将成为堆管理器代码库的一个组成部分,而不是简单地分层在 malloc/free 之上。因此,将其移植到不同的堆管理器时会遇到严重的麻烦,尤其是当您无法访问它的内部结构时。
回答by Jonathan Leffler
Why does the software you are porting need memalign() or posix_memalign()? Does it use it for alignments bigger than the 16-byte alignments referenced by austirg?
为什么您要移植的软件需要 memalign() 或 posix_memalign()?它是否将它用于比 austirg 引用的 16 字节对齐更大的对齐?
I see Mike F posted some code - it looks relatively neat, though I think the while loop may be sub-optimal (if the alignment required is 1KB, it could iterate quite a few times).
我看到 Mike F 发布了一些代码 - 它看起来相对整洁,但我认为 while 循环可能不是最佳的(如果所需的对齐是 1KB,它可以迭代很多次)。
Doesn't:
不会:
amem += align - ((uintptr)amem & (align - 1));
get there in one operation?
一次手术就能到达那里?
回答by paxos1977
From the macosx man pages:
从 macosx 手册页:
The malloc(), calloc(), valloc(), realloc(), and reallocf() functions allocate memory. The allocated memory is aligned such that it can be used for any data type, including AltiVec- and SSE-related types. The free() function frees allocations that were created via the preceding allocation functions.
malloc()、calloc()、valloc()、realloc() 和 reallocf() 函数分配内存。分配的内存是对齐的,因此它可以用于任何数据类型,包括与 AltiVec 和 SSE 相关的类型。free() 函数释放通过前面的分配函数创建的分配。
回答by paxos1977
回答by Ted
Yes Mac OS X does have 16 Byte memory alignmentin the ABI. You should not need to use memalign(). If you memory requirements are a factor of 16 then I would not implement it and maybe just add an assert.
是的 Mac OS X在ABI中有 16 字节内存对齐。您不应该需要使用 memalign()。如果您的内存要求是 16 倍,那么我不会实现它,也许只是添加一个断言。
回答by Dark Shikari
If you need an arbitrarily aligned malloc, check out x264's malloc (common/common.c in the git repository), which has a custom memalign for systems without malloc.h. Its extremely trivial code, to the point where I would not even consider it copyrightable, but you should easily be able to implement your own after seeing it.
如果您需要任意对齐的 malloc,请查看 x264 的 malloc(git 存储库中的 common/common.c),它为没有 malloc.h 的系统提供了自定义的 memalign。它非常琐碎的代码,以至于我什至不认为它具有版权,但是您应该可以在看到它后轻松实现自己的代码。
Of course, if you only need 16-byte alignment, as stated above, its in the OS X ABI.
当然,如果你只需要 16 字节对齐,如上所述,它在 OS X ABI 中。
回答by meh
Thanks for the help, guys... helped in my case (OpenCascade src/Image/Image_PixMap.cxx, OSX10.5.8 PPC)
感谢您的帮助,伙计们......在我的情况下有所帮助(OpenCascade src/Image/Image_PixMap.cxx,OSX10.5.8 PPC)
Combined with the answers above, this might save someone some digging around or instill hope if not particularly familiar with malloc, etc.:
结合上面的答案,如果不是特别熟悉 malloc 等,这可能会节省一些人的挖掘时间或灌输希望:
The rather large project I'm building only had one reference to posix_memalign, and it turns out it was the result of a bunch of preprocessor conditions that didn't include OSX but DID include BORLANDC, which confirms what others suggested about it being safe to use malloc in some cases:
我正在构建的相当大的项目只有一个对 posix_memalign 的引用,结果证明这是一堆不包括 OSX 但确实包括 BORLANDC 的预处理器条件的结果,这证实了其他人关于它安全的建议在某些情况下使用 malloc:
#if defined(_MSC_VER)
return (TypePtr )_aligned_malloc (theBytesCount, theAlign);
#elif (defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 1)
return (TypePtr ) _mm_malloc (theBytesCount, theAlign);
#elif defined(__BORLANDC__)
return (TypePtr ) malloc (theBytesCount);
#else
void* aPtr;
if (posix_memalign (&aPtr, theAlign, theBytesCount))
{
aPtr = NULL;
}
return (TypePtr )aPtr;
#endif
So, it could be as simple as just using malloc, as suggested by others.
因此,正如其他人所建议的那样,它可以像使用 malloc 一样简单。
e.g. here: moving __BORLANDC__
condition above __GNUC__
and adding APPLE:
例如在这里:移动__BORLANDC__
上面的条件__GNUC__
并添加APPLE:
#elif (defined(__BORLANDC__) || defined(__APPLE__)) //now above `__GNUC__`
NOTE: I did NOTcheck that BORLANDC uses 16-byte alignment like someone above stated OS X does. Nor did I verify that PPC OS X does. However, this usage suggests that this alignment isn't particularly important. (Here's hoping it works, and that it could be that easy for you searchers, as well!)
注:我没有不检查BORLANDC使用16字节对齐好像有人如上所述OS X一样。我也没有验证 PPC OS X 是否可以。但是,这种用法表明这种对齐并不是特别重要。(希望它有效,并且对您的搜索者来说也很容易!)