MemoryMappedFileWinImpl.cc 4 KB
Newer Older
1 2 3 4 5
/***********************************************************************
* Copyright 2011-2014 Computer Graphics Group RWTH Aachen University. *
* All rights reserved.                                                *
* Distributed under the terms of the MIT License (see LICENSE.TXT).   *
**********************************************************************/
Martin Schultz's avatar
Martin Schultz committed
6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#ifdef _WIN32
#include "ACGL/Utils/MemoryMappedFileWinImpl.hh"

MemoryMappedFileWinImpl::MemoryMappedFileWinImpl(
    const char* _fileName, 
    ACGL::Utils::MemoryMappedFile::accessMode _accessMode, 
    ACGL::Utils::MemoryMappedFile::shareMode _shareMode, 
    size_t _length, 
    off_t _offset) :
    mErrorCode(0),
    mPageOffset(0),
    mFileHandle(nullptr),
    mFileMappingHandle(NULL)
{
    DWORD fileAccess = 0;
    DWORD fileShare = 0;
    DWORD fileSize = 0;
24
	(void)fileSize;
25 26 27 28 29 30 31 32 33 34 35 36 37 38
    DWORD offsetLow = 0;
    DWORD offsetHigh = 0;

    DWORD mapAccess = 0;

    DWORD mapViewAccess = 0;
    DWORD mapSize = 0;

    /////////////////////////////////////////////////////
    //
    // initialize flags
    //
    /////////////////////////////////////////////////////

39
    if (_shareMode == ACGL::Utils::MemoryMappedFile::MAPPING_SHARED)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    {
        fileShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
                
    }

    
    if (_accessMode == ACGL::Utils::MemoryMappedFile::READ_ONLY)
    {
        //msdn example says if we want to read the file we need to specify share_read
        fileAccess = GENERIC_READ;
        fileShare |= FILE_SHARE_READ;
        mapAccess = PAGE_READONLY;
        mapViewAccess = FILE_MAP_READ;
    }
    else
    {
        fileAccess = GENERIC_READ | GENERIC_WRITE;
        mapAccess = PAGE_READWRITE;
        mapViewAccess = FILE_MAP_READ | FILE_MAP_WRITE;
    }

    //////////////////////
    //
    // open the file
    //
    //////////////////////
66
    mFileHandle = CreateFileA(_fileName, fileAccess, fileShare,
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    
    if (mFileHandle == INVALID_HANDLE_VALUE)
    {
        std::cout << _fileName << std::endl;
        mErrorCode = GetLastError();
        return;
    }
    
        //determine the size of the mapped view
       // fileSize = GetFileSize(mFileHandle, NULL);
        mapSize = GetFileSize(mFileHandle, NULL);
        if (_offset > 0)
        {
            offsetLow = _offset;
            offsetHigh = ((_offset >> 16)>>16);            
            SYSTEM_INFO info;
            GetSystemInfo(&info);
            mPageOffset = _offset % info.dwAllocationGranularity;
86
            mapSize -= (_offset);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        }
        if (_length > 0 )
        {
            mapSize = _length;
        }

        //create the file mapping handle
        mFileMappingHandle = CreateFileMapping(mFileHandle, NULL, mapAccess, 0, 0, NULL);
        if (mFileMappingHandle == NULL)
        {
            mErrorCode = GetLastError();
            return;
        }
        //create the file mapping view
        mpData = reinterpret_cast<PCHAR>
102
            (MapViewOfFile(mFileMappingHandle, mapViewAccess, offsetHigh, offsetLow, mapSize));
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        if (!mpData)
        {
            mErrorCode = GetLastError();
            return;
        }
        mLength = mapSize;
}
char * MemoryMappedFileWinImpl::data()
{ 
    return mpData; 
}
const char * MemoryMappedFileWinImpl::data() const
{ 
    return mpData; 
}
int MemoryMappedFileWinImpl::errorCode() 
{ 
    return mErrorCode; 
}
int MemoryMappedFileWinImpl::errorCode() const
{ 
    return mErrorCode; 
}
off_t MemoryMappedFileWinImpl::pageOffset()
{ 
    return mPageOffset; 
}
off_t MemoryMappedFileWinImpl::pageOffset() const
{ 
    return mPageOffset; 
}
off_t MemoryMappedFileWinImpl::length()
{ 
    return mLength; 
}
off_t MemoryMappedFileWinImpl::length() const
{ 
    return mLength; 
}
MemoryMappedFileWinImpl::~MemoryMappedFileWinImpl()
{
    if (mpData)
        UnmapViewOfFile(mpData);
    if (mFileMappingHandle)
        CloseHandle(mFileMappingHandle);
    if (mFileHandle != INVALID_HANDLE_VALUE)
        CloseHandle(mFileHandle);
}
#endif       
152