| 1 | (* A general allocator, Heap.heap, safe to call from several threads. |
| 2 | |
| 3 | A small block comes from one of eight free lists, by its size: 16 to |
| 4 | 2048 bytes, each twice the one before, a header of 16 bytes included. |
| 5 | The lists are filled from chunks of pages. A big block, or one aligned |
| 6 | to more than 16 bytes, has pages of its own. The header says which |
| 7 | list a block belongs to, or how many bytes of pages it has, and |
| 8 | whether it is given out: freeing a block twice, or one the heap never |
| 9 | gave, stops the program. *) |
| 10 | module Heap; |
| 11 | import Mem, Lock, Rt; |
| 12 | |
| 13 | const |
| 14 | classes = 8; (* the free lists: blocks of 16 shl i bytes *) |
| 15 | chunkSize = 65536; (* the pages a list is filled from at once *) |
| 16 | given = 0A110CA7EDH; (* a header's mark for a block given out *) |
| 17 | freed = 0F7EEDH; (* and for one on a free list *) |
| 18 | |
| 19 | type |
| 20 | Header = record |
| 21 | size: INT; (* the list, from 0; or minus the bytes of the block's own pages *) |
| 22 | mark: INT; (* given or freed *) |
| 23 | end; |
| 24 | Free = record next: ^Free; end; (* a block on a free list, past its header *) |
| 25 | |
| 26 | var |
| 27 | allocator: Mem.Allocator; |
| 28 | heap*: ^Mem.Allocator; |
| 29 | lists: [classes]^Free; |
| 30 | lock: Lock.Lock; |
| 31 | |
| 32 | (* A block with pages of its own: the header sits just before the |
| 33 | aligned start. *) |
| 34 | proc big(size, align: INT): ^U8; |
| 35 | var bytes, lead: INT; p: ^U8; h: ^Header; base: ^^U8; |
| 36 | begin |
| 37 | lead := 16; |
| 38 | if align > 16 then lead := align; end; |
| 39 | bytes := (size + lead + Mem.Page - 1) div Mem.Page * Mem.Page; |
| 40 | p := Mem.pages(bytes); |
| 41 | if p = nil then return nil; end; |
| 42 | p := p + lead; |
| 43 | (* past an alignment's lead, the pages' start, for free *) |
| 44 | if lead > 16 then base := ^^U8(p - 24); base^ := p - lead; end; |
| 45 | h := ^Header(p - 16); |
| 46 | h^.size := -bytes; |
| 47 | h^.mark := given; |
| 48 | return p; |
| 49 | end; |
| 50 | |
| 51 | (* List c filled from a new chunk; false if the system has no room. *) |
| 52 | proc fill(c: INT): BOOL; |
| 53 | var chunk: ^U8; size, i: INT; f: ^Free; h: ^Header; |
| 54 | begin |
| 55 | chunk := Mem.pages(chunkSize); |
| 56 | if chunk = nil then return false; end; |
| 57 | size := 16 shl c; |
| 58 | for i := 0 to chunkSize div size - 1 do |
| 59 | h := ^Header(chunk + i * size); |
| 60 | h^.size := c; |
| 61 | h^.mark := freed; |
| 62 | f := ^Free(chunk + i * size + 16); |
| 63 | f^.next := lists[c]; |
| 64 | lists[c] := f; |
| 65 | end; |
| 66 | return true; |
| 67 | end; |
| 68 | |
| 69 | proc alloc(a: ^Mem.Allocator; size, align: INT): ^U8; |
| 70 | var c: INT; f: ^Free; h: ^Header; |
| 71 | begin |
| 72 | (* a size the memory can't hold, or an alignment that isn't a power |
| 73 | of 2 up to a page, is refused *) |
| 74 | if size < 0 or size > 1 shl 46 or align <= 0 or align > Mem.Page or align & (align - 1) # 0 then |
| 75 | return nil; |
| 76 | end; |
| 77 | if size + 16 > 16 shl (classes - 1) or align > 16 then return big(size, align); end; |
| 78 | c := 0; |
| 79 | while 16 shl c < size + 16 do INC(c); end; |
| 80 | Lock.take(ADR(lock)); |
| 81 | if lists[c] = nil and not fill(c) then Lock.give(ADR(lock)); return nil; end; |
| 82 | f := lists[c]; |
| 83 | lists[c] := f^.next; |
| 84 | h := ^Header(^U8(f) - 16); |
| 85 | h^.mark := given; |
| 86 | Lock.give(ADR(lock)); |
| 87 | return ^U8(f); |
| 88 | end; |
| 89 | |
| 90 | proc free(a: ^Mem.Allocator; p: ^U8); |
| 91 | var h: ^Header; f: ^Free; |
| 92 | begin |
| 93 | if p = nil then return; end; |
| 94 | h := ^Header(p - 16); |
| 95 | if h^.mark # given then Rt.stop("Heap: a block freed twice, or one the heap never gave"); end; |
| 96 | if h^.size < 0 then |
| 97 | h^.mark := 0; |
| 98 | if INT(p) mod Mem.Page = 16 then |
| 99 | Mem.unmap(p - 16, -h^.size); |
| 100 | else |
| 101 | Mem.unmap(^^U8(p - 24)^, -h^.size); |
| 102 | end; |
| 103 | return; |
| 104 | end; |
| 105 | Lock.take(ADR(lock)); |
| 106 | h^.mark := freed; |
| 107 | f := ^Free(p); |
| 108 | f^.next := lists[h^.size]; |
| 109 | lists[h^.size] := f; |
| 110 | Lock.give(ADR(lock)); |
| 111 | end; |
| 112 | |
| 113 | begin |
| 114 | allocator.alloc := alloc; |
| 115 | allocator.free := free; |
| 116 | heap := ADR(allocator); |
| 117 | end Heap. |