xo-interpreter/xo-alloc: GlobalEnv + mm -> shallow_copy()

This commit is contained in:
Roland Conybeare 2025-11-23 22:57:52 -05:00
commit 4d2cd54365
18 changed files with 52 additions and 37 deletions

View file

@ -12,6 +12,9 @@ namespace xo {
* @brief Top-level global environment
**/
class GlobalEnv : public Env {
public:
using map_type = std::map<std::string, gp<Object>>;
public:
/** Create top-level global environment, allocating via @p mm.
* Expect one of these per interpreter session.
@ -19,7 +22,9 @@ namespace xo {
static gp<GlobalEnv> make_empty(gc::IAlloc * mm,
const rp<GlobalSymtab> & symtab);
#ifdef NOT_USING
gc::IAlloc * get_mm() const { return mm_; }
#endif
// inherited from Env..
virtual bool local_contains_var(const std::string & vname) const final override;
@ -29,10 +34,11 @@ namespace xo {
virtual TaggedPtr self_tp() const final override;
virtual void display(std::ostream & os) const final override;
virtual std::size_t _shallow_size() const final override;
virtual Object * _shallow_copy() const final override;
virtual Object * _shallow_copy(gc::IAlloc * mm) const final override;
virtual std::size_t _forward_children() final override;
private:
GlobalEnv(const GlobalEnv & x);
GlobalEnv(gc::IAlloc * mm, const rp<GlobalSymtab> & symtab);
private:
@ -56,7 +62,7 @@ namespace xo {
* TODO: probably want to hash here instead.
* May also want lhs names to be separately hashed symbols
**/
std::map<std::string, gp<Object>> slot_map_;
up<map_type> slot_map_;
};
} /*namespace scm*/
} /*namespace xo*/

View file

@ -102,7 +102,7 @@ namespace xo {
virtual TaggedPtr self_tp() const final override;
virtual void display(std::ostream & os) const final override;
virtual std::size_t _shallow_size() const final override;
virtual Object * _shallow_copy() const final override;
virtual Object * _shallow_copy(gc::IAlloc * mm) const final override;
virtual std::size_t _forward_children() final override;
private:

View file

@ -16,9 +16,17 @@ namespace xo {
return new GlobalEnv(mm, symtab);
}
GlobalEnv::GlobalEnv(const GlobalEnv & x)
: mm_{x.mm_},
symtab_{x.symtab_},
slot_map_{std::make_unique<map_type>(*x.slot_map_)}
{
}
GlobalEnv::GlobalEnv(gc::IAlloc * mm,
const rp<GlobalSymtab> & symtab) : mm_{mm},
symtab_{symtab}
symtab_{symtab},
slot_map_{std::make_unique<map_type>()}
{}
bool
@ -52,7 +60,7 @@ namespace xo {
this->symtab_->require_global(var->name(), var);
this->slot_map_[var->name()] = gp<Object>();
(*this->slot_map_)[var->name()] = gp<Object>();
}
TaggedPtr
@ -64,31 +72,29 @@ namespace xo {
void
GlobalEnv::display(std::ostream & os) const
{
os << "<global-env" << xtag("n", slot_map_.size()) << ">";
os << "<global-env" << xtag("n", slot_map_->size()) << ">";
}
std::size_t
GlobalEnv::_shallow_size() const
{
/** 0: since not allocated in gc-space */
return 0;
return sizeof(GlobalEnv);
}
Object *
GlobalEnv::_shallow_copy() const
GlobalEnv::_shallow_copy(gc::IAlloc * mm) const
{
/* by design, don't copy; not subject to GC */
return const_cast<GlobalEnv *>(this);
Cpof cpof(mm, this);
return new (cpof) GlobalEnv(*this);
}
std::size_t
GlobalEnv::_forward_children()
{
/* All global slots are treated as GC roots; this means we
* don't have to forward them
*
* This works only as long as global env is immortal.
*/
for (auto & ix : *slot_map_) {
Object::_forward_inplace(ix.second);
}
return _shallow_size();
}
} /*namespace scm*/

View file

@ -97,9 +97,9 @@ namespace xo {
}
Object *
LocalEnv::_shallow_copy() const
LocalEnv::_shallow_copy(gc::IAlloc * mm) const
{
Cpof cpof(Object::mm, this);
Cpof cpof(mm, this);
size_t z = size();