+ xo-alloc + xo-object + xo-alloc docs + GC utests
This commit is contained in:
parent
03c8d66401
commit
e1d5ae46d2
58 changed files with 3948 additions and 83 deletions
52
xo-object/src/object/Boolean.cpp
Normal file
52
xo-object/src/object/Boolean.cpp
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/* @file Boolean.cpp
|
||||
*
|
||||
* author: Roland Conybeare, Aug 2025
|
||||
*/
|
||||
|
||||
#include "Boolean.hpp"
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
namespace xo {
|
||||
namespace obj {
|
||||
gp<Boolean>
|
||||
Boolean::boolean_obj(bool x)
|
||||
{
|
||||
static std::array<gp<Boolean>, 2> s_boolean_v
|
||||
= {{ new Boolean{false}, new Boolean{true} }};
|
||||
|
||||
return s_boolean_v[static_cast<std::size_t>(x)];
|
||||
}
|
||||
|
||||
std::size_t
|
||||
Boolean::_shallow_size() const
|
||||
{
|
||||
return sizeof(Boolean);
|
||||
}
|
||||
|
||||
Object *
|
||||
Boolean::_shallow_copy() const
|
||||
{
|
||||
/* Boolean instances not created in GC-owned space,
|
||||
* so GC will not traverse them.
|
||||
*
|
||||
* If we wanted booleans in GC-owned space, would need
|
||||
* to pad Boolean::value_ with enough space to hold a forwarding
|
||||
* pointer
|
||||
*/
|
||||
|
||||
assert(false);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
Boolean::_forward_children()
|
||||
{
|
||||
assert(false);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end Boolean.cpp */
|
||||
11
xo-object/src/object/CMakeLists.txt
Normal file
11
xo-object/src/object/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# object/CMakeLists.txt
|
||||
|
||||
set(SELF_LIB xo_object)
|
||||
set(SELF_SRCS
|
||||
Boolean.cpp
|
||||
String.cpp
|
||||
List.cpp
|
||||
Integer.cpp)
|
||||
|
||||
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})
|
||||
xo_dependency(${SELF_LIB} xo_alloc)
|
||||
38
xo-object/src/object/Integer.cpp
Normal file
38
xo-object/src/object/Integer.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/* @file Integer.cpp
|
||||
*
|
||||
* author: Roland Conybeare, Aug 2025
|
||||
*/
|
||||
|
||||
#include "Integer.hpp"
|
||||
#include <cstddef>
|
||||
|
||||
namespace xo {
|
||||
namespace obj {
|
||||
Integer::Integer(int_type x) : value_{x} {}
|
||||
|
||||
gp<Integer>
|
||||
Integer::make(int_type x) {
|
||||
return new (MMPtr(mm)) Integer(x);
|
||||
}
|
||||
|
||||
std::size_t
|
||||
Integer::_shallow_size() const {
|
||||
return sizeof(Integer);
|
||||
}
|
||||
|
||||
Object *
|
||||
Integer::_shallow_copy() const {
|
||||
Cpof cpof(this);
|
||||
|
||||
return new (cpof) Integer(*this);
|
||||
}
|
||||
|
||||
std::size_t
|
||||
Integer::_forward_children() {
|
||||
return Integer::_shallow_size();
|
||||
}
|
||||
|
||||
} /*namespace obj*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end Integer.cpp */
|
||||
74
xo-object/src/object/List.cpp
Normal file
74
xo-object/src/object/List.cpp
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/** @file List.cpp
|
||||
*
|
||||
* author: Roland Conybeare, Aug 2025
|
||||
**/
|
||||
|
||||
#include "List.hpp"
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
namespace xo {
|
||||
namespace obj {
|
||||
List::List(gp<Object> head, gp<List> tail)
|
||||
: head_{head}, tail_{tail} {}
|
||||
|
||||
gp<List>
|
||||
List::nil = new List(nullptr, nullptr);
|
||||
|
||||
gp<List>
|
||||
List::cons(gp<Object> car, gp<List> cdr) {
|
||||
return new (MMPtr(mm)) List(car, cdr);
|
||||
}
|
||||
|
||||
std::size_t
|
||||
List::size() const {
|
||||
std::size_t retval = 0;
|
||||
|
||||
gp<const List> l(this);
|
||||
while (!l->is_nil()) {
|
||||
++retval;
|
||||
l = l->tail();
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
gp<Object>
|
||||
List::list_ref(std::size_t i) const {
|
||||
gp<const List> rem(this);
|
||||
|
||||
while (i > 0) {
|
||||
assert(!(rem->is_nil()));
|
||||
|
||||
rem = rem->tail();
|
||||
--i;
|
||||
}
|
||||
|
||||
return rem->head();
|
||||
|
||||
}
|
||||
|
||||
std::size_t
|
||||
List::_shallow_size() const {
|
||||
return sizeof(List);
|
||||
}
|
||||
|
||||
Object *
|
||||
List::_shallow_copy() const {
|
||||
assert(!(this->is_nil()));
|
||||
|
||||
Cpof cpof(this);
|
||||
|
||||
return new (cpof) List(*this);
|
||||
}
|
||||
|
||||
std::size_t
|
||||
List::_forward_children() {
|
||||
Object::_forward_inplace(head_);
|
||||
Object::_forward_inplace(tail_);
|
||||
return List::_shallow_size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* end List.cpp */
|
||||
133
xo-object/src/object/String.cpp
Normal file
133
xo-object/src/object/String.cpp
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
/* @file String.cpp
|
||||
*
|
||||
* author: Roland Conybeare, Aug 2025
|
||||
*/
|
||||
|
||||
#include "String.hpp"
|
||||
#include "GC.hpp"
|
||||
#include <bsd/string.h>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
|
||||
namespace xo {
|
||||
namespace obj {
|
||||
String::String(Owner owner, std::size_t z, char * s)
|
||||
: owner_{owner}, z_chars_{z}, chars_{s}
|
||||
{}
|
||||
|
||||
String::String(gc::IAlloc * mm, Owner owner, std::size_t z, char * s, bool copy)
|
||||
: owner_{owner}, z_chars_{z}
|
||||
{
|
||||
if (copy) {
|
||||
chars_ = reinterpret_cast<char *>(mm->alloc(z));
|
||||
|
||||
assert(chars_);
|
||||
|
||||
strlcpy(chars_, s, z);
|
||||
} else {
|
||||
chars_ = s;
|
||||
}
|
||||
}
|
||||
|
||||
gp<String>
|
||||
String::from(gp<Object> x)
|
||||
{
|
||||
return dynamic_cast<String*>(x.ptr());
|
||||
}
|
||||
|
||||
gp<String>
|
||||
String::copy(const char * s)
|
||||
{
|
||||
return copy(Object::mm, s);
|
||||
}
|
||||
|
||||
gp<String>
|
||||
String::copy(gc::IAlloc * mm, const char * s)
|
||||
{
|
||||
std::size_t z = 1 + (s ? ::strlen(s) : 0);
|
||||
const char * chars = s ? s : "";
|
||||
|
||||
// const-cast ok since chars copied with Owner::unique
|
||||
return new (MMPtr(mm)) String(mm, Owner::unique, z, const_cast<char *>(chars), true /*copy*/);
|
||||
}
|
||||
|
||||
gp<String>
|
||||
String::allocate(std::size_t z)
|
||||
{
|
||||
return new (MMPtr(Object::mm)) String(mm, Owner::unique, z, const_cast<char *>(""), true /*copy*/);
|
||||
}
|
||||
|
||||
gp<String>
|
||||
String::append(gp<String> s1, gp<String> s2)
|
||||
{
|
||||
std::size_t z1 = s1->length();
|
||||
std::size_t z2 = s2->length();
|
||||
std::size_t z = z1 + z2;
|
||||
|
||||
gp<String> retval = allocate(z);
|
||||
|
||||
strlcpy(retval->chars_, s1->chars_, z1);
|
||||
strlcpy(retval->chars_ + z1, s2->chars_, z2);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
String::length() const
|
||||
{
|
||||
return ::strlen(chars_);
|
||||
}
|
||||
|
||||
// ----- GC support -----
|
||||
|
||||
std::size_t
|
||||
String::_shallow_size() const
|
||||
{
|
||||
/* no child Object* pointers to fixup,
|
||||
* but must count for amount of storage used by _shallow_move()
|
||||
*/
|
||||
std::size_t retval = gc::IAlloc::with_padding(sizeof(String));
|
||||
|
||||
if (owner_ == Owner::unique)
|
||||
retval += gc::IAlloc::with_padding(z_chars_);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
Object *
|
||||
String::_shallow_copy() const
|
||||
{
|
||||
// Reminder: String must come before secondary allocation,
|
||||
|
||||
Cpof cpof(this);
|
||||
|
||||
// might expect to write:
|
||||
// gp<String> copy = new (gcm) String(Object::mm, owner_, z_chars_, chars_);
|
||||
// but this would always put string contents in nursery to-space.
|
||||
//
|
||||
// We need to choose nursery/tenured based on location of this,
|
||||
// achieved by calling GC::alloc_copy() instead of GC::alloc()
|
||||
//
|
||||
gp<String> copy = new (cpof) String(owner_, z_chars_, chars_);
|
||||
|
||||
if (owner_ == Owner::unique) {
|
||||
std::byte * mem = reinterpret_cast<std::byte *>(chars_);
|
||||
|
||||
copy->chars_ = reinterpret_cast<char *>(Object::mm->alloc_gc_copy(z_chars_, mem));
|
||||
strlcpy(copy->chars_, chars_, z_chars_);
|
||||
}
|
||||
|
||||
return copy.ptr();
|
||||
}
|
||||
|
||||
std::size_t
|
||||
String::_forward_children()
|
||||
{
|
||||
return this->_shallow_size();
|
||||
}
|
||||
|
||||
} /*namespace obj*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end String.cpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue