2022-08-10 11:12:36 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
File: AuProcessSectionFileMapView.Unix.cpp
|
2022-08-10 11:12:36 +00:00
|
|
|
Date: 2022-08-09
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2022-12-17 20:14:19 +00:00
|
|
|
#include "AuProcessSectionFileMapView.Unix.hpp"
|
|
|
|
#include "AuProcessSectionViewReserved.Unix.hpp"
|
|
|
|
#include "AuProcessSectionView.Unix.hpp"
|
2022-08-10 11:12:36 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
if (this->uAddress)
|
2022-08-10 11:12:36 +00:00
|
|
|
{
|
2022-12-16 00:41:01 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 11:12:36 +00:00
|
|
|
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;
|
|
|
|
}
|
2023-04-17 14:55:51 +00:00
|
|
|
|
|
|
|
bool ProcessSectionFileMapView::LockSwap()
|
|
|
|
{
|
|
|
|
if (!this->uAddress)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
return AuMemory::SwapLock::Lock({ { this->uAddress, this->uLength_ } });
|
2023-04-17 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessSectionFileMapView::UnlockSwap()
|
|
|
|
{
|
|
|
|
if (!this->uAddress)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
return AuMemory::SwapLock::Unlock({ { this->uAddress, this->uLength_ } });
|
2023-04-17 14:55:51 +00:00
|
|
|
}
|
2022-08-10 11:12:36 +00:00
|
|
|
}
|