xo-alloc2: streamlining + bugfixes [wip]

This commit is contained in:
Roland Conybeare 2025-12-12 19:44:38 -05:00
commit 6cf535eaef
11 changed files with 312 additions and 10 deletions

View file

@ -23,6 +23,10 @@ namespace xo {
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,
};
struct AllocatorError {
@ -67,6 +71,10 @@ namespace xo {
///@{
/** @brief type used for allocation amounts **/
using size_type = std::size_t;
/** @brief type used for allocation responses **/
using value_type = std::byte *;
/** object header, if configured **/
using header_type = std::uint64_t;
///@}
/*
@ -121,7 +129,19 @@ namespace xo {
**/
virtual bool expand(Opaque d, std::size_t z) const noexcept = 0;
/** allocate @p z bytes of memory from allocator @p d. **/
virtual std::byte * alloc(Opaque d, std::size_t z) const = 0;
virtual value_type alloc(Opaque d, size_type z) const = 0;
/** like @ref alloc, but follow with one or more consecutive
* @ref sub_alloc() calls. This sequence of allocs will share
* the initial allocation header.
**/
virtual value_type super_alloc(Opaque d, size_type z) const = 0;
/** follow a preceding @ref super_alloc call with additional
* subsidiary allocs that share the same object header.
* Must finish sequence with exactly one sub_alloc call
* with @p complete_flag set. This sub_alloc call may have
* zero @p z
**/
virtual value_type sub_alloc(Opaque d, size_type z, bool complete_flag) const = 0;
/** reset allocator @p d to empty state **/
virtual void clear(Opaque d) const = 0;
/** destruct allocator @p d **/

View file

@ -26,6 +26,22 @@ namespace xo {
* (provided you use their full extent :)
**/
std::size_t hugepage_z_ = 2 * 1024 * 1024;
/** true to store header (8 bytes) at the beginning of each allocation.
* necessary and sufficient to allows iterating over allocs
* present in arena
**/
bool store_header_flag_ = false;
/** 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::size_t guard_z_ = 0;
/** if store_header_flag_ is true: mask bits for allocation size.
* remaining bits can be stolen for other purposes
* otherwise ignored
**/
std::uint64_t header_size_mask_ = 0xffffffff;
/** true to enable debug logging **/
bool debug_flag_ = false;
@ -59,6 +75,8 @@ namespace xo {
using size_type = std::size_t;
/** @brief a contiguous memory range **/
using range_type = std::pair<std::byte*,std::byte*>;
/** @brief type for allocation header (if enabled) **/
using header_type = std::uint64_t;
///@}
@ -103,6 +121,11 @@ namespace xo {
**/
size_type committed_z_ = 0;
/** if config_.store_header_flag_:
* Pointer to header for last allocation.
**/
header_type * last_header_ = nullptr;
/** free pointer.
* Memory in range [@ref lo_, @ref free_) current in use
**/

View file

@ -41,7 +41,9 @@ namespace xo {
[[noreturn]] AllocatorError last_error(Copaque) const noexcept override { _fatal(); }
[[noreturn]] bool expand(Opaque, std::size_t) const noexcept override { _fatal(); }
[[noreturn]] std::byte * alloc(Opaque, std::size_t) const override { _fatal(); }
[[noreturn]] value_type alloc(Opaque, std::size_t) const override { _fatal(); }
[[noreturn]] value_type super_alloc(Opaque, std::size_t) const override { _fatal(); }
[[noreturn]] value_type sub_alloc(Opaque, std::size_t, bool) const override { _fatal(); }
[[noreturn]] void clear(Opaque) const override { _fatal(); }
[[noreturn]] void destruct_data(Opaque) const override { _fatal(); }

View file

@ -29,6 +29,14 @@ namespace xo {
*/
struct IAllocator_DArena {
using size_type = std::size_t;
using value_type = std::byte *;
enum class alloc_mode : uint8_t {
standard,
super,
sub,
sub_complete,
};
static std::string_view name(const DArena &) noexcept;
static size_type reserved(const DArena &) noexcept;
@ -43,11 +51,34 @@ namespace xo {
* to size at least @p z
* In practice will round up to a multiple of @ref page_z_.
**/
static bool expand(DArena & d, std::size_t z) noexcept;
static bool expand(DArena & d, size_type z) noexcept;
static std::byte * alloc(DArena &, std::size_t z);
static value_type alloc(DArena &, size_type z);
/** when store_header_flag enabled:
* like alloc(), but combine memory consumed by this alloc
* plus following consecutive sub_alloc()'s into a single header.
* otherwise equivalent to alloc()
**/
static value_type super_alloc(DArena &, size_type z);
/** when store_header_flag enabled:
* follow preceding super_alloc() by one or more sub_allocs().
* accumulate total allocated size (including padding) into
* single header. All sub_allocs() except the last must set
* @p complete_flag to false. The last sub_alloc() must set
* @p complete_flag to true.
**/
static value_type sub_alloc(DArena &, size_type z, bool complete_flag);
static void clear(DArena &);
static void destruct_data(DArena &);
private:
/** alloc driver. shared by alloc(), super_alloc(), sub_alloc() **/
static value_type _alloc(DArena &,
size_type z,
alloc_mode mode,
bool store_header_flag,
bool remember_header_flag);
};
// template <>

View file

@ -18,7 +18,8 @@ namespace xo {
struct IAllocator_Xfer : public AAllocator {
// parallel interface to AAllocator, with specific data type
using Impl = IAllocator_DRepr;
using size_type = std::size_t;
using size_type = AAllocator::size_type;
using value_type = AAllocator::value_type;
static const DRepr & _dcast(Copaque d) { return *(const DRepr *)d; }
static DRepr & _dcast(Opaque d) { return *(DRepr *)d; }
@ -43,8 +44,15 @@ namespace xo {
bool expand(Opaque d,
std::size_t z) const noexcept override { return I::expand(_dcast(d), z); }
std::byte * alloc(Opaque d,
value_type alloc(Opaque d,
std::size_t z) const override { return I::alloc(_dcast(d), z); }
value_type super_alloc(Opaque d,
std::size_t z) const override { return I::super_alloc(_dcast(d), z); }
value_type sub_alloc(Opaque d,
std::size_t z,
bool complete_flag) const override {
return I::sub_alloc(_dcast(d), z, complete_flag);
}
void clear(Opaque d) const override { return I::clear(_dcast(d)); }
void destruct_data(Opaque d) const override { return I::destruct_data(_dcast(d)); }

View file

@ -18,6 +18,7 @@ namespace xo {
public:
using ObjectType = Object;
using size_type = std::size_t;
using value_type = std::byte *;
RAllocator() {}
RAllocator(Object::DataPtr data) : Object{std::move(data)} {}
@ -33,7 +34,11 @@ namespace xo {
AllocatorError last_error() const { return O::iface()->last_error(O::data()); }
bool expand(size_type z) { return O::iface()->expand(O::data(), z); }
std::byte * alloc(size_type z) { return O::iface()->alloc(O::data(), z); }
value_type alloc(size_type z) { return O::iface()->alloc(O::data(), z); }
value_type super_alloc(size_type z) { return O::iface()->super_alloc(O::data(), z); }
value_type sub_alloc(size_type z,
bool complete_flag) { return O::iface()->sub_alloc(O::data(),
z, complete_flag); }
static bool _valid;
};

View file

@ -50,7 +50,7 @@ namespace xo {
**/
static inline
std::size_t with_padding(std::size_t z,
std::size_t align)
std::size_t align = c_alloc_alignment)
{
return z + alloc_padding(z, align);
}