Skip to content

Commit 8ac7cf1

Browse files
committed
Fix bugs
1 parent 13242d3 commit 8ac7cf1

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

include/jshm.h

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,56 @@
1212
#endif
1313

1414
namespace jshm {
15-
class shared_memory {
16-
public:
17-
[[nodiscard]] int size() const noexcept { return _size; }
18-
[[nodiscard]] const char* name() const noexcept { return _name; }
19-
20-
~shared_memory() {
15+
class shared_memory {
16+
public:
17+
[[nodiscard]] int size() const noexcept { return _size; }
18+
[[nodiscard]] const char* name() const noexcept { return _name; }
19+
20+
~shared_memory() {
2121
#ifdef _WIN32
22-
UnmapViewOfFile(_pBuf);
23-
CloseHandle(hMapFile);
22+
UnmapViewOfFile(_pBuf);
23+
CloseHandle(hMapFile);
2424
#elif __linux__ || __APPLE__
2525
if (_pBuf) munmap(_pBuf, (size_t) _size);
2626
if (_isCreate) shm_unlink(_name);
2727
#endif
28-
}
28+
delete[] _name;
29+
}
2930

30-
[[nodiscard]] void* address() const noexcept { return _pBuf; }
31+
[[nodiscard]] void* address() const noexcept { return _pBuf; }
3132

3233
[[nodiscard]] static shared_memory* create(const char* name, int size) { return init(name, size, true); }
3334
[[nodiscard]] static shared_memory* open(const char* name, int size) { return init(name, size, false); }
3435

35-
private:
36-
int _size;
37-
const char* _name;
36+
private:
37+
int _size;
38+
const char* _name;
3839

3940
#ifdef _WIN32
40-
HANDLE hMapFile;
41-
LPTSTR _pBuf;
41+
HANDLE hMapFile;
42+
LPTSTR _pBuf;
4243

43-
shared_memory(HANDLE hMapFile, LPTSTR pBuf, int size, const char* name) : hMapFile(hMapFile), _pBuf(pBuf), _size(size), _name(name) { }
44+
shared_memory(HANDLE hMapFile, LPTSTR pBuf, int size, const char* name) : hMapFile(hMapFile), _pBuf(pBuf), _size(size) {
4445
#elif __linux__ || __APPLE__
4546
void* _pBuf = nullptr;
4647
bool _isCreate;
47-
shared_memory(void* pBuf, int size, const char* name, bool isCreate) : _size(size), _name(name), _pBuf(pBuf), _isCreate(isCreate) { }
48+
shared_memory(void* pBuf, int size, const char* name, bool isCreate) : _size(size), _pBuf(pBuf), _isCreate(isCreate) {
4849
#endif
50+
_name = new char[strlen(name) + 1];
51+
strcpy((char*) _name, name);
52+
}
4953

50-
static shared_memory* init(const char* name, int size, bool isCreate) {
54+
static shared_memory* init(const char* name, int size, bool isCreate) {
5155
#ifdef _WIN32
52-
auto hMapFile = isCreate ? CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, size, name)
53-
: OpenFileMapping(SECTION_MAP_WRITE | SECTION_MAP_READ, FALSE, name);
54-
if (hMapFile == NULL) return nullptr;
55-
auto pBuf = (LPTSTR)MapViewOfFile(hMapFile, SECTION_MAP_WRITE | SECTION_MAP_READ, 0, 0, size);
56-
if (pBuf == NULL) {
57-
CloseHandle(hMapFile);
58-
throw nullptr;
59-
}
60-
return new shared_memory(hMapFile, pBuf, size, name);
56+
auto hMapFile = isCreate ? CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, size, name)
57+
: OpenFileMapping(SECTION_MAP_WRITE | SECTION_MAP_READ, FALSE, name);
58+
if (hMapFile == NULL) return nullptr;
59+
auto pBuf = (LPTSTR)MapViewOfFile(hMapFile, SECTION_MAP_WRITE | SECTION_MAP_READ, 0, 0, size);
60+
if (pBuf == NULL) {
61+
CloseHandle(hMapFile);
62+
throw nullptr;
63+
}
64+
return new shared_memory(hMapFile, pBuf, size, name);
6165
#elif __linux__ || __APPLE__
6266
auto mode = O_RDWR;
6367
if (isCreate) mode |= O_CREAT | O_EXCL;
@@ -74,8 +78,8 @@ namespace jshm {
7478
}
7579
return new shared_memory(pBuf, size, name, isCreate);
7680
#endif
77-
}
78-
};
81+
}
82+
};
7983
}
8084

8185
#endif

0 commit comments

Comments
 (0)