From 3666bf3dd174998568f7d1d42b0e874aec453657 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sun, 15 Feb 2026 14:12:31 -0500 Subject: [PATCH] xo-expression2 stack: + dp<> template + robustify DGlobalSymtab --- include/xo/expression2/DGlobalSymtab.hpp | 17 +++++----- src/expression2/DGlobalSymtab.cpp | 41 +++++++++++++----------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/include/xo/expression2/DGlobalSymtab.hpp b/include/xo/expression2/DGlobalSymtab.hpp index 32fb9570..566d8981 100644 --- a/include/xo/expression2/DGlobalSymtab.hpp +++ b/include/xo/expression2/DGlobalSymtab.hpp @@ -8,6 +8,7 @@ #include "Binding.hpp" #include "DVariable.hpp" #include +#include #include namespace xo { @@ -35,16 +36,16 @@ namespace xo { /** @defgroup scm-globalsymtab-ctors constructors **/ ///@{ - DGlobalSymtab(repr_type * map, DArray * vars); + DGlobalSymtab(dp 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 fixed_mm, - obj mm, - const ArenaHashMapConfig & cfg); + dp make(obj fixed_mm, + obj 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 mm) const noexcept; std::size_t forward_children(obj 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 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. diff --git a/src/expression2/DGlobalSymtab.cpp b/src/expression2/DGlobalSymtab.cpp index 85fd6e93..b7eb4d73 100644 --- a/src/expression2/DGlobalSymtab.cpp +++ b/src/expression2/DGlobalSymtab.cpp @@ -18,35 +18,25 @@ namespace xo { namespace scm { - DGlobalSymtab::DGlobalSymtab(repr_type * map, + DGlobalSymtab::DGlobalSymtab(dp map, DArray * vars) - : map_{map}, vars_{vars} + : map_{std::move(map)}, vars_{vars} { } - DGlobalSymtab * - DGlobalSymtab::make(obj global_mm, + dp + DGlobalSymtab::make(obj aux_mm, obj 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(); - - map = new (global_mem) repr_type(cfg); - } + auto map = dp::make(aux_mm, cfg); assert(map); - void * symtab_mem = mm.alloc_for(); - /* 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::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 mm) const noexcept { - return mm.std_copy_for(this); + /** can't use std_copy_for because of non-copyable dp + * + * 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(this); + + return new (copy_mem) DGlobalSymtab(std::move(self->map_), vars_); + } + + return nullptr; } std::size_t