AuroraRuntime/Source/Memory/SwapLock.cpp
2022-03-30 15:15:50 +01:00

70 lines
2.2 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: SwapLock.cpp
Date: 2022-3-21
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Memory.hpp"
#include "SwapLock.hpp"
#include <Source/HWInfo/HWInfo.hpp>
#if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/mman.h>
#endif
namespace Aurora::Memory::SwapLock
{
AUKN_SYM bool Lock(const AuList<AuPair<AuUInt, AuUInt>> &addressRanges)
{
for (const auto &addressRange : addressRanges)
{
auto base = AuPageRound(addressRange.first, AuUInt(AuHwInfo::gPageSize));
auto length = AuPageRoundUp(addressRange.second + (addressRange.first - base), AuUInt(AuHwInfo::gPageSize));
#if defined(AURORA_IS_MODERNNT_DERIVED)
if (!VirtualLock(AuReinterpretCast<LPVOID>(base), length))
{
SysPushErrorHAL("Couldn't lock memory 0x{:x} {}", base, length);
return false;
}
#endif
#if defined(AURORA_IS_POSIX_DERIVED)
if (mlock(AuReinterpretCast<const void *>(base), length) != 0)
{
SysPushErrorHAL("Couldn't lock memory 0x{:x} {}", base, length);
return false;
}
#endif
}
return true;
}
AUKN_SYM bool Unlock(const AuList<AuPair<AuUInt, AuUInt>> &addressRanges)
{
for (const auto &addressRange : addressRanges)
{
auto base = AuPageRound(addressRange.first, AuUInt(AuHwInfo::gPageSize));
auto length = AuPageRoundUp(addressRange.second + (addressRange.first - base), AuUInt(AuHwInfo::gPageSize));
#if defined(AURORA_IS_MODERNNT_DERIVED)
if (!VirtualUnlock(AuReinterpretCast<LPVOID>(base), length))
{
SysPushErrorHAL("Couldn't unlock memory 0x{:x} {}", base, length);
}
#endif
#if defined(AURORA_IS_POSIX_DERIVED)
if (munlock(AuReinterpretCast<const void *>(base), length) != 0)
{
SysPushErrorHAL("Couldn't unlock memory 0x{:x} {}", base, length);
}
#endif
}
return true;
}
}