90 lines
2.4 KiB
C++
90 lines
2.4 KiB
C++
/***
|
|
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;
|
|
}
|
|
} |