memory

package
v0.0.0-...-f836197 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2025 License: EPL-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OffsetMask  = 0xFFFFFFFF
	DrawerShift = 32
)

Bit masks and shifts

View Source
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

View Source
const HEAP_SIZE = 10 * 1024 * 1024

Size of our Virtual RAM (e.g., 10 MB for starter)

View Source
const HeaderSize = int(unsafe.Sizeof(Header{})) // Should be 8 usually (1 byte + 4 bytes + padding)

Size of header

View Source
const SWAP_FILE = ".morph_cache.z"

Variables

View Source
var ErrOOM = fmt.Errorf("virtual memory limit reached")
View Source
var ErrPageFault = fmt.Errorf("page fault")

Functions

func CloseUpvalue

func CloseUpvalue(ptr Ptr, val Ptr) error

func CompareAndSwapPtr

func CompareAndSwapPtr(addr Ptr, old, new Ptr) (bool, error)

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 InitCabinet

func InitCabinet()

Initialize the Cabinet structure (carving up the Arena)

func InitSwap

func InitSwap() error

func MemCpy

func MemCpy(src Ptr, dst Ptr, size int) error

MemCpy copies `size` bytes from `src` to `dst`. This represents moving the glass from Nampan A to Nampan B. Thread-safe.

func Read

func Read(src Ptr, size int) ([]byte, error)

Read reads data from the pointer address. Thread-safe.

func ReadArrayLength

func ReadArrayLength(arrayPtr Ptr) (int, error)

ReadArrayLength reads the length of the array.

func ReadBoolean

func ReadBoolean(ptr Ptr) (bool, error)

ReadBoolean reads the boolean value.

func ReadBuiltin

func ReadBuiltin(ptr Ptr) (int, error)

ReadBuiltin reads the index.

func ReadClosure

func ReadClosure(ptr Ptr) (Ptr, []Ptr, error)

func ReadCompiledFunction

func ReadCompiledFunction(ptr Ptr) ([]byte, int, int, error)

ReadCompiledFunction reads metadata and instructions.

func ReadCompiledFunctionMeta

func ReadCompiledFunctionMeta(ptr Ptr) (int, int, error)

ReadCompiledFunctionMeta reads only metadata (Locals, Params).

func ReadError

func ReadError(ptr Ptr) (Ptr, Ptr, int, int, error)

func ReadFloat

func ReadFloat(ptr Ptr) (float64, error)

ReadFloat reads the value of a raw Float object.

func ReadHashCount

func ReadHashCount(hashPtr Ptr) (int, error)

func ReadHashPair

func ReadHashPair(hashPtr Ptr, index int) (Ptr, Ptr, error)

func ReadInteger

func ReadInteger(ptr Ptr) (int64, error)

ReadInteger reads the value of a raw Integer object.

func ReadModule

func ReadModule(ptr Ptr) (Ptr, Ptr, error)

func ReadPointer

func ReadPointer(ptr Ptr) (uint64, error)

func ReadResource

func ReadResource(ptr Ptr) (int64, error)

func ReadSchema

func ReadSchema(ptr Ptr) (name Ptr, fields Ptr, err error)

ReadSchema reads the schema components.

func ReadString

func ReadString(ptr Ptr) (string, error)

ReadString reads the string value.

func Restore

func Restore(filename string) error

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

func Snapshot(filename string) error

Snapshot saves the entire memory state to a file. Format: Gob Stream (Cabinet Metadata, then Drawer 0 Blob, Drawer 1 Blob, ...)

func StartGC

func StartGC(interval time.Duration)

StartGC starts the background Garbage Collector daemon. It runs periodically to manage memory pressure and apply LFU aging.

func StorePtr

func StorePtr(addr Ptr, val Ptr) error

StorePtr writes a Ptr atomically.

func Write

func Write(dst Ptr, data []byte) error

Write writes data to the pointer address. Thread-safe.

func WriteArrayElement

func WriteArrayElement(arrayPtr Ptr, index int, valuePtr Ptr) error

WriteArrayElement updates the pointer at the given index.

func WriteFloat

func WriteFloat(ptr Ptr, value float64) error

WriteFloat updates the value of an existing Float object.

func WriteHashPair

func WriteHashPair(hashPtr Ptr, index int, key, value Ptr) error

func WriteInteger

func WriteInteger(ptr Ptr, value int64) error

WriteInteger updates the value of an existing Integer object.

func WriteModuleExports

func WriteModuleExports(ptr Ptr, exports Ptr) error

func WriteModuleInit

func WriteModuleInit(ptr Ptr, initFn Ptr) error

func WriteStructField

func WriteStructField(ptr Ptr, index int, val Ptr) error

WriteStructField writes a value to a struct field by index.

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

func (a *Arena) BasePointer() unsafe.Pointer

GetPointer returns the raw unsafe pointer to the start of our heap

func (*Arena) Reset

func (a *Arena) Reset()

Reset completely wipes the memory (dangerous!)

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

func (c *Cabinet) Alloc(size int) (Ptr, error)

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

func (c *Cabinet) MarkAndCompact(roots []*Ptr) error

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

func (c *Cabinet) RestoreDrawer(drawerID int, snapshotID int64) error

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.

func (*Cabinet) SnapshotDrawer

func (c *Cabinet) SnapshotDrawer(drawerID int) (int64, error)

SnapshotDrawer creates an in-memory snapshot of a specific drawer.

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 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

func ReadHeader(ptr Ptr) (Header, error)

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

func AllocArray(length int, capacity int) (Ptr, error)

AllocArray allocates an Array object in the Cabinet. Layout: Header[int32 Capacity][int32 Length][Ptr... elements]

func AllocBoolean

func AllocBoolean(value bool) (Ptr, error)

AllocBoolean allocates a Boolean object in the Cabinet. Layout: Header[int8 Value]

func AllocBuiltin

func AllocBuiltin(index int) (Ptr, error)

AllocBuiltin allocates a wrapper for a Builtin function index. Layout: Header[int32 Index]

func AllocClosure

func AllocClosure(fnPtr Ptr, freeVars []Ptr) (Ptr, error)

Layout: Header[FnPtr(8)][FreeCount(4)][FreePtr0(8)]...

func AllocCompiledFunction

func AllocCompiledFunction(instructions []byte, numLocals, numParams int) (Ptr, error)

func AllocError

func AllocError(message Ptr, code Ptr, line, col int) (Ptr, error)

AllocError allocates an Error object. Layout: Header[PtrMessage][PtrCode][Line(4)][Col(4)] We store Message and Code as Strings (Ptr).

func AllocFloat

func AllocFloat(value float64) (Ptr, error)

AllocFloat allocates a raw Float object in the Cabinet. Layout: Header[float64 Value]

func AllocHash

func AllocHash(count int) (Ptr, error)

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

func AllocInteger(value int64) (Ptr, error)

AllocInteger allocates a raw Integer object in the Cabinet. Layout: Header[int64 Value]

func AllocModule

func AllocModule(initFn Ptr, exports Ptr) (Ptr, error)

AllocModule allocates a Module object. Layout: Header[Ptr Init][Ptr Exports]

func AllocNull

func AllocNull() (Ptr, error)

AllocNull allocates a Null object in the Cabinet. Layout: Header (No payload)

func AllocPointer

func AllocPointer(addr uint64) (Ptr, error)

func AllocResource

func AllocResource(id int64) (Ptr, error)

AllocResource allocates a wrapper for a Host Resource ID. Layout: Header[int64 ResourceID]

func AllocSchema

func AllocSchema(name Ptr, fields Ptr) (Ptr, error)

AllocSchema allocates a Schema object. Layout: Header[Ptr Name][Ptr FieldNames(Array)]

func AllocString

func AllocString(s string) (Ptr, error)

AllocString allocates a String object in the Cabinet. Layout: Header[int32 Length][Bytes...]

func AllocStruct

func AllocStruct(schema Ptr, fieldCount int) (Ptr, error)

AllocStruct allocates a Struct instance. Layout: Header[Ptr Schema][Ptr... Fields]

func AllocUpvalue

func AllocUpvalue(stackIdx int) (Ptr, error)

AllocUpvalue allocates an Upvalue object. Layout: Header[ValuePtr(8)][StackIdx(8)][IsOpen(8)]

func LoadPtr

func LoadPtr(addr Ptr) (Ptr, error)

LoadPtr reads a Ptr atomically.

func MoveObject

func MoveObject(src Ptr, size int) (Ptr, error)

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 NewPtr

func NewPtr(drawerID int, offset uint32) Ptr

NewPtr creates a Virtual Pointer from Drawer ID and Offset.

func ReadArrayElement

func ReadArrayElement(arrayPtr Ptr, index int) (Ptr, error)

ReadArrayElement reads the pointer at the given index.

func ReadStructField

func ReadStructField(ptr Ptr, index int) (Ptr, error)

ReadStructField reads a value from a struct field by index.

func ReadStructSchema

func ReadStructSchema(ptr Ptr) (Ptr, error)

ReadStructSchema reads the schema pointer of a struct.

func ReadUpvalue

func ReadUpvalue(ptr Ptr) (val Ptr, stackIdx int64, isOpen bool, err error)

func Scan

func Scan(ptr Ptr) ([]*Ptr, error)

Scan returns a list of pointers to the child pointers contained in the object. Assumes Lemari.mu is Locked.

func (Ptr) Add

func (p Ptr) Add(offset uint32) Ptr

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).

func (Ptr) DrawerID

func (p Ptr) DrawerID() int

DrawerID extracts the Drawer ID from the virtual pointer.

func (Ptr) Offset

func (p Ptr) Offset() uint32

Offset extracts the intra-drawer offset from the virtual pointer.

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)

func (*SwapSystem) Restore

func (s *SwapSystem) Restore(offset int64, dest []byte) error

Restore reads data from swap file at offset

func (*SwapSystem) Spill

func (s *SwapSystem) Spill(data []byte) (int64, error)

Spill writes data to swap file and returns the offset

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.

func (*Tray) Remaining

func (t *Tray) Remaining() int

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
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL