15.4 SparseMemoryBacking – On-Demand Page Allocator

<< Click to Display Table of Contents >>

Navigation:  ASA-EMulatR Reference Guide > Introduction > Architecture Overview > Chapter 15 – Memory System Implementation Details >

15.4 SparseMemoryBacking – On-Demand Page Allocator

SparseMemoryBacking is the physical storage engine behind SafeMemory. It implements on-demand page allocation so that the emulator can declare a 32+ GB address space without consuming host memory for unused regions.

 

15.4.1 Design

 

Page size: 64 KB (kPageSize = 64 * 1024)

Page table: array of std::atomic<quint8*> pointers, one per page

Pages allocated on first write via ensurePage(), which uses compare_exchange_strong for thread-safe allocation

Reads from unallocated pages return zero (no page fault)

All multi-byte accesses use qFromLittleEndian / qToLittleEndian for correct byte ordering

Cross-page accesses handled via loadCrossing<T> / storeCrossing<T> template methods

Optional dirty tracking via atomic bitmap (64 pages per word) for snapshot/migration support

 


 

15.4.2 Thread Safety

 

The ensurePage() method uses a compare-and-swap pattern: it creates a new zeroed page, then atomically attempts to install it. If another thread has already allocated the page (the CAS fails), the duplicate is deleted and the existing page is returned. Page pointer loads use memory_order_acquire; stores use memory_order_release. The m_allocatedPages counter tracks the total resident page count.

 


 

15.4.3 Source Files

 

File

Lines (approx)

Content

memoryLib/SparseMemoryBacking.h

~507

Complete class: page allocation, load/store 8/16/32/64, block ops, dirty tracking, cross-page handling

 

See Also: memoryLib/MemorySpan.h (~121 lines) – Contiguous span for safe cross-page buffer access; memoryLib/SafeMemory.h – Consumer of SparseMemoryBacking.