https://git.reactos.org/?p=reactos.git;a=commitdiff;h=4a3a78857f14ec4125f0591ce67d268aa41bab9d

commit 4a3a78857f14ec4125f0591ce67d268aa41bab9d
Author:     Katayama Hirofumi MZ <katayama.hirofumi...@gmail.com>
AuthorDate: Sun Dec 24 11:50:11 2023 +0900
Commit:     Katayama Hirofumi MZ <katayama.hirofumi...@gmail.com>
CommitDate: Sun Dec 24 11:50:11 2023 +0900

    [SDK] Add <cicero/cicfmap.h> for File Mapping
    
    CORE-19361
---
 sdk/include/reactos/cicero/cicfmap.h | 165 +++++++++++++++++++++++++++++++++++
 1 file changed, 165 insertions(+)

diff --git a/sdk/include/reactos/cicero/cicfmap.h 
b/sdk/include/reactos/cicero/cicfmap.h
new file mode 100644
index 00000000000..201f3227db8
--- /dev/null
+++ b/sdk/include/reactos/cicero/cicfmap.h
@@ -0,0 +1,165 @@
+/*
+ * 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...@gmail.com>
+ */
+
+#pragma once
+
+#include "cicmutex.h"
+
+// class CicFileMappingStatic;
+// class CicFileMapping;
+
+class CicFileMappingStatic
+{
+protected:
+    LPCWSTR m_pszName;
+    LPVOID m_pView;
+    HANDLE m_hMapping;
+    BOOL m_bCreated;
+    BOOL m_bHasMutex;
+    CicMutex *m_pMutex;
+
+    LPVOID _Map();
+
+public:
+    CicFileMappingStatic() { }
+    ~CicFileMappingStatic() { }
+
+    void Init(LPCWSTR pszName, CicMutex *pMutex);
+
+    LPVOID Create(LPSECURITY_ATTRIBUTES pSA, DWORD dwMaximumSizeLow, LPBOOL 
pbAlreadyExists);
+    LPVOID Open();
+    void Close();
+
+    BOOL Enter();
+    void Leave();
+    BOOL Flush(SIZE_T dwNumberOfBytesToFlush);
+    void Finalize();
+};
+
+class CicFileMapping : public CCicFileMappingStatic
+{
+public:
+    CicFileMapping(LPCWSTR pszName, CicMutex *pMutex);
+    virtual ~CicFileMapping() { Finalize(); }
+};
+
+/******************************************************************************/
+
+inline
+CicFileMapping::CicFileMapping(LPCWSTR pszName, CicMutex *pMutex)
+    : m_pszName(NULL)
+    , m_pView(NULL)
+    , m_hMapping(NULL)
+    , m_bCreated(FALSE)
+    , m_bHasMutex(FALSE)
+    , m_pMutex(NULL)
+{
+    Init(pszName, pMutex);
+}
+
+inline void CicFileMappingStatic::Close()
+{
+    if (m_pView)
+    {
+        ::UnmapViewOfFile(m_pView);
+        m_pView = NULL;
+    }
+
+    if (m_hMapping)
+    {
+        ::CloseHandle(m_hMapping);
+        m_hMapping = NULL;
+    }
+
+    m_bCreated = FALSE;
+}
+
+inline void CicFileMappingStatic::Init(LPCWSTR pszName, CicMutex *pMutex)
+{
+    if (pMutex)
+        m_pMutex = pMutex;
+
+    m_bCreated = FALSE;
+    m_pszName = pszName;
+    m_bHasMutex = (pMutex != NULL);
+}
+
+inline LPVOID
+CicFileMappingStatic::Create(
+    LPSECURITY_ATTRIBUTES pSA,
+    DWORD dwMaximumSizeLow,
+    LPBOOL pbAlreadyExists)
+{
+    if (!m_pszName)
+        return NULL;
+
+    m_hMapping = ::CreateFileMappingW(INVALID_HANDLE_VALUE,
+                                      pSA,
+                                      PAGE_READWRITE,
+                                      0,
+                                      dwMaximumSizeLow,
+                                      m_pszName);
+    if (pbAlreadyExists)
+        *pbAlreadyExists = (::GetLastError() == ERROR_ALREADY_EXISTS);
+    if (!m_hMapping)
+        return NULL;
+
+    m_bCreated = TRUE;
+    return _Map();
+}
+
+inline LPVOID CicFileMappingStatic::Open()
+{
+    if (!m_pszName)
+        return NULL;
+    m_hMapping = ::OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, m_pszName);
+    if (!m_hMapping)
+        return NULL;
+
+    return _Map();
+}
+
+inline LPVOID CicFileMappingStatic::_Map()
+{
+    m_pView = ::MapViewOfFile(m_hMapping, FILE_MAP_WRITE, 0, 0, 0);
+    if (!m_pView)
+    {
+        Close();
+        return NULL;
+    }
+    return m_pView;
+}
+
+inline BOOL CicFileMappingStatic::Enter()
+{
+    if (!m_bHasMutex)
+        return TRUE;
+    return m_pMutex->Enter();
+}
+
+inline void CicFileMappingStatic::Leave()
+{
+    if (!m_bHasMutex)
+        return;
+    m_pMutex->Leave();
+}
+
+inline BOOL CCicFileMappingStatic::Flush(SIZE_T dwNumberOfBytesToFlush)
+{
+    if (!m_pView)
+        return FALSE;
+    return ::FlushViewOfFile(m_pView, dwNumberOfBytesToFlush);
+}
+
+inline void CicFileMappingStatic::Finalize()
+{
+    if (!m_bHasMutex)
+        return;
+
+    Close();
+    Leave();
+}

Reply via email to