xo-interpreter: Object->TaggedPtr conversion (prep for primitives)

This commit is contained in:
Roland Conybeare 2025-11-26 20:15:03 -05:00
commit daf729292e
3 changed files with 98 additions and 0 deletions

40
include/xo/alloc/Blob.hpp Normal file
View file

@ -0,0 +1,40 @@
/** @file Blob.hpp
*
* @author Roland Conybeare, Nov 2025
**/
#pragma once
#include "Object.hpp"
#include "IAlloc.hpp"
namespace xo {
/** Use to allocate opaque binary data,
* with object header.
*
* Not sure if we want to bother implementing reflection for this...
**/
class Blob : public Object {
public:
Blob(std::size_t z) : z_{z} {};
static gp<Blob> make(gc::IAlloc * mm, std::size_t z);
std::size_t size() const { return z_; }
std::byte * data() { return data_; }
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(gc::IAlloc * gc) const final override;
virtual std::size_t _forward_children(gc::IAlloc * gc) final override;
private:
std::size_t z_ = 0;
/** flexible array, with @ref z_ bytes **/
std::byte data_[];
};
}
/* end Blob.hpp */

57
src/alloc/Blob.cpp Normal file
View file

@ -0,0 +1,57 @@
/** @file Blob.cpp
*
* @author Roland Conybeare, Nov 2025
**/
#include "Blob.hpp"
#include "xo/reflect/Reflect.hpp"
#include "xo/alloc/IAlloc.hpp"
namespace xo {
using xo::reflect::Reflect;
using xo::reflect::TaggedPtr;
gp<Blob>
Blob::make(gc::IAlloc * mm, std::size_t z) {
std::byte * mem = mm->alloc(sizeof(Blob) + z);
return new (mem) Blob(z);
}
TaggedPtr
Blob::self_tp() const
{
return Reflect::make_tp(const_cast<Blob*>(this));
}
void
Blob::display(std::ostream & os) const
{
os << "<blob" << xtag("z", z_) << ">";
}
std::size_t
Blob::_shallow_size() const {
return sizeof(Blob) + z_;
}
Object *
Blob::_shallow_copy(gc::IAlloc * mm) const {
Cpof cpof(mm, this);
std::byte * cp_mem = mm->alloc_gc_copy(sizeof(Blob) + z_, this);
gp<Blob> copy = new (cp_mem) Blob(z_);
::memcpy(copy->data(), data_, z_);
return copy.get();
}
std::size_t
Blob::_forward_children(gc::IAlloc *)
{
return this->_shallow_size();
}
}
/* end Blob.cpp */

View file

@ -9,6 +9,7 @@ set(SELF_SRCS
GcStatistics.cpp GcStatistics.cpp
ObjectStatistics.cpp ObjectStatistics.cpp
Object.cpp Object.cpp
Blob.cpp
Forwarding1.cpp Forwarding1.cpp
generation.cpp generation.cpp
) )