最好的跨平台的方法来得到对齐的内存 [英] best cross-platform method to get aligned memory

查看:227
本文介绍了最好的跨平台的方法来得到对齐的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是code我通常用它来获得对齐的内存与Visual Studio和GCC

Here is the code I normally use to get aligned memory with Visual Studio and GCC

inline void* aligned_malloc(size_t size, size_t align) {
    void *result;
    #ifdef _MSC_VER 
    result = _aligned_malloc(size, align);
    #else 
     if(posix_memalign(&result, align, size)) result = 0;
    #endif
    return result;
}

inline void aligned_free(void *ptr) {
    #ifdef _MSC_VER 
        _aligned_free(ptr);
    #else 
      free(ptr);
    #endif

}

时,一般这code罚款?我也看到有人用 _mm_malloc,_mm_free 。在这我要对齐的内存大多数情况下,它的使用SSE / AVX。我可以使用这些功能有什么看法?这将让我的code简单得多。

Is this code fine in general? I have also seen people use _mm_malloc, _mm_free . In most cases that I want aligned memory it's to use SSE/AVX. Can I use those functions in general? It would make my code a lot simpler.

最后,可以很容易地创建自己的功能来调整内存(见下文)。那么,为什么有这么多不同的常用功能得到对准内存(其中许多只在一个平台上工作)?

Lastly, it's easy to create my own function to align memory (see below). Why then are there so many different common functions to get aligned memory (many of which only work on one platform)?

这code做16位对齐。

This code does 16 bit alignment.

float* array = (float*)malloc(SIZE*sizeof(float)+15);

// find the aligned position
// and use this pointer to read or write data into array
float* alignedArray = (float*)(((unsigned long)array + 15) & (~0x0F));

// dellocate memory original "array", NOT alignedArray
free(array);
array = alignedArray = 0;

请参阅:<一href=\"http://www.songho.ca/misc/alignment/dataalign.html\">http://www.songho.ca/misc/alignment/dataalign.html和
<一href=\"http://stackoverflow.com/questions/227897/solve-the-memory-alignment-in-c-interview-question-that-stumped-me\">Solve用C面试问题内存对齐是难倒我

编辑:
万一有人关心,我的想法从我的本征aligned_malloc()函数(艾根/ src目录/核心/ UTIL / Memory.h)

In case anyone cares, I got the idea for my aligned_malloc() function from Eigen (Eigen/src/Core/util/Memory.h)

编辑:
我刚刚发现, posix_memalign 是未定义的MinGW。然而, _mm_malloc 适用于Visual Studio的2012年,海湾合作委员会,MinGW的,而英特尔C ++编译器,它似乎是一般最方便的解决方案。

I just discovered that posix_memalign is undefined for MinGW. However, _mm_malloc works for Visual Studio 2012, GCC, MinGW, and the Intel C++ compiler so it seems to be the most convenient solution in general.

推荐答案

您提出的第一个功能确实会正常工作。

The first function you propose would indeed work fine.

您的自制的功能也适用,但如果值已经对齐,你刚才浪费了15个字节的缺点。可能并不重要,有时,但操作系统可能是能够提供不带任何浪费正确分配(内存,如果它需要对齐到256或4096个字节,你可能加入对准-1浪费了大量的内存字节)。

Your "homebrew" function also works, but has the drawback that if the value is already aligned, you have just wasted 15 bytes. May not matter sometimes, but the OS may well be able to provide memory that is correctly allocated without any waste (and if it needs to be aligned to 256 or 4096 bytes, you risk wasting a lot of memory by adding "alignment-1" bytes).

这篇关于最好的跨平台的方法来得到对齐的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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