void CMappedInFile::MapToMemory (const std::wstring& fileName)
{
    file = CreateFile ( fileName.c_str()
                      , GENERIC_READ
                      , FILE_SHARE_READ
                      , NULL
                      , OPEN_EXISTING
                      , FILE_ATTRIBUTE_NORMAL
                      , NULL);
    if (file == INVALID_HANDLE_VALUE)
        return;

    LARGE_INTEGER fileSize;
    fileSize.QuadPart = 0;
    GetFileSizeEx (file, &fileSize);
    size = (size_t)fileSize.QuadPart;

    mapping = CreateFileMapping (file, NULL, PAGE_READONLY, 0, 0, NULL);
    if (mapping == NULL)
    {
        UnMap();
        return;
    }

    LPVOID address = MapViewOfFile (mapping, FILE_MAP_READ, 0, 0, 0);
    if (address == NULL)
    {
        UnMap();
        return;
    }

    buffer = reinterpret_cast<const unsigned char*>(address);
}
Example #2
0
// メモリマップトファイルを開く
inline bool tapetums::File::Open
(
    LPCWSTR lpName, ACCESS accessMode
)
{
    if ( m_map ) { return true; }
    if ( m_handle != INVALID_HANDLE_VALUE ) { return false; }

    ::StringCchCopyW(m_name, MAX_PATH, lpName);

    m_map = ::OpenFileMappingW
    (
        accessMode == ACCESS::READ ? FILE_MAP_READ : FILE_MAP_WRITE,
        FALSE, lpName
    );
    if ( nullptr == m_map )
    {
        return false;
    }

    m_ptr = (uint8_t*)::MapViewOfFile
    (
        m_map,
        accessMode == ACCESS::READ ? FILE_MAP_READ : FILE_MAP_WRITE,
        0, 0, 0
    );
    if ( nullptr == m_ptr )
    {
        UnMap();
        return false;
    }

    return true;
}
Example #3
0
BOOL CXMemMapFile::MapExistingMemory(LPCTSTR lpszName, DWORD dwBytes, BOOL bReadOnly)
{
	m_dwLength = dwBytes;
	m_bReadOnly = bReadOnly;

	DWORD dwDesiredAccess = (!bReadOnly) ? FILE_MAP_WRITE : FILE_MAP_READ;
	CreateMappingName(lpszName, 
		m_szMappingName, sizeof(m_szMappingName)/sizeof(TCHAR), TRUE);
	m_hMapping = OpenFileMapping(dwDesiredAccess, 0, m_szMappingName);
	if (m_hMapping == NULL)
	{
		TRACE(_T("Failed in call to OpenFileMapping, GetLastError returned %d\n"), 
			GetLastError());
		UnMap();
		return FALSE;
	}

	//Map the view
	m_lpData = MapViewOfFile(m_hMapping, dwDesiredAccess, 0, 0, m_dwLength);

	//Create the mutex to sync access
	//m_hMutex = CreateMutex(NULL, FALSE, CreateMutexName());

	BOOL bRet = m_lpData != NULL;

	if (bRet)
		m_bOpen = TRUE;

	return bRet;
}
Example #4
0
int
main( int argc, char * argv[] )
{
  size_t    count = 1;
  
  int	    fd;
  size_t    size = ( (4096 * 2) * 10240 ) ; /* 80 meg */
  caddr_t   map;

  for( ;; ++ count )
    {
      if( (count % 10) == 0 )
	printf( "%d iterations.\n", count );
      
	  
      remove( FileName );
      
      if( (fd = open( FileName, O_RDWR | O_CREAT, 0666 ) ) < 0 )
	{
	  perror( "open" );
	  exit( 1 );
	}

      map = MapIt( fd, size );

      TestLongs( map, size );
      TestChar( map, size ); 

      UnMap( fd, map, size );
    }
}
Example #5
0
TInt DMemModelAlignedShBuf::Close(TAny* aPtr)
	{
	__KTRACE_OPT(KMMU, Kern::Printf(">DMemModelAlignedShBuf::Close(0x%08x)", aPtr));

	if (aPtr)
		{
		DProcess* pP = reinterpret_cast<DProcess*>(aPtr);
		UnMap(pP);
		iPool->CloseClient(pP);
		}

	return DShBuf::Close(aPtr);
	}
Example #6
0
// ファイルを閉じる
inline void tapetums::File::Close()
{
    UnMap();
    Flush();

    if ( m_handle != INVALID_HANDLE_VALUE )
    {
        ::CloseHandle(m_handle);
        m_handle = INVALID_HANDLE_VALUE;
    }

    m_pos  = 0;
    m_size = 0;
}
Example #7
0
BOOL CXMemMapFile::MapHandle(HANDLE hHandle, 
							 LPSECURITY_ATTRIBUTES lpSecurityAttributes, 
							 DWORD dwStartOffset)
{
	//Create the file mapping object
	DWORD flProtect = (!m_bReadOnly) ? PAGE_READWRITE : PAGE_READONLY;

  // flProtect |= PAGE_NOCACHE; // otherwisze using the memory to cache.
  flProtect |= SEC_COMMIT;
  
	//work out the length of the file mapping to create
	DWORD dwLength = m_dwLength;
	if (m_bAppendNull)
		dwLength += 2;
  
	m_hMapping = ::CreateFileMapping(hHandle, lpSecurityAttributes, flProtect, 0, 
						dwLength, m_szMappingName);
	if (m_hMapping == NULL)
	{
		OutputDbgStr_i(_T("Failed in call to ::CreateFileMapping, GetLastError %d\n"), GetLastError());
		UnMap();
		return FALSE;
	}

	//Map the view
	DWORD dwDesiredAccess = (!m_bReadOnly) ? FILE_MAP_WRITE : FILE_MAP_READ;  // FILE_MAP_ALL_ACCESS
	m_lpData = MapViewOfFile(m_hMapping, dwDesiredAccess, 0, dwStartOffset, dwLength );

	//null terminare if asked to do so
	if (m_bAppendNull && m_lpData)
	{
		//use two Nulls just incase the data as viewed as an array of wide characters
		BYTE* lpData = (BYTE*) m_lpData;
		lpData[m_dwLength] = 0;
		lpData[m_dwLength+1] = 0;
	}

	//Create the mutex to sync access
	//m_hMutex = CreateMutex(lpSecurityAttributes, FALSE, CreateMutexName());

	BOOL bRet = m_lpData != NULL;

	if (bRet)
		m_bOpen = TRUE;

	return bRet;
}
Example #8
0
// メモリマップトファイルを生成する
inline bool tapetums::File::Map
(
    int64_t size, LPCWSTR lpName, ACCESS accessMode
)
{
    if ( m_map ) { return true; }

    LARGE_INTEGER li;
    li.QuadPart = (size > 0) ? size : m_size;
    if ( li.QuadPart == 0 )
    {
        return false;
    }

    if ( m_handle == INVALID_HANDLE_VALUE )
    {
        ::StringCchCopyW(m_name, MAX_PATH, lpName);
    }

    m_map = ::CreateFileMappingW
    (
        m_handle, nullptr,
        accessMode == ACCESS::READ ? PAGE_READONLY : PAGE_READWRITE,
        li.HighPart, li.LowPart, lpName
    );
    if ( nullptr == m_map )
    {
        return false;
    }

    m_ptr = (uint8_t*)::MapViewOfFile
    (
        m_map,
        accessMode == ACCESS::READ ? FILE_MAP_READ : FILE_MAP_WRITE,
        0, 0, 0
    );
    if ( nullptr == m_ptr )
    {
        UnMap();
        return false;
    }

    return true;
}
Example #9
0
bool MemMap::Map(const char* szFileName)  
{  
    UnMap();  
    m_nFile = open(szFileName, O_RDONLY);  
    if (m_nFile < 0)   
    {   
        m_nFile = 0;  
        return false;   
    }  
  
    struct stat status;  
    fstat(m_nFile, &status);  
  
    m_uSize = status.st_size;  
    m_pData = mmap(0, m_uSize, PROT_READ, MAP_SHARED, m_nFile, 0);  
    if (MAP_FAILED != m_pData) { return true;}  
  
    close(m_nFile);  
    m_pData = NULL;  
    m_nFile = 0;  
    m_uSize = 0;  
    return false;  
}  
CMappedInFile::~CMappedInFile()
{
    UnMap();
}
Example #11
0
BOOL CXMemMapFile::MapFile(LPCTSTR lpszFilename, 
						   BOOL bReadOnly, 
						   DWORD dwShareMode, 
						   BOOL bAppendNull, 
						   BOOL bNamed, 
						   BOOL bGrowable, 
						   DWORD dwStartOffset, 
						   DWORD dwNumberOfBytesToMap, 
						   LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
	//Work out the file access flags
	m_bReadOnly = bReadOnly;
	DWORD dwDesiredFileAccess = GENERIC_READ;
	if (!m_bReadOnly)
		dwDesiredFileAccess |= GENERIC_WRITE;

	//store away the append Null flag
	m_bAppendNull = bAppendNull;

	//Open the real file on the file system
	m_hFile = CreateFile(lpszFilename, dwDesiredFileAccess, dwShareMode, NULL, 
	OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (m_hFile == INVALID_HANDLE_VALUE)
	{
		TRACE(_T("Failed in call to CreateFile, GetLastError returned %d\n"), 
			GetLastError());
		UnMap();
		return FALSE;
	}

	// Make the file sparse, if requested to make the memory mapped file growable
	if (bGrowable)
	{
		DWORD dw;
		if (!::DeviceIoControl(m_hFile, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &dw, NULL))
		{
			TRACE(_T("Failed in call to make file sparse, You need Windows 2000 ")
				  _T("and an NTFS 5 volume for this !!, GetLastError returned %d\n"), 
				  GetLastError());
			UnMap();
			return FALSE;
		}
	}

	// Get the size of the file we are mapping
	DWORD dwFileSizeHigh=0;
	DWORD dwLength = GetFileSize(m_hFile, &dwFileSizeHigh);
	if (dwLength == 0xFFFFFFFF)
	{
		//There was an error calling GetFileSize
		TRACE(_T("Failed in call to GetFileSize, GetLastError returned %d\n"), 
			GetLastError());
		UnMap();
		return FALSE;
	}

	//Fail if file is greater than 4GB in size
	if (dwFileSizeHigh)
	{
		//There was an error calling GetFileSize
		TRACE(_T("File size is greater than 4GB, Memory mapping this file ")
			  _T("size will not work until CXMemMapFile supports Win64 !!\n"));
		UnMap();
		return FALSE;
	}

	//Fail if file is 0 length in size, calling CreateFileMapping on a 
	//zero length file on 95/98 can cause problems
	if (dwFileSizeHigh == 0 && dwLength == 0)
	{
		TRACE(_T("File size is 0, not attempting to memory map the file\n"));
		UnMap();
		return FALSE;
	}

	//Now work out the size of the mapping we will be performing
	if (dwNumberOfBytesToMap)
		m_dwLength = dwNumberOfBytesToMap;
	else
		m_dwLength = dwLength;

	//Do the actual mapping
	CreateMappingName(lpszFilename, 
		m_szMappingName, sizeof(m_szMappingName)/sizeof(TCHAR), bNamed);

	return MapHandle(m_hFile, lpSecurityAttributes, dwStartOffset);
}
Example #12
0
CXMemMapFile::~CXMemMapFile()
{
  UnMap();
}
Example #13
0
MemMap::~MemMap()  
{  
    UnMap();  
}