From daf729292e6ba9ac23d4f8d78f40b210bc498a38 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 26 Nov 2025 20:15:03 -0500 Subject: [PATCH] xo-interpreter: Object->TaggedPtr conversion (prep for primitives) --- include/xo/alloc/Blob.hpp | 40 +++++++++++++++++++++++++++ src/alloc/Blob.cpp | 57 +++++++++++++++++++++++++++++++++++++++ src/alloc/CMakeLists.txt | 1 + 3 files changed, 98 insertions(+) create mode 100644 include/xo/alloc/Blob.hpp create mode 100644 src/alloc/Blob.cpp diff --git a/include/xo/alloc/Blob.hpp b/include/xo/alloc/Blob.hpp new file mode 100644 index 00000000..9e3ae44a --- /dev/null +++ b/include/xo/alloc/Blob.hpp @@ -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 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 */ diff --git a/src/alloc/Blob.cpp b/src/alloc/Blob.cpp new file mode 100644 index 00000000..97932844 --- /dev/null +++ b/src/alloc/Blob.cpp @@ -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::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(this)); + } + + void + Blob::display(std::ostream & os) const + { + os << ""; + } + + 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 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 */ diff --git a/src/alloc/CMakeLists.txt b/src/alloc/CMakeLists.txt index 6bbc2f32..4a9d1db4 100644 --- a/src/alloc/CMakeLists.txt +++ b/src/alloc/CMakeLists.txt @@ -9,6 +9,7 @@ set(SELF_SRCS GcStatistics.cpp ObjectStatistics.cpp Object.cpp + Blob.cpp Forwarding1.cpp generation.cpp )