xo-expression2 stack: + dp<> template + robustify DGlobalSymtab

This commit is contained in:
Roland Conybeare 2026-02-15 14:12:31 -05:00
commit 6209c812d3
3 changed files with 142 additions and 26 deletions

View file

@ -0,0 +1,110 @@
/** @file dp.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include "Allocator.hpp"
#include <xo/facet/obj.hpp>
#include <cassert>
namespace xo {
namespace mm {
/** unimorphic "data pointer" with known representation and owned memory.
* runs dtor *but not delete*. Does not store allocator!
*
* Compare with abox<AFacet,DRepr>
**/
template <typename DRepr>
struct dp {
dp() = default;
/** dp takes ownership of data @p ptr;
* will run dtor when dp goes out of scope.
*
* Note this is not useful when DRepr=DVariablePlaceholder
**/
explicit dp(DRepr * ptr) : ptr_{ptr} {}
/** (copy ctor not supported -- ownership is unique) **/
dp(const dp & other) = delete;
/** Move constructor **/
dp(dp<DRepr> && other)
{
ptr_ = other.ptr_;
other.ptr_ = nullptr;
}
/** allocates for sizeof(DRepr), so DRepr must not use flexible array **/
template <typename... Args>
static dp make(obj<AAllocator> alloc, Args&&... args) {
void * mem = alloc.alloc_for<DRepr>();
if (mem) {
DRepr * data = ::new (mem) DRepr(std::forward<Args>(args)...);
assert(data);
return dp(data);
} else {
assert(false);
return dp();
}
}
dp<DRepr> & operator=(const dp<DRepr> & x) = delete;
/** move assignment **/
dp<DRepr> & operator=(dp<DRepr> && x) {
ptr_ = x.ptr_;
x.ptr_ = nullptr;
}
// --------------------------------
DRepr * data() const noexcept { return ptr_; }
operator bool() const noexcept { return ptr_ != nullptr; }
DRepr * operator->() const noexcept { return ptr_; }
DRepr & operator*() const noexcept { return *ptr_; }
#ifdef NOT_YET
/** explicit conversion to obj<AFacet,DRepr> **/
obj<AFacet, DRepr> to_op() const noexcept {
return obj<AFacet, DRepr>(this->iface(), this->data());
}
#endif
#ifdef NOT_YET
/** Take ownership from unowned object **/
template <typename AFacet>
dp & adopt(const obj<AFacet, DRepr> & other)
requires (std::is_same_v<DRepr, DVariantPlaceholder>
|| std::is_same_v<DRepr, DOther>)
{
/* replace .iface_ along w/ .data_ */
this->from_obj(other);
return *this;
}
#endif
~dp() {
if (ptr_) {
ptr_->~DRepr();
}
}
private:
DRepr * ptr_ = nullptr;
};
} /*namespace mm*/
using mm::dp;
} /*namespace xo*/
/* end dp.hpp */

View file

@ -8,6 +8,7 @@
#include "Binding.hpp"
#include "DVariable.hpp"
#include <xo/object2/DArray.hpp>
#include <xo/alloc2/dp.hpp>
#include <xo/arena/DArenaHashMap.hpp>
namespace xo {
@ -35,16 +36,16 @@ namespace xo {
/** @defgroup scm-globalsymtab-ctors constructors **/
///@{
DGlobalSymtab(repr_type * map, DArray * vars);
DGlobalSymtab(dp<repr_type> map, DArray * vars);
/** create instance.
* Use memory from @p fixed_mm for @ref map_.
* Use memory from @p mm for DGlobalSymtab instance.
* Hashmap configured per @p cfg.
**/
DGlobalSymtab * make(obj<AAllocator> fixed_mm,
obj<AAllocator> mm,
const ArenaHashMapConfig & cfg);
dp<DGlobalSymtab> make(obj<AAllocator> fixed_mm,
obj<AAllocator> mm,
const ArenaHashMapConfig & cfg);
///@}
/** @defgroup scm-globalsymtab-access-methods access methods **/
@ -81,21 +82,21 @@ namespace xo {
///@}
/** @defgroup scm-globalsymtab-gcobject-facet gcobject facet **/
///@{
std::size_t shallow_size() const noexcept;
DGlobalSymtab * shallow_copy(obj<AAllocator> mm) const noexcept;
std::size_t forward_children(obj<ACollector> gc) noexcept;
///@}
private:
/** map symbols -> bindings.
* Minor point: storing offsets instead of Variables allows us to omit
* iterating over map elements during GC. Possible savings if map_ slots
* sparsely populated.
**/
repr_type * map_ = nullptr;
dp<repr_type> map_;
/** array of variables.
* When S is a unique-string for a global symbol, then:
* 1. map_[S] is unique global index i(S) for S.

View file

@ -18,35 +18,25 @@ namespace xo {
namespace scm {
DGlobalSymtab::DGlobalSymtab(repr_type * map,
DGlobalSymtab::DGlobalSymtab(dp<repr_type> map,
DArray * vars)
: map_{map}, vars_{vars}
: map_{std::move(map)}, vars_{vars}
{
}
DGlobalSymtab *
DGlobalSymtab::make(obj<AAllocator> global_mm,
dp<DGlobalSymtab>
DGlobalSymtab::make(obj<AAllocator> aux_mm,
obj<AAllocator> mm,
const ArenaHashMapConfig & cfg)
{
repr_type * map = nullptr;
{
/** memory DGlobalSymtab::map_
* (but not counting the mmap()'s that map will make for itself)
**/
void * global_mem = global_mm.alloc_for<repr_type>();
map = new (global_mem) repr_type(cfg);
}
auto map = dp<repr_type>::make(aux_mm, cfg);
assert(map);
void * symtab_mem = mm.alloc_for<DGlobalSymtab>();
/* choosing same capacity for hash, vars */
DArray * vars = DArray::empty(mm, map->capacity());
assert(vars);
DGlobalSymtab * symtab = new (symtab_mem) DGlobalSymtab(map, vars);
auto symtab = dp<DGlobalSymtab>::make(mm, std::move(map), vars);
assert(symtab);
return symtab;
@ -133,7 +123,7 @@ namespace xo {
auto ix = map_->find(sym);
if (ix == map_->end())
if (ix == map_->end())
return Binding::null();
return Binding::global(ix->second);
@ -150,7 +140,22 @@ namespace xo {
DGlobalSymtab *
DGlobalSymtab::shallow_copy(obj<AAllocator> mm) const noexcept
{
return mm.std_copy_for<DGlobalSymtab>(this);
/** can't use std_copy_for because of non-copyable dp<repr_type>
*
* TODO: rename to shallow_move() throughout, and have std_copy_for()
* -> std_move_for()
*
**/
void * copy_mem = mm.alloc_copy_for(this);
if (copy_mem) {
DGlobalSymtab * self = const_cast<DGlobalSymtab*>(this);
return new (copy_mem) DGlobalSymtab(std::move(self->map_), vars_);
}
return nullptr;
}
std::size_t