xo-gc: refactor to extract DX1Collector etc from xo-alloc2/

This commit is contained in:
Roland Conybeare 2025-12-22 22:24:10 -05:00
commit 093cf3c969
56 changed files with 452 additions and 83 deletions

View file

@ -5,7 +5,7 @@
#pragma once
#include "alloc/AllocInfo.hpp"
#include "AllocInfo.hpp"
#include "cmpresult.hpp"
#include <xo/facet/obj.hpp>

View file

@ -1,77 +0,0 @@
/** @file AllocError.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include <cstdint>
#include <cstddef>
namespace xo {
namespace mm {
enum class error : int32_t {
/** sentinel **/
invalid = -1,
/** not an error **/
none,
/** reserved size exhauged **/
reserve_exhausted,
/** unable to commit (i.e. mprotect failure) **/
commit_failed,
/** allocation size too big (See @ref ArenaConfig::header_size_mask_) **/
header_size_mask,
/** sub_alloc not preceded by super alloc (or another sub_alloc) **/
orphan_sub_alloc,
/** attempt to call alloc_info for allocator with alloc header feature disabled
* (e.g. @ref see ArenaConfig::store_header_flag_)
**/
alloc_info_disabled,
/** attempt to call alloc_info for address not owned by allocator **/
alloc_info_address,
/** for example: alloc iteration not supported in arenas with
* AllocConfig.store_header_flag_ = false
**/
alloc_iterator_not_supported,
/** attempt to deref an iterator that does not refer to an alloc **/
alloc_iterator_deref,
/** attempt to advance an iterator that does not refer to an alloc **/
alloc_iterator_next,
};
struct AllocError {
using size_type = std::size_t;
using value_type = std::byte*;
AllocError() = default;
explicit AllocError(error err,
uint32_t seq) : error_{err},
error_seq_{seq} {}
AllocError(error err,
uint32_t seq,
size_type req_z,
size_type com_z,
size_type rsv_z) : error_{err},
error_seq_{seq},
request_z_{req_z},
committed_z_{com_z},
reserved_z_{rsv_z} {}
/** error code **/
error error_ = error::none;
/** sequence# of this error.
* Each error event within an allocator gets next sequence number
**/
uint32_t error_seq_ = 0;
/** reqeust size assoc'd with errror **/
size_type request_z_ = 0;
/** committed allocator memory at time of error **/
size_type committed_z_ = 0;
/** reserved allocator memory at time of error **/
size_type reserved_z_ = 0;
};
} /*namespace mm*/
} /*namespace xo*/
/* end AllocError.hpp */

View file

@ -1,28 +0,0 @@
/** @file AllocHeader.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include <type_traits>
#include <cstdint>
#include <cstddef>
namespace xo {
namespace mm {
struct AllocHeader {
using repr_type = std::uintptr_t;
using size_type = std::size_t;
explicit AllocHeader(repr_type x) : repr_{x} {}
repr_type repr_;
};
static_assert(sizeof(AllocHeader) == sizeof(AllocHeader::repr_type));
static_assert(std::is_standard_layout_v<AllocHeader>);
}
}
/* end AllocHeader.hpp */

View file

@ -1,144 +0,0 @@
/** @file AllocHeaderConfig.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "AllocHeader.hpp"
#include <utility>
namespace xo {
namespace mm {
/*
* Each allocation is preceded by a 64-bit header.
* Header is split into 3 configurable-width bit fields,
* labelled (from hi to lo bit order) {tseq, age, size}.
*
* 1. tseq. seq# identifying object types; needed for gc.
* 2. gen. age cohort; increases when alloc survives gc.
* 3. size. alloc size.
*
* Arena allocator only uses size.
* X1 collector uses {tseq, gen, size}
*
* alloc header
*
* TTTTTTTTTTTTGGGGGZZZZZZZZZZZZ
* < tseq ><gen>< size >
*
* masking
*
* ..432107654321076543210 bit
*
* > < .gen_bits
* 0..............01111111 gen_mask_unshifted
* 0..011111110..........0 gen_mask_shifted
* > < gen_shift
*/
struct AllocHeaderConfig {
using repr_type = AllocHeader;
using span_type = std::pair<const std::byte *, const std::byte *>;
AllocHeaderConfig() = default;
AllocHeaderConfig(std::uint32_t gz,
std::uint8_t guard_byte,
std::uint8_t t,
std::uint8_t a,
std::uint8_t z) noexcept : guard_z_{gz},
guard_byte_{guard_byte},
tseq_bits_{t},
age_bits_{a},
size_bits_{z} {}
std::uint64_t tseq_mask() const noexcept {
// e.g.
// FF FF FF 00 00 00 00 00
// with tseq_bits=24, age_bits=8, size_bits=32
//
return ((1ul << tseq_bits_) - 1) << (age_bits_ + size_bits_);
}
std::uint64_t age_mask() const noexcept {
// e.g.
// 00 00 00 FF 00 00 00 00
// with age_bits=8, size_bits=32
//
return ((1ul << age_bits_) - 1) << size_bits_;
}
std::uint64_t size_mask() const noexcept {
// e.g.
// 00 00 00 00 FF FF FF FF
// with size_bits=32
//
return ((1ul << size_bits_) - 1);
}
/** extract type id from alloc header @p hdr **/
std::uint32_t tseq(repr_type hdr) const noexcept {
// e.g.
// 0x302010
// for header
// 30 20 10 -- -- -- -- --
// with tseq_bits_ = 24, age_bits_ + size_bits_ = 40
//
return (hdr.repr_ & tseq_mask()) >> (age_bits_ + size_bits_);
}
/** extract age from alloc header @p hdr **/
std::uint32_t age(repr_type hdr) const noexcept {
// e.g.
// 0xa0
// for header
// -- -- -- a0 -- -- -- --
// with age_bits_ = 8, size_bits_ = 32
//
return (hdr.repr_ & age_mask()) >> size_bits_;
}
/** extract size from alloc header @p hdr **/
std::size_t size(repr_type hdr) const noexcept {
// e.g.
// 0x01020300
// for header
// -- -- -- -- 01 02 03 00
// with size_bits_ = 32
//
return (hdr.repr_ & size_mask());
}
/** true iff sentinel tseq, flagging a forwarding pointer **/
bool is_forwarding_tseq(repr_type hdr) const noexcept {
// e.g.
// 0xFFFFFF
// i.e. header
// FF FF FF -- -- -- -- --
// with tseq_bits_ = 24, age_bits + size_bits_ = 40
//
return (hdr.repr_ & tseq_mask()) == tseq_mask();
}
bool is_size_enabled() const noexcept { return size_bits_ > 0; }
/** if non-zero, allocate extra space between allocs, and fill
* with fixed test-pattern contents. Allows for simple
* runtime arena sanitizing checks.
* Will be rounded up to multiple of @ref padding::c_alloc_alignment
**/
std::uint32_t guard_z_ = 0;
/** if guard_z_ > 0, write at least that many copies
* of this guard byte following each complete allocation
**/
std::uint8_t guard_byte_ = 0xfd;
/** number of bits for tseq **/
std::uint8_t tseq_bits_ = 24;
/** number of bits for age **/
std::uint8_t age_bits_ = 8;
/** number of bits for size **/
std::uint8_t size_bits_ = 32;
};
} /*namespace mm*/
} /*namespace xo*/
/* end AllocHeaderConfig.hpp */

View file

@ -1,70 +0,0 @@
/** @file AllocInfo.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "AllocHeaderConfig.hpp"
#include <utility>
namespace xo {
namespace mm {
/** @class AllocInfo
* @brief bookkeeping information for an allocation
*
* AllocInfo instances are 1:1 with sum of calls to
* {@ref AAllocator::alloc, @ref AAllocator::alloc_super}
*
**/
struct AllocInfo {
using size_type = AllocHeader::size_type;
using byte = std::byte;
using span_type = std::pair<const byte *, const byte *>;
AllocInfo(const AllocHeaderConfig * p_cfg,
const byte * p_guard_lo,
const AllocHeader * p_hdr,
const byte * p_guard_hi) : p_config_{p_cfg},
p_guard_lo_{p_guard_lo},
p_header_{p_hdr},
p_guard_hi_{p_guard_hi} {}
/** error when alloc-header not configured **/
static AllocInfo error_not_configured(const AllocHeaderConfig * p_cfg) {
return AllocInfo(p_cfg, nullptr, nullptr, nullptr);
}
/** error on deref empty iterator **/
static AllocInfo error_invalid_iterator(const AllocHeaderConfig * p_cfg) {
return AllocInfo(p_cfg, nullptr, nullptr, nullptr);
}
/** true for non-sentinel AllocInfo instance **/
bool is_valid() const { return (p_config_ != nullptr) && (p_header_ != nullptr); }
/** Guard bytes preceding allocation-header **/
span_type guard_lo() const noexcept;
/** Type sequence number in garbage collector **/
std::uint32_t tseq() const noexcept { return p_config_->tseq(*p_header_); }
/** Allocation age in garbage collector **/
std::uint32_t age() const noexcept { return p_config_->age (*p_header_); }
/** Allocation size (including allocator-supplied padding, excluding alloc header) **/
size_type size() const noexcept { return p_config_->size(*p_header_); }
/** Payload for this allocation. This is the memory available to application **/
span_type payload() const noexcept;
/** Guard bytes immediately following allocation **/
span_type guard_hi() const noexcept;
/** Number of guard bytes **/
size_type guard_z() const noexcept { return p_config_->guard_z_; }
/** Value (fixed test pattern) of guard byte **/
char guard_byte() const noexcept { return p_config_->guard_byte_; }
const AllocHeaderConfig * p_config_ = nullptr;
const byte * p_guard_lo_ = nullptr;
const AllocHeader * p_header_ = nullptr;
const byte * p_guard_hi_ = nullptr;
};
} /*namespace mm*/
} /*namespace xo*/
/* end AllocInfo.hpp */

View file

@ -1,34 +0,0 @@
/** @file AllocRange.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "AllocIterator.hpp"
namespace xo {
namespace mm {
/** @class AllocRange
* @brief Provide range iteration over an @ref AAllcator
*
* Return value type for @ref AAllocator::alloc_range
**/
struct AllocRange {
public:
using repr_type = std::pair<obj<AAllocIterator>, obj<AAllocIterator>>;
public:
AllocRange() = default;
explicit AllocRange(repr_type range) : range_{std::move(range)} {}
obj<AAllocIterator> begin() const { return range_.first; }
obj<AAllocIterator> end() const { return range_.second; }
/** state: {begin,end} pair of alloc iterators **/
repr_type range_;
};
} /*namsepace mm*/
} /*namespace xo*/
/* end AllocRange.hpp */

View file

@ -1,4 +0,0 @@
AAllocator |<-- IAllocator_Any (D=DVariantPlaceholder)
|<-- IAllocator_Xfer<D,..>
OObject<AAllocator,D> |<-- RAllocator<O>