1+ /* *
2+ * @file shm.hpp
3+ * @brief Shared memory abstraction for POSIX systems.
4+ *
5+ *
6+ */
7+
8+ #pragma once
9+
10+ #include " cpputils2/common/types.hpp"
11+
12+ #include < windows.h>
13+ #include < stdio.h>
14+ #include < conio.h>
15+ #include < tchar.h>
16+
17+ namespace CppUtils2
18+ {
19+
20+
21+ class Shm
22+ {
23+ public:
24+ Shm ()
25+ : hMapFile(nullptr ), pBuf{nullptr }
26+ {
27+ }
28+
29+ Shm (const std::string& file_mem_path)
30+ : mem_path(file_mem_path), hMapFile(nullptr ), pBuf{ nullptr }
31+ {
32+ }
33+
34+ // / @brief Open an existing shared memory
35+ // / @param file_mem_path Path to the shared memory
36+ // / @param mem_size Size of the shared memory
37+ // / @return 0 on success, -1 on error
38+ Result open_existing (const std::string& file_mem_path, std::size_t mem_size)
39+ {
40+ mem_path = file_mem_path;
41+ return open_existing (mem_size);
42+ }
43+
44+ // / @brief Open an existing shared memory
45+ // / @param mem_size Size of the shared memory
46+ // / @return 0 on success, -1 on error
47+ Result open_existing (std::size_t mem_size)
48+ {
49+ if (hMapFile != nullptr )
50+ {
51+ return Result::RET_ERROR;
52+ }
53+ int flags = 0 ;
54+ return shared_open (flags, mem_size);
55+ }
56+
57+ // / @brief Allocate a new shared memory
58+ // / @param mem_size Size of the shared memory
59+ // / @return 0 on success, -1 on error
60+ Result allocate (std::size_t mem_size)
61+ {
62+ if (hMapFile != nullptr )
63+ {
64+ return Result::RET_ERROR;
65+ }
66+
67+ int flags = 1 ;
68+
69+ return shared_open (flags, mem_size);
70+ }
71+
72+ // / @brief Removes the shared memory file
73+ // / @return 0 on success, -1 on error
74+ Result unlink ()
75+ {
76+ if (hMapFile != nullptr )
77+ {
78+ UnmapViewOfFile (pBuf);
79+ CloseHandle (hMapFile);
80+ }
81+ return Result::RET_ERROR;
82+ }
83+
84+ // / @brief Closes the shared memory
85+ void close ()
86+ {
87+ if (hMapFile != nullptr )
88+ {
89+ UnmapViewOfFile (pBuf);
90+ CloseHandle (hMapFile);
91+ }
92+ hMapFile = nullptr ;
93+ }
94+
95+ // / @brief Get the pointer to the shared memory
96+ // / @return Pointer to the shared memory
97+ void * get_raw_ptr ()
98+ {
99+ return pBuf;
100+ }
101+
102+ virtual ~Shm ()
103+ {
104+ close ();
105+ }
106+
107+ private:
108+ std::string mem_path;
109+ HANDLE hMapFile;
110+ LPVOID pBuf;
111+
112+ Result shared_open (int flags, std::size_t mem_size)
113+ {
114+ if (flags == 1 ) {
115+
116+ // TCHAR szName[] = TEXT(mem_path.c_str());
117+
118+ hMapFile = CreateFileMappingA (
119+ INVALID_HANDLE_VALUE, // use paging file
120+ nullptr , // default security
121+ PAGE_READWRITE, // read/write access
122+ 0 , // maximum object size (high-order DWORD)
123+ mem_size, // maximum object size (low-order DWORD)
124+ mem_path.c_str ()); // name of mapping object
125+
126+ if (hMapFile == NULL )
127+ {
128+
129+ return Result::RET_ERROR;
130+ }
131+
132+ pBuf = MapViewOfFile (hMapFile, // handle to map object
133+ FILE_MAP_ALL_ACCESS, // read/write permission
134+ 0 ,
135+ 0 ,
136+ mem_size);
137+
138+ if (pBuf == nullptr )
139+ {
140+ // _tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError());
141+
142+ CloseHandle (hMapFile);
143+
144+ return Result::RET_ERROR;
145+ }
146+
147+ }
148+ else {
149+
150+ hMapFile = OpenFileMappingA (
151+ FILE_MAP_ALL_ACCESS, // read/write access
152+ FALSE , // do not inherit the name
153+ mem_path.c_str ()); // name of mapping object
154+
155+ if (hMapFile == NULL )
156+ {
157+ // _tprintf(TEXT("Could not open file mapping object (%d).\n"),
158+ // GetLastError());
159+ return Result::RET_ERROR;
160+ }
161+
162+ pBuf = MapViewOfFile (hMapFile, // handle to map object
163+ FILE_MAP_ALL_ACCESS, // read/write permission
164+ 0 ,
165+ 0 ,
166+ mem_size);
167+
168+ if (pBuf == nullptr )
169+ {
170+ // _tprintf(TEXT("Could not map view of file (%d).\n"),
171+ // GetLastError());
172+
173+ CloseHandle (hMapFile);
174+
175+ return Result::RET_ERROR;
176+ }
177+
178+ }
179+ return Result::RET_OK;
180+ }
181+ };
182+ }
0 commit comments