raz / sbcl / lib / Mem.raz

35 lines · 679 B · raw · history · last change cfa2bc20

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