| 1 | (* Allocator, the interface of every allocator, and pages from the system. *) |
| 2 | module Mem; |
| 3 | import Sys; |
| 4 | |
| 5 | type |
| 6 | Allocator* = record |
| 7 | alloc*: proc(a: ^Allocator; size, align: INT): ^U8; (* nil if out of room *) |
| 8 | free*: proc(a: ^Allocator; p: ^U8); |
| 9 | end; |
| 10 | |
| 11 | const Page* = 4096; |
| 12 | |
| 13 | (* size bytes, in whole pages, zeroed; or nil *) |
| 14 | proc pages*(size: INT): ^U8; |
| 15 | begin |
| 16 | return Sys.mmap(size); |
| 17 | end; |
| 18 | |
| 19 | (* pages given back *) |
| 20 | proc unmap*(p: ^U8; size: INT); |
| 21 | begin |
| 22 | Sys.munmap(p, size); |
| 23 | end; |
| 24 | |
| 25 | proc alloc*(a: ^Allocator; size, align: INT): ^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. |