-
-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for a shared memory mapping on Kernel & User Memory #1468
Changes from all commits
e0ee5f4
d79f5f3
bda29b5
520bb2e
27f90ec
d9cdbfb
728731a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2022 PCSX-Redux authors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
|
||
#if !defined(_WIN32) && !defined(_WIN64) | ||
|
||
#include "support/sharedmem.h" | ||
|
||
#include <assert.h> | ||
#include <fcntl.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
#include <sys/mman.h> | ||
#include <sys/stat.h> | ||
|
||
bool PCSX::SharedMem::init(const char* id, size_t size, bool initToZero) { | ||
assert(m_mem == nullptr); | ||
bool doRawAlloc = true; | ||
m_size = size; | ||
// Try to create a shared memory mapping, if an ID is provided | ||
if (id != nullptr) { | ||
// Build the full name to share as | ||
m_sharedName = getSharedName(id, static_cast<uint32_t>(getpid())); | ||
// Try to create a shared memory mapping, if a name is provided | ||
m_fd = shm_open(m_sharedName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); | ||
if (m_fd >= 0) { | ||
// fd is ready, reserve the memory we need | ||
int result = ftruncate(m_fd, static_cast<off_t>(size)); | ||
if (result < 0) { | ||
shm_unlink(m_sharedName.c_str()); | ||
close(m_fd); | ||
m_fd = -1; | ||
} else { | ||
// ftruncate completed, now map the memory at 0 offset | ||
void* basePointer = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0); | ||
// Validate success and assign the view to m_mem | ||
if (basePointer != MAP_FAILED) { | ||
doRawAlloc = false; | ||
m_mem = static_cast<uint8_t*>(basePointer); | ||
// Initialise memory to zero, if requested | ||
if (initToZero) { | ||
memset(m_mem, 0, size); | ||
} | ||
} else { | ||
shm_unlink(m_sharedName.c_str()); | ||
close(m_fd); | ||
m_fd = -1; | ||
} | ||
} | ||
} | ||
} | ||
// Alloc memory directly if we opted out or had problems creating the memory map | ||
if (doRawAlloc) { | ||
// calloc will automatically init memory to zero | ||
m_mem = (uint8_t*)calloc(size, 1); | ||
} | ||
|
||
// Return false if we had to fall back to a raw alloc | ||
return !(doRawAlloc && id != nullptr); | ||
} | ||
Check warning on line 83 in src/support/sharedmem-unix.cc CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)❌ New issue: Bumpy Road Ahead
Check warning on line 83 in src/support/sharedmem-unix.cc CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)❌ New issue: Deep, Nested Complexity
|
||
|
||
PCSX::SharedMem::~SharedMem() { | ||
if (m_fd == -1) { | ||
free(m_mem); | ||
} else { | ||
munmap(m_mem, m_size); | ||
shm_unlink(m_sharedName.c_str()); | ||
close(m_fd); | ||
} | ||
} | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2022 PCSX-Redux authors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
#if defined(_WIN32) || defined(_WIN64) | ||
|
||
#include "support/sharedmem.h" | ||
#include "support/windowswrapper.h" | ||
|
||
#include <assert.h> | ||
|
||
bool PCSX::SharedMem::init(const char* id, size_t size, bool initToZero) { | ||
assert(m_mem == nullptr); | ||
bool doRawAlloc = true; | ||
m_size = size; | ||
// Try to create a shared memory mapping, if an id is provided | ||
if (id != nullptr) { | ||
// Build the full name to share as | ||
std::string fullname = getSharedName(id, static_cast<uint32_t>(GetCurrentProcessId())); | ||
// Create the memory mapping handle | ||
m_fileHandle = CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, | ||
static_cast<uint32_t>(size >> 32), static_cast<uint32_t>(size), fullname.c_str()); | ||
if (m_fileHandle != INVALID_HANDLE_VALUE) { | ||
// Create a view of the memory mapping at 0 offset | ||
void* basePointer = MapViewOfFileEx(m_fileHandle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, size, nullptr); | ||
// Validate success and assign the view to m_mem | ||
if (basePointer != nullptr) { | ||
doRawAlloc = false; | ||
m_mem = static_cast<uint8_t *>(basePointer); | ||
// Initialise memory to zero, if requested | ||
if (initToZero) { | ||
memset(m_mem, 0, size); | ||
} | ||
} else { | ||
CloseHandle(m_fileHandle); | ||
m_fileHandle = nullptr; | ||
} | ||
} else { | ||
m_fileHandle = nullptr; | ||
} | ||
} | ||
// Alloc memory directly if we opted out or had problems creating the memory map | ||
if (doRawAlloc) { | ||
// calloc will automatically init memory to zero | ||
m_mem = (uint8_t *)calloc(size, 1); | ||
} | ||
// Return false if we had to fall back to a raw alloc | ||
return !(doRawAlloc && id != nullptr); | ||
} | ||
Check warning on line 70 in src/support/sharedmem-windows.cc CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)❌ New issue: Deep, Nested Complexity
|
||
|
||
PCSX::SharedMem::~SharedMem() { | ||
if (m_fileHandle != nullptr) { | ||
UnmapViewOfFile(m_mem); | ||
m_mem = nullptr; | ||
CloseHandle(m_fileHandle); | ||
m_fileHandle = nullptr; | ||
} else { | ||
free(m_mem); | ||
} | ||
} | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2022 PCSX-Redux authors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
|
||
#include "support/sharedmem.h" | ||
#include "fmt/format.h" | ||
|
||
std::string PCSX::SharedMem::getSharedName(const char* id, uint32_t pid) { | ||
// Example name: pcsx-redux-wram-37045 | ||
return fmt::format("pcsx-redux-{}-{}", id, pid); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2022 PCSX-Redux authors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
#include <string> | ||
|
||
namespace PCSX { | ||
|
||
class SharedMem { | ||
public: | ||
SharedMem() {} | ||
~SharedMem(); | ||
|
||
/** | ||
* Returns true if: | ||
* - the memory was sucessfully shared with the given ID, or | ||
* - no ID was given and a raw alloc was performed | ||
* Returns false if: | ||
* - the memory failed to successfully share and defaulted to a raw alloc | ||
*/ | ||
bool init(const char* id, size_t size, bool initToZero); | ||
|
||
uint8_t* getPtr() { return m_mem; } | ||
size_t getSize() { return m_size; } | ||
|
||
private: | ||
std::string getSharedName(const char* id, uint32_t pid); | ||
|
||
private: | ||
uint8_t* m_mem = nullptr; | ||
size_t m_size = 0; | ||
|
||
void* m_fileHandle = nullptr; | ||
std::string m_sharedName; | ||
int m_fd = -1; | ||
}; | ||
|
||
} // namespace PCSX |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably keep it private for now.