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.
| @@ -94,7 +94,7 @@ | ||
| 94 | 94 | |
| 95 | 95 | - [x] 7. Multiple results and errors; `INC` and `DEC`. |
| 96 | 96 | - [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`. | |
| 98 | 98 | - [ ] 10. Generic modules, with type and procedure arguments; |
| 99 | 99 | `lib/Str.jed`, `lib/Buf.jed`, `lib/Fmt.jed`; examples. |
| 100 | 100 |
| @@ -752,11 +752,11 @@ | ||
| 752 | 752 | | Module | What | |
| 753 | 753 | |---|---| |
| 754 | 754 | | `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 | | |
| 756 | 756 | | `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 | | |
| 760 | 760 | | `Str` | operations on `[]U8` strings | |
| 761 | 761 | | `Buf` | a growable byte buffer | |
| 762 | 762 | | `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)])` | |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -15,6 +15,21 @@ | ||
| 15 | 15 | return SYSCALL(1, fd, ADR(s[0]), LEN(s)); |
| 16 | 16 | end; |
| 17 | 17 | |
| 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 | + | |
| 18 | 33 | (* Ends the program, at once, with status. *) |
| 19 | 34 | proc exit*(status: I64); |
| 20 | 35 | begin |
| @@ -473,6 +473,51 @@ | ||
| 473 | 473 | x.mode := mReg; |
| 474 | 474 | end; |
| 475 | 475 | |
| 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 | + | |
| 476 | 521 | procedure Builtin(var x: TItem); |
| 477 | 522 | const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9); |
| 478 | 523 | var which, n, i: longint; y: TItem; t: PType; slots: array[0..6] of int64; |
| @@ -544,7 +589,7 @@ | ||
| 544 | 589 | MakeConst(x, tNone, 0); |
| 545 | 590 | x.mode := mNone; |
| 546 | 591 | end; |
| 547 | - bNew: Mark('NEW comes in step 9'); | |
| 592 | + bNew: New_(x); | |
| 548 | 593 | end; |
| 549 | 594 | Expect(sRparen, ')'); |
| 550 | 595 | end; |
| @@ -686,6 +731,7 @@ | ||
| 686 | 731 | begin |
| 687 | 732 | Next; |
| 688 | 733 | ZeroTemp(x, t); |
| 734 | + FillChar(seen, SizeOf(seen), 0); | |
| 689 | 735 | n := 0; |
| 690 | 736 | if tok <> sRbrace then |
| 691 | 737 | repeat |
Binary file; the patch has it.
| @@ -0,0 +1,5 @@ | ||
| 1 | +1001000 | |
| 2 | +1001000 | |
| 3 | +7 | |
| 4 | +10100 | |
| 5 | +nil again 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. |