jam 0.0.1
A compacting generational garbage collector for C++26
Loading...
Searching...
No Matches
mapping.ccm
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3
6module;
7#include <hint.h>
8#include <cstddef>
9#include <cstdint>
10#include <memory>
11#include <utility>
12#include <vector>
13export module jam.mapping;
14
15export namespace jam::detail {
16hint_cold hint_noreturn
17void heap_failure(char const * operation, int code = 0) noexcept;
18
19// One reservation gives both generations stable, ordered address ranges. Only
20// the active ring views acquire backing; maximum capacities reserve address space.
21class generation_reservation {
22 std::byte * base_;
23 std::size_t bytes_;
24public:
25 generation_reservation(std::size_t old_maximum, std::size_t young_maximum) noexcept;
26 generation_reservation(generation_reservation const &) = delete;
27 ~generation_reservation() noexcept;
28 std::byte * data() const noexcept { return base_; }
29};
30
33class heap_mapping {
34#if defined(_WIN32)
35 struct backing;
36#endif
37 struct span {
38 std::size_t begin, end;
39#if defined(_WIN32)
40 std::shared_ptr<backing> section;
41 std::size_t offset;
42#endif
43 };
44 struct reservation {};
45 std::byte * address = nullptr;
46 std::size_t capacity = 0;
47 std::size_t fixed_extent = 0;
48 // Linux's zero-old-size mremap must remain within one original VMA/object.
49 std::vector<span> spans;
50
51 [[noreturn]] static void system_failure(char const * operation) noexcept;
52 static void validate_size(std::size_t bytes) noexcept;
53 explicit heap_mapping(reservation, std::size_t bytes) noexcept;
54#if defined(_WIN32)
55 static void alias(span const & source, std::byte * target, std::size_t count,
56 std::size_t offset = 0) noexcept;
57#else
58 static void alias(std::byte * source, std::byte * target, std::size_t count) noexcept;
59#endif
60 void fresh(std::size_t begin, std::size_t count) noexcept;
61 void map_arc(heap_mapping const & source, std::size_t start, std::size_t target, std::size_t count) noexcept;
62 void finish_aliases() noexcept;
63 void discard_gap(std::size_t start, std::size_t live) const noexcept;
64
65 void reset_fixed() noexcept;
66
67public:
69 explicit heap_mapping(std::size_t bytes) noexcept;
70 heap_mapping(std::size_t bytes, std::byte * base, std::size_t maximum) noexcept;
71 // Stage surviving aliases before touching the fixed range: source and target
72 // rings may overlap during growth, shrinking or view rotation.
73 void replace(heap_mapping & staged) noexcept;
74 heap_mapping(heap_mapping const &) = delete;
75 heap_mapping & operator=(heap_mapping const &) = delete;
76 heap_mapping(heap_mapping && other) noexcept
77 : address(std::exchange(other.address, nullptr)), capacity(std::exchange(other.capacity, 0)),
78 fixed_extent(std::exchange(other.fixed_extent, 0)), spans(std::move(other.spans)) {}
79 heap_mapping & operator=(heap_mapping && other) noexcept { swap(other); return *this; }
81 ~heap_mapping() noexcept;
83 hint_inline hint_pure std::byte * data() const noexcept { return address; }
85 hint_inline hint_pure std::size_t bytes() const noexcept { return capacity; }
87 static std::size_t page_size() noexcept;
91 void publish(std::size_t start, std::byte * target, std::size_t count) const noexcept;
94 static void unpublish(std::byte * target, std::size_t count) noexcept;
97 [[nodiscard]] heap_mapping alias_run(std::size_t start, std::size_t count) const noexcept;
100 void replace_pages(heap_mapping const & source, std::size_t start,
101 std::size_t target, std::size_t count) noexcept;
103 void swap(heap_mapping & other) noexcept {
104 std::swap(address, other.address);
105 std::swap(capacity, other.capacity);
106 std::swap(fixed_extent, other.fixed_extent);
107 spans.swap(other.spans);
108 }
113 heap_mapping resized(std::size_t old_start_bytes, std::size_t live_bytes,
114 std::size_t new_bytes, std::size_t new_start_bytes) const noexcept;
115};
116}