raz / commit 8c6df0ee

commit
8c6df0ee762fe0d7a8a61f6712d0e36f4b7be603
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
03224cab
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.

patch · log

10 files changed, +300 -6

PLAN.md (history) modified +1 -1

@@ -94,7 +94,7 @@
9494
9595 - [x] 7. Multiple results and errors; `INC` and `DEC`.
9696 - [x] 8. Enumerations and sets, record literals, `defer`; examples.
97-- [ ] 9. `NEW`; `lib/Mem.jed`, `lib/Arena.jed`, `lib/Heap.jed`.
97+- [x] 9. `NEW`; `lib/Mem.jed`, `lib/Arena.jed`, `lib/Heap.jed`.
9898 - [ ] 10. Generic modules, with type and procedure arguments;
9999 `lib/Str.jed`, `lib/Buf.jed`, `lib/Fmt.jed`; examples.
100100

doc/jed.md modified +4 -4

@@ -752,11 +752,11 @@
752752 | Module | What |
753753 |---|---|
754754 | `Rt` | what the compiler's checks call when they fail, and string comparison |
755-| `Sys` | system calls: `write`, `exit`; one file per target |
755+| `Sys` | system calls: `write`, `mmap`, `munmap`, `exit`; one file per target |
756756 | `Out` | writing text and numbers to standard output |
757-| `Mem` | `mmap`, and `Allocator`, the interface of every allocator |
758-| `Heap` | a general allocator |
759-| `Arena` | an allocator that frees everything at once |
757+| `Mem` | `Allocator`, the interface of every allocator, and pages from the system |
758+| `Heap` | a general allocator, `Heap.heap` |
759+| `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator |
760760 | `Str` | operations on `[]U8` strings |
761761 | `Buf` | a growable byte buffer |
762762 | `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)])` |

lib/Arena.jed added +52 -0

@@ -0,0 +1,52 @@
1+(* An allocator that hands out memory from one block and frees it all
2+ at once: free does nothing, reset forgets everything. An Arena is
3+ its allocator: ADR(arena.base) is the ^Mem.Allocator to give NEW. *)
4+module Arena;
5+import Mem;
6+
7+type
8+ Arena* = record
9+ base*: Mem.Allocator; (* first, so that ^Arena(a) finds the rest *)
10+ buf: ^U8;
11+ used, cap: I64;
12+ end;
13+
14+proc alloc(a: ^Mem.Allocator; size, align: I64): ^U8;
15+var ar: ^Arena; at: I64;
16+begin
17+ ar := ^Arena(a);
18+ at := (ar^.used + align - 1) div align * align;
19+ if (size < 0) or (at + size > ar^.cap) then return nil; end;
20+ ar^.used := at + size;
21+ return ar^.buf + at;
22+end;
23+
24+proc free(a: ^Mem.Allocator; p: ^U8);
25+begin
26+end;
27+
28+(* An arena of size bytes; false if the system has none. *)
29+proc init*(ar: ^Arena; size: I64): BOOL;
30+begin
31+ ar^.base := {alloc: alloc, free: free};
32+ ar^.buf := Mem.pages(size);
33+ ar^.used := 0;
34+ ar^.cap := size;
35+ if ar^.buf = nil then ar^.cap := 0; end;
36+ return ar^.buf # nil;
37+end;
38+
39+(* Forgets every allocation; the memory is used again. *)
40+proc reset*(ar: ^Arena);
41+begin
42+ ar^.used := 0;
43+end;
44+
45+proc done*(ar: ^Arena);
46+begin
47+ if ar^.buf # nil then Mem.unmap(ar^.buf, ar^.cap); end;
48+ ar^.buf := nil;
49+ ar^.cap := 0;
50+end;
51+
52+end Arena.

lib/Heap.jed added +75 -0

@@ -0,0 +1,75 @@
1+(* A general allocator: Heap.heap. Small blocks come in nine sizes,
2+ 16 to 4096 bytes with their header, each with a list of the free
3+ ones; a block bigger than that gets pages of its own. Every block
4+ has a 16-byte header before it: its size class, or minus its size
5+ in bytes for a big one. *)
6+module Heap;
7+import Mem;
8+
9+const
10+ Classes = 9;
11+ Header = 16;
12+ Refill = 65536;
13+
14+type
15+ Block = record next: ^Block; end;
16+
17+var
18+ lists: [Classes]^Block;
19+ cur: ^U8; (* where the next new small block comes from *)
20+ left: I64;
21+ std: Mem.Allocator;
22+ heap*: ^Mem.Allocator;
23+
24+proc alloc(a: ^Mem.Allocator; size, align: I64): ^U8;
25+var c, n: I64; b: ^Block; p: ^U8; h: ^I64;
26+begin
27+ n := size + Header;
28+ if (size < 0) or (align > Header) and (align > Mem.Page) then return nil; end;
29+ c := 0;
30+ while (c < Classes) and (16 shl c < n) do INC(c); end;
31+ if (c = Classes) or (align > Header) then
32+ n := (n + Mem.Page - 1) div Mem.Page * Mem.Page;
33+ p := Mem.pages(n);
34+ if p = nil then return nil; end;
35+ h := ^I64(p);
36+ h^ := -n;
37+ return p + Header;
38+ end;
39+ b := lists[c];
40+ if b # nil then
41+ lists[c] := b^.next;
42+ p := ^U8(b);
43+ else
44+ if left < 16 shl c then
45+ cur := Mem.pages(Refill);
46+ if cur = nil then left := 0; return nil; end;
47+ left := Refill;
48+ end;
49+ p := cur;
50+ cur := cur + (16 shl c);
51+ left := left - (16 shl c);
52+ end;
53+ h := ^I64(p);
54+ h^ := c;
55+ return p + Header;
56+end;
57+
58+proc free(a: ^Mem.Allocator; p: ^U8);
59+var h: ^I64; b: ^Block;
60+begin
61+ if p = nil then return; end;
62+ h := ^I64(p - Header);
63+ if h^ < 0 then
64+ Mem.unmap(^U8(h), -h^);
65+ else
66+ b := ^Block(h);
67+ b^.next := lists[h^];
68+ lists[h^] := b;
69+ end;
70+end;
71+
72+begin
73+ std := {alloc: alloc, free: free};
74+ heap := ADR(std);
75+end Heap.

lib/Mem.jed added +35 -0

@@ -0,0 +1,35 @@
1+(* Memory: Allocator, the interface every allocator offers, and pages
2+ from the system. NEW(p, a) calls a^.alloc. *)
3+module Mem;
4+import Sys;
5+
6+type
7+ Allocator* = record
8+ alloc*: proc(a: ^Allocator; size, align: I64): ^U8; (* nil if out of room *)
9+ free*: proc(a: ^Allocator; p: ^U8);
10+ end;
11+
12+const Page* = 4096;
13+
14+(* size bytes, rounded up to whole pages, zeroed; or nil. *)
15+proc pages*(size: I64): ^U8;
16+begin
17+ return Sys.mmap((size + Page - 1) div Page * Page);
18+end;
19+
20+proc unmap*(p: ^U8; size: I64);
21+begin
22+ Sys.munmap(p, (size + Page - 1) div Page * Page);
23+end;
24+
25+proc alloc*(a: ^Allocator; size, align: I64): ^U8;
26+begin
27+ return a^.alloc(a, size, align);
28+end;
29+
30+proc free*(a: ^Allocator; p: ^U8);
31+begin
32+ a^.free(a, p);
33+end;
34+
35+end Mem.

lib/Sys.linux-x86_64.jed modified +15 -0

@@ -15,6 +15,21 @@
1515 return SYSCALL(1, fd, ADR(s[0]), LEN(s));
1616 end;
1717
18+(* size bytes of fresh, zeroed memory from the system, a multiple of
19+ pages, or nil. *)
20+proc mmap*(size: I64): ^U8;
21+var p: I64;
22+begin
23+ p := SYSCALL(9, 0, size, 3, 22H, -1, 0); (* read, write; private, anonymous *)
24+ if p < 0 then return nil; end;
25+ return ^U8(p);
26+end;
27+
28+proc munmap*(p: ^U8; size: I64);
29+begin
30+ SYSCALL(11, p, size);
31+end;
32+
1833 (* Ends the program, at once, with status. *)
1934 proc exit*(status: I64);
2035 begin

src/parse.pas modified +47 -1

@@ -473,6 +473,51 @@
473473 x.mode := mReg;
474474 end;
475475
476+{ NEW(p, a): p := a^.alloc(a, SIZE(T), alignment), zeroed if not nil.
477+ a is a ^Mem.Allocator, the one interface the compiler knows. }
478+procedure New_(var x: TItem);
479+var y, z: TItem; t: PType; f: PObj; s: int64; c: TCall; L: longint;
480+begin
481+ Expression(x, nil);
482+ if (x.mode <> mVar) or not x.isVar or (x.typ^.form <> fPtr) then Mark('NEW needs a pointer variable');
483+ t := x.typ^.base;
484+ Save(x);
485+ Expect(sComma, ',');
486+ Expression(y, nil);
487+ if (y.typ^.form <> fPtr) or (y.typ^.base^.obj = nil) or (y.typ^.base^.obj^.module = nil)
488+ or (NameOf(y.typ^.base^.obj^.name) <> 'Allocator')
489+ or (NameOf(y.typ^.base^.obj^.module^.name) <> 'Mem') then Mark('NEW needs a ^Mem.Allocator');
490+ f := Find(y.typ^.base^.scope, Name('alloc'));
491+ s := ToSlot(y);
492+ Load(RAX, Mem(RBP, s), 8, false);
493+ MakeConst(z, f^.typ, 0);
494+ z.mode := mVar; z.base := bReg; z.reg := RAX; z.off := f^.val;
495+ BeginCall(c, z);
496+ Load(RAX, Mem(RBP, s), 8, false);
497+ MakeConst(z, y.typ, 0);
498+ z.mode := mReg;
499+ PassArg(c, z);
500+ MakeConst(z, tI64, t^.size);
501+ PassArg(c, z);
502+ MakeConst(z, tI64, t^.align);
503+ PassArg(c, z);
504+ EndCall(c, z);
505+ L := NoLabel;
506+ TestRR(RAX, RAX);
507+ Jcc(ccE, L);
508+ MovRR(RDX, RAX);
509+ MovRR(RDI, RAX);
510+ MovRI(RCX, t^.size);
511+ MovRI(RAX, 0);
512+ RepStosb;
513+ MovRR(RAX, RDX);
514+ FixLink(L);
515+ z.typ := x.typ;
516+ Assign(x, z);
517+ MakeConst(x, tNone, 0);
518+ x.mode := mNone;
519+end;
520+
476521 procedure Builtin(var x: TItem);
477522 const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9);
478523 var which, n, i: longint; y: TItem; t: PType; slots: array[0..6] of int64;
@@ -544,7 +589,7 @@
544589 MakeConst(x, tNone, 0);
545590 x.mode := mNone;
546591 end;
547- bNew: Mark('NEW comes in step 9');
592+ bNew: New_(x);
548593 end;
549594 Expect(sRparen, ')');
550595 end;
@@ -686,6 +731,7 @@
686731 begin
687732 Next;
688733 ZeroTemp(x, t);
734+ FillChar(seen, SizeOf(seen), 0);
689735 n := 0;
690736 if tok <> sRbrace then
691737 repeat

test/run/Alloc added (executable)

Binary file; the patch has it.

test/run/Alloc.expect (history) added +5 -0

@@ -0,0 +1,5 @@
1+1001000
2+1001000
3+7
4+10100
5+nil again 0

test/run/Alloc.jed added +66 -0

@@ -0,0 +1,66 @@
1+module Alloc;
2+import Out, Mem, Arena, Heap;
3+
4+type
5+ Node = record next: ^Node; val: I64; pad: [5]I64; end;
6+ Big = record data: [1000]I64; end;
7+
8+proc build(a: ^Mem.Allocator; n: I64): ^Node;
9+var head, p: ^Node; i: I64;
10+begin
11+ head := nil;
12+ for i := 1 to n do
13+ NEW(p, a);
14+ if p = nil then return nil; end;
15+ if p^.val # 0 then Out.str("not zeroed "); end;
16+ p^.val := i;
17+ p^.pad[4] := i;
18+ p^.next := head;
19+ head := p;
20+ end;
21+ return head;
22+end;
23+
24+proc sum(p: ^Node): I64;
25+var s: I64;
26+begin
27+ s := 0;
28+ while p # nil do s := s + p^.val + p^.pad[4]; p := p^.next; end;
29+ return s;
30+end;
31+
32+proc release(a: ^Mem.Allocator; p: ^Node);
33+var next: ^Node;
34+begin
35+ while p # nil do
36+ next := p^.next;
37+ Mem.free(a, ^U8(p));
38+ p := next;
39+ end;
40+end;
41+
42+proc main*;
43+var ar: Arena.Arena; l, m: ^Node; b: ^Big; i: I64; small: ^U8; again: ^Node;
44+begin
45+ l := build(Heap.heap, 1000);
46+ Out.int(sum(l)); Out.ln;
47+ release(Heap.heap, l);
48+ m := build(Heap.heap, 1000);
49+ Out.int(sum(m)); Out.ln;
50+ NEW(b, Heap.heap);
51+ b^.data[999] := 7;
52+ Out.int(b^.data[999] + b^.data[0]); Out.ln;
53+ Mem.free(Heap.heap, ^U8(b));
54+ if not Arena.init(ADR(ar), 8192) then Out.str("no arena"); end;
55+ l := build(ADR(ar.base), 100);
56+ Out.int(sum(l)); Out.ln;
57+ l := build(ADR(ar.base), 1000);
58+ if l = nil then Out.str("nil "); end;
59+ Arena.reset(ADR(ar));
60+ NEW(again, ADR(ar.base));
61+ if again # nil then Out.str("again "); Out.int(again^.val); end;
62+ Out.ln;
63+ Arena.done(ADR(ar));
64+end;
65+
66+end Alloc.