From 8c6df0ee762fe0d7a8a61f6712d0e36f4b7be603 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 10 Oct 2026 08:34:12 +0000 Subject: [PATCH] Step 9: NEW, and the allocators NEW(p, a) calls a^.alloc(a, SIZE(T), alignment) through the one interface the compiler knows, Mem.Allocator, and zeroes what it gets. lib/Mem.jed: Allocator, pages from the system. lib/Arena.jed: a bump allocator over one block. lib/Heap.jed: nine size classes with free lists, and pages of their own for big blocks; Heap.heap. Sys gains mmap and munmap. --- diff --git a/PLAN.md b/PLAN.md index ea57af7..a002abe 100644 --- a/PLAN.md +++ b/PLAN.md @@ -94,7 +94,7 @@ - [x] 7. Multiple results and errors; `INC` and `DEC`. - [x] 8. Enumerations and sets, record literals, `defer`; examples. -- [ ] 9. `NEW`; `lib/Mem.jed`, `lib/Arena.jed`, `lib/Heap.jed`. +- [x] 9. `NEW`; `lib/Mem.jed`, `lib/Arena.jed`, `lib/Heap.jed`. - [ ] 10. Generic modules, with type and procedure arguments; `lib/Str.jed`, `lib/Buf.jed`, `lib/Fmt.jed`; examples. diff --git a/doc/jed.md b/doc/jed.md index a285a7a..1c4c523 100644 --- a/doc/jed.md +++ b/doc/jed.md @@ -752,11 +752,11 @@ | Module | What | |---|---| | `Rt` | what the compiler's checks call when they fail, and string comparison | -| `Sys` | system calls: `write`, `exit`; one file per target | +| `Sys` | system calls: `write`, `mmap`, `munmap`, `exit`; one file per target | | `Out` | writing text and numbers to standard output | -| `Mem` | `mmap`, and `Allocator`, the interface of every allocator | -| `Heap` | a general allocator | -| `Arena` | an allocator that frees everything at once | +| `Mem` | `Allocator`, the interface of every allocator, and pages from the system | +| `Heap` | a general allocator, `Heap.heap` | +| `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | | `Str` | operations on `[]U8` strings | | `Buf` | a growable byte buffer | | `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)])` | diff --git a/lib/Arena.jed b/lib/Arena.jed new file mode 100644 index 0000000..bb9ae1e --- /dev/null +++ b/lib/Arena.jed @@ -0,0 +1,52 @@ +(* An allocator that hands out memory from one block and frees it all + at once: free does nothing, reset forgets everything. An Arena is + its allocator: ADR(arena.base) is the ^Mem.Allocator to give NEW. *) +module Arena; +import Mem; + +type + Arena* = record + base*: Mem.Allocator; (* first, so that ^Arena(a) finds the rest *) + buf: ^U8; + used, cap: I64; + end; + +proc alloc(a: ^Mem.Allocator; size, align: I64): ^U8; +var ar: ^Arena; at: I64; +begin + ar := ^Arena(a); + at := (ar^.used + align - 1) div align * align; + if (size < 0) or (at + size > ar^.cap) then return nil; end; + ar^.used := at + size; + return ar^.buf + at; +end; + +proc free(a: ^Mem.Allocator; p: ^U8); +begin +end; + +(* An arena of size bytes; false if the system has none. *) +proc init*(ar: ^Arena; size: I64): BOOL; +begin + ar^.base := {alloc: alloc, free: free}; + ar^.buf := Mem.pages(size); + ar^.used := 0; + ar^.cap := size; + if ar^.buf = nil then ar^.cap := 0; end; + return ar^.buf # nil; +end; + +(* Forgets every allocation; the memory is used again. *) +proc reset*(ar: ^Arena); +begin + ar^.used := 0; +end; + +proc done*(ar: ^Arena); +begin + if ar^.buf # nil then Mem.unmap(ar^.buf, ar^.cap); end; + ar^.buf := nil; + ar^.cap := 0; +end; + +end Arena. diff --git a/lib/Heap.jed b/lib/Heap.jed new file mode 100644 index 0000000..4d549ff --- /dev/null +++ b/lib/Heap.jed @@ -0,0 +1,75 @@ +(* A general allocator: Heap.heap. Small blocks come in nine sizes, + 16 to 4096 bytes with their header, each with a list of the free + ones; a block bigger than that gets pages of its own. Every block + has a 16-byte header before it: its size class, or minus its size + in bytes for a big one. *) +module Heap; +import Mem; + +const + Classes = 9; + Header = 16; + Refill = 65536; + +type + Block = record next: ^Block; end; + +var + lists: [Classes]^Block; + cur: ^U8; (* where the next new small block comes from *) + left: I64; + std: Mem.Allocator; + heap*: ^Mem.Allocator; + +proc alloc(a: ^Mem.Allocator; size, align: I64): ^U8; +var c, n: I64; b: ^Block; p: ^U8; h: ^I64; +begin + n := size + Header; + if (size < 0) or (align > Header) and (align > Mem.Page) then return nil; end; + c := 0; + while (c < Classes) and (16 shl c < n) do INC(c); end; + if (c = Classes) or (align > Header) then + n := (n + Mem.Page - 1) div Mem.Page * Mem.Page; + p := Mem.pages(n); + if p = nil then return nil; end; + h := ^I64(p); + h^ := -n; + return p + Header; + end; + b := lists[c]; + if b # nil then + lists[c] := b^.next; + p := ^U8(b); + else + if left < 16 shl c then + cur := Mem.pages(Refill); + if cur = nil then left := 0; return nil; end; + left := Refill; + end; + p := cur; + cur := cur + (16 shl c); + left := left - (16 shl c); + end; + h := ^I64(p); + h^ := c; + return p + Header; +end; + +proc free(a: ^Mem.Allocator; p: ^U8); +var h: ^I64; b: ^Block; +begin + if p = nil then return; end; + h := ^I64(p - Header); + if h^ < 0 then + Mem.unmap(^U8(h), -h^); + else + b := ^Block(h); + b^.next := lists[h^]; + lists[h^] := b; + end; +end; + +begin + std := {alloc: alloc, free: free}; + heap := ADR(std); +end Heap. diff --git a/lib/Mem.jed b/lib/Mem.jed new file mode 100644 index 0000000..fa26f08 --- /dev/null +++ b/lib/Mem.jed @@ -0,0 +1,35 @@ +(* Memory: Allocator, the interface every allocator offers, and pages + from the system. NEW(p, a) calls a^.alloc. *) +module Mem; +import Sys; + +type + Allocator* = record + alloc*: proc(a: ^Allocator; size, align: I64): ^U8; (* nil if out of room *) + free*: proc(a: ^Allocator; p: ^U8); + end; + +const Page* = 4096; + +(* size bytes, rounded up to whole pages, zeroed; or nil. *) +proc pages*(size: I64): ^U8; +begin + return Sys.mmap((size + Page - 1) div Page * Page); +end; + +proc unmap*(p: ^U8; size: I64); +begin + Sys.munmap(p, (size + Page - 1) div Page * Page); +end; + +proc alloc*(a: ^Allocator; size, align: I64): ^U8; +begin + return a^.alloc(a, size, align); +end; + +proc free*(a: ^Allocator; p: ^U8); +begin + a^.free(a, p); +end; + +end Mem. diff --git a/lib/Sys.linux-x86_64.jed b/lib/Sys.linux-x86_64.jed index 367e730..591acbb 100644 --- a/lib/Sys.linux-x86_64.jed +++ b/lib/Sys.linux-x86_64.jed @@ -15,6 +15,21 @@ return SYSCALL(1, fd, ADR(s[0]), LEN(s)); end; +(* size bytes of fresh, zeroed memory from the system, a multiple of + pages, or nil. *) +proc mmap*(size: I64): ^U8; +var p: I64; +begin + p := SYSCALL(9, 0, size, 3, 22H, -1, 0); (* read, write; private, anonymous *) + if p < 0 then return nil; end; + return ^U8(p); +end; + +proc munmap*(p: ^U8; size: I64); +begin + SYSCALL(11, p, size); +end; + (* Ends the program, at once, with status. *) proc exit*(status: I64); begin diff --git a/src/parse.pas b/src/parse.pas index 6383400..04208df 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -473,6 +473,51 @@ x.mode := mReg; end; +{ NEW(p, a): p := a^.alloc(a, SIZE(T), alignment), zeroed if not nil. + a is a ^Mem.Allocator, the one interface the compiler knows. } +procedure New_(var x: TItem); +var y, z: TItem; t: PType; f: PObj; s: int64; c: TCall; L: longint; +begin + Expression(x, nil); + if (x.mode <> mVar) or not x.isVar or (x.typ^.form <> fPtr) then Mark('NEW needs a pointer variable'); + t := x.typ^.base; + Save(x); + Expect(sComma, ','); + Expression(y, nil); + if (y.typ^.form <> fPtr) or (y.typ^.base^.obj = nil) or (y.typ^.base^.obj^.module = nil) + or (NameOf(y.typ^.base^.obj^.name) <> 'Allocator') + or (NameOf(y.typ^.base^.obj^.module^.name) <> 'Mem') then Mark('NEW needs a ^Mem.Allocator'); + f := Find(y.typ^.base^.scope, Name('alloc')); + s := ToSlot(y); + Load(RAX, Mem(RBP, s), 8, false); + MakeConst(z, f^.typ, 0); + z.mode := mVar; z.base := bReg; z.reg := RAX; z.off := f^.val; + BeginCall(c, z); + Load(RAX, Mem(RBP, s), 8, false); + MakeConst(z, y.typ, 0); + z.mode := mReg; + PassArg(c, z); + MakeConst(z, tI64, t^.size); + PassArg(c, z); + MakeConst(z, tI64, t^.align); + PassArg(c, z); + EndCall(c, z); + L := NoLabel; + TestRR(RAX, RAX); + Jcc(ccE, L); + MovRR(RDX, RAX); + MovRR(RDI, RAX); + MovRI(RCX, t^.size); + MovRI(RAX, 0); + RepStosb; + MovRR(RAX, RDX); + FixLink(L); + z.typ := x.typ; + Assign(x, z); + MakeConst(x, tNone, 0); + x.mode := mNone; +end; + procedure Builtin(var x: TItem); const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9); var which, n, i: longint; y: TItem; t: PType; slots: array[0..6] of int64; @@ -544,7 +589,7 @@ MakeConst(x, tNone, 0); x.mode := mNone; end; - bNew: Mark('NEW comes in step 9'); + bNew: New_(x); end; Expect(sRparen, ')'); end; @@ -686,6 +731,7 @@ begin Next; ZeroTemp(x, t); + FillChar(seen, SizeOf(seen), 0); n := 0; if tok <> sRbrace then repeat diff --git a/test/run/Alloc b/test/run/Alloc new file mode 100755 index 0000000..9028c39 Binary files /dev/null and b/test/run/Alloc differ diff --git a/test/run/Alloc.expect b/test/run/Alloc.expect new file mode 100644 index 0000000..db5ee40 --- /dev/null +++ b/test/run/Alloc.expect @@ -0,0 +1,5 @@ +1001000 +1001000 +7 +10100 +nil again 0 diff --git a/test/run/Alloc.jed b/test/run/Alloc.jed new file mode 100644 index 0000000..6d5245c --- /dev/null +++ b/test/run/Alloc.jed @@ -0,0 +1,66 @@ +module Alloc; +import Out, Mem, Arena, Heap; + +type + Node = record next: ^Node; val: I64; pad: [5]I64; end; + Big = record data: [1000]I64; end; + +proc build(a: ^Mem.Allocator; n: I64): ^Node; +var head, p: ^Node; i: I64; +begin + head := nil; + for i := 1 to n do + NEW(p, a); + if p = nil then return nil; end; + if p^.val # 0 then Out.str("not zeroed "); end; + p^.val := i; + p^.pad[4] := i; + p^.next := head; + head := p; + end; + return head; +end; + +proc sum(p: ^Node): I64; +var s: I64; +begin + s := 0; + while p # nil do s := s + p^.val + p^.pad[4]; p := p^.next; end; + return s; +end; + +proc release(a: ^Mem.Allocator; p: ^Node); +var next: ^Node; +begin + while p # nil do + next := p^.next; + Mem.free(a, ^U8(p)); + p := next; + end; +end; + +proc main*; +var ar: Arena.Arena; l, m: ^Node; b: ^Big; i: I64; small: ^U8; again: ^Node; +begin + l := build(Heap.heap, 1000); + Out.int(sum(l)); Out.ln; + release(Heap.heap, l); + m := build(Heap.heap, 1000); + Out.int(sum(m)); Out.ln; + NEW(b, Heap.heap); + b^.data[999] := 7; + Out.int(b^.data[999] + b^.data[0]); Out.ln; + Mem.free(Heap.heap, ^U8(b)); + if not Arena.init(ADR(ar), 8192) then Out.str("no arena"); end; + l := build(ADR(ar.base), 100); + Out.int(sum(l)); Out.ln; + l := build(ADR(ar.base), 1000); + if l = nil then Out.str("nil "); end; + Arena.reset(ADR(ar)); + NEW(again, ADR(ar.base)); + if again # nil then Out.str("again "); Out.int(again^.val); end; + Out.ln; + Arena.done(ADR(ar)); +end; + +end Alloc.