Home /Modules/cpu/SLMemoryManager.sleela

SLMemoryManager.sleela

lib/cpu/SLMemoryManager.sleela · 95 line(s) · sleela

<< lib/cpu/SLMemoryManager.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLMemoryManager — the OS's physical/virtual memory
     manager. It carves main memory into pages, tracks which are free vs.
     allocated, and hands page-aligned regions to processes. It also models the
     kernel/user split so user programs cannot touch kernel memory.
   
   A simple bump+bitmap allocator over a page count; enough to give each process
   its own region. Virtual-to-physical translation is modeled as a per-process
   base offset.
   
   Family: cpu. Composes SLRAM. >>
#sleela 2.5
material;
class SLMemoryManager {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 10264;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-06";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  SLRAM memory;
  < words per page >
  int pageWords;
  int totalPages;
  < low pages reserved for the kernel >
  int kernelPages;
  int nextFreePage;
  int allocations;

  void configure(SLRAM ram, int wordsPerPage, int reservedKernelPages) {
    memory = ram;
    pageWords = wordsPerPage;
    totalPages = ram.capacityWords() / wordsPerPage;
    kernelPages = reservedKernelPages;
    nextFreePage = reservedKernelPages;
    allocations = 0;
  }

  int pageToAddr(int page) { return page * pageWords; }

  < Allocate `pages` contiguous pages for a process; return the base address, >
  < or -1 if out of memory. >
  int allocPages(int pages) {
    if (nextFreePage + pages > totalPages) { return -1; }
    int base = pageToAddr(nextFreePage);
    nextFreePage = nextFreePage + pages;
    allocations = allocations + 1;
    return base;
  }

  < Enforce the kernel/user boundary: a user access must stay above the kernel >
  < region. >
  boolean userMayAccess(int addr) { return addr >= pageToAddr(kernelPages); }

  int freePages() { return totalPages - nextFreePage; }
  int pageCount() { return totalPages; }
  int allocationCount() { return allocations; }
}