在 MS Visual Studio 2013 中,我可以使用什么来代替 std::aligned_alloc? [英] What can I use instead of std::aligned_alloc in MS Visual Studio 2013?

查看:23
本文介绍了在 MS Visual Studio 2013 中,我可以使用什么来代替 std::aligned_alloc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 C++11 的 std::aligned_alloc,但不幸的是它不适用于 Microsoft Visual Studio 2013.

I would like to use C++11's std::aligned_alloc, but unfortunately it isn't available with Microsoft Visual Studio 2013.

我正在考虑,intsead,自己实现 aligned_alloc.一个实现应该是什么样的?以下示例无法编译,因为它无法从 void* 转换为 void*&.

I'm considering, intsead, implementing aligned_alloc on my own. How should an implementation look like? The following for example doesn't compile, because it cannot convert from void* to void*&.

 template<typename T>
 T* aligned_alloc( std::size_t size, std::size_t align )
 {
        T* ptr = new T[size + align];
        std::align(align, size, reinterpret_cast<void*>(ptr), align + size);
        return ptr;
 }

推荐答案

免责声明:我没有彻底测试此代码.

Disclaimer: I didn't thoroughly test this code.

void* aligned_alloc(std::size_t size, std::size_t alignment){
    if(alignment < alignof(void*)) { 
        alignment = alignof(void*); 
    }
    std::size_t space = size + alignment - 1;
    void* allocated_mem = ::operator new(space + sizeof(void*));
    void* aligned_mem = static_cast<void*>(static_cast<char*>(allocated_mem) + sizeof(void*)); 
    ////////////// #1 ///////////////
    std::align(alignment, size, aligned_mem, space);
    ////////////// #2 ///////////////
    *(static_cast<void**>(aligned_mem) - 1) = allocated_mem;
    ////////////// #3 ///////////////
    return aligned_mem;
}

void aligned_free(void* p) noexcept {
    ::operator delete(*(static_cast<void**>(p) - 1));
}

说明:

如果小于这个值,对齐将调整为 alignof(void*),因为,正如我们将看到的,我们需要存储一个(正确对齐的)void*>.

The alignment is adjusted to alignof(void*) if it's less than that, because, as we will see, we need to store a (properly aligned) void*.

我们需要 size + alignment - 1 字节以确保我们可以在其中找到具有正确对齐的 size 字节块,以及额外的 sizeof(void*) 字节来存储 ::operator new 返回的指针,以便我们稍后可以释放它.

We need size + alignment - 1 bytes to ensure that we can find a size byte block in there with the right alignment, plus an additional sizeof(void*) bytes to store the pointer returned by ::operator new so that we can free it later.

我们用::operator new 分配这块内存,并将返回的指针存储在allocated_mem 中.然后我们将 sizeof(void*) 字节添加到 allocated_mem 并将结果存储在 aligned_mem 中.在这一点上,我们还没有对齐.

We allocate this memory with ::operator new and store the returned pointer in allocated_mem. We then add sizeof(void*) bytes to allocated_mem and store the result in aligned_mem. At this point, we haven't aligned it yet.

在点 #1,内存块和两个点看起来像这样:

At point #1, the memory block and the two points look like this:

              aligned_mem (not actually aligned yet)
              V
+-------------+-----------------------------------------+
|sizeof(void*)|    size + alignment - 1  bytes          |
+-------------+-----------------------------------------+
^
allocated_mem points here

std::align 调用调整 aligned_mem 以获得所需的对齐方式.在第 2 点,它现在看起来像这样:

The std::align call adjusts aligned_mem to obtain the desired alignment. At point #2, it now looks like this:

                      aligned_mem (correctly aligned now)
                      V
+---------------------+---------------------------------+
| extra space         |  at least size bytes            |
+---------------------+---------------------------------+
^
allocated_mem points here

因为我们从 sizeof(void*) 字节开始,超过 allocated_mem,所以额外空间"至少是 sizeof(void*)字节.此外,aligned_memvoid* 正确对齐,因此我们可以在它之前存储一个 void*.在第 3 点,内存块看起来像这样

Because we started at sizeof(void*) bytes past allocated_mem, the "extra space" is at least sizeof(void*) bytes. Moreover, aligned_mem is correctly aligned for void*, so we can store a void* right before it. At point #3, the block of memory looks like this

                      aligned_mem (returned to caller)
                      V
+---------------+-----+---------------------------------+
|               |  ^  |  at least size bytes            |
+---------------+--+--+---------------------------------+
^                  |
allocated_mem      value of allocated_mem
points here        stored here

对于aligned_free,它只是读取存储在那里的指针并将其传递给::operator delete.

As to aligned_free, it simply reads the pointer stored there and passes it to ::operator delete.

这篇关于在 MS Visual Studio 2013 中,我可以使用什么来代替 std::aligned_alloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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