The Pedigree Project 0.1
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
MemoryMappedFile Class Reference

#include <MemoryMappedFile.h>

+ Inheritance diagram for MemoryMappedFile:
+ Collaboration diagram for MemoryMappedFile:

Public Member Functions

 MemoryMappedFile (uintptr_t address, size_t length, size_t offset, File *backing, bool bCopyOnWrite, Permissions perms, Permissions maximumPerms=Read|Write|Exec, const SharedPointer< MappingAttachment > &attachment=SharedPointer< MappingAttachment >(), const FileMappingOrigin &origin={}, bool executableUse=false)
 
virtual MemoryMappedObject * clone () override
 
virtual MemoryMappedObject * split (uintptr_t at) override
 
MemoryMappedObject * stageSlice (uintptr_t source, size_t sourceLength, uintptr_t destination, size_t destinationLength) override
 
void releaseDetachedPage (uintptr_t oldAddress, const VirtualAddressSpace::DetachedPage &page) override
 
void discardRange (VirtualAddressSpace &space, uintptr_t base, size_t length) override
 
File * backingFile () const override
 
bool backingRangeValid (uintptr_t source, size_t length) const override
 
bool resident (VirtualAddressSpace &space, uintptr_t address, FileResidencyAccess access, void *credentials) override
 
virtual bool remove (size_t length) override
 
virtual void setPermissions (MemoryMappedObject::Permissions perms) override
 
virtual bool sync (uintptr_t at, bool async) override
 
bool syncRange (uintptr_t at, size_t length, bool async) override
 
virtual void invalidate (uintptr_t at) override
 
virtual bool sharedBacking (uintptr_t at, uintptr_t &identity, size_t &offset) const override
 
virtual bool usesBacking (uintptr_t identity) const override
 
virtual bool beyondBackingEnd (uintptr_t at) const override
 
virtual void discardFilePages (VirtualAddressSpace &space, size_t end) override
 
virtual bool preparePermissions (uintptr_t base, size_t length, Permissions perms) override
 
virtual void unmap () override
 
virtual bool trap (VirtualAddressSpace &space, uintptr_t address, bool bWrite, PopulationStatus *population=nullptr) override
 
virtual bool compact () override
 
- Public Member Functions inherited from MemoryMappedObject
 MemoryMappedObject (uintptr_t address, bool bCopyOnWrite, size_t length, Permissions perms, Permissions maximumPerms=Read|Write|Exec)
 
void setMappingOwnership (bool ownsMappings)
 
virtual PopulationStatus prepareResidentAccess (VirtualAddressSpace &space, uintptr_t address)
 
PopulationStatus populatePage (VirtualAddressSpace &space, uintptr_t address)
 
MemoryLockMode lockMode () const
 
virtual bool supportsPageOut (VirtualAddressSpace &space, uintptr_t address)
 
virtual SwapStatus pageOutAt (VirtualAddressSpace &space, uintptr_t address, bool &released)
 
virtual bool reclaimAnonymousPage (VirtualAddressSpace &space)
 
virtual SwapStatus restoreSwapPages (VirtualAddressSpace &space)
 
bool matches (uintptr_t address)
 
uintptr_t address () const
 
size_t length () const
 
Permissions permissions () const
 
Permissions maximumPermissions () const
 

Private Member Functions

void unmapUnlocked ()
 
physical_uintptr_t getBackingPage (size_t fileOffset, size_t mappingBytes)
 
void releaseDetachedPageUnlocked (uintptr_t oldAddress, const VirtualAddressSpace::DetachedPage &page)
 
void trackMapping (uintptr_t, physical_uintptr_t)
 
void untrackMapping (uintptr_t)
 
physical_uintptr_t getMapping (uintptr_t)
 
size_t getMappingCount ()
 
void clearMappings ()
 

Private Attributes

File * m_pBacking
 
size_t m_Offset
 
size_t m_ReadAheadEnd = ~size_t{0}
 
size_t m_ReadAheadPages = 4
 
Tree< uintptr_t, physical_uintptr_t > m_Mappings
 
FileMappingOrigin m_Origin
 
Mutex m_Lock
 
bool m_bVfsLease
 
bool m_ExecutableUse
 
bool m_SharedWriteUse
 
bool m_UseAdmitted
 

Friends

class MemoryMapManager
 

Additional Inherited Members

- Public Types inherited from MemoryMappedObject
typedef int Permissions
 
- Static Public Attributes inherited from MemoryMappedObject
static const int None = 0x0
 
static const int Read = 0x1
 
static const int Write = 0x2
 
static const int Exec = 0x4
 
- Protected Attributes inherited from MemoryMappedObject
bool m_bCopyOnWrite
 
uintptr_t m_Address
 
size_t m_Length
 
Permissions m_Permissions
 
Permissions m_MaximumPermissions
 
SharedPointer< MappingAttachment > m_Attachment
 
Process * m_OwnerProcess
 
bool m_OwnsMappings
 
MemoryLockMode m_LockMode
 

Detailed Description

File map object.

File maps actually provide a backing file for a memory region. These are used for loading binaries or for opening files for reading without the overhead of read/write syscalls (assuming the syscall is more expensive than a page fault).

Definition at line 442 of file MemoryMappedFile.h.

Constructor & Destructor Documentation

◆ MemoryMappedFile()

MemoryMappedFile::MemoryMappedFile ( uintptr_t  address,
size_t  length,
size_t  offset,
File *  backing,
bool  bCopyOnWrite,
MemoryMappedObject::Permissions  perms,
MemoryMappedObject::Permissions  maximumPerms = Read | Write | Exec,
const SharedPointer< MappingAttachment > &  attachment = SharedPointer<MappingAttachment>(),
const FileMappingOrigin &  origin = {},
bool  executableUse = false 
)

Definition at line 176 of file MemoryMappedFile.cc.

◆ ~MemoryMappedFile()

MemoryMappedFile::~MemoryMappedFile ( )
overridevirtual

Definition at line 195 of file MemoryMappedFile.cc.

Member Function Documentation

◆ backingFile()

File * MemoryMappedFile::backingFile ( ) const
inlineoverridevirtual

Reimplemented from MemoryMappedObject.

Definition at line 461 of file MemoryMappedFile.h.

◆ backingRangeValid()

bool MemoryMappedFile::backingRangeValid ( uintptr_t  source,
size_t  length 
) const
overridevirtual

Reimplemented from MemoryMappedObject.

Definition at line 13 of file MemoryMappedFile-remap.cc.

◆ beyondBackingEnd()

bool MemoryMappedFile::beyondBackingEnd ( uintptr_t  at) const
overridevirtual

Reimplemented from MemoryMappedObject.

Definition at line 382 of file MemoryMappedFile.cc.

◆ clearMappings()

void MemoryMappedFile::clearMappings ( )
private

Clear all mappings.

Definition at line 778 of file MemoryMappedFile.cc.

References Tree< K, E >::clear(), and m_Mappings.

◆ clone()

MemoryMappedObject * MemoryMappedFile::clone ( )
overridevirtual

Clones the existing metadata of this object into another.

Returns a MemoryMappedObject that exactly matches this one, but which can be used for reference in a new address space. Used for address space clones.

Note that mappings are automatically cloned - only clone metadata in this method.

Implements MemoryMappedObject.

Definition at line 208 of file MemoryMappedFile.cc.

References assert, Tree< K, E >::begin(), Tree< K, E >::end(), File::getPhysicalPage(), MemoryMappedObject::m_Address, MemoryMappedObject::m_bCopyOnWrite, MemoryMappedObject::m_Length, m_Lock, m_Mappings, m_Offset, m_pBacking, and MemoryMappedObject::m_Permissions.

◆ compact()

bool MemoryMappedFile::compact ( )
overridevirtual

Syncs back all dirty pages to their respective backing store.

Compacting begins by doing a pass to find any pages that can be synced and then unpinned, allowing a future Cache eviction. Should that pass successfully free memory, the compact will be considered successful.

If that pass is not successful, read-only pages are evicted. At this stage, this is done in a linear fashion; there is no LRU or other type of algorithm at play.

Todo:
Improve this.
Returns
true if at least one page was released, false otherwise.

Reimplemented from MemoryMappedObject.

Definition at line 688 of file MemoryMappedFile.cc.

References MemoryMappedObject::address(), VirtualAddressSpace::detachMapping(), getMapping(), PhysicalMemoryManager::getPageSize(), ProcessorBase::information(), MemoryMappedObject::m_Address, MemoryMappedObject::m_bCopyOnWrite, MemoryMappedObject::m_Length, m_Lock, m_Offset, m_pBacking, Semaphore::release(), File::returnPhysicalPage(), File::sync(), Semaphore::tryAcquire(), File::tryBeginMappingRelease(), and untrackMapping().

◆ discardFilePages()

void MemoryMappedFile::discardFilePages ( VirtualAddressSpace &  space,
size_t  end 
)
overridevirtual

Reimplemented from MemoryMappedObject.

Definition at line 390 of file MemoryMappedFile.cc.

◆ discardRange()

void MemoryMappedFile::discardRange ( VirtualAddressSpace &  space,
uintptr_t  base,
size_t  length 
)
overridevirtual

Implements MemoryMappedObject.

Definition at line 57 of file MemoryMappedFile-advice.cc.

◆ getBackingPage()

physical_uintptr_t MemoryMappedFile::getBackingPage ( size_t  fileOffset,
size_t  mappingBytes 
)
private

Definition at line 423 of file MemoryMappedFile.cc.

◆ getMapping()

physical_uintptr_t MemoryMappedFile::getMapping ( uintptr_t  addr)
private

Get a specific mapping.

Definition at line 770 of file MemoryMappedFile.cc.

References Tree< K, E >::lookup(), and m_Mappings.

Referenced by compact(), remove(), split(), sync(), and syncRange().

+ Here is the caller graph for this function:

◆ getMappingCount()

size_t MemoryMappedFile::getMappingCount ( )
private

Get the number of mappings we currently have.

Definition at line 774 of file MemoryMappedFile.cc.

References Tree< K, E >::count(), and m_Mappings.

◆ invalidate()

void MemoryMappedFile::invalidate ( uintptr_t  at)
overridevirtual

Invalidate cached state without discarding private modifications.

Reimplemented from MemoryMappedObject.

Definition at line 517 of file MemoryMappedFile.cc.

◆ preparePermissions()

bool MemoryMappedFile::preparePermissions ( uintptr_t  base,
size_t  length,
Permissions  perms 
)
overridevirtual

Reimplemented from MemoryMappedObject.

Definition at line 354 of file MemoryMappedFile.cc.

◆ releaseDetachedPage()

void MemoryMappedFile::releaseDetachedPage ( uintptr_t  oldAddress,
const VirtualAddressSpace::DetachedPage &  page 
)
overridevirtual

Implements MemoryMappedObject.

Definition at line 51 of file MemoryMappedFile-advice.cc.

◆ releaseDetachedPageUnlocked()

void MemoryMappedFile::releaseDetachedPageUnlocked ( uintptr_t  oldAddress,
const VirtualAddressSpace::DetachedPage &  page 
)
private

Definition at line 35 of file MemoryMappedFile-advice.cc.

◆ remove()

bool MemoryMappedFile::remove ( size_t  length)
overridevirtual

◆ resident()

bool MemoryMappedFile::resident ( VirtualAddressSpace &  space,
uintptr_t  address,
FileResidencyAccess  access,
void *  credentials 
)
overridevirtual

Reimplemented from MemoryMappedObject.

Definition at line 17 of file MemoryMappedFile-advice.cc.

◆ setPermissions()

void MemoryMappedFile::setPermissions ( MemoryMappedObject::Permissions  perms)
overridevirtual

◆ sharedBacking()

bool MemoryMappedFile::sharedBacking ( uintptr_t  at,
uintptr_t &  identity,
size_t &  offset 
) const
overridevirtual

Reimplemented from MemoryMappedObject.

Definition at line 369 of file MemoryMappedFile.cc.

◆ split()

MemoryMappedObject * MemoryMappedFile::split ( uintptr_t  at)
overridevirtual

◆ stageSlice()

MemoryMappedObject * MemoryMappedFile::stageSlice ( uintptr_t  source,
size_t  sourceLength,
uintptr_t  destination,
size_t  destinationLength 
)
overridevirtual

◆ sync()

bool MemoryMappedFile::sync ( uintptr_t  at,
bool  async 
)
overridevirtual

Sync back the given page to a backing store, if one exists.

Reimplemented from MemoryMappedObject.

Definition at line 507 of file MemoryMappedFile.cc.

References getMapping(), MemoryMappedObject::m_Address, MemoryMappedObject::m_bCopyOnWrite, MemoryMappedObject::m_Length, m_Lock, m_Offset, m_pBacking, and File::sync().

◆ syncRange()

bool MemoryMappedFile::syncRange ( uintptr_t  at,
size_t  length,
bool  async 
)
overridevirtual

◆ trackMapping()

void MemoryMappedFile::trackMapping ( uintptr_t  addr,
physical_uintptr_t  phys 
)
private

Track a new mapping.

Definition at line 762 of file MemoryMappedFile.cc.

References Tree< K, E >::insert(), and m_Mappings.

Referenced by split().

+ Here is the caller graph for this function:

◆ trap()

bool MemoryMappedFile::trap ( VirtualAddressSpace &  space,
uintptr_t  address,
bool  bWrite,
PopulationStatus *  population = nullptr 
)
overridevirtual

◆ unmap()

void MemoryMappedFile::unmap ( )
overridevirtual

Unmaps existing mappings in this object from the address space.

Implementations are expected to track these as necessary for the implementation.

Implements MemoryMappedObject.

Definition at line 522 of file MemoryMappedFile.cc.

References m_Lock.

◆ unmapUnlocked()

void MemoryMappedFile::unmapUnlocked ( )
private

Definition at line 726 of file MemoryMappedFile.cc.

◆ untrackMapping()

void MemoryMappedFile::untrackMapping ( uintptr_t  addr)
private

Stop tracking a mapping.

Definition at line 766 of file MemoryMappedFile.cc.

References m_Mappings, and Tree< K, E >::remove().

Referenced by compact(), remove(), split(), and trap().

+ Here is the caller graph for this function:

◆ usesBacking()

bool MemoryMappedFile::usesBacking ( uintptr_t  identity) const
overridevirtual

Reimplemented from MemoryMappedObject.

Definition at line 378 of file MemoryMappedFile.cc.

Friends And Related Symbol Documentation

◆ MemoryMapManager

friend class MemoryMapManager
friend

Definition at line 443 of file MemoryMappedFile.h.

Member Data Documentation

◆ m_bVfsLease

bool MemoryMappedFile::m_bVfsLease
private

Whether this mapping retained an established VFS File owner.

Definition at line 547 of file MemoryMappedFile.h.

◆ m_ExecutableUse

bool MemoryMappedFile::m_ExecutableUse
private

Definition at line 548 of file MemoryMappedFile.h.

◆ m_Lock

Mutex MemoryMappedFile::m_Lock
private

Lock for anything to do with the memory mapped file.

Backing-file reads and synchronisation may block, so this must be a sleeping lock rather than a Spinlock.

Definition at line 544 of file MemoryMappedFile.h.

Referenced by clone(), compact(), remove(), setPermissions(), split(), sync(), syncRange(), trap(), and unmap().

◆ m_Mappings

Tree<uintptr_t, physical_uintptr_t> MemoryMappedFile::m_Mappings
private

◆ m_Offset

size_t MemoryMappedFile::m_Offset
private

Offset within the file that this mapping begins at.

Definition at line 529 of file MemoryMappedFile.h.

Referenced by clone(), compact(), remove(), setPermissions(), split(), stageSlice(), sync(), syncRange(), and trap().

◆ m_Origin

FileMappingOrigin MemoryMappedFile::m_Origin
private

Definition at line 536 of file MemoryMappedFile.h.

◆ m_pBacking

File* MemoryMappedFile::m_pBacking
private

Backing file.

Definition at line 526 of file MemoryMappedFile.h.

Referenced by clone(), compact(), remove(), setPermissions(), split(), stageSlice(), sync(), syncRange(), and trap().

◆ m_ReadAheadEnd

size_t MemoryMappedFile::m_ReadAheadEnd = ~size_t{0}
private

Definition at line 531 of file MemoryMappedFile.h.

◆ m_ReadAheadPages

size_t MemoryMappedFile::m_ReadAheadPages = 4
private

Definition at line 532 of file MemoryMappedFile.h.

◆ m_SharedWriteUse

bool MemoryMappedFile::m_SharedWriteUse
private

Definition at line 549 of file MemoryMappedFile.h.

◆ m_UseAdmitted

bool MemoryMappedFile::m_UseAdmitted
private

Definition at line 550 of file MemoryMappedFile.h.


The documentation for this class was generated from the following files: