-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[LINUX/BASE] Fixed mmap usage and (re)-implemented most parts of memory_posix.cc #2230
base: master
Are you sure you want to change the base?
Conversation
flags |= MAP_FIXED_NOREPLACE; | ||
} | ||
void* result = mmap(base_address, length, prot, flags, -1, 0); | ||
|
||
if (result == MAP_FAILED) { |
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.
I would personally reverse this statement to prevent nesting.
aka.
if (result != MAP_FAILED) {
return result;
}
<remaining code without else>
src/xenia/base/memory_posix.cc
Outdated
if (result == MAP_FAILED) { | ||
// If the address is within this range, the mmap failed because we have | ||
// already mapped this memory. | ||
size_t region_begin = (size_t)base_address; |
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.
constify because why not
src/xenia/base/memory_posix.cc
Outdated
size_t region_end = (size_t)base_address + length; | ||
for (const auto mapped_range : mapped_file_ranges) { | ||
// Check if the allocation is within this range... | ||
if (region_begin >= mapped_range.region_begin && |
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.
Maybe same thing as above. Reverse this condition and check for less nesting.
When it is outside of region then just return nullptr
src/xenia/base/memory_posix.cc
Outdated
|
||
int flags = MAP_SHARED; | ||
if (base_address != nullptr) { | ||
flags = flags | MAP_FIXED_NOREPLACE; |
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.
flags |= MAP_FIXED_NOREPLACE;
src/xenia/base/memory_posix.cc
Outdated
|
||
if (result == MAP_FAILED) { | ||
return nullptr; | ||
}else { |
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.
else is unnecessary here
src/xenia/base/memory_posix.cc
Outdated
@@ -96,6 +127,15 @@ void* AllocFixed(void* base_address, size_t length, | |||
|
|||
bool DeallocFixed(void* base_address, size_t length, | |||
DeallocationType deallocation_type) { | |||
size_t region_begin = (size_t)base_address; |
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.
constify
So I did implement some of these recommendations. I also added a mutex to avoid race conditions in multi-threaded scenarios. |
1cf68b7
to
3e18402
Compare
3e18402
to
7f0a8b0
Compare
return munmap(base_address, length) == 0; | ||
default: | ||
assert_unhandled_case(deallocation_type); | ||
} |
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.
In a release build the compiler here complained that there is no return value for all control paths. Just add a return false at the end of the function just to make it happy for now.
As the title says. Previously the MapFileView function did not take into account the handle argument, as it used the MAP_ANONYMOUS and the MAP_PRIVATE flags.
Additionally, AllocFixed used mmap and DeallocFixed used munmap always, with the MAP_FIXED flag. This was not ideal for several reasons:
For more information regarding mmap, see:
https://man7.org/linux/man-pages/man2/mmap.2.html