AuroraRuntime/Source/Process/AuProcessSectionFileMapView.Unix.cpp

110 lines
2.8 KiB
C++
Raw Normal View History

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuProcessSectionFileMapView.Unix.cpp
Date: 2022-08-09
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuProcessSectionFileMapView.Unix.hpp"
#include "AuProcessSectionViewReserved.Unix.hpp"
#include "AuProcessSectionView.Unix.hpp"
#include <sys/mman.h>
namespace Aurora::Process
{
ProcessSectionFileMapView::ProcessSectionFileMapView(AuUInt uAddress, AuUInt uLength, bool bShouldUnmap, int optFd) :
uAddress(uAddress),
uLength_(uLength),
bShouldUnmap_(bShouldUnmap),
optFd_(optFd)
{
}
ProcessSectionFileMapView::~ProcessSectionFileMapView()
{
Unmap();
}
void ProcessSectionFileMapView::Unmap()
{
if (AuExchange(this->bIsDead_, true))
{
return;
}
if (this->uAddress)
{
if (this->pSharedSectionHint)
{
AuStaticCast<ProcessSectionViewReserved>(this->pSharedSectionHint)->Release(this->uAddress);
}
if (this->bShouldUnmap_)
{
::munmap(this->GetBasePointer(), this->uLength_);
}
else
{
if (!::mmap(this->GetBasePointer(), this->uLength_, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, 0, 0))
{
::mmap(this->GetBasePointer(), this->uLength_, PROT_READ /*coping*/, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, 0, 0);
}
}
this->uAddress = 0;
}
int fd {-1};
if ((fd = AuExchange(this->optFd_, -1)) != -1)
{
::close(fd);
}
}
bool ProcessSectionFileMapView::Flush(AuUInt offset, AuUInt length)
{
return ::msync(this->GetPointer(offset), length, MS_SYNC) == 0;
}
AuUInt ProcessSectionFileMapView::GetBaseAddress()
{
return this->uAddress;
}
AuUInt ProcessSectionFileMapView::GetAddress(AuUInt offset)
{
return this->uAddress ? this->uAddress + offset : 0;
}
AuUInt8 *ProcessSectionFileMapView::GetBasePointer()
{
return AuReinterpretCast<AuUInt8 *>(this->uAddress);
}
AuUInt8 *ProcessSectionFileMapView::GetPointer(AuUInt offset)
{
return this->uAddress ? AuReinterpretCast<AuUInt8 *>(this->uAddress) + offset : 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_ } });
}
}