2023-12-24 02:50:11 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Cicero
|
|
|
|
* LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
|
|
|
|
* PURPOSE: Cicero file mapping
|
|
|
|
* COPYRIGHT: Copyright 2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2024-02-17 00:53:50 +00:00
|
|
|
#include "precomp.h"
|
|
|
|
#include "cicfmap.h"
|
2023-12-24 02:50:11 +00:00
|
|
|
|
2023-12-26 03:07:01 +00:00
|
|
|
CicFileMapping::CicFileMapping(LPCTSTR pszName, CicMutex *pMutex)
|
2023-12-24 02:50:11 +00:00
|
|
|
{
|
2023-12-26 07:17:06 +00:00
|
|
|
m_pszName = NULL;
|
|
|
|
m_pView = NULL;
|
|
|
|
m_hMapping = NULL;
|
|
|
|
m_bCreated = FALSE;
|
|
|
|
m_bHasMutex = FALSE;
|
|
|
|
m_pMutex = NULL;
|
2023-12-24 02:50:11 +00:00
|
|
|
Init(pszName, pMutex);
|
|
|
|
}
|
|
|
|
|
2024-02-17 00:53:50 +00:00
|
|
|
void CicFileMappingStatic::Close()
|
2023-12-24 02:50:11 +00:00
|
|
|
{
|
|
|
|
if (m_pView)
|
|
|
|
{
|
|
|
|
::UnmapViewOfFile(m_pView);
|
|
|
|
m_pView = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_hMapping)
|
|
|
|
{
|
|
|
|
::CloseHandle(m_hMapping);
|
|
|
|
m_hMapping = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bCreated = FALSE;
|
|
|
|
}
|
|
|
|
|
2024-02-17 00:53:50 +00:00
|
|
|
void CicFileMappingStatic::Init(LPCTSTR pszName, CicMutex *pMutex)
|
2023-12-24 02:50:11 +00:00
|
|
|
{
|
|
|
|
if (pMutex)
|
|
|
|
m_pMutex = pMutex;
|
|
|
|
|
|
|
|
m_bCreated = FALSE;
|
|
|
|
m_pszName = pszName;
|
|
|
|
m_bHasMutex = (pMutex != NULL);
|
|
|
|
}
|
|
|
|
|
2024-02-17 00:53:50 +00:00
|
|
|
LPVOID
|
2023-12-24 02:50:11 +00:00
|
|
|
CicFileMappingStatic::Create(
|
|
|
|
LPSECURITY_ATTRIBUTES pSA,
|
|
|
|
DWORD dwMaximumSizeLow,
|
|
|
|
LPBOOL pbAlreadyExists)
|
|
|
|
{
|
|
|
|
if (!m_pszName)
|
|
|
|
return NULL;
|
|
|
|
|
2023-12-26 03:07:01 +00:00
|
|
|
m_hMapping = ::CreateFileMapping(INVALID_HANDLE_VALUE,
|
|
|
|
pSA,
|
|
|
|
PAGE_READWRITE,
|
|
|
|
0,
|
|
|
|
dwMaximumSizeLow,
|
|
|
|
m_pszName);
|
2023-12-24 02:50:11 +00:00
|
|
|
if (pbAlreadyExists)
|
|
|
|
*pbAlreadyExists = (::GetLastError() == ERROR_ALREADY_EXISTS);
|
|
|
|
if (!m_hMapping)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
m_bCreated = TRUE;
|
|
|
|
return _Map();
|
|
|
|
}
|
|
|
|
|
2024-02-17 00:53:50 +00:00
|
|
|
LPVOID CicFileMappingStatic::Open()
|
2023-12-24 02:50:11 +00:00
|
|
|
{
|
|
|
|
if (!m_pszName)
|
|
|
|
return NULL;
|
2023-12-26 03:07:01 +00:00
|
|
|
m_hMapping = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, m_pszName);
|
2023-12-24 02:50:11 +00:00
|
|
|
if (!m_hMapping)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return _Map();
|
|
|
|
}
|
|
|
|
|
2024-02-17 00:53:50 +00:00
|
|
|
LPVOID CicFileMappingStatic::_Map()
|
2023-12-24 02:50:11 +00:00
|
|
|
{
|
|
|
|
m_pView = ::MapViewOfFile(m_hMapping, FILE_MAP_WRITE, 0, 0, 0);
|
|
|
|
if (!m_pView)
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return m_pView;
|
|
|
|
}
|
|
|
|
|
2024-02-17 00:53:50 +00:00
|
|
|
BOOL CicFileMappingStatic::Enter()
|
2023-12-24 02:50:11 +00:00
|
|
|
{
|
|
|
|
if (!m_bHasMutex)
|
|
|
|
return TRUE;
|
|
|
|
return m_pMutex->Enter();
|
|
|
|
}
|
|
|
|
|
2024-02-17 00:53:50 +00:00
|
|
|
void CicFileMappingStatic::Leave()
|
2023-12-24 02:50:11 +00:00
|
|
|
{
|
|
|
|
if (!m_bHasMutex)
|
|
|
|
return;
|
|
|
|
m_pMutex->Leave();
|
|
|
|
}
|
|
|
|
|
2024-02-17 00:53:50 +00:00
|
|
|
BOOL CicFileMappingStatic::Flush(SIZE_T dwNumberOfBytesToFlush)
|
2023-12-24 02:50:11 +00:00
|
|
|
{
|
|
|
|
if (!m_pView)
|
|
|
|
return FALSE;
|
|
|
|
return ::FlushViewOfFile(m_pView, dwNumberOfBytesToFlush);
|
|
|
|
}
|
|
|
|
|
2024-02-17 00:53:50 +00:00
|
|
|
void CicFileMappingStatic::Finalize()
|
2023-12-24 02:50:11 +00:00
|
|
|
{
|
|
|
|
if (!m_bHasMutex)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Close();
|
|
|
|
Leave();
|
|
|
|
}
|