0

假设我有一个共享文件映射对象:

HANDLE hFileMapping = ::CreateFileMapping(INVALID_HANDLE_VALUE, &sa,
    PAGE_READWRITE, 0, 0x8000, L"MyObjectName");

我得到了一小部分供查看,如下所示:

BYTE* pData = (BYTE*)::MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0x10);
if(!pData) return false;   //fail
DWORD dwcbFullSize = *(DWORD*)(pData + 0xC);

那么,如果我需要分配更多数据,是否可以在MapViewOfFile不先取消映射的情况下再次调用pData

BYTE* pFullData = (BYTE*)::MapViewOfFile(hFileMapping,
      FILE_MAP_READ, 0, 0, dwcbFullSize);

PS。我的目标不是在映射整个 32K 共享内存段时浪费 CPU 周期,因为我可能需要读取的内容可能远少于此。

4

2 回答 2

1

SEC_RESERVE当我们创建文件映射(部分)时,此任务需要使用属性

如果文件映射对象由操作系统分页文件支持(hfile参数为INVALID_HANDLE_VALUE),则指定当文件的视图映射到进程地址空间时,保留整个页面范围供进程稍后使用而不是承诺。保留的页面可以在随后的VirtualAlloc函数调用中提交。提交页面后,无法使用该VirtualFree功能释放或取消提交页面。此属性对由可执行图像文件或数据文件支持的文件映射对象没有影响(hfile参数是文件句柄)。 SEC_RESERVE不能与SEC_COMMIT结合使用。

所以在创建具有SEC_RESERVE属性的部分后需要一次调用MapViewOfFile- 此调用不保留内存范围但不提交页面。提交页面需要调用VirtualAlloc。最后我们可以通过调用释放所有内存UnmapViewOfFile

void DoDemo(ULONG cb)
{
    if (!cb)
    {
        return;
    }

    SYSTEM_INFO si;
    GetSystemInfo(&si);

    cb = (cb + (si.dwPageSize - 1)) & ~(si.dwPageSize - 1);

    if (HANDLE hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE, 0,
        PAGE_READWRITE|SEC_RESERVE, 0, cb, L"MyObjectName"))
    {
        // reserve address space with PAGE_READWRITE initial protection

        PVOID BaseAddress = MapViewOfFile(hFileMapping, 
            FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, cb);

        // hFileMapping no more need
        CloseHandle(hFileMapping);

        if (BaseAddress)
        {
            // check, for test only
            ::MEMORY_BASIC_INFORMATION mbi;
            if (VirtualQuery(BaseAddress, &mbi, sizeof(mbi)) < sizeof(mbi) ||
                mbi.Type != MEM_MAPPED || mbi.State != MEM_RESERVE)
            {
                __debugbreak();
            }

            // map page by page
            PBYTE pb = (BYTE*)BaseAddress;
            do 
            {
                if (!VirtualAlloc(pb, si.dwPageSize, MEM_COMMIT, PAGE_READWRITE))
                {
                    GetLastError();
                    break;
                }
                *pb = '*';//write something
            } while (pb += si.dwPageSize, cb -= si.dwPageSize);

            //unmap all
            UnmapViewOfFile(BaseAddress);
        }
    }
}

然而,所有这些都只对大尺寸部分有意义。对于 32kb(小尺寸),最好的方法是在一次调用中映射所有页面

于 2017-10-29T01:31:08.540 回答
1

我有可能误解了这个问题,所以请多多包涵。我已经决定使用一些工作代码更容易在评论中更清楚地显示我所说的内容。@OP,也许从这里您可以进一步澄清这是否不能解决您的问题?

因此,我采用了 MSFT 提供的示例代码并一起破解来演示我在说什么(所以代码本质上是 MS 示例代码)。

https://msdn.microsoft.com/en-us/library/windows/desktop/aa366551(v=vs.85).aspx

您可以使用 2 个项目创建 VS 解决方案,这是项目/流程 A 的代码:

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>

#define BUF_SIZE 256
TCHAR szName[] = TEXT("Global\\MyFileMappingObject");
TCHAR szMsg[] = TEXT("Message from first process.");

int _tmain()
{
    HANDLE hMapFile;
    LPCTSTR pBuf;

    hMapFile = CreateFileMapping(
        INVALID_HANDLE_VALUE,    // use paging file
        NULL,                    // default security
        PAGE_READWRITE,          // read/write access
        0,                       // maximum object size (high-order DWORD)
        BUF_SIZE,                // maximum object size (low-order DWORD)
        szName);                 // name of mapping object

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).\n"),
            GetLastError());
        return 1;
    }
    pBuf = (LPTSTR)MapViewOfFile(hMapFile,   // handle to map object
        FILE_MAP_ALL_ACCESS, // read/write permission
        0,
        0,
        BUF_SIZE);

    if (pBuf == NULL)
    {
        _tprintf(TEXT("Could not map view of file (%d).\n"),
            GetLastError());

        CloseHandle(hMapFile);

        return 1;
    }


    CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
    _getch();

    UnmapViewOfFile(pBuf);

    CloseHandle(hMapFile);

    return 0;
}

现在这是第二个项目/流程 B 的一些修改代码。请参阅此内容以供参考:https ://msdn.microsoft.com/en-us/library/windows/desktop/aa366548(v=vs.85).aspx

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")

#define BUF_SIZE 256
TCHAR szName[] = TEXT("Global\\MyFileMappingObject");

int mapDataAtOffset(DWORD offset, size_t bytesToRead, LPVOID* outData, LPVOID* outMapAddress);

int _tmain()
{
    LPCTSTR pBuf;
    LPVOID outMapAddress = nullptr;
    LPVOID outMapAddress2 = nullptr;

    auto ret = mapDataAtOffset(0, 8, (LPVOID*)&pBuf, &outMapAddress);

    MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);

    ret = mapDataAtOffset(8, 8, (LPVOID*)&pBuf, &outMapAddress2);

    MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);

    if(outMapAddress)UnmapViewOfFile(outMapAddress);
    if (outMapAddress2)UnmapViewOfFile(outMapAddress2);

    return 0;
}

int mapDataAtOffset(DWORD offset, size_t bytesToRead, LPVOID* outData, LPVOID* outMapAddress) {
    HANDLE hMapFile;      // handle for the file's memory-mapped region
    BOOL bFlag;           // a result holder
    DWORD dBytesWritten;  // number of bytes written
    DWORD dwFileMapSize;  // size of the file mapping
    DWORD dwMapViewSize;  // the size of the view
    DWORD dwFileMapStart; // where to start the file map view
    DWORD dwSysGran;      // system allocation granularity
    SYSTEM_INFO SysInfo;  // system information; used to get granularity
    LPVOID lpMapAddress;  // pointer to the base address of the
                          // memory-mapped region
    char * pData;         // pointer to the data
    int i;                // loop counter
    int iData;            // on success contains the first int of data
    int iViewDelta;       // the offset into the view where the data
                          //shows up

    DWORD FILE_MAP_START = offset;

    // Get the system allocation granularity.
    GetSystemInfo(&SysInfo);
    dwSysGran = SysInfo.dwAllocationGranularity;

    // Now calculate a few variables. Calculate the file offsets as
    // 64-bit values, and then get the low-order 32 bits for the
    // function calls.

    // To calculate where to start the file mapping, round down the
    // offset of the data into the file to the nearest multiple of the
    // system allocation granularity.
    dwFileMapStart = (FILE_MAP_START / dwSysGran) * dwSysGran;
    _tprintf(TEXT("The file map view starts at %ld bytes into the file.\n"),
        dwFileMapStart);

    // Calculate the size of the file mapping view.
    dwMapViewSize = (FILE_MAP_START % dwSysGran) + bytesToRead;
    _tprintf(TEXT("The file map view is %ld bytes large.\n"),
        dwMapViewSize);

    // How large will the file mapping object be?
    dwFileMapSize = FILE_MAP_START + bytesToRead;
    _tprintf(TEXT("The file mapping object is %ld bytes large.\n"),
        dwFileMapSize);

    // The data of interest isn't at the beginning of the
    // view, so determine how far into the view to set the pointer.
    iViewDelta = FILE_MAP_START - dwFileMapStart;
    _tprintf(TEXT("The data is %d bytes into the view.\n"),
        iViewDelta);

    hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS,   // read/write access
        FALSE,                 // do not inherit the name
        szName);               // name of mapping object

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not open file mapping object (%d).\n"),
            GetLastError());
        return 1;
    }

    // Map the view and test the results.

    lpMapAddress = MapViewOfFile(hMapFile,            // handle to
                                                      // mapping object
        FILE_MAP_ALL_ACCESS, // read/write
        0,                   // high-order 32
                             // bits of file
                             // offset
        dwFileMapStart,      // low-order 32
                             // bits of file
                             // offset
        dwMapViewSize);      // number of bytes
                             // to map
    if (lpMapAddress == NULL)
    {
        _tprintf(TEXT("lpMapAddress is NULL: last error: %d\n"), GetLastError());
        return 3;
    }

    // Calculate the pointer to the data.
    pData = (char *)lpMapAddress + iViewDelta;
    *outData = pData;
    *outMapAddress = lpMapAddress;

    CloseHandle(hMapFile); // close the file mapping object, doesn't matter as long as view is still mapped
    return 0;
}

所以回答你的问题

在没有首先取消映射 pData 的情况下再次调用 MapViewOfFile 是否可以接受?

上面的例子实现了这一点。除非我误解了您的查询,在这种情况下,如果您能澄清一下,那就太好了。

于 2017-10-29T18:16:01.277 回答