-
-
Notifications
You must be signed in to change notification settings - Fork 124
Add support for a shared memory mapping on Kernel & User Memory #1468
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e0ee5f4
Add Windows-only support for a shared memory mapping on Kernel & User…
d79f5f3
Refactored named shared memory handling into SharedMem class, with pl…
bda29b5
Add missing headers, fix line endings
520bb2e
Remove unused code
27f90ec
Refactoring SharedMem
d9cdbfb
Core review tweaks
728731a
Add missing headers
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
PCSX::SharedMem::~SharedMem() { | ||
if (m_fileHandle != nullptr) { | ||
UnmapViewOfFile(m_mem); | ||
m_mem = nullptr; | ||
CloseHandle(m_fileHandle); | ||
m_fileHandle = nullptr; | ||
} else { | ||
free(m_mem); | ||
} | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.