refactor focusing on xo-alloc2/ xo-gc/ write-barrier
ability to inform allocator of gco->gco mutation, via AAllocator i/face.
This commit is contained in:
parent
d24f851eb5
commit
024574350a
9 changed files with 75 additions and 64 deletions
|
|
@ -6,7 +6,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <xo/alloc2/GCObject.hpp>
|
||||
//#include <xo/alloc2/Collector.hpp>
|
||||
#include <xo/alloc2/Allocator.hpp>
|
||||
#include <xo/facet/obj.hpp>
|
||||
#include <xo/indentlog/print/ppindentinfo.hpp>
|
||||
|
|
@ -20,7 +19,7 @@ namespace xo {
|
|||
namespace detail {
|
||||
/** null base case **/
|
||||
static inline bool do_array_push_back(DArray *,
|
||||
obj<xo::mm::ACollector>)
|
||||
obj<xo::mm::AAllocator>)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
|
@ -28,7 +27,7 @@ namespace xo {
|
|||
template <typename A, typename... Rest>
|
||||
requires (std::convertible_to<A, obj<xo::mm::AGCObject>>)
|
||||
static bool do_array_push_back(DArray * lhs,
|
||||
obj<xo::mm::ACollector> gc,
|
||||
obj<xo::mm::AAllocator> mm,
|
||||
A arg1,
|
||||
Rest... rest);
|
||||
}
|
||||
|
|
@ -49,7 +48,7 @@ namespace xo {
|
|||
/** type for array size **/
|
||||
using size_type = std::uint32_t;
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
using ACollector = xo::mm::ACollector;
|
||||
//using ACollector = xo::mm::ACollector;
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
/** gc-centric object visitor **/
|
||||
using AGCObjectVisitor = xo::mm::AGCObjectVisitor;
|
||||
|
|
@ -114,8 +113,6 @@ namespace xo {
|
|||
obj<AGCObject> at(size_type index) const;
|
||||
|
||||
const obj<AGCObject> & operator[](size_type index) const noexcept { return elts_[index]; }
|
||||
// TODO: nuke this or provide LValue shim. need write barrier!
|
||||
obj<AGCObject> & operator[](size_type index) noexcept { return elts_[index]; }
|
||||
|
||||
///@}
|
||||
/** @defgroup darray-iterators iterators **/
|
||||
|
|
@ -128,17 +125,17 @@ namespace xo {
|
|||
/** store @p elt at position @p index.
|
||||
* true on success, false otherwise
|
||||
**/
|
||||
bool assign_at(obj<ACollector> gc, size_type index, obj<AGCObject> elt) noexcept;
|
||||
bool assign_at(obj<AAllocator> mm, size_type index, obj<AGCObject> elt) noexcept;
|
||||
|
||||
/** append @p elt at the end of array.
|
||||
* true on success, false otherwise.
|
||||
* on failure array is unaltered
|
||||
**/
|
||||
bool push_back(obj<ACollector> gc, obj<AGCObject> elt) noexcept;
|
||||
bool push_back(obj<AAllocator> mm, obj<AGCObject> elt) noexcept;
|
||||
|
||||
template <typename... Args>
|
||||
requires (std::convertible_to<Args, obj<AGCObject>> && ...)
|
||||
bool push_back_all(obj<ACollector> gc, Args... args) noexcept;
|
||||
bool push_back_all(obj<AAllocator> mm, Args... args) noexcept;
|
||||
|
||||
/** store last element in array into @p elt and decrement array size.
|
||||
* true on success; false on failure (implies array was empty)
|
||||
|
|
@ -206,10 +203,10 @@ namespace xo {
|
|||
DArray *
|
||||
DArray::array(obj<AAllocator> mm, Args... args)
|
||||
{
|
||||
obj<ACollector> gc = mm.try_to_facet<ACollector>();
|
||||
//obj<ACollector> gc = mm.try_to_facet<ACollector>();
|
||||
DArray * result = _empty(mm, sizeof...(args));
|
||||
if (result) {
|
||||
detail::do_array_push_back(result, gc, args...);
|
||||
detail::do_array_push_back(result, mm, args...);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -218,20 +215,20 @@ namespace xo {
|
|||
template <typename A, typename... Rest>
|
||||
requires (std::convertible_to<A, obj<xo::mm::AGCObject>>)
|
||||
static bool do_array_push_back(DArray * lhs,
|
||||
obj<xo::mm::ACollector> gc,
|
||||
obj<xo::mm::AAllocator> mm,
|
||||
A arg1,
|
||||
Rest... rest)
|
||||
{
|
||||
return (lhs->push_back(gc, arg1)
|
||||
&& do_array_push_back(lhs, gc, rest...));
|
||||
return (lhs->push_back(mm, arg1)
|
||||
&& do_array_push_back(lhs, mm, rest...));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
requires (std::convertible_to<Args, obj<xo::mm::AGCObject>> && ...)
|
||||
bool
|
||||
DArray::push_back_all(obj<ACollector> gc, Args... args) noexcept {
|
||||
return detail::do_array_push_back(this, gc, args...);
|
||||
DArray::push_back_all(obj<AAllocator> mm, Args... args) noexcept {
|
||||
return detail::do_array_push_back(this, mm, args...);
|
||||
}
|
||||
|
||||
} /*namespace scm*/
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
//#include <xo/alloc2/Collector.hpp>
|
||||
#include <xo/alloc2/Allocator.hpp>
|
||||
#include <xo/alloc2/GCObjectVisitor.hpp>
|
||||
#include <xo/indentlog/print/ppindentinfo.hpp>
|
||||
|
|
@ -16,7 +15,6 @@ namespace xo {
|
|||
namespace scm {
|
||||
struct DBoolean {
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
//using ACollector = xo::mm::ACollector;
|
||||
using AGCObjectVisitor = xo::mm::AGCObjectVisitor;
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
using VisitReason = xo::mm::VisitReason;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@
|
|||
#include "DString.hpp"
|
||||
#include <xo/alloc2/GCObject.hpp>
|
||||
#include <xo/alloc2/GCObjectVisitor.hpp>
|
||||
#include <xo/alloc2/Collector.hpp>
|
||||
#include <xo/alloc2/Allocator.hpp>
|
||||
#include <xo/facet/obj.hpp>
|
||||
#include <xo/indentlog/print/ppindentinfo.hpp>
|
||||
|
|
@ -34,8 +33,6 @@ namespace xo {
|
|||
using size_type = std::uint32_t;
|
||||
/** xo allocator facet **/
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
/** garbage collector facet **/
|
||||
using ACollector = xo::mm::ACollector;
|
||||
/** gc-centric object visitor **/
|
||||
using AGCObjectVisitor = xo::mm::AGCObjectVisitor;
|
||||
/** gc-aware object facet **/
|
||||
|
|
@ -144,12 +141,12 @@ namespace xo {
|
|||
*
|
||||
* @return true if key-value pair updated; false if key not found
|
||||
**/
|
||||
bool try_update(obj<ACollector> gc, const pair_type & kvpair);
|
||||
bool try_update(obj<AAllocator> mm, const pair_type & kvpair);
|
||||
|
||||
/** update key-value pair for existing @p key to map to @p value.
|
||||
* false if @p key not already present.
|
||||
**/
|
||||
bool try_update_cstr(obj<ACollector> gc, const char * key, obj<AGCObject> value);
|
||||
bool try_update_cstr(obj<AAllocator> gc, const char * key, obj<AGCObject> value);
|
||||
|
||||
/** convenience method:
|
||||
* try_upsert pair (k, @p value), after boxing c-style string @p key with @p mm to get k
|
||||
|
|
@ -167,7 +164,7 @@ namespace xo {
|
|||
*
|
||||
* False if dictionary already at capacity
|
||||
**/
|
||||
bool try_upsert(obj<ACollector> gc, const pair_type & kvpair);
|
||||
bool try_upsert(obj<AAllocator> gc, const pair_type & kvpair);
|
||||
|
||||
/** upsert key-value pair @p kvpair into dictionary.
|
||||
* If at capacity, expand capacity, getting new memory from @p mm.
|
||||
|
|
@ -217,7 +214,7 @@ namespace xo {
|
|||
/** append {key, value} pair @p kv_pair to this dictionary
|
||||
* Require: @p kv_pair.first not already present in @ref keys_
|
||||
**/
|
||||
bool _append_kv_aux(obj<ACollector> gc, const pair_type & kv_pair);
|
||||
bool _append_kv_aux(obj<AAllocator> mm, const pair_type & kv_pair);
|
||||
///@}
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
//#include <xo/alloc2/Collector.hpp>
|
||||
#include <xo/alloc2/GCObjectVisitor.hpp>
|
||||
#include <xo/alloc2/Allocator.hpp>
|
||||
#include <xo/indentlog/print/ppindentinfo.hpp>
|
||||
|
|
@ -16,7 +15,6 @@ namespace xo {
|
|||
namespace scm {
|
||||
struct DInteger {
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
//using ACollector = xo::mm::ACollector;
|
||||
using AGCObjectVisitor = xo::mm::AGCObjectVisitor;
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
using VisitReason = xo::mm::VisitReason;
|
||||
|
|
|
|||
|
|
@ -64,7 +64,8 @@ namespace xo {
|
|||
obj<AGCObject> at(size_type ix) const;
|
||||
|
||||
/** assign head **/
|
||||
void assign_head(obj<ACollector> gc, obj<AGCObject> h);
|
||||
void assign_head(obj<AAllocator> mm, obj<AGCObject> h);
|
||||
void assign_head_gc(obj<ACollector> gc, obj<AGCObject> h);
|
||||
/** assign rest-pointer. Caller responsible for preserving acyclic property! **/
|
||||
void _assign_rest(DList * r);
|
||||
|
||||
|
|
|
|||
|
|
@ -75,20 +75,23 @@ namespace xo {
|
|||
}
|
||||
|
||||
bool
|
||||
DArray::assign_at(obj<ACollector> gc, size_type ix, obj<AGCObject> x) noexcept
|
||||
DArray::assign_at(obj<AAllocator> mm, size_type ix, obj<AGCObject> x) noexcept
|
||||
{
|
||||
if (ix >= size_)
|
||||
return false;
|
||||
|
||||
scope log(XO_DEBUG(true), "need write barrier");
|
||||
|
||||
mm_do_assign(gc, this, &elts_[ix], x);
|
||||
mm.barrier_assign_aux(this,
|
||||
elts_[ix].iface(), elts_[ix].opaque_data_addr(),
|
||||
x.iface(), x.opaque_data());
|
||||
// mm_do_assign(gc, this, &elts_[ix], x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
DArray::push_back(obj<ACollector> gc, obj<AGCObject> elt) noexcept
|
||||
DArray::push_back(obj<AAllocator> mm, obj<AGCObject> elt) noexcept
|
||||
{
|
||||
if (size_ >= capacity_) {
|
||||
return false;
|
||||
|
|
@ -98,7 +101,11 @@ namespace xo {
|
|||
void * mem = &(elts_[size_]);
|
||||
new (mem) obj<AGCObject>();
|
||||
|
||||
mm_do_assign(gc, this, &(elts_[size_]), elt);
|
||||
mm.barrier_assign_aux(this,
|
||||
elts_[size_].iface(), elts_[size_].opaque_data_addr(),
|
||||
elt.iface(), elt.opaque_data());
|
||||
|
||||
//mm_do_assign(gc, this, &(elts_[size_]), elt);
|
||||
|
||||
++(this->size_);
|
||||
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
bool
|
||||
DDictionary::try_update(obj<ACollector> gc, const pair_type & kv_pair)
|
||||
DDictionary::try_update(obj<AAllocator> mm, const pair_type & kv_pair)
|
||||
{
|
||||
for (size_type i = 0, n = keys_->size(); i < n; ++i) {
|
||||
auto key_i = obj<AGCObject,DString>::from((*keys_)[i]);
|
||||
|
|
@ -116,7 +116,7 @@ namespace xo {
|
|||
assert(key_i);
|
||||
|
||||
if (*(key_i.data()) == *(kv_pair.first)) {
|
||||
values_->assign_at(gc, i, kv_pair.second);
|
||||
values_->assign_at(mm, i, kv_pair.second);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -125,7 +125,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
bool
|
||||
DDictionary::try_update_cstr(obj<ACollector> gc, const char * key, obj<AGCObject> value)
|
||||
DDictionary::try_update_cstr(obj<AAllocator> mm, const char * key, obj<AGCObject> value)
|
||||
{
|
||||
for (size_type i = 0, n = keys_->size(); i < n; ++i) {
|
||||
auto key_i = obj<AGCObject,DString>::from((*keys_)[i]);
|
||||
|
|
@ -133,7 +133,7 @@ namespace xo {
|
|||
assert(key_i);
|
||||
|
||||
if (strcmp(key, key_i->data()) == 0) {
|
||||
values_->assign_at(gc, i, value);
|
||||
values_->assign_at(mm, i, value);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -148,8 +148,8 @@ namespace xo {
|
|||
const DString * k1 = DString::from_cstr(mm, key_cstr);
|
||||
|
||||
if (k1) {
|
||||
obj<ACollector> gc = mm.try_to_facet<ACollector>();
|
||||
return this->try_upsert(gc, std::make_pair(k1, value));
|
||||
//obj<ACollector> gc = mm.try_to_facet<ACollector>();
|
||||
return this->try_upsert(mm, std::make_pair(k1, value));
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
@ -167,23 +167,23 @@ namespace xo {
|
|||
}
|
||||
|
||||
bool
|
||||
DDictionary::try_upsert(obj<ACollector> gc, const pair_type & kv_pair)
|
||||
DDictionary::try_upsert(obj<AAllocator> mm, const pair_type & kv_pair)
|
||||
{
|
||||
if (this->try_update(gc, kv_pair))
|
||||
if (this->try_update(mm, kv_pair))
|
||||
return true;
|
||||
|
||||
if (keys_->size() == keys_->capacity())
|
||||
return false;
|
||||
|
||||
return this->_append_kv_aux(gc, kv_pair);
|
||||
return this->_append_kv_aux(mm, kv_pair);
|
||||
}
|
||||
|
||||
bool
|
||||
DDictionary::upsert(obj<AAllocator> mm, const pair_type & kv_pair)
|
||||
{
|
||||
obj<ACollector> gc = mm.try_to_facet<ACollector>();
|
||||
//obj<ACollector> gc = mm.try_to_facet<ACollector>();
|
||||
|
||||
if (this->try_update(gc, kv_pair))
|
||||
if (this->try_update(mm, kv_pair))
|
||||
return true;
|
||||
|
||||
// key not present -> must expand {key array, value array}
|
||||
|
|
@ -204,19 +204,19 @@ namespace xo {
|
|||
}
|
||||
}
|
||||
|
||||
return this->_append_kv_aux(gc, kv_pair);
|
||||
return this->_append_kv_aux(mm, kv_pair);
|
||||
}
|
||||
|
||||
bool
|
||||
DDictionary::_append_kv_aux(obj<ACollector> gc, const pair_type & kv_pair)
|
||||
DDictionary::_append_kv_aux(obj<AAllocator> mm, const pair_type & kv_pair)
|
||||
{
|
||||
DString * key = const_cast<DString *>(kv_pair.first);
|
||||
|
||||
bool ok
|
||||
= keys_->push_back(gc, obj<AGCObject,DString>(key));
|
||||
= keys_->push_back(mm, obj<AGCObject,DString>(key));
|
||||
|
||||
if (ok) {
|
||||
ok = values_->push_back(gc, kv_pair.second);
|
||||
ok = values_->push_back(mm, kv_pair.second);
|
||||
|
||||
if (!ok) {
|
||||
// since we couldn't insert value, also drop key
|
||||
|
|
|
|||
|
|
@ -120,7 +120,20 @@ namespace xo {
|
|||
}
|
||||
|
||||
void
|
||||
DList::assign_head(obj<ACollector> gc, obj<AGCObject> rhs)
|
||||
DList::assign_head(obj<AAllocator> mm, obj<AGCObject> rhs)
|
||||
{
|
||||
scope log(XO_DEBUG(true), xtag("mm.data", mm.data_));
|
||||
|
||||
mm.barrier_assign_aux(this,
|
||||
head_.iface(), head_.opaque_data_addr(),
|
||||
rhs.iface(), rhs.opaque_data());
|
||||
|
||||
//mm_do_assign(gc, this, &head_, rhs);
|
||||
}
|
||||
|
||||
// vestigial. used in MockCollector
|
||||
void
|
||||
DList::assign_head_gc(obj<ACollector> gc, obj<AGCObject> rhs)
|
||||
{
|
||||
scope log(XO_DEBUG(true), xtag("gc.data", gc.data_));
|
||||
|
||||
|
|
|
|||
|
|
@ -30,12 +30,12 @@ namespace xo {
|
|||
REQUIRE(arr.size() == 0);
|
||||
REQUIRE(arr.capacity() == 0);
|
||||
|
||||
REQUIRE(arr.is_empty());;
|
||||
REQUIRE(arr.is_empty());
|
||||
|
||||
// null_gc: for no memory barrier
|
||||
obj<ACollector> null_gc;
|
||||
// null_mm: for no memory barrier
|
||||
obj<AAllocator> null_mm;
|
||||
|
||||
REQUIRE(arr.push_back(null_gc, ListOps::nil()) == false);
|
||||
REQUIRE(arr.push_back(null_mm, ListOps::nil()) == false);
|
||||
}
|
||||
|
||||
TEST_CASE("DArray-empty", "[object2][DArray]")
|
||||
|
|
@ -61,7 +61,7 @@ namespace xo {
|
|||
.size_ = 4*1024 };
|
||||
DArena arena = DArena::map(cfg);
|
||||
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
||||
obj<ACollector> null_gc;
|
||||
obj<AAllocator> null_mm;
|
||||
|
||||
DArray * arr = DArray::_empty(alloc, 16);
|
||||
REQUIRE(arr != nullptr);
|
||||
|
|
@ -70,7 +70,7 @@ namespace xo {
|
|||
|
||||
obj<AGCObject> elt = DInteger::box<AGCObject>(alloc, 42);
|
||||
|
||||
bool ok = arr->push_back(null_gc, elt);
|
||||
bool ok = arr->push_back(null_mm, elt);
|
||||
|
||||
REQUIRE(ok == true);
|
||||
REQUIRE(arr->is_empty() == false);
|
||||
|
|
@ -84,7 +84,7 @@ namespace xo {
|
|||
.size_ = 4*1024 };
|
||||
DArena arena = DArena::map(cfg);
|
||||
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
||||
obj<ACollector> null_gc;
|
||||
obj<AAllocator> null_mm;
|
||||
|
||||
DArray * arr = DArray::_empty(alloc, 4);
|
||||
REQUIRE(arr != nullptr);
|
||||
|
|
@ -96,7 +96,7 @@ namespace xo {
|
|||
REQUIRE(arr->size() == i);
|
||||
|
||||
obj<AGCObject> elt = DInteger::box<AGCObject>(alloc, 100 + i);
|
||||
bool ok = arr->push_back(null_gc, elt);
|
||||
bool ok = arr->push_back(null_mm, elt);
|
||||
REQUIRE(ok == true);
|
||||
|
||||
REQUIRE(arr->capacity() == 4);
|
||||
|
|
@ -113,7 +113,7 @@ namespace xo {
|
|||
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
||||
|
||||
DArray * arr = DArray::_empty(alloc, 2);
|
||||
obj<ACollector> null_gc;
|
||||
obj<AAllocator> null_mm;
|
||||
|
||||
REQUIRE(arr != nullptr);
|
||||
REQUIRE(arr->capacity() == 2);
|
||||
|
|
@ -123,11 +123,11 @@ namespace xo {
|
|||
obj<AGCObject> e2 = DInteger::box<AGCObject>(alloc, 2);
|
||||
obj<AGCObject> e3 = DInteger::box<AGCObject>(alloc, 3);
|
||||
|
||||
REQUIRE(arr->push_back(null_gc, e1) == true);
|
||||
REQUIRE(arr->push_back(null_mm, e1) == true);
|
||||
REQUIRE(arr->size() == 1);
|
||||
REQUIRE(arr->push_back(null_gc, e2) == true);
|
||||
REQUIRE(arr->push_back(null_mm, e2) == true);
|
||||
REQUIRE(arr->size() == 2);
|
||||
REQUIRE(arr->push_back(null_gc, e3) == false);
|
||||
REQUIRE(arr->push_back(null_mm, e3) == false);
|
||||
REQUIRE(arr->size() == 2);
|
||||
REQUIRE(arr->capacity() == 2);
|
||||
}
|
||||
|
|
@ -140,7 +140,7 @@ namespace xo {
|
|||
auto alloc = with_facet<AAllocator>::mkobj(&arena);
|
||||
|
||||
DArray * arr = DArray::_empty(alloc, 4);
|
||||
obj<ACollector> null_gc;
|
||||
obj<AAllocator> null_mm;
|
||||
|
||||
REQUIRE(arr != nullptr);
|
||||
REQUIRE(arr->size() == 0);
|
||||
|
|
@ -150,9 +150,9 @@ namespace xo {
|
|||
obj<AGCObject> e1 = DInteger::box<AGCObject>(alloc, 200);
|
||||
obj<AGCObject> e2 = DInteger::box<AGCObject>(alloc, 300);
|
||||
|
||||
arr->push_back(null_gc, e0);
|
||||
arr->push_back(null_gc, e1);
|
||||
arr->push_back(null_gc, e2);
|
||||
arr->push_back(null_mm, e0);
|
||||
arr->push_back(null_mm, e1);
|
||||
arr->push_back(null_mm, e2);
|
||||
|
||||
REQUIRE(arr->size() == 3);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue