Documentation
¶
Index ¶
- Constants
- Variables
- func CloseUpvalue(ptr Ptr, val Ptr) error
- func CompareAndSwapPtr(addr Ptr, old, new Ptr) (bool, error)
- func InitCabinet()
- func InitSwap() error
- func MemCpy(src Ptr, dst Ptr, size int) error
- func Read(src Ptr, size int) ([]byte, error)
- func ReadArrayLength(arrayPtr Ptr) (int, error)
- func ReadBoolean(ptr Ptr) (bool, error)
- func ReadBuiltin(ptr Ptr) (int, error)
- func ReadClosure(ptr Ptr) (Ptr, []Ptr, error)
- func ReadCompiledFunction(ptr Ptr) ([]byte, int, int, error)
- func ReadCompiledFunctionMeta(ptr Ptr) (int, int, error)
- func ReadError(ptr Ptr) (Ptr, Ptr, int, int, error)
- func ReadFloat(ptr Ptr) (float64, error)
- func ReadHashCount(hashPtr Ptr) (int, error)
- func ReadHashPair(hashPtr Ptr, index int) (Ptr, Ptr, error)
- func ReadInteger(ptr Ptr) (int64, error)
- func ReadModule(ptr Ptr) (Ptr, Ptr, error)
- func ReadPointer(ptr Ptr) (uint64, error)
- func ReadResource(ptr Ptr) (int64, error)
- func ReadSchema(ptr Ptr) (name Ptr, fields Ptr, err error)
- func ReadString(ptr Ptr) (string, error)
- func Restore(filename string) error
- func SetupTrayPointers(d *Drawer)
- func Snapshot(filename string) error
- func StartGC(interval time.Duration)
- func StorePtr(addr Ptr, val Ptr) error
- func Write(dst Ptr, data []byte) error
- func WriteArrayElement(arrayPtr Ptr, index int, valuePtr Ptr) error
- func WriteFloat(ptr Ptr, value float64) error
- func WriteHashPair(hashPtr Ptr, index int, key, value Ptr) error
- func WriteInteger(ptr Ptr, value int64) error
- func WriteModuleExports(ptr Ptr, exports Ptr) error
- func WriteModuleInit(ptr Ptr, initFn Ptr) error
- func WriteStructField(ptr Ptr, index int, val Ptr) error
- type Allocator
- type Arena
- type Cabinet
- func (c *Cabinet) AcquireDrawer(unitID int) (*DrawerLease, error)
- func (c *Cabinet) Alloc(size int) (Ptr, error)
- func (c *Cabinet) CommitDrawer(lease *DrawerLease) error
- func (c *Cabinet) LFUAging()
- func (c *Cabinet) MarkAndCompact(roots []*Ptr) error
- func (c *Cabinet) RestoreDrawer(drawerID int, snapshotID int64) error
- func (c *Cabinet) RollbackDrawer(lease *DrawerLease) error
- func (c *Cabinet) SnapshotDrawer(drawerID int) (int64, error)
- type Drawer
- type DrawerLease
- type Header
- type Ptr
- func AllocArray(length int, capacity int) (Ptr, error)
- func AllocBoolean(value bool) (Ptr, error)
- func AllocBuiltin(index int) (Ptr, error)
- func AllocClosure(fnPtr Ptr, freeVars []Ptr) (Ptr, error)
- func AllocCompiledFunction(instructions []byte, numLocals, numParams int) (Ptr, error)
- func AllocError(message Ptr, code Ptr, line, col int) (Ptr, error)
- func AllocFloat(value float64) (Ptr, error)
- func AllocHash(count int) (Ptr, error)
- func AllocInteger(value int64) (Ptr, error)
- func AllocModule(initFn Ptr, exports Ptr) (Ptr, error)
- func AllocNull() (Ptr, error)
- func AllocPointer(addr uint64) (Ptr, error)
- func AllocResource(id int64) (Ptr, error)
- func AllocSchema(name Ptr, fields Ptr) (Ptr, error)
- func AllocString(s string) (Ptr, error)
- func AllocStruct(schema Ptr, fieldCount int) (Ptr, error)
- func AllocUpvalue(stackIdx int) (Ptr, error)
- func LoadPtr(addr Ptr) (Ptr, error)
- func MoveObject(src Ptr, size int) (Ptr, error)
- func NewPtr(drawerID int, offset uint32) Ptr
- func ReadArrayElement(arrayPtr Ptr, index int) (Ptr, error)
- func ReadStructField(ptr Ptr, index int) (Ptr, error)
- func ReadStructSchema(ptr Ptr) (Ptr, error)
- func ReadUpvalue(ptr Ptr) (val Ptr, stackIdx int64, isOpen bool, err error)
- func Scan(ptr Ptr) ([]*Ptr, error)
- type SwapSystem
- type Tray
- type TypeTag
Constants ¶
const ( OffsetMask = 0xFFFFFFFF DrawerShift = 32 )
Bit masks and shifts
const ( TRAY_SIZE = 64 * 1024 // 64KB per Tray DRAWER_SIZE = TRAY_SIZE * 2 // 128KB per Drawer (2 Trays) // PHYSICAL_SLOTS is the actual RAM capacity (16 slots of 128KB = 2MB) PHYSICAL_SLOTS = 16 // MAX_VIRTUAL_DRAWERS is the limit of our "virtual" cabinet MAX_VIRTUAL_DRAWERS = 1024 )
Constants for sizing
const HEAP_SIZE = 10 * 1024 * 1024
Size of our Virtual RAM (e.g., 10 MB for starter)
const HeaderSize = int(unsafe.Sizeof(Header{})) // Should be 8 usually (1 byte + 4 bytes + padding)
Size of header
const SWAP_FILE = ".morph_cache.z"
Variables ¶
var ErrOOM = fmt.Errorf("virtual memory limit reached")
var ErrPageFault = fmt.Errorf("page fault")
Functions ¶
func CloseUpvalue ¶
func CompareAndSwapPtr ¶
CompareAndSwapPtr performs an atomic compare-and-swap operation on a Ptr value in memory. It checks if the value at 'addr' is equal to 'old'. If so, it sets it to 'new' and returns true. Optimization: Uses RLock (Optimistic) -> Lock (Page Fault) strategy.
func MemCpy ¶
MemCpy copies `size` bytes from `src` to `dst`. This represents moving the glass from Nampan A to Nampan B. Thread-safe.
func ReadArrayLength ¶
ReadArrayLength reads the length of the array.
func ReadCompiledFunction ¶
ReadCompiledFunction reads metadata and instructions.
func ReadCompiledFunctionMeta ¶
ReadCompiledFunctionMeta reads only metadata (Locals, Params).
func ReadHashCount ¶
func ReadInteger ¶
ReadInteger reads the value of a raw Integer object.
func ReadPointer ¶
func ReadResource ¶
func ReadSchema ¶
ReadSchema reads the schema components.
func Restore ¶
Restore loads memory state from a file. It effectively rewinds the memory to the snapshot state. Strategy: Load all data into the Swap File and mark all Drawers as "Swapped". This minimizes RAM usage usage initially and relies on Demand Paging.
func SetupTrayPointers ¶
func SetupTrayPointers(d *Drawer)
SetupTrayPointers sets up the VIRTUAL pointers for the drawer. These are invariant of the physical location.
func Snapshot ¶
Snapshot saves the entire memory state to a file. Format: Gob Stream (Cabinet Metadata, then Drawer 0 Blob, Drawer 1 Blob, ...)
func StartGC ¶
StartGC starts the background Garbage Collector daemon. It runs periodically to manage memory pressure and apply LFU aging.
func WriteArrayElement ¶
WriteArrayElement updates the pointer at the given index.
func WriteFloat ¶
WriteFloat updates the value of an existing Float object.
func WriteInteger ¶
WriteInteger updates the value of an existing Integer object.
func WriteModuleExports ¶
func WriteModuleInit ¶
Types ¶
type Allocator ¶
type Allocator interface {
// Alloc allocates a block of `size` bytes and returns its address.
Alloc(size int) (Ptr, error)
// Free releases the memory block pointed to by `ptr`.
Free(ptr Ptr) error
}
Allocator defines the contract for our memory managers.
type Arena ¶
type Arena struct {
Memory [HEAP_SIZE]byte
Offset uintptr // Points to the next free byte (simple bump pointer for now)
}
Arena represents our raw memory block. In the spirit of Graydon Hoare, we take control of the bytes.
var RAM Arena
Global "RAM" instance
func (*Arena) BasePointer ¶
GetPointer returns the raw unsafe pointer to the start of our heap
type Cabinet ¶
type Cabinet struct {
// Virtual Drawers
Drawers []Drawer
// Physical RAM Slots (Map SlotIndex -> DrawerID)
// -1 means empty slot
RAMSlots [PHYSICAL_SLOTS]int
ActiveDrawerIndex int
// Snapshot Store (Simple In-Memory Map for Phase X.1)
Snapshots map[int64][]byte
NextSnapshotID int64
// GC Interface
RootProvider func() []*Ptr
GCTrigger func()
IsGCRunning bool
// contains filtered or unexported fields
}
Cabinet represents the entire Heap (Lemari).
var Lemari Cabinet
Global Cabinet instance
func (*Cabinet) AcquireDrawer ¶
func (c *Cabinet) AcquireDrawer(unitID int) (*DrawerLease, error)
AcquireDrawer attempts to lock a drawer for exclusive use by a unit. It creates a snapshot of the drawer state immediately.
func (*Cabinet) Alloc ¶
Alloc allocates memory. It handles "Draft Otomatis" (Swapping) if RAM is full. Thread-safe.
func (*Cabinet) CommitDrawer ¶
func (c *Cabinet) CommitDrawer(lease *DrawerLease) error
CommitDrawer releases the lease and treats current state as final.
func (*Cabinet) LFUAging ¶
func (c *Cabinet) LFUAging()
LFUAging performs background maintenance. 1. Aging: Decays AccessCounts to ensure recent usage is prioritized. 2. Preemptive Eviction: Frees up RAM slots if pressure is high.
func (*Cabinet) MarkAndCompact ¶
MarkAndCompact performs a Stop-the-World, Copying Garbage Collection. 1. Flips the Active Trays (Semi-Space). 2. Evacuates Roots (Stack + Globals). 3. Scans and Evacuates reachable objects (Cheney's Algorithm). 4. Discards old Trays (Implicitly by reuse).
func (*Cabinet) RestoreDrawer ¶
RestoreDrawer reverts a drawer to the state stored in snapshotID.
func (*Cabinet) RollbackDrawer ¶
func (c *Cabinet) RollbackDrawer(lease *DrawerLease) error
RollbackDrawer reverts the drawer to the state at acquisition.
type Drawer ¶
type Drawer struct {
ID int
// State for Swap/Draft
IsSwapped bool
SwapOffset int64
// Physical Mapping
PhysicalSlot int // -1 if swapped out
PrimaryTray Tray
SecondaryTray Tray
IsPrimaryActive bool // Which tray is currently receiving allocations?
// GC Metadata
AccessCount int64 // LFU Tracking
// Lease System
Lease *DrawerLease
}
Drawer represents a memory region (Laci). It contains two Trays for copying garbage collection (FromSpace/ToSpace).
func CreateDrawer ¶
func CreateDrawer() *Drawer
CreateDrawer creates a new virtual drawer and tries to assign a physical slot
type DrawerLease ¶
type DrawerLease struct {
DrawerID int
UnitID int
SnapshotID int64 // Pointer to the snapshot taken at acquire time
IsActive bool
}
DrawerLease represents exclusive ownership of a drawer by a unit
type Header ¶
type Header struct {
Type TypeTag
Size uint32 // Total size including header
Forwarding Ptr // Forwarding pointer for GC (0 if not forwarded)
}
Header is the metadata for every object in our heap. Aligned to 16 bytes.
func ReadHeader ¶
ReadHeader reads the header at the given pointer safely. It returns a copy of the Header struct.
type Ptr ¶
type Ptr uint64
Ptr represents a VIRTUAL address in our Morph Memory System. Unlike a standard pointer, it does not point to a physical memory address directly. Instead, it acts like a handle:
[ Drawer ID (32 bits) ] [ Offset (32 bits) ] Total 64 bits.
This indirection allows us to move "Drawers" (Memory Pages) between RAM and Disk (Swapping/Draft Otomatis) without breaking the pointers held by the program.
To access the data, you must call `Resolve()` which translates this Virtual Ptr to a Physical unsafe.Pointer (and potentially triggers a page fault/swap-in).
const (
NilPtr Ptr = 0
)
func AllocArray ¶
AllocArray allocates an Array object in the Cabinet. Layout: Header[int32 Capacity][int32 Length][Ptr... elements]
func AllocBoolean ¶
AllocBoolean allocates a Boolean object in the Cabinet. Layout: Header[int8 Value]
func AllocBuiltin ¶
AllocBuiltin allocates a wrapper for a Builtin function index. Layout: Header[int32 Index]
func AllocClosure ¶
Layout: Header[FnPtr(8)][FreeCount(4)][FreePtr0(8)]...
func AllocCompiledFunction ¶
func AllocError ¶
AllocError allocates an Error object. Layout: Header[PtrMessage][PtrCode][Line(4)][Col(4)] We store Message and Code as Strings (Ptr).
func AllocFloat ¶
AllocFloat allocates a raw Float object in the Cabinet. Layout: Header[float64 Value]
func AllocHash ¶
AllocHash allocates a Hash map container. For MVP Phase X, this is a linear list of Key-Value pairs. Layout: Header[int32 Count][Pair0_Key][Pair0_Value]...
func AllocInteger ¶
AllocInteger allocates a raw Integer object in the Cabinet. Layout: Header[int64 Value]
func AllocModule ¶
AllocModule allocates a Module object. Layout: Header[Ptr Init][Ptr Exports]
func AllocPointer ¶
func AllocResource ¶
AllocResource allocates a wrapper for a Host Resource ID. Layout: Header[int64 ResourceID]
func AllocSchema ¶
AllocSchema allocates a Schema object. Layout: Header[Ptr Name][Ptr FieldNames(Array)]
func AllocString ¶
AllocString allocates a String object in the Cabinet. Layout: Header[int32 Length][Bytes...]
func AllocStruct ¶
AllocStruct allocates a Struct instance. Layout: Header[Ptr Schema][Ptr... Fields]
func AllocUpvalue ¶
AllocUpvalue allocates an Upvalue object. Layout: Header[ValuePtr(8)][StackIdx(8)][IsOpen(8)]
func MoveObject ¶
MoveObject simulates the "Owner Tanpa Borrowing" concept. It allocates space in the destination tray (implicitly handled by Alloc in higher logic) and copies the data, essentially cloning it.
func ReadArrayElement ¶
ReadArrayElement reads the pointer at the given index.
func ReadStructField ¶
ReadStructField reads a value from a struct field by index.
func ReadStructSchema ¶
ReadStructSchema reads the schema pointer of a struct.
func Scan ¶
Scan returns a list of pointers to the child pointers contained in the object. Assumes Lemari.mu is Locked.
func (Ptr) Add ¶
Add adds an offset to the pointer. Note: This does not handle crossing Drawer boundaries! If an object spans across drawers, we are in trouble. For now, we assume objects fit in a single Drawer (128KB).
type SwapSystem ¶
type SwapSystem struct {
// contains filtered or unexported fields
}
var Swap SwapSystem
func (*SwapSystem) FreeCache ¶
func (s *SwapSystem) FreeCache() error
FreeCache clears the swap file (Manual Free)
type Tray ¶
type Tray struct {
Start Ptr // Virtual Start Pointer
End Ptr // Virtual End Pointer
Current Ptr // Current Virtual allocation pointer
}
Tray represents a semi-space (Nampan). It's a contiguous block of memory where we bump-allocate objects.
type TypeTag ¶
type TypeTag uint8
ObjectType tag (byte)
const ( TagInteger TypeTag = 1 TagBoolean TypeTag = 2 TagString TypeTag = 3 TagFloat TypeTag = 4 TagNull TypeTag = 5 TagArray TypeTag = 6 TagCompiledFunction TypeTag = 7 TagClosure TypeTag = 8 TagBuiltin TypeTag = 9 TagThread TypeTag = 10 TagHash TypeTag = 11 TagError TypeTag = 12 TagResource TypeTag = 13 TagPointer TypeTag = 14 TagModule TypeTag = 15 TagUpvalue TypeTag = 16 TagStruct TypeTag = 17 TagSchema TypeTag = 18 )