2022-08-10 09:50:49 +00:00
|
|
|
/***
|
|
|
|
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>
|
2022-12-17 20:14:19 +00:00
|
|
|
#include "AuProcessSectionFileMapView.NT.hpp"
|
|
|
|
#include "AuProcessSectionViewReserved.NT.hpp"
|
|
|
|
#include "AuProcessSectionView.NT.hpp"
|
2022-08-10 09:50:49 +00:00
|
|
|
|
|
|
|
namespace Aurora::Process
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
ProcessSectionFileMapView::ProcessSectionFileMapView(AuUInt uAddress,
|
|
|
|
HANDLE hSection) :
|
|
|
|
uAddress_(uAddress),
|
|
|
|
hFileSection(hSection)
|
2022-08-10 09:50:49 +00:00
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
ProcessSectionFileMapView::~ProcessSectionFileMapView()
|
2022-08-10 09:50:49 +00:00
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
Unmap();
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessSectionFileMapView::Unmap()
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex_);
|
|
|
|
|
|
|
|
if (AuAtomicLoad(&this->uInUse_))
|
|
|
|
{
|
|
|
|
SysPushErrorResourceLocked("Cannot unmap memory while it's in use");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-10 09:50:49 +00:00
|
|
|
if (AuExchange(this->bIsDead_, true))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2024-04-12 12:12:35 +00:00
|
|
|
|
|
|
|
if (this->uAddress_)
|
2022-08-10 09:50:49 +00:00
|
|
|
{
|
2022-09-30 22:47:18 +00:00
|
|
|
if (this->pSharedSectionHint)
|
|
|
|
{
|
|
|
|
SysAssert(AuStaticCast<ProcessSectionViewReserved>(this->pSharedSectionHint)->ReleaseAndCoaleceAddress(this->uOffset, this->uLength));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
::UnmapViewOfFile((PVOID)this->uAddress_);
|
2022-09-30 22:47:18 +00:00
|
|
|
}
|
2024-04-12 12:12:35 +00:00
|
|
|
this->uAddress_ = 0;
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuWin32CloseHandle(this->hFileSection);
|
2022-09-30 22:47:18 +00:00
|
|
|
this->pSharedSectionHint.reset();
|
|
|
|
this->pProcessGlobalHint = nullptr;
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
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()
|
2022-08-10 09:50:49 +00:00
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
return AuMemoryViewRead((char *)this->uAddress_, this->uLength, &this->uInUse_);
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt ProcessSectionFileMapView::GetBaseAddress()
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
return this->uAddress_;
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
AuUInt ProcessSectionFileMapView::GetAddress(AuUInt uOffset)
|
2022-08-10 09:50:49 +00:00
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
return this->uAddress_ && this->uLength > uOffset ?
|
|
|
|
this->uAddress_ + uOffset :
|
|
|
|
0;
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt8 *ProcessSectionFileMapView::GetBasePointer()
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
return AuReinterpretCast<AuUInt8 *>(this->uAddress_);
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
AuUInt8 *ProcessSectionFileMapView::GetPointer(AuUInt uOffset)
|
2022-08-10 09:50:49 +00:00
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
return this->uAddress_ && this->uLength > uOffset ?
|
|
|
|
AuReinterpretCast<AuUInt8 *>(this->uAddress_) + uOffset :
|
|
|
|
0;
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|
2023-04-17 14:55:51 +00:00
|
|
|
|
|
|
|
bool ProcessSectionFileMapView::LockSwap()
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
if (!this->uAddress_)
|
2023-04-17 14:55:51 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
return AuMemory::SwapLock::Lock({ { this->uAddress_, this->uLength } });
|
2023-04-17 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessSectionFileMapView::UnlockSwap()
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
if (!this->uAddress_)
|
2023-04-17 14:55:51 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
return AuMemory::SwapLock::Unlock({ { this->uAddress_, this->uLength } });
|
2023-04-17 14:55:51 +00:00
|
|
|
}
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|