28#include <unordered_map>
39extern "C" long __builtin_expect(
long,
long)
noexcept;
49export template<
class T>
class root;
50export template<
class T>
class ptr;
51export template<
class T>
class weak_ptr;
53export template<
class T>
class weak;
57export template<
class V>
58concept visitor = std::same_as<typename V::heap_type, heap>;
61template<
class T,
class H = heap>
62concept member_trace =
requires(T & value,
typename H::visitor & visit) {
65template<
class T,
class H = heap>
66concept cooperative_trace =
requires(
typename H::visitor & visit,
ptr<T> const & at) {
69template<
class T,
class H = heap>
70concept allocation_trace =
requires(T
const & value,
typename H::visitor & visit) {
71 { value.claim_and_trace(visit) }
noexcept -> std::same_as<void>;
74concept has_allocation_trace =
requires { &T::claim_and_trace; } || allocation_trace<T>;
76concept has_trace =
requires { &T::trace; } || member_trace<T> || cooperative_trace<T>;
78export struct structural_tracer {};
79template<
class T,
class... Members>
80struct member_manifest {
81 std::tuple<Members T::*...> members;
82 template<
class Visitor>
83 static constexpr bool accepts = (
requires(Visitor & visit, Members
const & value) {
87template<
class T,
class Descriptor>
inline constexpr bool manifest_for =
false;
88template<
class T,
class... Members>
89inline constexpr bool manifest_for<T, member_manifest<T, Members...>> =
true;
91concept has_manifest =
requires {
92 requires manifest_for<T, std::remove_cvref_t<
decltype(T::manifest)>>;
94struct invalid_tracer {};
113export template<
class T,
class... Members>
requires (std::is_object_v<Members> && ...)
115 if (((members ==
nullptr) || ...))
throw "manifest members must not be null";
116 return detail::member_manifest<T, Members...>{{members...}};
125export template<
class T>
128 template<
class Visitor>
129 static constexpr void trace(Visitor &, T
const &)
noexcept {}
139export template<
class T>
140struct tracer : std::conditional_t<requires { T::manifest; } || detail::has_allocation_trace<T>, detail::invalid_tracer, leaf<T>> {};
147export template<
class T>
requires detail::has_trace<T>
150 template<
class Visitor>
151 static constexpr auto trace(Visitor & visit, T
const & value)
152 noexcept(
noexcept(value.trace(visit))) ->
decltype(value.trace(visit)) {
153 return value.trace(visit);
156 template<
class Visitor>
157 static constexpr decltype(
auto)
trace(Visitor & visit,
ptr<T> const & at)
noexcept
158 requires requires { T::trace(visit, at); } {
159 return T::trace(visit, at);
164template<
class T,
class H>
165concept traceable_in = (!has_allocation_trace<T> || allocation_trace<T, H>)
166 && (allocation_trace<T, H> ||
requires(T
const & value,
typename H::visitor & visit) {
169} ||
requires(ptr<T>
const & at,
typename H::visitor & visit) {
181export template<
class T>
184export namespace detail {
185template<
class T>
inline constexpr bool is_ptr =
false;
186template<
class T>
inline constexpr bool is_ptr<ptr<T>> =
true;
187template<
class T>
inline constexpr bool is_weak_ptr =
false;
188template<
class T>
inline constexpr bool is_weak_ptr<weak_ptr<T>> =
true;
189template<
class T,
class Visitor>
190concept visits =
requires(Visitor & visit, std::remove_reference_t<T>
const & value) {
197export template<
class T>
200 template<visitor Visitor>
201 static constexpr void trace(Visitor & visit,
ptr<T> const & value)
202 noexcept(
noexcept(visit(value))) {
209export template<
class T>
212 template<visitor Visitor>
214 noexcept(
noexcept(visit(value))) { visit(value); }
217export namespace detail {
218template<
class Visitor,
class... Ts>
219constexpr void trace_parts(Visitor & visit, Ts
const &... values)
noexcept {
220 auto part = [&](
auto const & value)
constexpr noexcept {
221 if constexpr (
requires { visit(value); }) visit(value);
222 else tracer<std::remove_cvref_t<
decltype(value)>>::trace(visit, value);
230export template<
class... Ts>
231struct tracer<std::tuple<Ts...>> : detail::structural_tracer {
233 template<
class Visitor>
234 static constexpr void trace(Visitor & visit, std::tuple<Ts...>
const & value)
235 requires (detail::visits<Ts, Visitor> && ...) {
236 std::apply([&](
auto const &... part)
constexpr {
237 detail::trace_parts(visit, part...);
244export template<
class T, std::
size_t N>
245struct tracer<std::array<T, N>> : detail::structural_tracer {
247 template<
class Visitor>
248 static constexpr void trace(Visitor & visit, std::array<T, N>
const & value)
249 requires detail::visits<T, Visitor> {
250 for (
auto const & part : value) detail::trace_parts(visit, part);
256export template<
class... Ts>
257struct tracer<std::variant<Ts...>> : detail::structural_tracer {
259 template<
class Visitor>
260 static constexpr void trace(Visitor & visit, std::variant<Ts...>
const & value)
261 requires (detail::visits<Ts, Visitor> && ...) {
262 if (value.valueless_by_exception())
return;
263 std::visit([&](
auto const & part)
constexpr {
264 detail::trace_parts(visit, part);
271export template<
class T>
requires (!detail::has_trace<T>) && detail::has_manifest<T>
272struct tracer<T> : detail::structural_tracer {
274 template<class Visitor>
275 static hint_inline constexpr void
trace(Visitor & visit, T const & value) noexcept
276 requires std::remove_cvref_t<decltype(T::manifest)>::template accepts<Visitor> {
277 auto const & [...member] = T::manifest.members;
278 visit((value.*member)...);
281 template<
class Visitor>
283 requires std::remove_cvref_t<
decltype(T::manifest)>::template accepts<Visitor> {
284 visit.template trace_manifest<T>(at);
293#if defined(JAM_CONTEXT_X28)
294#if !defined(__aarch64__)
295#error "JAM_CONTEXT_X28 requires AArch64 and -ffixed-x28"
297register heap * active_heap
asm(
"x28");
299inline thread_local heap * active_heap =
nullptr;
311 [[nodiscard]] hint_inline
explicit heap_scope(
heap & arena)
noexcept;
313 hint_inline
~heap_scope() noexcept { detail::active_heap = previous; }
328export template<
class T>
337 mutable offset position = 0;
338 hint_inline
constexpr void update_barrier() noexcept;
342 constexpr
ptr() noexcept = default;
344 constexpr
ptr(std::same_as<std::nullptr_t> auto) noexcept {}
346 hint_inline
explicit constexpr ptr(
offset at)
noexcept;
348 hint_inline
constexpr ptr(
ptr const & other)
noexcept;
351 template<
class U>
requires (!std::same_as<T, U>) && std::derived_from<U, T>
352 && detail::allocation_trace<T> &&
requires(T * p) {
static_cast<U *
>(p); }
353 hint_inline
constexpr ptr(
ptr<U> const & other)
noexcept;
355 hint_inline
constexpr ptr & operator=(
ptr const & other)
noexcept;
357 hint_inline
constexpr ptr(
ptr && other)
noexcept;
359 hint_inline
constexpr ptr & operator=(
ptr && other)
noexcept;
361 hint_inline
constexpr void swap(
ptr & other)
noexcept;
363 friend hint_inline
constexpr void swap(
ptr & left,
ptr & right)
noexcept { left.swap(right); }
365 hint_inline
constexpr ~ptr() noexcept;
371 [[nodiscard]]
constexpr bool is_young() const noexcept {
return (position & 0x80000000u) != 0; }
375 [[nodiscard]] hint_inline hint_pure
376 constexpr offset get() const noexcept {
return position; }
378 [[nodiscard]] hint_inline hint_pure
379 explicit constexpr operator bool() const noexcept {
380 return position != 0;
384 [[nodiscard]] hint_inline T *
operator->() const noexcept;
386 [[nodiscard]] hint_inline T & operator*() const noexcept;
394 hint_inline
void prefetch() const noexcept;
397 [[nodiscard]] constexpr auto operator<=>(
ptr const &) const noexcept = default;
407export template<class T>
415 mutable offset position = 0;
416 hint_inline
constexpr void update_barrier() noexcept;
421 constexpr
weak_ptr(std::same_as<std::nullptr_t> auto) noexcept {}
427 template<
class U>
requires (!std::same_as<T, U>) && std::convertible_to<
ptr<U>,
ptr<T>>
430 hint_inline
constexpr weak_ptr(weak_ptr
const & other)
noexcept;
431 hint_inline
constexpr weak_ptr & operator=(weak_ptr
const & other)
noexcept;
433 hint_inline
constexpr weak_ptr(weak_ptr && other)
noexcept;
435 hint_inline
constexpr weak_ptr & operator=(weak_ptr && other)
noexcept;
437 hint_inline
constexpr ~weak_ptr() noexcept;
439 hint_inline constexpr
void swap(weak_ptr & other) noexcept;
447 [[nodiscard]] hint_inline hint_pure
constexpr offset get() const noexcept {
return position; }
449 [[nodiscard]]
constexpr bool is_young() const noexcept {
return (position & 0x80000000u) != 0; }
451 [[nodiscard]]
constexpr bool expired() const noexcept {
return position == 0; }
454 [[nodiscard]] hint_inline
root<T> lock() const noexcept requires
traceable<T>;
456 [[nodiscard]] constexpr auto operator<=>(
weak_ptr const &) const noexcept = default;
503 template<
class T>
friend class ptr;
504 template<
class T>
friend class weak_ptr;
505 template<
class T>
friend class weak;
511 [[nodiscard]] hint_pure hint_inline
512 static heap *
current() noexcept {
return detail::active_heap; }
533 static_assert(
sizeof(std::size_t) == 8,
"jam requires 64-bit addressing");
534 static_assert(
sizeof(
block) == 16);
535 static_assert(std::atomic_ref<bitmap>::is_always_lock_free);
536 static_assert(std::atomic_ref<word>::is_always_lock_free);
542 struct trace_job {
offset at; trace_function trace; };
544 trace_job key, value, finalizer;
545 void (*invoke)(
heap &, weak_entry &)
noexcept;
547 weak_entry * previous_running =
nullptr;
549 template<
class F>
struct finalizer_entry : weak_entry {
550 void (*runner)(F *)
noexcept;
562 std::uint32_t slot = 0;
563 hint_inline
void retain()
noexcept {
566 hint_inline
void detach()
noexcept {
567 if (slot)
heap::current()->release_root(std::exchange(slot, 0));
569 hint_inline root_handle(heap & arena,
offset at, trace_function fn =
nullptr) noexcept
570 : slot(arena.add_root(at, fn)) {}
575 hint_inline root_handle(root_handle const & other) noexcept : slot(other.slot) { retain(); }
577 hint_inline
root_handle(root_handle && other) noexcept : slot(std::exchange(other.slot, 0)) {}
579 hint_inline root_handle &
operator=(root_handle
const & other)
noexcept {
580 if (
this != &other) { detach(); slot = other.slot; retain(); }
584 hint_inline root_handle &
operator=(root_handle && other)
noexcept {
585 if (
this != &other) { detach(); slot = std::exchange(other.slot, 0); }
597 [[nodiscard]] hint_inline
explicit operator bool() const noexcept {
return get() !=
null; }
609 work::context<trace_job> & pending;
610 void const * record =
nullptr;
612 std::size_t record_bytes = 0;
613 visitor(heap & h, work::context<trace_job> & tasks) noexcept
614 : arena(h), pending(tasks) {}
616 [[nodiscard]] hint_inline std::size_t pointer_slot(
void const *
field)
const noexcept {
617 auto const address =
reinterpret_cast<std::uintptr_t
>(
field);
618 auto const begin =
reinterpret_cast<std::uintptr_t
>(record);
619 auto const delta =
address - begin;
620 if (!record ||
address < begin || record_bytes <
sizeof(
offset)
621 || delta > record_bytes -
sizeof(
offset) || delta %
sizeof(
offset))
622 detail::heap_failure(
"typed visitor requires a field in its tracing value");
623 return delta /
sizeof(
offset);
626 hint_inline
void enqueue(
ptr<T> const & value)
noexcept {
627 static_assert(
traceable<T>,
"reference target must support tracing");
628 if (value && (!arena.minor_collection || value.
is_young())) pending.push({value.
get(), &heap::template trace_record<T>});
633 struct manifest_visit {
636 ptr<T> const * next =
nullptr;
638 std::size_t first_slot = 0;
640 hint_inline
void flush()
noexcept {
646 auto const slot = visit.pointer_slot(&value);
647 auto const window = slot & ~std::size_t{63};
648 if (window != first_slot) { flush(); first_slot = window; }
649 bits |=
word{1} << (slot - first_slot);
650 if constexpr (std::same_as<T, U>) {
652 if (next) visit.enqueue(*next);
653 next = std::addressof(value);
655 }
else visit.enqueue(value);
660 hint_inline
void operator()(U
const & value)
noexcept
661 requires (!detail::is_ptr<U> && !detail::is_weak_ptr<U>) && detail::visits<U, visitor> {
662 if constexpr (std::derived_from<tracer<U>, detail::structural_tracer>)
663 tracer<U>::trace(*
this, value);
666 template<
class... Us>
667 hint_inline
void operator()(Us
const &... values)
noexcept
668 requires (
sizeof...(Us) != 1) && (detail::visits<Us, visitor> && ...) {
669 ((*this)(values), ...);
674 void trace_manifest(
ptr<T> const & at)
noexcept {
676 auto const *
current = std::addressof(at);
680 manifest_visit<T> fields{*
this};
681 tracer<T>::trace(fields, *value);
695 [[nodiscard]] hint_inline
bool claim(
offset start, std::size_t words = 1,
696 std::size_t alignment_bytes = 8) noexcept {
697 return arena.claim(start, words, alignment_bytes);
706 heap::require_record<T>();
707 if (!value || !
claim(value.
get(), record_words<T>, std::max(
alignof(T),
sizeof(
word))))
709 auto const * result = arena.template record_address<T>(value.
get());
711 record_offset = value.
get();
712 record_bytes =
sizeof(T);
719 [[nodiscard]] hint_inline T
const *
claim_target(T
const * value)
noexcept {
720 heap::require_record<T>();
721 if (!value)
return nullptr;
722 auto const address =
reinterpret_cast<std::uintptr_t
>(value);
723 auto const old_delta =
address -
reinterpret_cast<std::uintptr_t
>(arena.arena.data());
724 auto const young_delta =
address -
reinterpret_cast<std::uintptr_t
>(arena.nursery.data());
725 bool const young = young_delta < arena.nursery.used() *
sizeof(
word);
726 auto const delta =
young ? young_delta : old_delta;
727 if ((!
young && old_delta >= arena.arena.used() *
sizeof(
word)) || delta %
sizeof(
word))
728 detail::heap_failure(
"claim_target requires a complete object in this heap");
729 auto const at =
static_cast<offset>(delta /
sizeof(
word)) | (
young ? young_bit : 0);
730 if (!
claim(at, record_words<T>, std::max(
alignof(T),
sizeof(
word))))
return nullptr;
733 record_bytes =
sizeof(T);
740 auto const heap_delta = std::size_t(record_offset & offset_mask) *
sizeof(
word) + pointer_slot(&value) *
sizeof(
offset);
741 arena.pointer((record_offset & young_bit) |
static_cast<offset>(heap_delta /
sizeof(
word)),
742 static_cast<unsigned>((heap_delta %
sizeof(
word)) /
sizeof(
offset)));
754 hint_inline
void pointers(
word bits, std::size_t first_slot = 0) noexcept {
756 auto const slots = record_bytes /
sizeof(
offset);
757 if (!record || first_slot > slots
758 ||
static_cast<std::size_t
>(std::bit_width(bits)) > slots - first_slot)
759 detail::heap_failure(
"pointer pattern exceeds its tracing value");
760 auto & g = arena.containing(record_offset);
761 auto const slot = std::size_t(record_offset & offset_mask) *
fields_per_word + first_slot;
763 auto const shift = slot % width;
764 std::atomic_ref<word>{g.metadata[slot / width].pointers}
765 .fetch_or(bits << shift, std::memory_order_relaxed);
766 if (shift && (bits >> (width - shift)))
767 std::atomic_ref<word>{g.metadata[slot / width + 1].pointers}
768 .fetch_or(bits >> (width - shift), std::memory_order_relaxed);
773 [[nodiscard]] hint_inline T
const *
claim(
ptr<T> const & value)
noexcept {
782 arena.pointer(cell, slot);
783 auto const target = arena.field(cell, slot);
785 pending.push({
target,
nullptr});
790 if (at && (!arena.minor_collection || (at & young_bit))) pending.push({at,
nullptr});
797 requires (!detail::is_ptr<T> && !detail::is_weak_ptr<T>) && detail::visits<T, visitor> {
798 tracer<T>::trace(*
this, value);
816 template<
class... Ts>
818 requires (
sizeof...(Ts) != 1) && (detail::visits<Ts, visitor> && ...) {
819 detail::trace_parts(*
this, values...);
823 hint_inline
void poll() noexcept { pending.poll(); }
828 static constexpr std::size_t record_words = (
sizeof(T) +
sizeof(word) - 1) /
sizeof(word);
831 static consteval void require_record() noexcept {
835 static_assert(std::is_object_v<T> && !std::is_const_v<T> && !std::is_volatile_v<T>,
836 "jam records must be unqualified objects that permit byte relocation");
837 static_assert(
alignof(T) <= 64,
"record alignment exceeds 64 bytes");
842 [[nodiscard]] hint_inline T * record_address(offset at)
noexcept {
844 assert(at && (at & offset_mask) < containing(at).cursor
845 && record_words<T> <= containing(at).cursor - (at & offset_mask));
846 auto * p = decode(at);
847 if constexpr (std::is_trivially_copyable_v<T>) {
848 return static_cast<T *
>(std::memmove(p, p,
sizeof(T)));
852 return reinterpret_cast<T *
>(p);
857 static void trace_record(visitor & visit, offset at)
noexcept {
859 static_assert(traceable<T>,
"tracing hooks must accept the heap visitor");
860 if constexpr (detail::allocation_trace<T>) {
862 visit.arena.template record_address<T>(at)->claim_and_trace(visit);
863 }
else if constexpr (
requires { tracer<T>::trace(visit, ptr<T>{at}); }) {
865 tracer<T>::trace(visit, ptr<T>{at});
866 }
else if (
auto const * value = visit.claim_target(ptr<T>{at})) {
867 tracer<T>::trace(visit, *value);
876 static constexpr offset young_bit = 0x80000000u;
877 static constexpr offset offset_mask = 0x7fffffffu;
880 detail::compactor
const & compactor;
881 work::pool & workers;
882 std::size_t
const page_word_count;
883 std::size_t
const reserve_word_count;
884 std::size_t
const minimum_capacity;
885 detail::heap_mapping storage;
887 std::vector<block> metadata;
891 std::vector<std::uint8_t> alignment;
892 bool has_alignment =
false;
893 std::size_t cursor = 1;
894 std::size_t origin = 0;
895 bool collecting =
false;
896 void require_mutator()
const noexcept {
897 if (collecting) detail::heap_failure(
"mutation during collection");
899 static constexpr unsigned block_shift = 5;
900 static constexpr std::size_t maximum_bytes = std::size_t{1} << 34;
902 [[nodiscard]]
static constexpr bool valid_options(
generation_options const & config)
noexcept {
905 && config.
shrink_shift < std::numeric_limits<std::size_t>::digits;
908 explicit generation(settings s, detail::compactor
const & c, work::pool & w, std::byte *
address)
noexcept;
909 [[nodiscard]]
static constexpr unsigned exponent(std::size_t bytes)
noexcept {
910 if (!std::has_single_bit(bytes) || bytes < 8 || bytes > 64u)
911 detail::heap_failure(
"unsupported record alignment");
912 return std::countr_zero(bytes) - 3;
914 [[nodiscard]]
unsigned flags(std::size_t index)
const noexcept {
915 return (alignment[index >> 1] >> ((index & 1) * 4)) & 15;
917 hint_inline
void add_flags(std::size_t index,
unsigned bits)
noexcept {
918 std::atomic_ref<std::uint8_t>{alignment[index >> 1]}.fetch_or(
919 static_cast<std::uint8_t
>(bits << ((index & 1) * 4)), std::memory_order_relaxed);
921 hint_inline
void note_record(
offset first, std::size_t words, std::size_t bytes)
noexcept {
922 assert(std::has_single_bit(bytes) && bytes >= 8 && bytes <= 64);
923 assert((
static_cast<std::size_t
>(first) * 8) % bytes == 0);
925 auto const k = std::countr_zero(bytes) - 3;
926 if (k) add_flags(first >> block_shift, (1u << k) - 1);
927 auto const last = (
static_cast<std::size_t
>(first) + words - 1) >> block_shift;
928 for (
auto i =
static_cast<std::size_t
>(first) >> block_shift; i < last; ++i) add_flags(i, 8);
930 hint_inline
void mark_range(
offset first, std::size_t words)
noexcept {
932 auto const bit =
static_cast<std::size_t
>(first & (
block_words - 1));
933 auto const count = std::min(words,
block_words - bit);
934 std::atomic_ref<bitmap>{metadata[first >> block_shift].live}.fetch_or(
936 first +=
static_cast<offset>(count);
940 [[nodiscard]]
static constexpr word pointer_cells(
bitmap live)
noexcept {
942 x = (x | (x << 16)) & 0x0000ffff0000ffffULL;
943 x = (x | (x << 8)) & 0x00ff00ff00ff00ffULL;
944 x = (x | (x << 4)) & 0x0f0f0f0f0f0f0f0fULL;
945 x = (x | (x << 2)) & 0x3333333333333333ULL;
946 x = (x | (x << 1)) & 0x5555555555555555ULL;
949 [[nodiscard]] std::uint8_t
const * alignment_table()
const noexcept {
950 return has_alignment ? alignment.data() :
nullptr;
952 [[nodiscard]]
offset base(
block const & item)
const noexcept {
953 return item.destination;
955 [[nodiscard]]
offset forward(
offset value)
const noexcept {
957 assert(value < cursor);
958 auto const & item = metadata[value >> block_shift];
960 if (!(item.live & bit))
return null;
961 return base(item) +
static_cast<offset>(std::popcount(detail::retained(item, alignment_table(), value >> block_shift) & (bit - 1)));
963 void resize(std::size_t words)
noexcept;
969 [[nodiscard]] std::size_t
capacity() const noexcept {
return storage.bytes() / 8; }
971 [[nodiscard]] std::size_t
reserved() const noexcept {
return reserve_word_count; }
973 [[nodiscard]] std::size_t
used() const noexcept {
return cursor; }
975 [[nodiscard]] std::size_t
start() const noexcept {
return origin; }
977 [[nodiscard]] std::size_t
page_words() const noexcept {
return page_word_count; }
980 [[nodiscard]] hint_inline hint_pure
981 auto data(
this Self & self hint_lifetimebound)
noexcept
982 -> std::conditional_t<std::is_const_v<Self>,
word const,
word> * {
983 using cell = std::conditional_t<std::is_const_v<Self>,
word const,
word>;
984 return static_cast<cell *
>(self.view_base);
987 [[nodiscard]]
decltype(
auto)
operator[](
this auto & self hint_lifetimebound,
offset index)
noexcept {
988 assert(index < self.cursor);
return self.data()[index];
999 auto const address =
reinterpret_cast<std::byte
const *
>(
data() + cell) + slot *
sizeof(
offset);
1000 std::memcpy(&value,
address,
sizeof(value));
1014 auto const address =
reinterpret_cast<std::byte *
>(
data() + cell) + slot *
sizeof(
offset);
1015 std::memcpy(
address, &value,
sizeof(value));
1018 [[nodiscard]] std::span<block const>
blocks() const noexcept {
return metadata; }
1028 [[nodiscard]]
offset allocate(std::size_t words, std::size_t alignment_bytes = 8) noexcept
1029 hint_diagnose_if(words == 0,
"jam records must contain at least one cell")
1030 hint_diagnose_if(!std::has_single_bit(alignment_bytes) || alignment_bytes < 8 || alignment_bytes > 64u,
"unsupported jam record alignment") {
1032 auto const k = exponent(alignment_bytes);
1033 auto const aligned = (cursor + (std::size_t{1} << k) - 1) & ~((std::size_t{1} << k) - 1);
1034 if (!words) detail::heap_failure(
"zero-word allocation");
1035 auto const limit =
units::bytes{policy.maximum}.count() / 8 - reserve_word_count;
1036 if (aligned > limit || words > limit - aligned)
1037 detail::heap_failure(
"capacity overflow");
1038 while (words >
capacity() - reserve_word_count - std::min(aligned,
capacity() - reserve_word_count))
1041 cursor = aligned + words;
1042 return static_cast<offset>(aligned);
1045 void clear_marks() noexcept;
1054 void mark(
offset first, std::
size_t words = 1, std::
size_t alignment_bytes = 8) noexcept {
1055 assert(first !=
null && first <= cursor && words <= cursor - first);
1056 note_record(first, words, alignment_bytes); mark_range(first, words);
1065 [[nodiscard]] hint_inline
bool claim(
offset first, std::size_t words = 1,
1066 std::size_t alignment_bytes = 8) noexcept {
1067 assert(first !=
null && words && first < cursor && words <= cursor - first);
1069 auto const previous = std::atomic_ref<bitmap>{metadata[first >> block_shift].live}
1070 .fetch_or(bit, std::memory_order_relaxed);
1071 if ((previous & bit) != 0)
return false;
1072 note_record(first, words, alignment_bytes);
1073 mark_range(first + 1, words - 1);
1086 std::atomic_ref<word>{metadata[where >> block_shift].pointers}
1087 .fetch_or(
word{1} << bit, std::memory_order_relaxed);
1103 void compact(std::span<offset> roots = {})
noexcept;
1106 [[nodiscard]] std::size_t prepare(std::size_t start,
bool keep_null)
noexcept;
1107 void move_to(word * target, detail::forwarding_tables tables)
noexcept;
1108 void zero_gaps(word * target, std::size_t previous_end)
const noexcept;
1109 void pack_into(std::vector<block> & output)
const noexcept;
1110 void ensure(std::size_t words)
noexcept;
1111 [[nodiscard]] std::size_t rotated_origin() const noexcept {
1112 return (origin + capacity() - reserve_word_count) % capacity();
1114 [[nodiscard]] word * at_origin(std::size_t start)
const noexcept {
1115 return reinterpret_cast<word *
>(storage.data()) + start;
1117 void adopt(std::size_t next_origin, std::size_t words)
noexcept;
1118 template<
bool tracks_starts = false>
void reset(bitmap * starts =
nullptr) noexcept;
1119 void compact_impl(std::span<offset> roots) noexcept;
1122 detail::compactor const compactor = detail::choose_compactor();
1124 detail::generation_reservation reservation;
1127 heap_options const policy;
1128 std::
size_t minors_left;
1131 std::uint32_t copies, previous, next;
1132 trace_function trace;
1134 static_assert(
sizeof(root_slot) == 24);
1135 std::vector<root_slot> root_slots;
1136 std::uint32_t roots_head = 0, free_root = 0;
1137 [[nodiscard]] hint_inline std::uint32_t add_root(offset at, trace_function trace)
noexcept {
1138 assert(heap::current() ==
this);
1140 return at ? allocate_root(at, trace) : 0;
1142 [[nodiscard]] std::uint32_t allocate_root(offset at, trace_function trace)
noexcept;
1143 hint_inline
void retain_root(std::uint32_t slot)
noexcept {
1145 auto & count = root_slots[slot - 1].copies;
1146 if (count == std::numeric_limits<std::uint32_t>::max())
1147 detail::heap_failure(
"root reference count overflow");
1150 hint_inline
void release_root(std::uint32_t slot)
noexcept {
1152 auto & entry = root_slots[slot - 1];
1153 if (--entry.copies)
return;
1156 void recycle_root(std::uint32_t slot)
noexcept;
1157 std::vector<std::shared_ptr<weak_entry>> weak_entries;
1158 std::deque<std::shared_ptr<weak_entry>> pending_finalizers;
1159 weak_entry * running_finalizer =
nullptr;
1160 bool collecting =
false;
1161 bool minor_collection =
false;
1162 std::unordered_map<std::uint32_t, trace_function> remembered;
1165 static void weak_slot(visitor &, offset)
noexcept {}
1166 static constexpr unsigned block_shift = 5;
1167 static constexpr offset young_bit = 0x80000000u;
1168 static constexpr offset offset_mask = 0x7fffffffu;
1169 [[nodiscard]]
static heap_options configure(heap_options config);
1170 struct configured {};
1171 heap(configured, heap_options config)
noexcept;
1172 [[nodiscard]] hint_inline generation & containing(offset at)
noexcept {
return at & young_bit ? nursery : arena; }
1173 [[nodiscard]] hint_inline generation
const & containing(offset at)
const noexcept {
return at & young_bit ? nursery : arena; }
1174 template<
class Self>
1175 [[nodiscard]] hint_inline hint_pure
1176 auto decode(
this Self & self hint_lifetimebound, offset bits)
noexcept
1177 -> std::conditional_t<std::is_const_v<Self>, std::byte
const, std::byte> * {
1178 using byte = std::conditional_t<std::is_const_v<Self>, std::byte
const, std::byte>;
1180 std::uint32_t doubled;
1181 auto * base = (__builtin_add_overflow(bits, bits, &doubled) ? self.nursery : self.arena).data();
1182 return reinterpret_cast<byte *
>(base) + std::size_t(doubled) * 4;
1184 [[nodiscard]] hint_inline
bool old_slot(
void const * address, std::uint32_t & slot)
const noexcept {
1185 auto const delta =
reinterpret_cast<std::uintptr_t
>(address) -
reinterpret_cast<std::uintptr_t
>(arena.storage.data());
1186 auto const bytes = arena.storage.bytes();
1187 if (delta >= 2 * bytes)
return false;
1188 auto const physical = delta >=
bytes ? delta -
bytes : delta;
1189 auto const start = arena.origin *
sizeof(word);
1190 auto const logical = physical >= start ? physical - start : physical +
bytes - start;
1191 assert(logical %
sizeof(offset) == 0 && logical /
sizeof(offset) < 2 * arena.cursor);
1192 slot =
static_cast<std::uint32_t
>(logical /
sizeof(offset));
1195 hint_inline
void forget(
void const * address)
noexcept {
1197 if (old_slot(address, slot)) forget_slot(slot);
1199 hint_noinline
void forget_slot(std::uint32_t slot)
noexcept;
1200 hint_noinline
void remember_slot(std::uint32_t slot, trace_function trace)
noexcept;
1201 void require_mutator() const noexcept {
1202 if (collecting) detail::heap_failure(
"mutation during collection");
1204 [[nodiscard]] offset forward(offset value,
bool minor)
const noexcept {
1205 if (!value || (minor && !(value & young_bit)))
return value;
1206 return containing(value).forward(value & offset_mask);
1209 [[nodiscard]]
bool marked(offset at)
const noexcept {
1210 if (!at)
return false;
1211 if (minor_collection && !(at & young_bit))
return true;
1212 auto const local = at & offset_mask;
1213 return (containing(at).metadata[local >> block_shift].live >> (local % block_words)) & 1;
1215 void run_finalizer(std::shared_ptr<weak_entry>
const & entry)
noexcept;
1216 void drain_finalizers() noexcept;
1217 void finalize(std::shared_ptr<weak_entry> const & entry) noexcept;
1218 void compact_impl(std::span<offset> roots,
bool minor = false,
bool promote = false) noexcept;
1230 std::size_t words_, references_ = 1, residue_;
1231 detail::heap_mapping alias_;
1232 pin(
offset at, std::size_t words, std::size_t residue, detail::heap_mapping alias) noexcept
1233 : at_(at), words_(words), residue_(residue), alias_(std::move(alias)) {}
1235 [[nodiscard]]
void * address()
const noexcept {
return alias_.data() + residue_ *
sizeof(
word); }
1236 [[nodiscard]]
offset position()
const noexcept {
return at_; }
1241 std::map<offset, std::unique_ptr<pin>> pins_;
1242 struct pin_run { std::size_t first, end, block_begin, block_end; };
1243 std::array<std::vector<pin_run>, 2> pin_runs;
1244 std::vector<gap> pin_gaps;
1245 std::vector<gap> source_gaps;
1247 std::array<std::vector<bitmap>, 2> traced;
1248 [[nodiscard]]
bool alive(
offset at)
const noexcept {
1249 if (!at)
return false;
1250 if (owner.minor_collection && !(at & young_bit))
return true;
1251 auto const & live = traced[at >> 31];
1252 if (live.empty())
return owner.marked(at);
1253 auto const local = at & offset_mask;
1256 void prepare_pin_marks() noexcept;
1257 [[nodiscard]] std::
size_t prepare_pinned(
heap::generation & g,
unsigned which,
1258 std::
size_t start,
bool keep_null) noexcept;
1259 std::array<std::vector<bitmap>, 2> starts_;
1260 template<
bool tracks_starts>
void finish_pinned() noexcept;
1261 template<
bool tracks_starts>
void finish_impl() noexcept;
1263 std::
size_t const prefix;
1264 enum class phase { idle, marking, retry, prepared };
1265 phase state = phase::idle;
1266 std::size_t old_words = 0, young_words = 0;
1267 bool promoting =
false, moving_pins =
false;
1269 struct publication {
1270 detail::heap_mapping
const * source;
1274 mutable std::vector<publication> publications;
1283 explicit host(heap & storage, std::size_t prefix)
noexcept;
1285 host & operator=(
host const &) =
delete;
1294 void publish(
generation which,
void * target, std::
size_t skip, std::
size_t count) const noexcept;
1299 void unpublish(
void * target, std::
size_t count) const noexcept;
1304 owner.require_mutator();
1305 return (which == generation::young ? owner.nursery.allocate(words) : owner.arena.allocate(words)) | (which == generation::young ? young_bit : 0);
1309 [[nodiscard]] pin * pin_object(
offset at, std::size_t words)
noexcept;
1311 void unpin(pin * value)
noexcept;
1314 for (
auto const & [at, value] : pins_) visit(at);
1317 [[nodiscard]] std::span<gap const>
gaps() const noexcept {
return pin_gaps; }
1321 if (state != phase::idle) detail::heap_failure(
"enable starts during collection");
1322 if (!starts_[0].empty())
return;
1323 std::array<std::vector<bitmap>, 2> next;
1324 next[0].resize(owner.arena.metadata.size());
1325 next[1].resize(owner.nursery.metadata.size());
1329 [[nodiscard]]
bool tracks_starts() const noexcept {
return !starts_[0].empty(); }
1333 auto const local = at & offset_mask;
1334 auto & bits = starts_[at >> 31];
1335 if ((state != phase::idle && state != phase::marking) || local < prefix ||
1336 bits.empty() || local /
block_words >= bits.size())
1337 detail::heap_failure(
"invalid object start declaration");
1338 std::atomic_ref<bitmap>{bits[local /
block_words]}.fetch_or(
1343 return starts_[which == generation::young];
1347 void begin(
bool minor)
noexcept;
1352 template<
class Trace>
1354 std::size_t marker_limit = 1,
bool old_owners =
false) noexcept
1355 requires std::is_nothrow_invocable_v<Trace &,
visitor &,
offset> {
1356 if (state != phase::marking || !marker_limit ||
1357 (old_owners && (!owner.minor_collection || marker_limit != 1)))
1358 detail::heap_failure(
"host trace outside marking phase");
1359 std::vector<trace_job> initial;
1360 initial.reserve(roots.size());
1361 for (
auto at : roots)
1362 if (at && (!owner.minor_collection || old_owners || (at & young_bit))) initial.push_back({at,
nullptr});
1363 owner.drain_jobs(
trace, initial, marker_limit);
1367 if (state != phase::marking && state != phase::retry)
1368 detail::heap_failure(
"host liveness outside marking phase");
1375 [[nodiscard]]
bool prepare(
bool promote =
false) noexcept;
1378 if (state != phase::prepared)
1379 detail::heap_failure(
"host forward outside prepared phase");
1380 return alive(at) ? owner.forward(at, owner.minor_collection) :
null;
1385 void finish() noexcept;
1388 [[nodiscard]]
char const *
compactor_name() const noexcept {
return compactor.name; }
1393 if (!value.is_young())
return;
1395 if (!old_slot(&value, slot))
return;
1396 remember_slot(slot, &heap::template trace_record<T>);
1400 if (values.empty())
return;
1401 std::uint32_t first;
1402 if (!old_slot(values.data(), first))
return;
1404 assert(std::size_t(first) + values.size() <= 2 * arena.cursor);
1405 for (std::size_t i = 0; i != values.size(); ++i)
1406 if (values[i].is_young()) remember_slot(first +
static_cast<std::uint32_t
>(i), &heap::template trace_record<T>);
1411 if (!old_slot(&value, slot))
return;
1412 if (value.is_young()) remember_slot(slot, &weak_slot);
else forget_slot(slot);
1416 if (values.empty())
return;
1417 std::uint32_t first;
1418 if (!old_slot(values.data(), first))
return;
1420 assert(std::size_t(first) + values.size() <= 2 * arena.cursor);
1421 for (std::size_t i = 0; i != values.size(); ++i) {
1422 auto const slot = first +
static_cast<std::uint32_t
>(i);
1423 if (values[i].is_young()) remember_slot(slot, &weak_slot);
else forget_slot(slot);
1439 heap & operator=(
heap const &) =
delete;
1445 assert(at ==
null || (at & offset_mask) < containing(at).cursor);
1453 require_record<T>();
1462 require_record<T>();
1471 template<traceable K, traceable V, traceable F>
1473 ptr<F> const & finalizer, std::type_identity_t<
void (*)(F *)
noexcept> runner)
noexcept;
1500 template<class Trace>
1502 requires std::is_nothrow_invocable_v<Trace &,
visitor &,
offset> {
1512 template<
class Trace>
1514 requires std::is_nothrow_invocable_v<Trace &, visitor &, offset> {
1515 run_collection(std::forward<Trace>(trace),
false,
false);
1520 template<
class Trace>
1522 requires std::is_nothrow_invocable_v<Trace &,
visitor &,
offset> {
1523 run_collection(std::forward<Trace>(trace),
true, promote);
1526 template<
class Trace>
1527 void drain_jobs(Trace & trace, std::vector<trace_job> & initial, std::size_t marker_limit)
noexcept {
1528 if (initial.empty())
return;
1529 auto const marker_count = std::min(marker_limit, workers.available_workers());
1530 auto group = workers.gig<trace_job>([
this, &trace](work::context<trace_job> & pending)
noexcept {
1532 visitor visit{*
this, pending};
1534 while (pending.pop(job)) {
1535 if (job.trace) job.trace(visit, job.at);
1536 else std::invoke(trace, visit, job.at);
1538 }, {.concurrency = marker_count, .caller_only = marker_count == 1});
1539 group.push(std::span<trace_job const>{initial});
1544 hint_inline
void seed_job(std::vector<trace_job> & initial, trace_job job)
noexcept {
1545 if (job.at && !marked(job.at)) initial.push_back(job);
1547 [[nodiscard]] hint_inline std::vector<trace_job> begin_collection(
bool minor)
noexcept {
1551 std::atomic_signal_fence(std::memory_order_seq_cst);
1553 minor_collection = minor;
1554 if (minor) nursery.clear_marks();
else clear_marks();
1557 for (
auto & item : nursery.metadata) item.pointers = 0;
1558 if (!minor)
for (
auto & item : arena.metadata) item.pointers = 0;
1559 std::vector<trace_job> initial;
1560 for (
auto slot = roots_head; slot; slot = root_slots[slot - 1].next) {
1561 auto const & entry = root_slots[slot - 1];
1562 if (entry.trace != &weak_slot && entry.position != null && (!minor || (entry.position & young_bit)))
1563 initial.push_back({entry.position, entry.trace});
1565 if (minor)
for (
auto const & [slot, callback] : remembered) {
1567 std::memcpy(&value,
reinterpret_cast<std::byte
const *
>(arena.data()) + std::size_t(slot) *
sizeof(offset),
sizeof(value));
1568 if ((value & young_bit) && callback != &weak_slot) initial.push_back({value, callback});
1570 for (
auto const & entry : pending_finalizers) {
1571 seed_job(initial, entry->key);
1572 seed_job(initial, entry->finalizer);
1574 for (
auto * entry = running_finalizer; entry; entry = entry->previous_running) {
1575 seed_job(initial, entry->key);
1576 seed_job(initial, entry->finalizer);
1580 hint_inline
void order_weak_entries() noexcept {
1581 std::stable_sort(weak_entries.begin(), weak_entries.end(), [](
auto const & a,
auto const & b)
noexcept {
1582 return a->key.at < b->key.at;
1585 [[nodiscard]] hint_inline
bool retain_weak(std::shared_ptr<weak_entry>
const & entry,
1586 std::vector<trace_job> & initial)
noexcept {
1587 if (marked(entry->key.at)) {
1588 seed_job(initial, entry->value);
1589 seed_job(initial, entry->finalizer);
1593 entry->active =
false;
1594 pending_finalizers.push_back(entry);
1595 seed_job(initial, entry->key);
1596 seed_job(initial, entry->finalizer);
1599 void finish_collection(
bool minor,
bool promote)
noexcept {
1600 compact_impl({}, minor, promote);
1601 collecting =
false; minor_collection =
false;
1602 if (!minor) minors_left = policy.minor_collections;
1603 std::atomic_signal_fence(std::memory_order_seq_cst);
1606 void run_collection(
auto && trace,
bool minor,
bool promote)
noexcept {
1607 std::size_t retained = 0;
1609 auto initial = begin_collection(minor);
1610 drain_jobs(trace, initial, workers.available_workers());
1611 order_weak_entries();
1612 for (
auto const & entry : weak_entries) {
1613 if (retain_weak(entry, initial)) weak_entries[retained++] = entry;
1614 drain_jobs(trace, initial, workers.available_workers());
1617 weak_entries.resize(retained);
1618 finish_collection(minor, promote);
1625 template<
class T,
class... Args>
1627 require_record<T>();
1628 static_assert(
noexcept(T{std::forward<Args>(args)...}),
"record construction must be noexcept");
1632 T
const value{std::forward<Args>(args)...};
1633 auto const at = nursery.allocate(record_words<T>, std::max(
alignof(T),
sizeof(
word)));
1634 std::fill_n(nursery.data() + at, record_words<T>,
word{0});
1636 std::memcpy(nursery.data() + at,
static_cast<void const *
>(&value),
sizeof(T));
1637 return ptr<T>{young_bit | at};
1645 require_record<T>();
1646 assert(!collecting);
1647 return record_address<T>(at.
get());
1654 require_record<T>();
1656 && record_words<T> <= containing(at.
get()).cursor - at.
local_offset());
1657 return *std::launder(
reinterpret_cast<T
const *
>(decode(at.
get())));
1664 require_record<T>();
1667 auto * destination = record_address<T>(at.
get());
1668 if (destination != &value) *destination = value;
1670 [[nodiscard]] std::size_t capacity() const noexcept {
return arena.capacity(); }
1671 [[nodiscard]] std::size_t reserved() const noexcept {
return arena.reserved(); }
1672 [[nodiscard]] std::size_t used() const noexcept {
return arena.used() + nursery.used() - 1; }
1673 [[nodiscard]] std::size_t start() const noexcept {
return arena.start(); }
1674 [[nodiscard]] std::size_t page_words() const noexcept {
return arena.page_words(); }
1675 [[nodiscard]] hint_inline hint_pure
auto * data(
this auto & self)
noexcept {
return self.arena.data(); }
1676 [[nodiscard]]
decltype(
auto)
operator[](
this auto & self, offset index)
noexcept {
return self.containing(index)[index & offset_mask]; }
1677 [[nodiscard]] offset field(offset cell,
unsigned slot = 0) const noexcept {
return containing(cell).field(cell & offset_mask, slot); }
1678 void set_field(offset cell, offset value,
unsigned slot = 0) noexcept { require_mutator(); containing(cell).set_field(cell & offset_mask, value, slot);
1679 if (!(cell & young_bit) && (value & young_bit)) remembered.insert_or_assign(cell * 2 + slot,
nullptr); }
1680 [[nodiscard]] std::span<block const> blocks() const noexcept {
return arena.blocks(); }
1681 [[nodiscard]] offset allocate(std::size_t words, std::size_t alignment_bytes = 8) noexcept {
1682 require_mutator();
return arena.allocate(words, alignment_bytes);
1684 void clear_marks() noexcept { arena.clear_marks(); nursery.clear_marks(); }
1685 void mark(offset first, std::size_t words = 1, std::size_t alignment_bytes = 8) noexcept { containing(first).mark(first & offset_mask, words, alignment_bytes); }
1686 [[nodiscard]] hint_inline
bool claim(offset first, std::size_t words = 1, std::size_t alignment_bytes = 8) noexcept {
1687 if (minor_collection && !(first & young_bit))
return false;
1688 return containing(first).claim(first & offset_mask, words, alignment_bytes);
1690 hint_inline
void pointer(offset where,
unsigned slot = 0) noexcept { containing(where).pointer(where & offset_mask, slot); }
1691 void compact(std::span<offset> roots = {})
noexcept;
1701extern template void work::detail::state<jam::heap::trace_job>::communicate(lane &)
noexcept;
1702template void work::detail::state<jam::heap::trace_job>::communicate(lane &)
noexcept;
1708 detail::active_heap = &arena;
1715export template<
class T>
1719 hint_inline
explicit root(base value) noexcept : base(std::move(value)) {}
1725 hint_inline root(
ptr<T> const & value) noexcept requires
traceable<T> : root(heap::current()->root(value)) {}
1727 template<
class U>
requires (!std::same_as<T, U>) && std::convertible_to<ptr<U>,
ptr<T>>
1733 [[nodiscard]] hint_inline
operator ptr<T>() const noexcept {
return get(); }
1735 [[nodiscard]] hint_inline T *
operator->() const noexcept {
return get().operator->(); }
1737 [[nodiscard]] hint_inline T &
operator*() const noexcept {
return *
get(); }
1748export template<
class T>
1752 hint_inline
explicit weak_root(base value) noexcept : base(std::move(value)) {}
1758 : weak_root(heap::current()->weak_root(value)) {}
1761 : weak_root(value.
get()) {}
1763 template<
class U>
requires (!std::same_as<T, U>) && std::convertible_to<
ptr<U>,
ptr<T>>
1776 hint_inline
void swap(weak_root & other)
noexcept {
1777 std::swap(
static_cast<base &
>(*
this),
static_cast<base &
>(other));
1780 friend hint_inline
void swap(weak_root & a, weak_root & b)
noexcept { a.swap(b); }
1787export template<
class V>
1790 std::shared_ptr<heap::weak_entry> entry;
1791 explicit weak(std::shared_ptr<heap::weak_entry> value) noexcept : entry(std::move(value)) {}
1793 weak()
noexcept =
default;
1795 [[nodiscard]]
bool expired() const noexcept {
return !entry || !entry->active; }
1811template<traceable K, traceable V, traceable F>
1813 ptr<F> const & finalizer, std::type_identity_t<
void (*)(F *)
noexcept> runner)
noexcept {
1815 if (!runner) detail::heap_failure(
"mk_weak requires a runner");
1816 auto entry = std::make_shared<finalizer_entry<F>>();
1817 entry->key = {key.
get(), &trace_record<K>};
1818 entry->value = {value.
get(), &trace_record<V>};
1819 entry->finalizer = {finalizer.
get(), &trace_record<F>};
1820 entry->runner = runner;
1821 entry->invoke = [](heap & owner, weak_entry & base)
noexcept {
1822 auto & entry =
static_cast<finalizer_entry<F> &
>(base);
1823 entry.runner(entry.finalizer.at ? owner.template record_address<F>(entry.finalizer.at) :
nullptr);
1825 weak_entries.push_back(entry);
1830constexpr void weak_ptr<T>::update_barrier() noexcept {
1845 auto const changed = position ^ other.position;
1846 position = other.position;
1847 if (changed & 0x80000000u) update_barrier();
1852 if (
is_young()) { update_barrier(); other.update_barrier(); }
1856 if (
this == &other)
return *
this;
1857 *
this =
static_cast<weak_ptr const &
>(other);
1858 auto const previous = std::exchange(other.position, 0);
1859 if (previous & 0x80000000u) other.update_barrier();
1864 if !
consteval {
if (
auto * h =
heap::current()) h->forget(
this); }
1868 auto const changed = position ^ other.position;
1869 std::swap(position, other.position);
1870 if (changed & 0x80000000u) { update_barrier(); other.update_barrier(); }
1874 if (!position)
return {};
1879constexpr void ptr<T>::update_barrier() noexcept {
1881 if (is_young()) h->
remember(*
this);
else h->forget(
this);
1891template<
class U>
requires (!std::same_as<T, U>) && std::derived_from<U, T>
1892 && detail::allocation_trace<T> &&
requires(T * p) {
static_cast<U *
>(p); }
1895 assert(!other || !
heap::current() ||
static_cast<void const *
>(
static_cast<T
const *
>(other.operator->()))
1896 ==
static_cast<void const *
>(other.operator->()));
1900constexpr ptr<T> & ptr<T>::operator=(
ptr const & other)
noexcept {
1901 auto const changed = position ^ other.position;
1902 position = other.position;
1904 if (changed & heap::young_bit) update_barrier();
1908constexpr ptr<T>::ptr(
ptr && other) noexcept : position(std::exchange(other.position, 0)) {
1909 if (
is_young()) { update_barrier(); other.update_barrier(); }
1912constexpr ptr<T> & ptr<T>::operator=(
ptr && other)
noexcept {
1913 if (
this == &other)
return *
this;
1914 *
this =
static_cast<ptr const &
>(other);
1915 auto const previous = std::exchange(other.position, 0);
1916 if (previous & heap::young_bit) other.update_barrier();
1921 auto const changed = position ^ other.position;
1922 std::swap(position, other.position);
1923 if (changed & heap::young_bit) { update_barrier(); other.update_barrier(); }
1927 if !
consteval {
if (
auto * h =
heap::current()) h->forget(
this); }
1938 if (!position)
return;
1941 __builtin_prefetch(&arena->containing(position).
blocks()[
local_offset() >> heap::block_shift].live, 0, 3);
1945 if (!position)
return;
1948 __builtin_prefetch(arena->decode(position), 0, 3);
1953heap::generation::settings heap::generation::configure(generation_options config) {
1955 throw std::length_error(
"jam generation exceeds 16 GiB");
1956 constexpr auto page =
static_cast<std::size_t
>(units::pages::ratio::num);
1957 if (detail::heap_mapping::page_size() != page)
1958 detail::heap_failure(
"platform page size differs from build invariant");
1959 if (!valid_options(config) || page < 512 || page > maximum_bytes || !std::has_single_bit(page))
1960 detail::heap_failure(
"invalid byte sizes, page size or worker count");
1961 if (config.reserve > config.capacity / 2)
1962 detail::heap_failure(
"capacity must hold twice the reserve");
1965 return {page / 8, reserve / 8, 2 * reserve / 8,
capacity, config};
1968heap::generation::generation(settings s, detail::compactor
const & c, work::pool & w, std::byte *
address) noexcept
1969 : policy(s.policy), compactor(c), workers(w),
1970 page_word_count(s.page_words), reserve_word_count(s.reserved),
1972 view_base(
reinterpret_cast<word *
>(storage.data())), metadata(s.bytes / (8 *
block_words)) {
1973 alignment.resize(((metadata.size() + 7) / 8) * 4);
1976void heap::generation::resize(std::size_t words)
noexcept {
1977 assert(words % page_word_count == 0 && words >= minimum_capacity && words <= maximum_bytes / 8);
1978 assert(cursor <= words - reserve_word_count);
1979 std::vector<block> next_metadata(words /
block_words);
1981 auto const next_origin = words - reserve_word_count;
1982 auto next = storage.resized(origin * 8, cursor * 8, words * 8, next_origin * 8);
1983 alignment.resize(((next_metadata.size() + 7) / 8) * 4);
1984 storage.replace(next); metadata.swap(next_metadata);
1985 origin = next_origin;
1986 view_base =
reinterpret_cast<word *
>(storage.data()) + origin;
1989heap::generation::generation(generation_options config, detail::compactor
const & c, work::pool & w, std::byte *
address)
1993 for (
auto & item : metadata) {
1995 item.destination = 0;
1997 std::fill(alignment.begin(), alignment.end(), 0);
1998 has_alignment =
false;
2003 std::atomic_signal_fence(std::memory_order_seq_cst);
2005 compact_impl(roots);
2007 std::atomic_signal_fence(std::memory_order_seq_cst);
2010std::size_t heap::generation::prepare(std::size_t start,
bool keep_null)
noexcept {
2014 if (keep_null) metadata[0].live |=
bitmap{1};
2015 else metadata[0].live &= ~bitmap{1};
2028 has_alignment =
false;
2029 std::size_t live_words = start;
2030 for (std::size_t begin = 0; begin < old_blocks;) {
2031 auto end = begin + 1;
2032 unsigned k = std::popcount(flags(begin) & 7u);
2033 while ((flags(end - 1) & 8u) && end < old_blocks) {
2034 k = std::max(k,
static_cast<unsigned>(std::popcount(flags(end) & 7u))); ++end;
2036 live_words = (live_words + (std::size_t{1} << k) - 1) & ~((std::size_t{1} << k) - 1);
2037 for (
auto i = begin; i != end; ++i) {
2038 auto & item = metadata[i];
2039 item.pointers &= pointer_cells(item.live);
2042 auto const shift = (i & 1) * 4;
2043 alignment[i >> 1] =
static_cast<std::uint8_t
>((alignment[i >> 1] & ~(7u << shift))
2044 | (((1u << k) - 1) << shift));
2045 has_alignment |= k != 0;
2046 item.destination =
static_cast<offset>(live_words);
2047 live_words += std::popcount(detail::retained(item.live, k));
2054void heap::generation::move_to(
word * target, detail::forwarding_tables tables)
noexcept {
2056 auto const source = data();
2057 auto const source_pages = (cursor + page_word_count - 1) / page_word_count;
2058 auto const wave_pages = reserve_word_count / page_word_count;
2059 auto move_page = [&](std::size_t page)
noexcept {
2060 auto const begin = page * (page_word_count /
block_words);
2061 auto const end = std::min(old_blocks, begin + page_word_count /
block_words);
2062 compactor.move(source, target, metadata.data(), begin, end, tables, alignment_table());
2064 if (workers.available_workers() > 1 && source_pages > 1) {
2065 auto done = std::make_unique<std::atomic<bool>[]>(source_pages);
2066 std::atomic<std::size_t> next{0}, front{0};
2067 auto task = [&](std::size_t)
noexcept {
2069 auto const page = next.fetch_add(1, std::memory_order_relaxed);
2070 if (page >= source_pages)
return;
2071 auto floor = front.load(std::memory_order_acquire);
2072 while (page - floor >= wave_pages) {
2073 front.wait(floor, std::memory_order_relaxed);
2074 floor = front.load(std::memory_order_acquire);
2077 done[page].store(
true, std::memory_order_release);
2081 floor = front.fetch_add(0, std::memory_order_acq_rel);
2084 while (end != source_pages && done[end].
load(std::memory_order_acquire)) ++end;
2085 if (end == floor)
break;
2086 if (front.compare_exchange_strong(floor, end, std::memory_order_acq_rel)) {
2087 front.notify_all();
break;
2089 floor = front.load(std::memory_order_acquire);
2093 auto group = workers.gig<std::size_t>(task);
2094 for (std::size_t i = 0; i != std::min(workers.available_workers(), wave_pages); ++i) group.push(i);
2097 }
else for (std::size_t page = 0; page < source_pages; ++page) move_page(page);
2100void heap::generation::zero_gaps(
word * target, std::size_t previous_end)
const noexcept {
2102 for (std::size_t i = 0; i != old_blocks; ++i) {
2103 auto const next = std::size_t(metadata[i].destination & offset_mask);
2104 std::fill(target + previous_end, target + next,
word{0});
2105 previous_end = next + std::popcount(detail::retained(metadata[i], alignment_table(), i));
2109void heap::generation::pack_into(std::vector<block> & output)
const noexcept {
2111 compactor.pack(metadata.data(), output.data(), old_blocks, alignment_table());
2114void heap::generation::ensure(std::size_t words)
noexcept {
2116 if (words > maximum - reserve_word_count) detail::heap_failure(
"generation capacity overflow");
2117 while (words > capacity() - reserve_word_count) resize(std::min(2 * capacity(), maximum));
2120void heap::generation::adopt(std::size_t next_origin, std::size_t words)
noexcept {
2122 cursor = words; origin = next_origin; view_base = at_origin(origin);
2124 for (std::size_t i = 0; i != blocks; ++i) {
2127 metadata[i].destination = 0;
2129 for (
auto i = blocks; i < old_blocks; ++i) metadata[i] = {};
2130 std::fill(alignment.begin(), alignment.end(), 0);
2131 has_alignment =
false;
2132 auto const smaller = std::max(minimum_capacity, (capacity() / page_word_count / 2) * page_word_count);
2133 if (policy.shrink_shift && smaller < capacity()
2134 && cursor < (capacity() >> policy.shrink_shift)
2135 && cursor <= smaller - reserve_word_count) resize(smaller);
2138template<
bool tracks_starts>
2139void heap::generation::reset(
bitmap * starts)
noexcept {
2140 for (std::size_t i = 0; i != metadata.size(); ++i) {
2142 if constexpr (tracks_starts) starts[i] = 0;
2144 std::fill(alignment.begin(), alignment.end(), 0);
2145 has_alignment =
false;
2146 cursor = 1; data()[0] = 0;
2149void heap::generation::compact_impl(std::span<offset> roots)
noexcept {
2150 auto const live_words = prepare(0,
true);
2151 auto const next_origin = rotated_origin();
2152 auto * target = at_origin(next_origin);
2153 move_to(target, {metadata.data(),
nullptr, alignment_table(),
nullptr});
2155 zero_gaps(target, 0);
2156 pack_into(metadata);
2157 adopt(next_origin, live_words);
2163 static_cast<void>(generation::configure(config.old));
2164 static_cast<void>(generation::configure(config.young));
2165 if (!config.workers) detail::heap_failure(
"worker count must be positive");
2170 : workers(config.workers),
2172 arena(config.old, compactor, workers, reservation.data()),
2173 nursery(config.young, compactor, workers, reservation.data() + 2 *
units::bytes{config.old.maximum}.count()),
2174 policy(config), minors_left(config.minor_collections) {}
2176void heap::compact_impl(std::span<offset> roots,
bool minor,
bool promote)
noexcept {
2177 auto const old_end = minor ? arena.cursor : arena.prepare(0,
true);
2178 auto const young_start = minor && !promote ? std::size_t(young_bit) : old_end;
2179 auto const end = nursery.prepare(young_start, minor && !promote);
2180 auto & destination = minor && !promote ? nursery : arena;
2181 auto const live_words = end - (minor && !promote ? std::size_t(young_bit) : 0);
2182 destination.ensure(live_words);
2184 detail::forwarding_tables tables{minor ? nullptr : arena.metadata.data(), nursery.metadata.data(),
2185 minor ? nullptr : arena.alignment_table(), nursery.alignment_table()};
2186 auto const next_origin = minor && promote ? destination.origin : destination.rotated_origin();
2187 auto * target = destination.at_origin(next_origin);
2189 arena.move_to(target, tables);
2190 arena.zero_gaps(target, 0);
2192 nursery.move_to(target, tables);
2193 nursery.zero_gaps(target, minor && !promote ? 0 : old_end);
2195 for (
auto slot = roots_head; slot; slot = root_slots[slot - 1].next) {
2196 auto & entry = root_slots[slot - 1];
2197 entry.position = forward(entry.position, minor);
2199 if (minor)
for (
auto entry = remembered.begin(); entry != remembered.end();) {
2200 auto const slot = entry->first;
2201 auto * address = reinterpret_cast<std::byte *>(arena.data()) + std::size_t(slot) * sizeof(offset);
2203 std::memcpy(&value, address, sizeof(value));
2204 value = forward(value, true);
2205 std::memcpy(address, &value, sizeof(value));
2206 if (value) ++entry; else entry = remembered.erase(entry);
2208 auto forward_entry = [&](weak_entry & entry)
noexcept {
2209 entry.key.at = forward(entry.key.at, minor);
2210 entry.value.at = entry.active ? forward(entry.value.at, minor) : null;
2211 entry.finalizer.at = forward(entry.finalizer.at, minor);
2213 for (
auto const & entry : weak_entries) forward_entry(*entry);
2214 for (
auto const & entry : pending_finalizers) forward_entry(*entry);
2215 for (
auto * entry = running_finalizer; entry; entry = entry->previous_running) forward_entry(*entry);
2218 if (!minor) arena.pack_into(arena.metadata);
2219 nursery.pack_into(destination.metadata);
2220 destination.adopt(next_origin, live_words);
2221 if (!minor || promote) { nursery.reset(); remembered.clear(); }
2225 : heap(configured{}, configure(config)) {}
2228 if (roots_head || running_finalizer) detail::heap_failure(
"heap destroyed with attached roots or callbacks");
2229 for (
auto const & entry : weak_entries) entry->active =
false;
2233 collect([](
visitor &,
offset)
noexcept { detail::heap_failure(
"untyped target in typed collection"); });
2244void heap::compact(std::span<offset> roots)
noexcept {
2246 if (!weak_entries.empty() || !pending_finalizers.empty() || running_finalizer)
2247 detail::heap_failure(
"raw compact requires no weak registrations or pending finalizers");
2248 std::atomic_signal_fence(std::memory_order_seq_cst);
2249 collecting =
true; compact_impl(roots); collecting =
false;
2250 std::atomic_signal_fence(std::memory_order_seq_cst);
2255std::uint32_t heap::allocate_root(offset at, trace_function trace)
noexcept {
2256 auto slot = free_root;
2257 if (slot) free_root = root_slots[slot - 1].next;
2259 if (root_slots.size() == std::numeric_limits<std::uint32_t>::max())
2260 detail::heap_failure(
"root slot capacity overflow");
2261 root_slots.push_back({});
2262 slot =
static_cast<std::uint32_t
>(root_slots.size());
2264 root_slots[slot - 1] = {at, 1, 0, roots_head, trace};
2265 if (roots_head) root_slots[roots_head - 1].previous = slot;
2270void heap::recycle_root(std::uint32_t slot)
noexcept {
2271 auto & entry = root_slots[slot - 1];
2272 if (entry.previous) root_slots[entry.previous - 1].next = entry.next;
2273 else roots_head = entry.next;
2274 if (entry.next) root_slots[entry.next - 1].previous = entry.previous;
2275 entry = {0, 0, 0, free_root,
nullptr};
2279hint_noinline
void heap::forget_slot(std::uint32_t slot)
noexcept {
2280 remembered.erase(slot);
2283hint_noinline
void heap::remember_slot(std::uint32_t slot, trace_function trace)
noexcept {
2285 remembered.insert_or_assign(slot, trace);
2290void heap::run_finalizer(std::shared_ptr<weak_entry>
const & entry)
noexcept {
2291 entry->previous_running = running_finalizer;
2292 running_finalizer = entry.get();
2295 entry->invoke(*
this, *entry);
2297 running_finalizer = entry->previous_running;
2300void heap::drain_finalizers() noexcept {
2301 if (running_finalizer)
return;
2302 while (!pending_finalizers.empty()) {
2303 auto entry = std::move(pending_finalizers.front());
2304 pending_finalizers.pop_front();
2305 run_finalizer(entry);
2309void heap::finalize(std::shared_ptr<weak_entry>
const & entry)
noexcept {
2311 if (!entry->active)
return;
2312 entry->active =
false;
2313 std::erase(weak_entries, entry);
2314 run_finalizer(entry);
2320heap::host::host(heap & storage, std::size_t prefix) noexcept : owner(storage), prefix(prefix) {
2321 owner.require_mutator();
2322 for (
auto * g : {&owner.arena, &owner.nursery}) {
2323 if (g->used() != 1 || g->policy.capacity != g->policy.maximum || g->policy.shrink_shift ||
2324 !prefix || prefix % g->page_words() || prefix >= g->capacity() - g->reserved())
2325 detail::heap_failure(
"host requires fresh fixed generations and a whole-page guard");
2327 static_cast<void>(owner.arena.allocate(prefix - 1));
2328 static_cast<void>(owner.nursery.allocate(prefix - 1));
2332 if (state != phase::idle) detail::heap_failure(
"unfinished host collection");
2333 if (!pins_.empty()) detail::heap_failure(
"host destroyed with open pins");
2335 if (!publications.empty()) detail::heap_failure(
"host destroyed with published Windows views");
2340 if (state != phase::idle) detail::heap_failure(
"publish during host collection");
2341 auto const & g = which == generation::young ? owner.nursery : owner.arena;
2342 auto const & config = which == generation::young ? owner.policy.young : owner.policy.old;
2343 if (config.capacity != config.maximum || config.shrink_shift ||
2344 skip > g.capacity() || count > g.capacity() - skip)
2345 detail::heap_failure(
"published heap requires a fixed generation");
2347 auto const address =
reinterpret_cast<std::uintptr_t
>(target);
2348 auto const bytes = count * 8;
2349 if (
address % detail::heap_mapping::page_size() || skip % g.page_words() ||
2350 count % g.page_words() || bytes > std::numeric_limits<std::uintptr_t>::max() -
address)
2351 detail::heap_failure(
"invalid published heap window");
2353 for (
auto const * space : {&owner.arena, &owner.nursery}) {
2354 auto const begin =
reinterpret_cast<std::uintptr_t
>(space->storage.data());
2356 detail::heap_failure(
"published heap window overlaps a private view");
2359 for (
auto const & window : publications) {
2360 auto const begin =
reinterpret_cast<std::uintptr_t
>(window.target);
2362 if (window.target != target || window.bytes != bytes || window.source != &g.storage)
2363 detail::heap_failure(
"published heap windows must be disjoint or identical");
2364 detail::heap_mapping::unpublish(window.target, window.bytes);
2369 if (!found) publications.push_back({&g.storage,
static_cast<std::byte *
>(target), bytes});
2371 g.storage.publish(((g.origin + skip) % g.capacity()) * 8,
2372 static_cast<std::byte *
>(target), count * 8);
2376 if (state != phase::idle) detail::heap_failure(
"unpublish during host collection");
2377 if (count > std::numeric_limits<std::size_t>::max() / 8)
2378 detail::heap_failure(
"unpublished heap window is too large");
2380 auto const where = std::find_if(publications.begin(), publications.end(), [&](
auto const & window)
noexcept {
2381 return window.target == target && window.bytes == count * 8;
2383 if (where == publications.end()) detail::heap_failure(
"unpublish requires an exact published window");
2384 detail::heap_mapping::unpublish(where->target, where->bytes);
2385 publications.erase(where);
2387 detail::heap_mapping::unpublish(
static_cast<std::byte *
>(target), count * 8);
2393 owner.require_mutator();
2394 if (state != phase::idle || !at || !words) detail::heap_failure(
"invalid host pin");
2395 auto & g = owner.containing(at);
2396 auto const local = std::size_t(at & offset_mask);
2397 if (local < prefix || local >= g.cursor || words > g.cursor - local)
2398 detail::heap_failure(
"pin outside allocated storage");
2399 if (
auto found = pins_.find(at); found != pins_.end()) {
2400 if (found->second->words_ != words) detail::heap_failure(
"conflicting pin extents");
2401 ++found->second->references_;
2402 return found->second.get();
2404 auto const page = g.page_word_count;
2405 auto const first = local / page * page;
2406 auto const end = (local + words + page - 1) / page * page;
2407 auto alias = g.storage.alias_run(((g.origin + first) % g.capacity()) * 8, (end - first) * 8);
2408 auto value = std::unique_ptr<pin>(
new pin{at, words, local - first, std::move(alias)});
2409 auto * result = value.
get();
2410 pins_.emplace(at, std::move(value));
2415 owner.require_mutator();
2416 if (state != phase::idle || !value) detail::heap_failure(
"invalid host unpin");
2417 auto found = pins_.find(value->at_);
2418 if (found == pins_.end() || found->second.get() != value) detail::heap_failure(
"foreign host pin");
2419 if (!--value->references_) pins_.erase(found);
2422void heap::host::prepare_pin_marks() noexcept {
2424 for (
unsigned which = owner.minor_collection ? 1 : 0; which != 2; ++which) {
2425 auto & g = which ? owner.nursery : owner.arena;
2427 auto & live = traced[which];
2428 live.resize(blocks);
2429 for (std::size_t i = 0; i != blocks; ++i) {
2430 auto & item = g.metadata[i];
2431 live[i] = item.live;
2432 item.pointers &= heap::generation::pointer_cells(item.live);
2435 for (
auto const & [at, value] : pins_) {
2436 if (!owner.marked(at)) detail::heap_failure(
"open pin was not traced");
2437 auto const which = unsigned(at >> 31);
2438 if (!which && owner.minor_collection)
continue;
2439 auto & g = owner.containing(at);
2440 auto & runs = pin_runs[which];
2441 auto const first = std::size_t(at & offset_mask), end = first + value->words_;
2442 auto const page = g.page_word_count;
2443 if (!runs.empty() && first / page < (runs.back().end + page - 1) / page) {
2444 runs.back().end = std::max(runs.back().end, end);
2445 }
else runs.push_back({first, end, 0, 0});
2447 for (
unsigned which = 0; which != 2; ++which) {
2448 auto & g = which ? owner.nursery : owner.arena;
2449 auto & runs = pin_runs[which];
2451 std::vector<pin_run> joined;
2452 for (
auto run : runs) {
2455 while (run.block_begin && (g.flags(run.block_begin - 1) & 8u)) --run.block_begin;
2456 while (run.block_end *
block_words < g.cursor && (g.flags(run.block_end - 1) & 8u)) ++run.block_end;
2457 if (!joined.empty() && run.block_begin < joined.back().block_end) {
2458 joined.back().end = run.end;
2459 joined.back().block_end = std::max(joined.back().block_end, run.block_end);
2460 }
else joined.push_back(run);
2462 runs = std::move(joined);
2463 for (
auto const & run : runs) {
2464 for (
auto cell = run.block_begin *
block_words; cell < run.end; ++cell) {
2467 if (!(item.live & bit)) {
2468 auto const at =
static_cast<offset>(cell) | (which ? young_bit : 0);
2469 if (!source_gaps.empty() && source_gaps.back().at + source_gaps.back().words == at)
2470 ++source_gaps.back().words;
2471 else source_gaps.push_back({at, 1});
2479std::size_t heap::host::prepare_pinned(heap::generation & g,
unsigned which,
2480 std::size_t start,
bool keep_null)
noexcept {
2481 static_cast<void>(g.prepare(start, keep_null));
2483 auto const page = g.page_word_count;
2484 auto const & runs = pin_runs[which];
2485 std::size_t cursor = start, next_run = 0;
2487 std::size_t pinned_page_end = promoting && which ? (start + page - 1) / page * page : 0;
2488 for (std::size_t begin = 0; begin < blocks;) {
2489 auto end = begin + 1;
2490 while (end < blocks && (g.flags(end - 1) & 8u)) ++end;
2491 bool const constrained = next_run < runs.size() && runs[next_run].block_begin == begin;
2492 if (constrained) end = runs[next_run].block_end;
2494 for (
auto i = begin; i < end; ++i) k = std::max(k, static_cast<unsigned>(std::popcount(g.flags(i) & 7u)));
2495 auto const previous = cursor;
2496 cursor = (cursor + (std::size_t{1} << k) - 1) & ~((std::size_t{1} << k) - 1);
2498 auto const & run = runs[next_run++];
2499 cursor += (begin *
block_words - cursor) & (page - 1);
2500 auto mapped_page = (cursor + run.first - begin *
block_words) / page * page;
2501 if (mapped_page < pinned_page_end) cursor += pinned_page_end - mapped_page;
2502 pinned_page_end = (cursor + run.end - begin *
block_words + page - 1) / page * page;
2504 if (cursor > previous) pin_gaps.push_back({
static_cast<offset>(previous), cursor - previous});
2505 for (
auto i = begin; i != end; ++i) {
2506 g.metadata[i].destination =
static_cast<offset>(cursor);
2507 cursor += std::popcount(detail::retained(g.metadata[i], g.alignment_table(), i));
2514template<
bool tracks_starts>
2515void heap::host::finish_pinned() noexcept {
2516 std::array<std::optional<detail::heap_mapping>, 2> output;
2517 std::array<std::vector<block>, 2> metadata;
2518 std::array<std::vector<bitmap>, 2> next_starts;
2519 auto const minor = owner.minor_collection;
2520 for (
unsigned which = 0; which != 2; ++which) {
2521 auto & g = which ? owner.nursery : owner.arena;
2522 if (!which && minor && !promoting)
continue;
2523 if (!which && minor) {
2524 output[which].emplace(g.storage.alias_run(g.origin * 8, g.capacity() * 8));
2525 metadata[which] = g.metadata;
2527 output[which].emplace(g.capacity() * 8);
2528 metadata[which].resize(g.metadata.size());
2529 if constexpr (tracks_starts) next_starts[which].resize(g.metadata.size());
2532 std::vector<std::pair<offset, offset>> relocated;
2533 for (
auto const & [at, value] : pins_) relocated.emplace_back(at, owner.forward(at, minor));
2534 for (
auto const & hole : source_gaps) pin_gaps.push_back({owner.forward(hole.at, minor), hole.words});
2537 for (
unsigned which = minor ? 1 : 0; which != 2; ++which) {
2538 auto & g = which ? owner.nursery : owner.arena;
2539 unsigned const target_gen = which && !promoting ? 1 : 0;
2540 auto * target =
reinterpret_cast<word *
>(output[target_gen]->data());
2541 auto & target_meta = metadata[target_gen];
2542 auto pinned = pins_.lower_bound(which ? young_bit : 0);
2544 for (std::size_t i = 0; i < blocks; ++i) {
2545 auto const & item = g.metadata[i];
2546 auto retained = detail::retained(item, g.alignment_table(), i);
2547 auto destination = std::size_t(item.destination & offset_mask);
2549 auto const bit = std::countr_zero(retained);
2551 auto const at =
static_cast<offset>(cell) | (which ? young_bit : 0);
2552 while (pinned != pins_.end() && std::uint64_t(pinned->first) + pinned->second->words_ <= at) ++pinned;
2553 bool const fixed = pinned != pins_.end() && at >= pinned->first &&
2554 std::uint64_t(at) < std::uint64_t(pinned->first) + pinned->second->words_;
2555 auto const pointers = (item.pointers >> (bit * 2)) & 3u;
2556 if (!fixed) target[destination] = g.data()[cell];
2557 for (
unsigned slot = 0; slot != 2; ++slot)
if (pointers & (1u << slot)) {
2558 auto * from =
reinterpret_cast<std::byte *
>(g.data() + cell) + slot *
sizeof(
offset);
2560 std::memcpy(&before, from,
sizeof(before));
2561 auto const after = forward(before);
2562 auto * to = fixed ? from :
reinterpret_cast<std::byte *
>(target + destination) + slot *
sizeof(
offset);
2563 std::memcpy(to, &after,
sizeof(after));
2565 auto & packed = target_meta[destination /
block_words];
2566 if constexpr (tracks_starts)
2567 if (starts_[which][i] & (
bitmap{1} << bit))
2568 (minor && target_gen == 0 ? starts_[0] : next_starts[target_gen])[destination /
block_words]
2571 packed.pointers |=
word{pointers} << ((destination %
block_words) * 2);
2573 retained &= retained - 1;
2577 for (
auto const & hole : pin_gaps) {
2578 auto const which = unsigned(hole.at >> 31);
2579 auto * target =
reinterpret_cast<word *
>(output[which]->data());
2580 std::fill_n(target + (hole.at & offset_mask), hole.words,
word{0});
2582 for (
unsigned which = minor ? 1 : 0; which != 2; ++which) {
2583 auto & g = which ? owner.nursery : owner.arena;
2584 auto const page = g.page_word_count;
2585 unsigned const target_gen = which && !promoting ? 1 : 0;
2586 for (
auto const & run : pin_runs[which]) {
2587 auto const first = run.first / page * page, end = (run.end + page - 1) / page * page;
2588 auto const destination = (owner.forward(
static_cast<offset>(run.first) | (which ? young_bit : 0), minor) & offset_mask) / page * page;
2589 auto const * staged =
reinterpret_cast<word const *
>(output[target_gen]->data());
2590 auto pin_at = pins_.lower_bound(
static_cast<offset>(first) | (which ? young_bit : 0));
2591 for (
auto cell = first; cell != end; ++cell) {
2592 auto const at =
static_cast<offset>(cell) | (which ? young_bit : 0);
2593 while (pin_at != pins_.end() && std::uint64_t(pin_at->first) + pin_at->second->words_ <= at) ++pin_at;
2594 if (pin_at != pins_.end() && at >= pin_at->first && std::uint64_t(at) < std::uint64_t(pin_at->first) + pin_at->second->words_)
continue;
2595 g.data()[cell] = staged[destination + cell - first];
2597 output[target_gen]->replace_pages(g.storage, ((g.origin + first) % g.capacity()) * 8,
2598 destination * 8, (end - first) * 8);
2601 for (
unsigned which = 0; which != 2; ++which)
if (output[which]) {
2602 auto & g = which ? owner.nursery : owner.arena;
2603 g.storage.replace(*output[which]);
2604 g.metadata = std::move(metadata[which]);
2605 if constexpr (tracks_starts)
2606 if (!minor || which) starts_[which].swap(next_starts[which]);
2607 g.adopt(0, which ? young_words : old_words);
2609 std::map<offset, std::unique_ptr<pin>> next;
2610 for (
auto [before, after] : relocated) {
2611 auto node = pins_.extract(before);
2613 node.mapped()->at_ = after;
2614 next.insert(std::move(node));
2617 owner.collecting =
false;
2618 owner.minor_collection =
false;
2619 state = phase::idle;
2620 std::atomic_signal_fence(std::memory_order_seq_cst);
2624 owner.require_mutator();
2625 if (state != phase::idle) detail::heap_failure(
"host begin outside idle phase");
2626 if (owner.roots_head || !owner.weak_entries.empty() || !owner.pending_finalizers.empty() ||
2627 owner.running_finalizer || !owner.remembered.empty())
2628 detail::heap_failure(
"host collection requires exclusively VM-owned roots");
2629 std::atomic_signal_fence(std::memory_order_seq_cst);
2630 owner.collecting =
true;
2631 owner.minor_collection = minor;
2632 state = phase::marking;
2633 moving_pins = !pins_.empty() && (!minor || (pins_.rbegin()->first & young_bit));
2634 pin_gaps.clear(); source_gaps.clear();
2635 for (
auto & runs : pin_runs) runs.clear();
2636 for (
auto & live : traced) live.clear();
2639 for (std::size_t i = 0; i != g.metadata.size(); ++i) {
2640 g.metadata[i].pointers = 0;
2645 reset.template operator()<
true>(owner.nursery, 1);
2646 if (!minor) reset.template operator()<
true>(owner.arena, 0);
2648 reset.template operator()<
false>(owner.nursery, 1);
2649 if (!minor) reset.template operator()<
false>(owner.arena, 0);
2654 if (state != phase::marking && state != phase::retry)
2655 detail::heap_failure(
"host prepare outside marking phase");
2657 if (state == phase::marking && moving_pins) prepare_pin_marks();
2658 promoting = owner.minor_collection && promote;
2659 if (!owner.minor_collection) {
2660 if (prefix > 1) owner.arena.mark(1, prefix - 1);
2661 old_words = !moving_pins ? owner.arena.prepare(0,
true) : prepare_pinned(owner.arena, 0, 0,
true);
2662 }
else old_words = owner.arena.cursor;
2664 old_words = !moving_pins ? owner.nursery.prepare(owner.arena.cursor,
false) : prepare_pinned(owner.nursery, 1, owner.arena.cursor,
false);
2665 if (old_words > owner.arena.capacity() - owner.arena.reserved()) {
2666 state = phase::retry;
2669 young_words = prefix;
2671 if (prefix > 1) owner.nursery.mark(1, prefix - 1);
2672 young_words = (!moving_pins ? owner.nursery.prepare(young_bit,
true) : prepare_pinned(owner.nursery, 1, young_bit,
true)) - young_bit;
2674 state = phase::prepared;
2680 else finish_impl<false>();
2683template<
bool tracks_starts>
2684void heap::host::finish_impl() noexcept {
2685 if (state != phase::prepared)
2686 detail::heap_failure(
"host finish outside prepared phase");
2687 if (moving_pins) { finish_pinned<tracks_starts>();
return; }
2688 detail::forwarding_tables tables{owner.minor_collection ? nullptr : owner.arena.metadata.data(), owner.nursery.metadata.data(),
2689 owner.minor_collection ? nullptr : owner.arena.alignment_table(), owner.nursery.alignment_table()};
2690 auto const old_origin = owner.minor_collection ? owner.arena.origin : owner.arena.rotated_origin();
2691 auto const young_origin = owner.nursery.rotated_origin();
2692 if (!owner.minor_collection) {
2693 auto * target = owner.arena.at_origin(old_origin);
2694 owner.arena.move_to(target, tables);
2695 owner.arena.zero_gaps(target, 0);
2697 auto * target = promoting ? owner.arena.data() : owner.nursery.at_origin(young_origin);
2698 owner.nursery.move_to(target, tables);
2699 owner.nursery.zero_gaps(target, promoting ? owner.arena.cursor : 0);
2701 auto pack = [&](heap::generation & g,
unsigned source,
unsigned destination)
noexcept {
2702 auto & out = destination ? owner.nursery : owner.arena;
2703 if constexpr (tracks_starts)
2705 g.alignment_table(), starts_[source].data(), starts_[destination].data());
2706 else g.pack_into(out.metadata);
2708 if (!owner.minor_collection) pack(owner.arena, 0, 0);
2709 pack(owner.nursery, 1, promoting ? 0 : 1);
2710 if (!owner.minor_collection || promoting) owner.arena.adopt(old_origin, old_words);
2712 if constexpr (tracks_starts) owner.nursery.reset<
true>(starts_[1].data());
2713 else owner.nursery.reset<
false>();
2714 }
else owner.nursery.adopt(young_origin, young_words);
2715 owner.collecting =
false;
2716 owner.minor_collection =
false;
2717 state = phase::idle;
2718 if (promoting && young_words > 1)
2719 static_cast<void>(owner.nursery.allocate(young_words - 1));
2720 std::atomic_signal_fence(std::memory_order_seq_cst);
Stable alias registration; release through its owning host.
Borrow a heap whose roots, barriers and reference policy belong to a host runtime.
bool marked(offset at) const noexcept
Exact collected-generation liveness; old objects are live during minors.
void unpublish(void *target, std::size_t count) const noexcept
Remove a canonical alias without releasing the host's reservation.
offset allocate(generation which, std::size_t words) noexcept
Allocate raw cells in either generation, preserving the tag bit. A host may subdivide the extent; sca...
void unpin(pin *value) noexcept
Release one registration; the last release invalidates its native address.
generation
Select the arena for raw allocation or publication.
void trace(std::span< offset const > roots, Trace &&trace, std::size_t marker_limit=1, bool old_owners=false) noexcept
Drain roots using jam's ordinary frontier and persistent worker pool.
pin * pin_object(offset at, std::size_t words) noexcept
Alias a complete allocated object without collecting or changing its offset.
void record_start(offset at) noexcept
Declare an actual object start after claiming or formatting the object.
host(heap &storage, std::size_t prefix) noexcept
Reserve an identical guard in both fresh, fixed-size generations.
void visit_pins(F &&visit) const noexcept
Enumerate open pins as strong roots before reference policy is resolved.
~host() noexcept
Release the capability after finishing its last collection.
void begin(bool minor) noexcept
Start a VM-owned marking epoch; the VM supplies barriers and weak policy.
void finish() noexcept
Move survivors, preserving side aliases, then pack the consumed metadata.
bool prepare(bool promote=false) noexcept
Freeze both forwarding tables after resolving all reference policies.
std::span< bitmap const > starts(generation which) const noexcept
Borrow the bitmap in canonical cell coordinates, one word per mini-page.
bool tracks_starts() const noexcept
Whether the host opted into object-start transport.
void track_starts()
Lazily allocate VM-defined object-start bits; enable before a major epoch. Existing objects are disco...
std::span< gap const > gaps() const noexcept
Placement padding to format before a VM walks the collected object stream.
offset forward(offset at) const noexcept
Repair a root while original objects and both forwarding tables remain intact.
void publish(generation which, void *target, std::size_t skip, std::size_t count) const noexcept
Publish one generation at a stable VM-owned address.
A four-byte handle to a collector-updated external root slot.Copies share a registration; moves leave...
root_handle(root_handle &&other) noexcept
Transfer a registration, leaving the source detached.
offset get() const noexcept
Resolve the slot through the current heap; returned offsets expire on collection.
root_handle & operator=(root_handle &&other) noexcept
Transfer a registration in the same heap.
~root_handle() noexcept
Release this reference; the last release recycles the slot.
root_handle & operator=(root_handle const &other) noexcept
Replace this handle with a shared registration in the same heap.
root_handle() noexcept=default
Construct a detached null handle.
offset operator*() const noexcept
Read the current target offset.
void pointer(weak_ptr< T > const &value) noexcept
Declare a weak slot without following or claiming its target.
void pointer(ptr< T > const &value) noexcept
Declare one pointer slot without following or claiming its target.
void target(offset at) noexcept
Enqueue an external/wide target without declaring a narrow source slot.
offset field(offset cell, unsigned slot=0) noexcept
Declare a managed field and enqueue its nonnull target for tracing.
T const * claim_target(T const *value) noexcept
Claim the complete object from its allocation-level hook.
T const * claim_target(ptr< T > const &value) noexcept
Claim a typed target without declaring the pointer's source slot.
void operator()(Ts const &... values) noexcept
Visit parts in order, preserving their addresses; an empty pack does nothing.
void pointers(word bits, std::size_t first_slot=0) noexcept
OR a pattern of pointer slots into the current record's metadata. Bit zero names the 32-bit slot at f...
void operator()(T const &value) noexcept
Walk an embedded value without claiming cells again. Parts share the current allocation's pointer mas...
T const * claim(ptr< T > const &value) noexcept
Declare a pointer slot, then claim its target as with claim_target(). Null and already-claimed target...
void operator()(weak_ptr< T > const &value) noexcept
Declare a weak field for forwarding; never enqueue its target.
ptr< T > operator()(ptr< T > const &value) noexcept
Declare a typed field and enqueue its nonnull target.
void poll() noexcept
Service overdue stochastic donations without leaving this walker. Long cooperative hooks should poll ...
heap heap_type
Heap accepted by this visitor.
bool claim(offset start, std::size_t words=1, std::size_t alignment_bytes=8) noexcept
Claim a complete record; exactly one marker wins traversal.
Bind an existing heap, restoring the previous binding on scope exit.The heap must outlive the scope....
~heap_scope() noexcept
Restore the binding saved at construction.
heap_scope(heap &arena) noexcept
Use the supplied heap for implicit operations on this thread.
A circular, double-mapped arena with bounded parallel compaction.Compressed four-byte offsets address...
void remember(std::span< weak_ptr< T > > values) noexcept
Register weak slots after bulk unsafe assignment, dropping stale entries.
void remember(ptr< T > const &value) noexcept
Explicitly register a pointer after a caller-managed unsafe write.
generation const & young() const noexcept
Inspect generation one's arena.
char const * compactor_name() const noexcept
Runtime-selected compaction implementation, including SIMD ISA admission.
void remember(weak_ptr< T > const &value) noexcept
Register a weak old-to-young slot for rewriting, never for marking.
void remember(std::span< ptr< T > > values) noexcept
Register a contiguous group after bulk unsafe assignment.
jam::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
Associate a weak key and value with a managed finalizer and stateless runner.
std::size_t remembered_size() const noexcept
Number of distinct old source slots retained in the current GC epoch.
static heap * current() noexcept
Read the current thread's dynamically bound heap. Implicit operations require an active heap_scope....
jam::weak_ptr< T > weak_ptr
The weak field type for this heap.
generation const & old() const noexcept
Inspect generation zero's arena.
root_handle root(offset at) noexcept
Register a record or null in the current heap.
~heap() noexcept
Release the arena; every attached root must already be destroyed.
A typed heap offset, with no root registration or ownership.
constexpr ptr & unsafe_assign(offset bits) noexcept
Install encoded bits without registration; the caller owns the barrier.
void prefetch() const noexcept
Prefetch the cache line containing the target's first byte for reading. Null does nothing,...
constexpr void swap(ptr &other) noexcept
Exchange targets and update the remembered entries for both slots.
constexpr ~ptr() noexcept
Remove a dying old pointer slot from the remembered set.
T & operator*() const noexcept
Borrow the target in the current heap.
T element_type
Target record type.
constexpr offset local_offset() const noexcept
Cell index within the target generation.
constexpr ptr() noexcept=default
Construct a null reference.
friend constexpr void swap(ptr &left, ptr &right) noexcept
Exchange pointers through ADL, preserving each slot's write barrier.
constexpr offset get() const noexcept
Read the cell offset; unregistered copies expire on collection.
std::uint32_t offset
Eight-byte cell index, stored in exactly four bytes.
void prefetch_marks() const noexcept
Prefetch the target's mark metadata without claiming or reading it. Null does nothing,...
T * operator->() const noexcept
Borrow the target in the current heap; expires on growth or collection.
constexpr bool is_young() const noexcept
Whether the target belongs to generation one.
constexpr ptr & unsafe_assign(ptr const &other) noexcept
Assign without a write barrier; the caller must register old-to-young edges.
A copyable external root, updated during collection.Four-byte slot index; no heap or registration add...
root() noexcept=default
Construct a detached null root.
root(ptr< U > const &value) noexcept
Register a derived pointer through its base allocation-tracing hook.
ptr< T > get() const noexcept
Obtain a pointer valid until the next collection.
T * operator->() const noexcept
Borrow the target in its current heap scope.
T & operator*() const noexcept
Borrow the target in its current heap scope.
constexpr rep count() const noexcept
Retrieve the count in this unit explicitly.
A four-byte weak heap field; tracing updates its slot without retaining its target....
constexpr bool expired() const noexcept
Whether null or cleared by collection; this does not initiate collection.
T element_type
Target record type; may be incomplete.
constexpr bool is_young() const noexcept
Whether this field targets the young generation.
constexpr void swap(weak_ptr &other) noexcept
Exchange targets while preserving each slot's weak barrier.
constexpr ~weak_ptr() noexcept
Remove a dying old slot from the remembered set.
constexpr weak_ptr & unsafe_assign(weak_ptr const &other) noexcept
Assign without registration; the caller must remember old-to-young slots.
root< T > lock() const noexcept
Retain the current target as a strong root, or return an empty root.
constexpr weak_ptr & unsafe_assign(offset bits) noexcept
Assign encoded bits without registration; the caller owns the barrier.
std::uint32_t offset
The same generation-tagged cell index used by ptr.
constexpr weak_ptr() noexcept=default
Construct an empty weak pointer.
constexpr offset get() const noexcept
Read the encoded offset; an external copy expires on collection.
constexpr weak_ptr(ptr< U > const &value) noexcept
Observe a derived pointer through its supported base conversion.
An external weak slot, updated or cleared by collection without retaining its target....
void swap(weak_root &other) noexcept
Exchange slots in the same heap without allocating or retaining either target.
root< T > lock() const noexcept
Retain the current target, or return an empty root if expired. Requires the owning heap current; the ...
void reset() noexcept
Unregister the slot and leave it detached and expired.
weak_root(root< T > const &value) noexcept
Observe a strong root in the current heap without retaining its target.
weak_root() noexcept=default
Construct a detached, expired weak root.
friend void swap(weak_root &a, weak_root &b) noexcept
Exchange two external weak roots.
bool expired() const noexcept
Whether this slot is null; does not initiate collection.
weak_root(ptr< U > const &value) noexcept
Register a derived pointer through its base allocation-tracing hook.
An external conditional weak-value handle; registration survives dropped handles.Keep this handle out...
bool expired() const noexcept
Whether the registration has retired or its heap has been destroyed.
root< V > lock() const noexcept
Retain the current value, or return an empty root for a retired registration. Locking and using the r...
void finalize() const noexcept
Retire the registration and run its callback now, at most once. Requires the owning heap current....
T supports tracing after it is complete.Ordinary trace return types are unrestricted; collection uses...
A visitor for jam's compressed, alignment-preserving heap.
void collect_major() noexcept
Trace both generations and compact survivors into old; reset the countdown.
jam::ptr< T > ptr
The compact pointer type for this heap.
jam::weak_root< T > weak_root(ptr< T > const &at) noexcept
Register an external weak slot, forwarded but never used to seed marking.
void collect_minor(bool promote=false) noexcept
Collect young without walking old; optionally promote survivors; preserve the countdown.
offset field(offset cell, unsigned slot=0) const noexcept
Read one managed reference field by value.
std::size_t page_words() const noexcept
Cells per native OS page (distinct from a rank block).
T load(ptr< T > const &at) const noexcept
Read a typed value snapshot; any pointers in it expire on collection.
std::size_t capacity() const noexcept
Capacity in eight-byte cells, including the reserve.
T * address(ptr< T > const &at) noexcept
Borrow a typed target; the address expires on growth or collection.
detail::heap_block block
Live bitmap, forwarding base and pointer-field bitmap.
ptr< T > mk(Args &&... args) noexcept
Allocate and copy a typed record, returning an unrooted reference.Construct T from the arguments....
offset bitmap
Allocation/live bitmap.
static constexpr std::size_t block_words
Eight-byte cells in one rank block: 32.
auto data(this Self &self) noexcept -> std::conditional_t< std::is_const_v< Self >, word const, word > *
Borrow cells; dereference an offset as data()[offset].
consteval auto make_manifest(Members T::*... members)
Describe the pointer-bearing members of T, including embedded values.T may still be incomplete....
offset allocate(std::size_t words, std::size_t alignment_bytes=8) noexcept
Allocate a positive complete record measured in eight-byte cells.
bool claim(offset first, std::size_t words=1, std::size_t alignment_bytes=8) noexcept
Claim traversal by atomically marking the record's first cell.
std::size_t reserved() const noexcept
Reserved cells for bounded parallel compaction.
static constexpr unsigned fields_per_word
Four-byte pointer fields per allocation cell.
void clear_marks() noexcept
Clear marks and alignment requirements; pointer declarations survive.
void pointer(offset where, unsigned slot=0) noexcept
Declare a managed reference field; may overlap frozen-heap markers.
std::span< block const > blocks() const noexcept
Borrow rank descriptors while markers and compaction are quiescent.
void collect_minor(Trace &&trace, bool promote=false) noexcept
Minor collection with a fallback tracer and optional promotion; preserve the countdown....
static constexpr offset null
Unreachable reference preserved by forwarding.
void collect_major(Trace &&trace) noexcept
Major collection with a fallback tracer; reset the countdown.Uses the same tracing and synchronizatio...
void mark(offset first, std::size_t words=1, std::size_t alignment_bytes=8) noexcept
Mark a complete reachable record; concurrent markers are permitted.
std::uint32_t offset
Stored reference, in eight-byte units from data().
void store(ptr< T > const &at, T const &value) noexcept
Replace a typed record's bytes without changing its extent or type.
void set_field(offset cell, offset value, unsigned slot=0) noexcept
Write a managed reference field's bytes.
void collect() noexcept
Collect typed roots, running minor_collections minors between majors. The first minor_collections cal...
heap_options configuration() const noexcept
Read the immutable construction policy with typed page counts.
std::size_t start() const noexcept
Current view's offset in the circular backing, in cells.
std::size_t used() const noexcept
Allocated prefix in eight-byte cells, including the reserved null cell.
jam::root< T > root(ptr< T > const &at) noexcept
Register a typed external root with its type-specific tracing callback.
std::uint64_t word
Eight-byte allocation cell.
void compact(std::span< offset > roots={}) noexcept
Pack marked records, forwarding managed fields and external roots.
void collect_major() noexcept
Collect both generations of the current heap; reset its collection countdown.
void collect() noexcept
Collect the current heap; only registered roots remain meaningful outside it.
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...
void collect_minor(bool promote=false) noexcept
Collect the current heap's young generation; optionally promote survivors.
space< std::size_t, mebi > mebibytes
Groups of 1048576 bytes.
constexpr To ceil(space< R, P > value) noexcept
Least whole destination count not less than the source.
constexpr To floor(space< R, P > value) noexcept
Greatest whole destination count not exceeding the source.
space< std::size_t, std::ratio< JAM_PAGE_BYTES > > pages
Platform pages, fixed at build time.
space< std::size_t, gibi > gibibytes
Groups of 1073741824 bytes.
space< std::size_t, kibi > kibibytes
Groups of 1024 bytes.
space< std::size_t > bytes
Whole bytes.
Sizing policy for one generation. Sizes are platform pages.
units::pages maximum
Maximum ring size; reserves virtual addresses without committing backing.
units::pages reserve
Gap reserved for bounded parallel compaction.
units::pages capacity
Initial ring capacity, including the compaction reserve.
unsigned shrink_shift
Shrink below 1/2^shift occupancy; zero disables shrinking.
One generation's ordered double-mapped arena and collection metadata.
Padding retained by pin placement, in destination cells.
Two generations and one shared worker pool.
generation_options young
Generation one; ordinary typed allocation starts here.
std::size_t workers
Workers shared by marking and compaction, including the caller.
generation_options old
Generation zero, below young in virtual memory.
std::size_t minor_collections
Minor collections between scheduled majors; zero means always major.
Opt out of value traversal when specializing tracer<T>.Derive tracer<T>, not the heap object,...
static constexpr void trace(Visitor &, T const &) noexcept
Visit no fields; allocation tracing still marks the complete extent.
static constexpr auto trace(Visitor &visit, T const &value) noexcept(noexcept(value.trace(visit))) -> decltype(value.trace(visit))
Enumerate a value's parts and forward its member hook's result.
static constexpr decltype(auto) trace(Visitor &visit, ptr< T > const &at) noexcept
Forward an unclaimed target to its cooperative static hook.
static void trace(Visitor &visit, ptr< T > const &at) noexcept accepts< Visitor >
Claim records in a loop, queueing branches and walking one same-type edge locally.
static constexpr void trace(Visitor &visit, ptr< T > const &value) noexcept(noexcept(visit(value)))
Preserve the field address and dispatch to the matching heap visitor.
static constexpr void trace(Visitor &visit, std::array< T, N > const &value)
Visit elements in order, preserving their original addresses.
static constexpr void trace(Visitor &visit, std::tuple< Ts... > const &value)
Tuple views retain their elements' original addresses.
static constexpr void trace(Visitor &visit, std::variant< Ts... > const &value)
A valueless variant has no outgoing managed fields.
static constexpr void trace(Visitor &visit, weak_ptr< T > const &value) noexcept(noexcept(visit(value)))
Preserve the original field address for pointer-mask declaration.
Per-value tracing customization; values without a hook or manifest are leaves.An allocation-only clai...