Skip to content

Commit

Permalink
Merge pull request #2 from yangosoft/wip/shm-win
Browse files Browse the repository at this point in the history
Added wstring constructor for shm class
yangosoft authored Dec 27, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 76fa7a4 + 7d0e9d8 commit cf079ca
Showing 2 changed files with 27 additions and 18 deletions.
40 changes: 24 additions & 16 deletions src/include/cpputils2/win/shm/shm.hpp
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@

#include "cpputils2/common/types.hpp"

#include <string>

#include <windows.h>
#include <stdio.h>
#include <conio.h>
@@ -27,15 +29,22 @@ namespace CppUtils2
}

Shm(const std::string& file_mem_path)
: mem_path(file_mem_path.begin(), file_mem_path.end()), hMapFile(nullptr), pBuf{ nullptr }
{
}

Shm(const std::wstring& file_mem_path)
: mem_path(file_mem_path), hMapFile(nullptr), pBuf{ nullptr }
{
}



/// @brief Open an existing shared memory
/// @param file_mem_path Path to the shared memory
/// @param mem_size Size of the shared memory
/// @return 0 on success, -1 on error
Result open_existing(const std::string& file_mem_path, std::size_t mem_size)
Result open_existing(const std::wstring& file_mem_path, std::size_t mem_size)
{
mem_path = file_mem_path;
return open_existing(mem_size);
@@ -50,7 +59,7 @@ namespace CppUtils2
{
return Result::RET_ERROR;
}
int flags = 0;
auto flags = Operation::OPEN;
return shared_open(flags, mem_size);
}

@@ -64,7 +73,7 @@ namespace CppUtils2
return Result::RET_ERROR;
}

int flags = 1;
auto flags = Operation::CREATE;

return shared_open(flags, mem_size);
}
@@ -105,27 +114,28 @@ namespace CppUtils2
}

private:
std::string mem_path;
std::wstring mem_path;
HANDLE hMapFile;
LPVOID pBuf;

Result shared_open(int flags, std::size_t mem_size)
enum class Operation: int
{
if (flags == 1) {

//TCHAR szName[] = TEXT(mem_path.c_str());
OPEN,
CREATE
};

hMapFile = CreateFileMappingA(
Result shared_open(Operation flags, std::size_t mem_size)
{
if (flags == Operation::CREATE) {
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
nullptr, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
mem_size, // maximum object size (low-order DWORD)
mem_path.c_str()); // name of mapping object
(LPCTSTR)mem_path.c_str()); // name of mapping object

if (hMapFile == NULL)
{

return Result::RET_ERROR;
}

@@ -138,19 +148,17 @@ namespace CppUtils2
if (pBuf == nullptr)
{
//_tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError());

CloseHandle(hMapFile);

return Result::RET_ERROR;
}

}
else {

hMapFile = OpenFileMappingA(
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
mem_path.c_str()); // name of mapping object
(LPCTSTR)mem_path.c_str()); // name of mapping object

if (hMapFile == NULL)
{
5 changes: 3 additions & 2 deletions src/test/cpputils2_tests.cpp
Original file line number Diff line number Diff line change
@@ -145,7 +145,7 @@ namespace

#ifdef _WIN32
TEST(ExampleSHM, TestSHM) {
CppUtils2::Shm shm("test_shm");
CppUtils2::Shm shm(L"test_shm");
auto ret = shm.allocate(sizeof(int32_t));
EXPECT_NE(ret, CppUtils2::Result::RET_ERROR);
void* ptr = shm.get_raw_ptr();
@@ -159,9 +159,10 @@ namespace
shm.unlink();
EXPECT_TRUE(true);
}

TEST(ExampleShm, TestExisting)
{
CppUtils2::Shm shm("test_shm");
CppUtils2::Shm shm(L"test_shm");
auto ret = shm.allocate(sizeof(int32_t));
EXPECT_NE(ret, CppUtils2::Result::RET_ERROR);

0 comments on commit cf079ca

Please sign in to comment.