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
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
ProcessSectionFileMapView::ProcessSectionFileMapView(AuUInt uAddress,
|
|
|
|
AuUInt uLength,
|
|
|
|
bool bShouldUnmap,
|
|
|
|
int optFd) :
|
|
|
|
uAddress_(uAddress),
|
2022-08-10 11:12:36 +00:00
|
|
|
uLength_(uLength),
|
|
|
|
bShouldUnmap_(bShouldUnmap),
|
|
|
|
optFd_(optFd)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessSectionFileMapView::~ProcessSectionFileMapView()
|
|
|
|
{
|
|
|
|
Unmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 11:12:36 +00:00
|
|
|
if (AuExchange(this->bIsDead_, true))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
if (this->uAddress_)
|
2022-08-10 11:12:36 +00:00
|
|
|
{
|
2022-12-16 00:41:01 +00:00
|
|
|
if (this->pSharedSectionHint)
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
AuStaticCast<ProcessSectionViewReserved>(this->pSharedSectionHint)->Release(this->uAddress_);
|
2022-12-16 00:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
this->uAddress_ = 0;
|
2022-08-10 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
// TODO: posix explicitly states this isnt necessary
|
2022-08-10 11:12:36 +00:00
|
|
|
int fd {-1};
|
|
|
|
if ((fd = AuExchange(this->optFd_, -1)) != -1)
|
|
|
|
{
|
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
bool ProcessSectionFileMapView::Flush(AuUInt uOffset, AuUInt uLength)
|
|
|
|
{
|
|
|
|
return ::msync(this->GetPointer(uOffset), uLength, MS_SYNC) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuMemoryViewWrite ProcessSectionFileMapView::ToWriteView()
|
|
|
|
{
|
|
|
|
return AuMemoryViewWrite((char *)this->uAddress_, this->uLength_, &this->uInUse_);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuMemoryViewRead ProcessSectionFileMapView::ToReadView()
|
2022-08-10 11:12:36 +00:00
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
return AuMemoryViewRead((char *)this->uAddress_, this->uLength_, &this->uInUse_);
|
2022-08-10 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt ProcessSectionFileMapView::GetBaseAddress()
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
return this->uAddress_;
|
2022-08-10 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
AuUInt ProcessSectionFileMapView::GetAddress(AuUInt uOffset)
|
2022-08-10 11:12:36 +00:00
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
return this->uAddress_ && this->uLength_ > uOffset ?
|
|
|
|
this->uAddress_ + uOffset :
|
|
|
|
0;
|
2022-08-10 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt8 *ProcessSectionFileMapView::GetBasePointer()
|
|
|
|
{
|
2024-04-12 12:12:35 +00:00
|
|
|
return AuReinterpretCast<AuUInt8 *>(this->uAddress_);
|
2022-08-10 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 12:12:35 +00:00
|
|
|
AuUInt8 *ProcessSectionFileMapView::GetPointer(AuUInt uOffset)
|
2022-08-10 11:12:36 +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 11:12:36 +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 11:12:36 +00:00
|
|
|
}
|