Windows环形缓冲区,无需复制 [英] Windows ring buffer without copying

查看:114
本文介绍了Windows环形缓冲区,无需复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ring Buffer的Wikipedia条目上,有 UNIX 系统,通过该系统,与某块内存相邻的虚拟内存被映射到相同的phbysical内存,从而实现了环形缓冲区,而无需任何 memcpy 等我想知道 Windows 中是否有类似的方法?

On Ring Buffer's Wikipedia entry, there's example code showing a hack for UNIX systems whereby the adjacent virtual memory to a piece of memory is mapped to the same phbysical memory, thus implementing a ring buffer without the need for any memcpy, etc. I was wondering if there's a way to so something similar in Windows?

谢谢,弗雷泽

推荐答案

我并没有真正遵循维基百科中该示例的所有细节.考虑到这一点,您可以使用 CreateFileMapping a>和 MapViewOfFile ,但是MapViewOfFile不允许您可以指定映射的基地址. MapViewOfFileEx 可用于指定基本地址,因此也许您可以使用类似的技术.

I didn't really follow all the details of the example in wikipedia. With that in mind, you map memory in Windows using CreateFileMapping and MapViewOfFile, however MapViewOfFile does not allow you to specify a base address for the mapping. MapViewOfFileEx can be used to specify a base address so maybe you could use a similar technique.

我没有任何方法可以告诉您这是否真的有效:

I don't have any way of telling if this would actually work:

// determine valid buffer size
SYSTEM_INFO info;
GetSystemInfo(&info);

// note that the base address must be a multiple of the allocation granularity
DWORD bufferSize=info.dwAllocationGranularity;

HANDLE hMapFile = CreateFileMapping(
             INVALID_HANDLE_VALUE,
             NULL,
             PAGE_READWRITE,
             0,
             bufferSize*2,
             L"Mapping");

BYTE *pBuf = (BYTE*)MapViewOfFile(hMapFile,
                    FILE_MAP_ALL_ACCESS,
                    0,                   
                    0,                   
                    bufferSize);
MapViewOfFileEx(hMapFile,
                    FILE_MAP_ALL_ACCESS,
                    0,                   
                    0,                   
                    bufferSize,
                    pBuf+bufferSize);

这篇关于Windows环形缓冲区,无需复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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