Landin library reference discuss source

hosted/heap/windows.ldn

1--  The heap's pages on Windows.  A mapping reserves its address space in
2--  the system's allocation granularity, 64 KiB, and commits the pages it
3--  holds; the rest of its reservation is where it grows in place, and
4--  where a trimmed tail goes back to, decommitted.  Windows moves no
5--  pages, so a mapping that cannot grow where it is is copied.
6
7import platform/windows
8
9fixed if compiler.os == windows then
10    --  A mapping of `length` readable and writable bytes, or zero.
11    map_pages: (length: usize) -> (address: usize) =
12        unit: usize = windows.allocation_granularity
13        reserved: usize = (length + (unit - 1)) & (0 -% unit)
14        address = windows.virtual_alloc(0, reserved, windows.mem_reserve,
15                                        windows.page_readwrite)
16        return when address == 0
17        if windows.virtual_alloc(address, length, windows.mem_commit,
18                                 windows.page_readwrite) == 0
19        then
20            _ = windows.virtual_free(address, 0, windows.mem_release)
21            address = 0
22        end if
23    end map_pages
24
25    --  Release the reservation at `origin`, whatever it holds; a refusal
26    --  keeps it.
27    release_pages: (origin: usize, length: usize) -> none =
28        _ = length
29        _ = windows.virtual_free(origin, 0, windows.mem_release)
30    end release_pages
31
32    --  Decommit the `length` bytes at `tail`, the end of a mapping, which
33    --  its reservation keeps for growing, and say whether the system did.
34    return_tail: (tail: usize, length: usize) -> (returned: bool) =
35        returned = windows.virtual_free(tail, length, windows.mem_decommit)
36                   <> 0
37    end return_tail
38
39    --  Commit the reserved pages after the mapping of `length` bytes at
40    --  `origin` up to `wanted`, when its own reservation holds them
41    --  uncommitted, and say whether it did.
42    extend_pages: (origin: usize, length: usize, wanted: usize)
43                  -> (grown: bool) =
44        grown = false
45        mut found: windows.memory_information = zeroed
46        written: usize = windows.virtual_query(origin + length, addr found,
47                                               sizeof windows.memory_information)
48        return when written == 0
49        return when found.allocation_base <> origin
50          or found.state <> windows.state_reserved
51          or found.size < wanted - length
52        grown = windows.virtual_alloc(origin + length, wanted - length,
53                                      windows.mem_commit,
54                                      windows.page_readwrite) <> 0
55    end extend_pages
56
57    --  Windows moves no pages: zero, and the block is copied.
58    remap_pages: (origin: usize, length: usize, wanted: usize)
59                 -> (moved: usize) =
60        _ = origin
61        _ = length
62        _ = wanted
63        moved = 0
64    end remap_pages
65
66    page_bytes: () -> (bytes: usize) =
67        bytes = windows.page_bytes
68    end page_bytes
69end if