Repository navigation
6 1 core and allocation
Source: https://deepwiki.com/PonvasanthPon/TransCompilator/6-1-core-and-allocation
Relevant source files
The following files were used as context for generating this wiki page:
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.
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.
The module defines generic sum-like types via record structures:
-
Option of T: Encapsulates optional values using a truth flaghas valueand a payloadvalue[std/core.eng:6-6]. Constructors and inspectors includeoption_some,option_none,option_is_some, andoption_unwrap_orstd/core.eng:10-25. -
Result of T and E: Encapsulates success or failure using anis okflag alongsideok val(typeT) anderr val(typeE) [std/core.eng:8-8]. Corresponding functions includeresult_ok,result_err,result_is_ok, andresult_unwrap_orstd/core.eng:27-44.
When unrecoverable errors occur, panic invokes raw Linux system calls directly std/core.eng:46-49:
- Syscall
1(sys_write) writes the error message stringmsgto standard error (2). - Syscall
1writes a newline character\n. - Syscall
60(sys_exit) terminates the process with exit code1.
assert_true wraps this mechanism by verifying boolean conditions and conditionally triggering a panic std/core.eng:51-52.
Low-level memory routines provide unmanaged byte manipulation:
-
mem_zero: Clears a byte buffer of lengthlenstarting atdststd/core.eng:54-59. -
mem_eq: Compares two byte buffers (aandb) of lengthlenfor 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
Sources: std/core.eng:1-67
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.
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:
-
size: Unsigned number tracking payload size std/alloc.eng:8. -
free flag: Truth indicator (1 if free, 0 if allocated) std/alloc.eng:8. -
next link: Pointer to the subsequentMemoryBlockin the global list std/alloc.eng:8. -
padding: Alignment reservation std/alloc.eng:8.
Global state trackers include memory head, bytes in use, and free count std/alloc.eng:10-12.
When heap_alloc is invoked:
- The requested size is rounded up to 16-byte alignment (
round size up to 16 bytes) std/alloc.eng:57-58. - The free list (
memory head) is traversed to locate an existing block wherefree flagis 1 andsizeis sufficient std/alloc.eng:60-62. - If a suitable block is found,
claim blocksplits 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. - If no free block matches,
request a new memory pageallocates fresh virtual memory using Linux syscall9(mmap) with flags3(PROT_READ | PROT_WRITE) and34(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]"]
heap_free releases memory back to the allocator pool std/alloc.eng:71-80:
- Validates the pointer
pand fetches its header viaheader of pstd/alloc.eng:72-73. - Marks the block as free (
free flagset to 1) and decrementsbytes in usestd/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.
heap_realloc manages resizing operations std/alloc.eng:81-89:
- Delegates to
heap_allocifpis empty std/alloc.eng:82. - Returns
pdirectly ifnew_sizefits 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
Sources: std/alloc.eng:1-97
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
The TransCompilator Project