jam 0.0.1
A compacting generational garbage collector for C++26
Loading...
Searching...
No Matches
jam.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 <array>
9#include <cstddef>
10#include <span>
11#include <type_traits>
12#include <utility>
13export module jam;
14export import jam.heap;
15export import jam.units;
16
17namespace jam {
20export template<traceable K, traceable V, traceable F>
21[[nodiscard]] hint_inline weak<V> mk_weak(ptr<K> const & key, ptr<V> const & value,
22 ptr<F> const & finalizer, std::type_identity_t<void (*)(F *) noexcept> runner) noexcept {
23 return heap::current()->mk_weak(key, value, finalizer, runner);
24}
25
27export template<traceable T, class... Args>
28[[nodiscard]] hint_inline ptr<T> mk(Args &&... args) noexcept {
29 return heap::current()->mk<T>(std::forward<Args>(args)...);
30}
31
33export template<class T, std::size_t N>
34hint_inline constexpr void unsafe_assign(std::array<ptr<T>, N> & target, std::array<ptr<T>, N> const & source) noexcept {
35 for (std::size_t i = 0; i != N; ++i) target[i].unsafe_assign(source[i]);
36}
37
38export template<class T, std::size_t N>
39hint_inline constexpr void assign(std::array<ptr<T>, N> & target, std::array<ptr<T>, N> const & source) noexcept {
40 unsafe_assign(target, source);
41 if !consteval { if (auto * h = heap::current()) h->remember(std::span<ptr<T>>{target}); }
42}
43
45export template<class T, std::size_t N>
46hint_inline constexpr void unsafe_assign(std::array<weak_ptr<T>, N> & target,
47 std::array<weak_ptr<T>, N> const & source) noexcept {
48 for (std::size_t i = 0; i != N; ++i) target[i].unsafe_assign(source[i]);
49}
50
51export template<class T, std::size_t N>
52hint_inline constexpr void assign(std::array<weak_ptr<T>, N> & target,
53 std::array<weak_ptr<T>, N> const & source) noexcept {
54 unsafe_assign(target, source);
55 if !consteval { if (auto * h = heap::current()) h->remember(std::span<weak_ptr<T>>{target}); }
56}
57
59export hint_inline void collect() noexcept { heap::current()->collect(); }
61export hint_inline void collect_major() noexcept { heap::current()->collect_major(); }
63export hint_inline void collect_minor(bool promote = false) noexcept { heap::current()->collect_minor(promote); }
64
65}
static heap * current() noexcept
Read the current thread's dynamically bound heap. Implicit operations require an active heap_scope....
Definition heap.ccm:512
A typed heap offset, with no root registration or ownership.
Definition heap.ccm:329
A four-byte weak heap field; tracing updates its slot without retaining its target....
Definition heap.ccm:408
An external conditional weak-value handle; registration survives dropped handles.Keep this handle out...
Definition heap.ccm:1788
void collect_major() noexcept
Collect both generations of the current heap; reset its collection countdown.
Definition jam.ccm:61
void collect() noexcept
Collect the current heap; only registered roots remain meaningful outside it.
Definition jam.ccm:59
constexpr void unsafe_assign(std::array< ptr< T >, N > &target, std::array< ptr< T >, N > const &source) noexcept
Bulk-copy pointer bits without registration; the caller owns the barrier.
Definition jam.ccm:34
weak< V > mk_weak(ptr< K > const &key, ptr< V > const &value, ptr< F > const &finalizer, std::type_identity_t< void(*)(F *) noexcept > runner) noexcept
Register a weak key/value pair and managed finalizer in the current heap. The captureless runner rece...
Definition jam.ccm:21
ptr< T > mk(Args &&... args) noexcept
Allocate a record in the current heap; register a root before collection.
Definition jam.ccm:28
constexpr void assign(std::array< ptr< T >, N > &target, std::array< ptr< T >, N > const &source) noexcept
Copy an array and register young lanes with one old-range check.
Definition jam.ccm:39
void collect_minor(bool promote=false) noexcept
Collect the current heap's young generation; optionally promote survivors.
Definition jam.ccm:63