Skip to content

6 1 core and allocation

TransCompilator edited this page Nov 26, 2026 · 1 revision

6.1 Core and Allocation

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/6-1-core-and-allocation

Core and Allocation

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

This page documents the foundational layers of the TransCompilator (tc_r1) standard library implemented in structured English: std/core.eng and std/alloc.eng. These modules establish the zero-libc execution environment, providing essential algebraic data types (Option, Result), raw Linux system call wrappers for error handling and panics, low-level memory utilities, and a custom heap allocator built directly on top of anonymous memory pages.


1. Primitives, Algebraic Types, and Error Handling (std/core.eng)

std/core.eng defines the fundamental building blocks for safe programming without heap allocation overhead for basic control flow values std/core.eng:1-5.

Algebraic Data Types

The module defines generic sum-like types via record structures:

  • Option of T: Encapsulates optional values using a truth flag has value and a payload value [std/core.eng:6-6]. Constructors and inspectors include option_some, option_none, option_is_some, and option_unwrap_or std/core.eng:10-25.
  • Result of T and E: Encapsulates success or failure using an is ok flag alongside ok val (type T) and err val (type E) [std/core.eng:8-8]. Corresponding functions include result_ok, result_err, result_is_ok, and result_unwrap_or std/core.eng:27-44.

Panics and Assertions

When unrecoverable errors occur, panic invokes raw Linux system calls directly std/core.eng:46-49:

  1. Syscall 1 (sys_write) writes the error message string msg to standard error (2).
  2. Syscall 1 writes a newline character \n.
  3. Syscall 60 (sys_exit) terminates the process with exit code 1.

assert_true wraps this mechanism by verifying boolean conditions and conditionally triggering a panic std/core.eng:51-52.

Byte Utilities

Low-level memory routines provide unmanaged byte manipulation:

  • mem_zero: Clears a byte buffer of length len starting at dst std/core.eng:54-59.
  • mem_eq: Compares two byte buffers (a and b) of length len for equality, returning a truth value std/core.eng:61-67.
graph TD
    NaturalLanguageSpace["Natural English Language Space"] --> |"Expressed as"| CoreEng["std/core.eng Code Entities"]
    
    subgraph CodeEntitySpace["Code Entity Space"]
        OptionRecord["Option of T [std/core.eng:6]"]
        ResultRecord["Result of T and E [std/core.eng:8]"]
        PanicFunc["panic [std/core.eng:46]"]
        AssertFunc["assert_true [std/core.eng:51]"]
        MemZero["mem_zero [std/core.eng:54]"]
        MemEq["mem_eq [std/core.eng:61]"]
    end

    NaturalLanguageSpace --> OptionRecord
    NaturalLanguageSpace --> ResultRecord
    NaturalLanguageSpace --> PanicFunc
    NaturalLanguageSpace --> AssertFunc
    NaturalLanguageSpace --> MemZero
    NaturalLanguageSpace --> MemEq
Loading

Sources: std/core.eng:1-67


2. Zero-libc Memory Allocator (std/alloc.eng)

std/alloc.eng implements a custom heap memory allocator and reuse manager on top of Linux memory mapping primitives, bypassing standard C library dependencies std/alloc.eng:1-7.

Data Structures and Metadata

Heap management relies on the MemoryBlock record structure, which acts as a 32-byte header preceding every allocated or free payload std/alloc.eng:8-8, 28-32:

Global state trackers include memory head, bytes in use, and free count std/alloc.eng:10-12.

Allocation and Page Request Strategy

When heap_alloc is invoked:

  1. The requested size is rounded up to 16-byte alignment (round size up to 16 bytes) std/alloc.eng:57-58.
  2. The free list (memory head) is traversed to locate an existing block where free flag is 1 and size is sufficient std/alloc.eng:60-62.
  3. If a suitable block is found, claim block splits the block if the remaining space can accommodate another header plus payload (at least need plus 64), or absorbs it entirely std/alloc.eng:40-44, 63-64.
  4. If no free block matches, request a new memory page allocates fresh virtual memory using Linux syscall 9 (mmap) with flags 3 (PROT_READ | PROT_WRITE) and 34 (MAP_PRIVATE | MAP_ANONYMOUS) std/alloc.eng:45-48, 68.
graph TD
    UserRequest["heap_alloc size [std/alloc.eng:57]"] --> Align["round_size_to_16 [std/alloc.eng:58]"]
    Align --> Traverse["Traverse memory head list [std/alloc.eng:60]"]
    
    Traverse --> CheckBlock{"Block found and free? [std/alloc.eng:62]"}
    
    CheckBlock -->|Yes| Claim["Claim block & split if needed [std/alloc.eng:63]"]
    CheckBlock -->|No| RequestPage["request_a_new_memory_page [std/alloc.eng:68]"]
    
    Claim --> ReturnPayload["Give back payload of block [std/alloc.eng:64]"]
    
    RequestPage --> MmapSyscall["Ask Linux kernel 9 mmap [std/alloc.eng:48]"]
    MmapSyscall --> NewBlock["Initialize MemoryBlock fresh [std/alloc.eng:50]"]
    NewBlock --> ReturnPayloadFresh["Give back payload of fresh [std/alloc.eng:69]"]
Loading

Deallocation and Coalescing

heap_free releases memory back to the allocator pool std/alloc.eng:71-80:

  • Validates the pointer p and fetches its header via header of p std/alloc.eng:72-73.
  • Marks the block as free (free flag set to 1) and decrements bytes in use std/alloc.eng:74-76.
  • Performs immediate forward coalescing: if the adjacent subsequent block (following) is also free and contiguous in memory, they are merged into a single larger block std/alloc.eng:78-79.

Reallocation

heap_realloc manages resizing operations std/alloc.eng:81-89:

  • Delegates to heap_alloc if p is empty std/alloc.eng:82.
  • Returns p directly if new_size fits within the existing block's size std/alloc.eng:84.
  • Otherwise, allocates a new block, copies the overlapping data (copy length), and releases the old allocation std/alloc.eng:85-89.
graph TD
    NaturalMemoryConcept["Natural Memory Allocation Requirements"] --> |"Compiled to"| AllocEng["std/alloc.eng Allocator Entities"]

    subgraph AllocatorEntities["Allocator Entity Space"]
        MemoryBlockStruct["MemoryBlock Record [std/alloc.eng:8]"]
        HeapAlloc["heap_alloc [std/alloc.eng:57]"]
        HeapFree["heap_free [std/alloc.eng:71]"]
        HeapRealloc["heap_realloc [std/alloc.eng:81]"]
        MmapReq["request_a_new_memory_page [std/alloc.eng:45]"]
    end

    NaturalMemoryConcept --> MemoryBlockStruct
    NaturalMemoryConcept --> HeapAlloc
    NaturalMemoryConcept --> HeapFree
    NaturalMemoryConcept --> HeapRealloc
    NaturalMemoryConcept --> MmapReq
Loading

Sources: std/alloc.eng:1-97


3. Syscall Integration and Low-Level Primitives Summary

Both core and allocation modules rely on direct kernel communication via the TransCompilator structured English syscall syntax (Ask Linux kernel). The active syscall numbers utilized across these modules are summarized below:

Syscall Number Name / Purpose Used In File Line Reference
1 sys_write (stdout/stderr output) std/core.eng std/core.eng:47-48
60 sys_exit (process termination) std/core.eng std/core.eng:49
9 mmap (anonymous memory mapping) std/alloc.eng std/alloc.eng:48

Sources: std/core.eng:46-49, std/alloc.eng:45-48


Clone this wiki locally