jam 0.0.1
A compacting generational garbage collector for C++26
Loading...
Searching...
No Matches
packed.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 <bit>
9#include <cstddef>
10#include <cstdint>
11#include <span>
12export module jam.packed;
13import native.isa;
14
15export namespace jam::detail {
16struct heap_block {
17 std::uint32_t live = 0; // Claims, plus hosted pin padding after exact liveness is saved.
18 std::uint32_t destination = 0;
19 std::uint64_t pointers = 0;
20};
21
22// Fill every 2^k-cell group containing a claimed cell, without changing claims.
23[[nodiscard]] hint_inline constexpr std::uint32_t dilate(std::uint32_t live, unsigned k) noexcept {
24 for (unsigned shift = 1; shift < (1u << k); shift <<= 1) {
25 auto const low = ~std::uint32_t{0} / ((std::uint32_t{1} << shift) + 1);
26 live |= ((live >> shift) & low) | ((live & low) << shift);
27 }
28 return live;
29}
30
31// Keep alignment padding before live cells, but no suffix after the last one.
32// Target ranks are unchanged: forwarding only counts bits below a live target.
33[[nodiscard]] hint_inline constexpr std::uint32_t retained(std::uint32_t live, unsigned k) noexcept {
34 if (!k) return live;
35 return dilate(live, k) & (~std::uint32_t{0} >> std::countl_zero(live | 1u));
36}
37
38// The low three bits of each nibble encode k as a thermometer (0, 1, 3, 7).
39// Null means every k is zero. Tables are padded to complete dwords for gathers.
40[[nodiscard]] hint_inline constexpr std::uint32_t retained(heap_block const & item,
41 std::uint8_t const * alignment, std::size_t index) noexcept {
42 if (!alignment) return item.live;
43 auto const flags = (alignment[index >> 1] >> ((index & 1) * 4)) & 7u;
44 return retained(item.live, std::popcount(flags));
45}
46
47// Null old metadata means an old target is stationary during a minor collection.
48struct forwarding_tables {
49 heap_block const * old;
50 heap_block const * young;
51 std::uint8_t const * old_alignment = nullptr;
52 std::uint8_t const * young_alignment = nullptr;
53};
54
55// Dispatch remains per range, shared by both generations.
56struct compactor {
57 void (*move_impl)(std::uint64_t const *, std::uint64_t *, heap_block const *,
58 std::size_t, std::size_t, forwarding_tables, std::uint8_t const *) noexcept;
59 void (*pack_impl)(heap_block const *, heap_block *, std::size_t, std::uint8_t const *,
60 std::uint32_t const *, std::uint32_t *) noexcept;
61 void (*pack_starts_impl)(heap_block const *, heap_block *, std::size_t,
62 std::uint8_t const *, std::uint32_t const *, std::uint32_t *) noexcept;
63 native::isa<> requirement;
64 char const * name;
65 void move(std::uint64_t const * source, std::uint64_t * target, heap_block const * metadata,
66 std::size_t begin, std::size_t end) const noexcept {
67 move_impl(source, target, metadata, begin, end, {metadata, nullptr}, nullptr);
68 }
69 void move(std::uint64_t const * source, std::uint64_t * target, heap_block const * metadata,
70 std::size_t begin, std::size_t end, forwarding_tables tables,
71 std::uint8_t const * alignment = nullptr) const noexcept {
72 move_impl(source, target, metadata, begin, end, tables, alignment);
73 }
74 void pack(heap_block const * metadata, heap_block * output, std::size_t count,
75 std::uint8_t const * alignment = nullptr) const noexcept {
76 pack_impl(metadata, output, count, alignment, nullptr, nullptr);
77 }
81 void pack(heap_block const * metadata, heap_block * output, std::size_t count,
82 std::uint8_t const * alignment, std::uint32_t const * starts,
83 std::uint32_t * destination_starts) const noexcept {
84 pack_starts_impl(metadata, output, count, alignment, starts, destination_starts);
85 }
86};
87}
88
89export namespace jam::detail {
90std::span<compactor const> compactor_variants() noexcept;
91compactor choose_compactor() noexcept;
92}