diff options
Diffstat (limited to 'src/common/host_memory.cpp')
| -rw-r--r-- | src/common/host_memory.cpp | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index eb50fbd9f..8a328f916 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp | |||
| @@ -27,6 +27,7 @@ | |||
| 27 | 27 | ||
| 28 | #include <mutex> | 28 | #include <mutex> |
| 29 | 29 | ||
| 30 | #include "common/alignment.h" | ||
| 30 | #include "common/assert.h" | 31 | #include "common/assert.h" |
| 31 | #include "common/host_memory.h" | 32 | #include "common/host_memory.h" |
| 32 | #include "common/logging/log.h" | 33 | #include "common/logging/log.h" |
| @@ -35,6 +36,7 @@ | |||
| 35 | namespace Common { | 36 | namespace Common { |
| 36 | 37 | ||
| 37 | constexpr size_t PageAlignment = 0x1000; | 38 | constexpr size_t PageAlignment = 0x1000; |
| 39 | constexpr size_t HugePageSize = 0x200000; | ||
| 38 | 40 | ||
| 39 | #ifdef _WIN32 | 41 | #ifdef _WIN32 |
| 40 | 42 | ||
| @@ -385,9 +387,16 @@ private: | |||
| 385 | 387 | ||
| 386 | #endif | 388 | #endif |
| 387 | 389 | ||
| 388 | HostMemory::HostMemory(size_t backing_size, size_t virtual_size) | 390 | HostMemory::HostMemory(size_t backing_size_, size_t virtual_size_) |
| 389 | : impl{std::make_unique<HostMemory::Impl>(backing_size, virtual_size)}, | 391 | : backing_size(backing_size_), |
| 390 | backing_base{impl->backing_base}, virtual_base{impl->virtual_base} {} | 392 | virtual_size(virtual_size_), impl{std::make_unique<HostMemory::Impl>( |
| 393 | AlignUp(backing_size, PageAlignment), | ||
| 394 | AlignUp(virtual_size, PageAlignment) + 3 * HugePageSize)}, | ||
| 395 | backing_base{impl->backing_base}, virtual_base{impl->virtual_base} { | ||
| 396 | virtual_base += 2 * HugePageSize - 1; | ||
| 397 | virtual_base -= reinterpret_cast<size_t>(virtual_base) & (HugePageSize - 1); | ||
| 398 | virtual_base_offset = virtual_base - impl->virtual_base; | ||
| 399 | } | ||
| 391 | 400 | ||
| 392 | HostMemory::~HostMemory() = default; | 401 | HostMemory::~HostMemory() = default; |
| 393 | 402 | ||
| @@ -399,32 +408,32 @@ void HostMemory::Map(size_t virtual_offset, size_t host_offset, size_t length) { | |||
| 399 | ASSERT(virtual_offset % PageAlignment == 0); | 408 | ASSERT(virtual_offset % PageAlignment == 0); |
| 400 | ASSERT(host_offset % PageAlignment == 0); | 409 | ASSERT(host_offset % PageAlignment == 0); |
| 401 | ASSERT(length % PageAlignment == 0); | 410 | ASSERT(length % PageAlignment == 0); |
| 402 | ASSERT(virtual_offset + length <= impl->virtual_size); | 411 | ASSERT(virtual_offset + length <= virtual_size); |
| 403 | ASSERT(host_offset + length <= impl->backing_size); | 412 | ASSERT(host_offset + length <= backing_size); |
| 404 | if (length == 0) { | 413 | if (length == 0) { |
| 405 | return; | 414 | return; |
| 406 | } | 415 | } |
| 407 | impl->Map(virtual_offset, host_offset, length); | 416 | impl->Map(virtual_offset + virtual_base_offset, host_offset, length); |
| 408 | } | 417 | } |
| 409 | 418 | ||
| 410 | void HostMemory::Unmap(size_t virtual_offset, size_t length) { | 419 | void HostMemory::Unmap(size_t virtual_offset, size_t length) { |
| 411 | ASSERT(virtual_offset % PageAlignment == 0); | 420 | ASSERT(virtual_offset % PageAlignment == 0); |
| 412 | ASSERT(length % PageAlignment == 0); | 421 | ASSERT(length % PageAlignment == 0); |
| 413 | ASSERT(virtual_offset + length <= impl->virtual_size); | 422 | ASSERT(virtual_offset + length <= virtual_size); |
| 414 | if (length == 0) { | 423 | if (length == 0) { |
| 415 | return; | 424 | return; |
| 416 | } | 425 | } |
| 417 | impl->Unmap(virtual_offset, length); | 426 | impl->Unmap(virtual_offset + virtual_base_offset, length); |
| 418 | } | 427 | } |
| 419 | 428 | ||
| 420 | void HostMemory::Protect(size_t virtual_offset, size_t length, bool read, bool write) { | 429 | void HostMemory::Protect(size_t virtual_offset, size_t length, bool read, bool write) { |
| 421 | ASSERT(virtual_offset % PageAlignment == 0); | 430 | ASSERT(virtual_offset % PageAlignment == 0); |
| 422 | ASSERT(length % PageAlignment == 0); | 431 | ASSERT(length % PageAlignment == 0); |
| 423 | ASSERT(virtual_offset + length <= impl->virtual_size); | 432 | ASSERT(virtual_offset + length <= virtual_size); |
| 424 | if (length == 0) { | 433 | if (length == 0) { |
| 425 | return; | 434 | return; |
| 426 | } | 435 | } |
| 427 | impl->Protect(virtual_offset, length, read, write); | 436 | impl->Protect(virtual_offset + virtual_base_offset, length, read, write); |
| 428 | } | 437 | } |
| 429 | 438 | ||
| 430 | } // namespace Common | 439 | } // namespace Common |