AuroraRuntime/Source/Process/AuProcessSectionFileMapView.NT.cpp

119 lines
3.1 KiB
C++
Raw Normal View History

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ProcessSectionFileMapView.NT.cpp
Date: 2022-08-09
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuProcessSectionFileMapView.NT.hpp"
#include "AuProcessSectionViewReserved.NT.hpp"
#include "AuProcessSectionView.NT.hpp"
namespace Aurora::Process
{
ProcessSectionFileMapView::ProcessSectionFileMapView(AuUInt uAddress,
HANDLE hSection) :
uAddress_(uAddress),
hFileSection(hSection)
{
}
ProcessSectionFileMapView::~ProcessSectionFileMapView()
{
Unmap();
}
void ProcessSectionFileMapView::Unmap()
{
AU_LOCK_GUARD(this->mutex_);
if (AuAtomicLoad(&this->uInUse_))
{
SysPushErrorResourceLocked("Cannot unmap memory while it's in use");
return;
}
if (AuExchange(this->bIsDead_, true))
{
return;
}
if (this->uAddress_)
{
if (this->pSharedSectionHint)
{
SysAssert(AuStaticCast<ProcessSectionViewReserved>(this->pSharedSectionHint)->ReleaseAndCoaleceAddress(this->uOffset, this->uLength));
}
else
{
::UnmapViewOfFile((PVOID)this->uAddress_);
}
this->uAddress_ = 0;
}
AuWin32CloseHandle(this->hFileSection);
this->pSharedSectionHint.reset();
this->pProcessGlobalHint = nullptr;
}
bool ProcessSectionFileMapView::Flush(AuUInt uOffset, AuUInt uLength)
{
return ::FlushViewOfFile(this->GetPointer(uOffset), uLength);
}
AuMemoryViewWrite ProcessSectionFileMapView::ToWriteView()
{
return AuMemoryViewWrite((char *)this->uAddress_, this->uLength, &this->uInUse_);
}
AuMemoryViewRead ProcessSectionFileMapView::ToReadView()
{
return AuMemoryViewRead((char *)this->uAddress_, this->uLength, &this->uInUse_);
}
AuUInt ProcessSectionFileMapView::GetBaseAddress()
{
return this->uAddress_;
}
AuUInt ProcessSectionFileMapView::GetAddress(AuUInt uOffset)
{
return this->uAddress_ && this->uLength > uOffset ?
this->uAddress_ + uOffset :
0;
}
AuUInt8 *ProcessSectionFileMapView::GetBasePointer()
{
return AuReinterpretCast<AuUInt8 *>(this->uAddress_);
}
AuUInt8 *ProcessSectionFileMapView::GetPointer(AuUInt uOffset)
{
return this->uAddress_ && this->uLength > uOffset ?
AuReinterpretCast<AuUInt8 *>(this->uAddress_) + uOffset :
0;
}
bool ProcessSectionFileMapView::LockSwap()
{
if (!this->uAddress_)
{
return false;
}
return AuMemory::SwapLock::Lock({ { this->uAddress_, this->uLength } });
}
bool ProcessSectionFileMapView::UnlockSwap()
{
if (!this->uAddress_)
{
return false;
}
return AuMemory::SwapLock::Unlock({ { this->uAddress_, this->uLength } });
}
}