diff options
| author | Admiral H. Curtiss <pikachu025@gmail.com> | 2023-09-02 04:01:32 +0200 |
|---|---|---|
| committer | Admiral H. Curtiss <pikachu025@gmail.com> | 2023-09-02 04:02:10 +0200 |
| commit | 5bd7756064e4759fe13a557106cf9629908a04f0 (patch) | |
| tree | f90c763f93854f16c16da37b80d700ebc51d4882 /Source/Core/Common/MemArenaUnix.cpp | |
| parent | 5e5887a378db28324a8fc8825f21539525412e12 (diff) | |
Common/MemArena: Add LazyMemoryRegion to represent a zero-initialized memory region whose pages are only allocated on first access.
Diffstat (limited to 'Source/Core/Common/MemArenaUnix.cpp')
| -rw-r--r-- | Source/Core/Common/MemArenaUnix.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Source/Core/Common/MemArenaUnix.cpp b/Source/Core/Common/MemArenaUnix.cpp index 1532699276..452c2c50c8 100644 --- a/Source/Core/Common/MemArenaUnix.cpp +++ b/Source/Core/Common/MemArenaUnix.cpp @@ -16,6 +16,7 @@ #include <sys/mman.h> #include <unistd.h> +#include "Common/Assert.h" #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" @@ -108,4 +109,46 @@ void MemArena::UnmapFromMemoryRegion(void* view, size_t size) if (retval == MAP_FAILED) NOTICE_LOG_FMT(MEMMAP, "mmap failed"); } + +LazyMemoryRegion::LazyMemoryRegion() = default; + +LazyMemoryRegion::~LazyMemoryRegion() +{ + Release(); +} + +void* LazyMemoryRegion::Create(size_t size) +{ + ASSERT(!m_memory); + + void* memory = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (!memory) + { + NOTICE_LOG_FMT(MEMMAP, "Memory allocation of {} bytes failed.", size); + return nullptr; + } + + m_memory = memory; + m_size = size; + + return memory; +} + +void LazyMemoryRegion::Clear() +{ + ASSERT(m_memory); + + mmap(m_memory, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); +} + +void LazyMemoryRegion::Release() +{ + if (m_memory) + { + munmap(m_memory, m_size); + m_memory = nullptr; + m_size = 0; + } +} + } // namespace Common |
