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

@ -30,7 +30,7 @@ namespace xo {
/** required by Object i/face, but never called on Forwarding1 **/
virtual std::size_t _shallow_size() const final override;
/** required by Object i/face, but never called on Forwarding1 **/
virtual Object * _shallow_copy() const final override;
virtual Object * _shallow_copy(gc::IAlloc * mm) const final override;
/** required by Object i/face, but never called on Forwarding1 **/
virtual std::size_t _forward_children() final override;

View file

@ -57,6 +57,9 @@ namespace xo {
return (u2 <= u1 + sizeof(std::uintptr_t));
}
/** (for consistency's sake) **/
T * get() const { return ptr_; }
T * ptr() const { return ptr_; }
T ** ptr_address() { return &ptr_; }
@ -204,7 +207,7 @@ namespace xo {
*
* Require: @ref mm is an instance of @ref gc::GC
**/
virtual Object * _shallow_copy() const = 0;
virtual Object * _shallow_copy(gc::IAlloc * mm) const = 0;
/** update child pointers that refer to forwarding pointers,
* replacing them with the correct destination.

View file

@ -52,7 +52,7 @@ namespace xo {
// LCOV_EXCL_START
Object *
Forwarding1::_shallow_copy() const {
Forwarding1::_shallow_copy(gc::IAlloc *) const {
assert(false);
return nullptr;
}

View file

@ -170,7 +170,7 @@ namespace xo {
*/
if (gc->fromspace_contains(src))
{
Object * dest = src->_shallow_copy();
Object * dest = src->_shallow_copy(gc);
if (dest != src)
src->_forward_to(dest);

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();

View file

@ -24,7 +24,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

@ -29,7 +29,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

@ -29,7 +29,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

@ -58,7 +58,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

@ -36,7 +36,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

@ -56,7 +56,7 @@ namespace xo {
// LCOV_EXCL_START
Object *
Boolean::_shallow_copy() const
Boolean::_shallow_copy(gc::IAlloc *) const
{
/* Boolean instances not created in GC-owned space,

View file

@ -42,8 +42,8 @@ namespace xo {
}
Object *
Float::_shallow_copy() const {
Cpof cpof(Object::mm, this);
Float::_shallow_copy(gc::IAlloc * mm) const {
Cpof cpof(mm, this);
return new (cpof) Float(*this);
}

View file

@ -43,8 +43,8 @@ namespace xo {
}
Object *
Integer::_shallow_copy() const {
Cpof cpof(Object::mm, this);
Integer::_shallow_copy(gc::IAlloc * mm) const {
Cpof cpof(mm, this);
return new (cpof) Integer(*this);
}

View file

@ -97,12 +97,12 @@ namespace xo {
}
Object *
List::_shallow_copy() const {
scope log(XO_DEBUG(Object::mm->debug_flag()));
List::_shallow_copy(gc::IAlloc * mm) const {
scope log(XO_DEBUG(mm->debug_flag()));
assert(!(this->is_nil()));
Cpof cpof(Object::mm, this);
Cpof cpof(mm, this);
return new (cpof) List(*this);
}

View file

@ -126,11 +126,11 @@ namespace xo {
}
Object *
String::_shallow_copy() const
String::_shallow_copy(gc::IAlloc * mm) const
{
// Reminder: String must come before secondary allocation,
Cpof cpof(Object::mm, this);
Cpof cpof(mm, this);
// might expect to write:
// gp<String> copy = new (gcm) String(Object::mm, owner_, z_chars_, chars_);