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

This commit is contained in:
Roland Conybeare 2026-02-15 14:12:31 -05:00
commit 3666bf3dd1
2 changed files with 32 additions and 26 deletions

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