xo-interpreter2 stack: refactor: string clases -> xo-stringtable2/
This commit is contained in:
parent
5fc59b0df4
commit
f60f90d8f3
20 changed files with 966 additions and 6 deletions
|
|
@ -6,9 +6,16 @@ set(SELF_SRCS
|
|||
stringtable2_register_facets.cpp
|
||||
stringtable2_register_types.cpp
|
||||
|
||||
StringTable.cpp
|
||||
|
||||
DString.cpp
|
||||
IGCObject_DString.cpp
|
||||
IPrintable_DString.cpp
|
||||
|
||||
DUniqueString.cpp
|
||||
IGCObject_DUniqueString.cpp
|
||||
IPrintable_DUniqueString.cpp
|
||||
|
||||
)
|
||||
|
||||
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})
|
||||
|
|
|
|||
121
src/stringtable2/DUniqueString.cpp
Normal file
121
src/stringtable2/DUniqueString.cpp
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
/** @file DUniqueString.cpp
|
||||
*
|
||||
* @author Roland Conybeare, Jan 2026
|
||||
**/
|
||||
|
||||
#include "DUniqueString.hpp"
|
||||
#include "DString.hpp"
|
||||
#include <xo/arena/padding.hpp>
|
||||
#include <xo/indentlog/scope.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace xo {
|
||||
using xo::mm::padding;
|
||||
using xo::facet::typeseq;
|
||||
|
||||
namespace scm {
|
||||
int
|
||||
DUniqueString::compare(const DUniqueString & lhs, const DUniqueString & rhs)
|
||||
{
|
||||
if (&lhs == &rhs)
|
||||
return 0;
|
||||
|
||||
return DString::compare(*(lhs._text()), *(rhs._text()));
|
||||
}
|
||||
|
||||
DString *
|
||||
DUniqueString::_text() const noexcept
|
||||
{
|
||||
// location of paired DString is chosen
|
||||
// by allocator (DArena, probably).
|
||||
//
|
||||
// In general allocator alignment more conservative
|
||||
// than C++ alignment
|
||||
//
|
||||
// Remmebr also: although DUniqueString has zero members,
|
||||
// C++ requires it to behave asif size at least 1 byte
|
||||
// for iterator consistency
|
||||
// (e.g. because c++ would support iterating over
|
||||
// std::vector<EmptyStruct>)
|
||||
//
|
||||
size_t offset = padding::with_padding(sizeof(*this));
|
||||
assert(offset > 0);
|
||||
|
||||
return (DString *)(((std::byte *)this) + offset);
|
||||
}
|
||||
|
||||
bool
|
||||
DUniqueString::pretty(const ppindentinfo & ppii) const
|
||||
{
|
||||
return _text()->pretty(ppii);
|
||||
}
|
||||
|
||||
DUniqueString *
|
||||
DUniqueString::from_view(obj<AAllocator> mm,
|
||||
std::string_view sv)
|
||||
{
|
||||
scope log(XO_DEBUG(false));
|
||||
|
||||
/** fine point: choosing to allocate DUniqueString ahead of DString,
|
||||
* so it comes first in bump allocator
|
||||
**/
|
||||
|
||||
void * mem = mm.super_alloc(typeseq::id<DUniqueString>(),
|
||||
sizeof(DUniqueString));
|
||||
DUniqueString * result = new (mem) DUniqueString();
|
||||
|
||||
/** allocated in memory immediate following @p result.
|
||||
* This optimization saves us one pointer (8 bytes) in DUniqueString
|
||||
* itself, plus one allocation header (8 bytes) for 16 bytes total
|
||||
**/
|
||||
DString * text = DString::from_view_suballoc(mm, sv);
|
||||
|
||||
log && log(xtag("result", result), xtag("result.text", result->_text()), xtag("text", text));
|
||||
|
||||
assert(text);
|
||||
assert(text == result->_text());
|
||||
|
||||
/** must finish super-allocation before next alloc **/
|
||||
mm.sub_alloc(0, true);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t
|
||||
DUniqueString::shallow_size() const noexcept
|
||||
{
|
||||
return sizeof(DUniqueString);
|
||||
}
|
||||
|
||||
DUniqueString *
|
||||
DUniqueString::shallow_copy(obj<AAllocator> mm) const noexcept
|
||||
{
|
||||
// well-posed, but not expected to be used.
|
||||
assert(false);
|
||||
|
||||
DUniqueString * copy = (DUniqueString *)mm.alloc_copy((std::byte *)this);
|
||||
|
||||
if (copy) {
|
||||
// Copy assignment not implemented in general
|
||||
// *copy = *this;
|
||||
// in this case *copy already has the same size as *this
|
||||
|
||||
assert(size() <= capacity());
|
||||
|
||||
strncpy(copy->_text()->data(),
|
||||
this->_text()->chars(),
|
||||
this->size());
|
||||
}
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
size_t
|
||||
DUniqueString::forward_children(obj<ACollector>) noexcept
|
||||
{
|
||||
return shallow_size();
|
||||
}
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end DUniqueString.cpp */
|
||||
39
src/stringtable2/IGCObject_DUniqueString.cpp
Normal file
39
src/stringtable2/IGCObject_DUniqueString.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/** @file IGCObject_DUniqueString.cpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/IGCObject_DUniqueString.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_any.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/IGCObject_DUniqueString.json5]
|
||||
**/
|
||||
|
||||
#include "uniquestring/IGCObject_DUniqueString.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
auto
|
||||
IGCObject_DUniqueString::shallow_size(const DUniqueString & self) noexcept -> size_type
|
||||
{
|
||||
return self.shallow_size();
|
||||
}
|
||||
|
||||
auto
|
||||
IGCObject_DUniqueString::shallow_copy(const DUniqueString & self, obj<AAllocator> mm) noexcept -> Opaque
|
||||
{
|
||||
return self.shallow_copy(mm);
|
||||
}
|
||||
|
||||
auto
|
||||
IGCObject_DUniqueString::forward_children(DUniqueString & self, obj<ACollector> gc) noexcept -> size_type
|
||||
{
|
||||
return self.forward_children(gc);
|
||||
}
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end IGCObject_DUniqueString.cpp */
|
||||
28
src/stringtable2/IPrintable_DUniqueString.cpp
Normal file
28
src/stringtable2/IPrintable_DUniqueString.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/** @file IPrintable_DUniqueString.cpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/IPrintable_DUniqueString.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_any.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/IPrintable_DUniqueString.json5]
|
||||
**/
|
||||
|
||||
#include "uniquestring/IPrintable_DUniqueString.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
auto
|
||||
IPrintable_DUniqueString::pretty(const DUniqueString & self, const ppindentinfo & ppii) -> bool
|
||||
{
|
||||
return self.pretty(ppii);
|
||||
}
|
||||
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end IPrintable_DUniqueString.cpp */
|
||||
173
src/stringtable2/StringTable.cpp
Normal file
173
src/stringtable2/StringTable.cpp
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
/** @file StringTable.cpp
|
||||
*
|
||||
* @author Roland Conybeare, Jan 2026
|
||||
**/
|
||||
|
||||
#include "StringTable.hpp"
|
||||
#include <xo/alloc2/Allocator.hpp>
|
||||
#include <xo/alloc2/arena/IAllocator_DArena.hpp>
|
||||
|
||||
namespace xo {
|
||||
using xo::mm::ArenaConfig;
|
||||
using xo::mm::AAllocator;
|
||||
using xo::mm::MemorySizeInfo;
|
||||
using xo::facet::with_facet;
|
||||
using xo::facet::obj;
|
||||
|
||||
namespace scm {
|
||||
StringTable::StringTable(size_type hint_max_capacity,
|
||||
bool debug_flag)
|
||||
: strings_{DArena::map(ArenaConfig{.name_ = "strings",
|
||||
.size_ = hint_max_capacity})},
|
||||
map_{"stringkeys", hint_max_capacity}
|
||||
{
|
||||
(void)debug_flag;
|
||||
}
|
||||
|
||||
const DUniqueString *
|
||||
StringTable::lookup(std::string_view key) const
|
||||
{
|
||||
auto ix = map_.find(key);
|
||||
|
||||
if (ix != map_.end())
|
||||
return ix->second;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const DUniqueString *
|
||||
StringTable::intern(std::string_view key)
|
||||
{
|
||||
// 1a. lookup key in map_.
|
||||
// 1b. if present, return existing DString*
|
||||
|
||||
auto ix = map_.find(key);
|
||||
|
||||
if (ix != map_.end())
|
||||
return ix->second;
|
||||
|
||||
// 2. otherwise need to add.
|
||||
//
|
||||
// 2d. return key2 address
|
||||
|
||||
// 2a. allocate DUniqueString copy 'interned' of key in strings_
|
||||
auto mm = with_facet<AAllocator>::mkobj(&strings_);
|
||||
DUniqueString * interned = DUniqueString::from_view(mm, key);
|
||||
|
||||
assert(interned);
|
||||
if (interned) {
|
||||
// 2b. make string_view from *interned
|
||||
std::string_view interned_key = std::string_view(*interned);
|
||||
|
||||
// interned_key has same lifetime as StringTable,
|
||||
// we can use it in map_
|
||||
|
||||
// 2c. store address of 'interned' in map_
|
||||
auto & slot = this->map_[interned_key];
|
||||
|
||||
slot = interned;
|
||||
|
||||
return slot;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const DUniqueString *
|
||||
StringTable::gensym(std::string_view prefix)
|
||||
{
|
||||
static std::size_t s_counter = 0;
|
||||
|
||||
while (true) {
|
||||
++s_counter;
|
||||
|
||||
char buf[80];
|
||||
assert(prefix.size() + 20 < sizeof(buf));
|
||||
|
||||
int n = snprintf(buf, sizeof(buf),
|
||||
"%s:%lu",
|
||||
prefix.data(), s_counter);
|
||||
|
||||
if ((0 < n) && (std::size_t(n) < sizeof(buf)))
|
||||
buf[n] = '\0';
|
||||
else
|
||||
buf[sizeof(buf)-1] = '\0';
|
||||
|
||||
std::string_view sv(buf);
|
||||
const DUniqueString * retval = this->lookup(sv);
|
||||
if (!retval) {
|
||||
/* not already in string view -> we have viable candidate */
|
||||
retval = this->intern(sv);
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
StringTable::verify_ok(verify_policy policy) const
|
||||
{
|
||||
using xo::scope;
|
||||
using xo::xtag;
|
||||
|
||||
constexpr const char * c_self = "StringTable::verify_ok";
|
||||
scope log(XO_DEBUG(false));
|
||||
|
||||
/* ST1: underlying hash map passes its invariants */
|
||||
if (!map_.verify_ok(policy)) {
|
||||
return policy.report_error(log,
|
||||
c_self, ": map_.verify_ok failed");
|
||||
}
|
||||
|
||||
/* ST2: for each entry, key points to value's string data */
|
||||
for (const auto & kv : map_) {
|
||||
const std::string_view & key = kv.first;
|
||||
const DUniqueString * value = kv.second;
|
||||
|
||||
/* ST2.1: value is not null */
|
||||
if (value == nullptr) {
|
||||
return policy.report_error(log,
|
||||
c_self, ": null value in map",
|
||||
xtag("key", key));
|
||||
}
|
||||
|
||||
/* ST2.2: value lies within strings_ arena */
|
||||
if (!strings_.contains(value)) {
|
||||
return policy.report_error(log,
|
||||
c_self, ": value not in strings_ arena",
|
||||
xtag("key", key),
|
||||
xtag("value", (void*)value));
|
||||
}
|
||||
|
||||
/* ST2.3: key.data() points to value's chars */
|
||||
if (key.data() != value->chars()) {
|
||||
return policy.report_error(log,
|
||||
c_self, ": key.data() != value->chars()",
|
||||
xtag("key", key),
|
||||
xtag("key.data()", (void*)key.data()),
|
||||
xtag("value->chars()", (void*)value->chars()));
|
||||
}
|
||||
|
||||
/* ST2.4: key.size() == value->size() */
|
||||
if (key.size() != value->size()) {
|
||||
return policy.report_error(log,
|
||||
c_self, ": key.size() != value->size()",
|
||||
xtag("key", key),
|
||||
xtag("key.size()", key.size()),
|
||||
xtag("value->size()", value->size()));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
StringTable::visit_pools(const MemorySizeVisitor & visitor) const
|
||||
{
|
||||
strings_.visit_pools(visitor);
|
||||
map_.visit_pools(visitor);
|
||||
}
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end StringTable.cpp */
|
||||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "stringtable2_register_facets.hpp"
|
||||
|
||||
#include <xo/stringtable2/UniqueString.hpp>
|
||||
#include <xo/stringtable2/String.hpp>
|
||||
|
||||
#include <xo/facet/FacetRegistry.hpp>
|
||||
|
|
@ -23,6 +24,9 @@ namespace xo {
|
|||
{
|
||||
scope log(XO_DEBUG(true));
|
||||
|
||||
FacetRegistry::register_impl<AGCObject, DUniqueString>();
|
||||
FacetRegistry::register_impl<APrintable, DUniqueString>();
|
||||
|
||||
FacetRegistry::register_impl<AGCObject, DString>();
|
||||
FacetRegistry::register_impl<APrintable, DString>();
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
**/
|
||||
|
||||
#include "stringtable2_register_types.hpp"
|
||||
#include "UniqueString.hpp"
|
||||
#include "String.hpp"
|
||||
|
||||
//#include <xo/facet/FacetRegistry.hpp>
|
||||
|
|
@ -23,6 +24,7 @@ namespace xo {
|
|||
|
||||
bool ok = true;
|
||||
|
||||
ok &= gc.install_type(impl_for<AGCObject, DUniqueString>());
|
||||
ok &= gc.install_type(impl_for<AGCObject, DString>());
|
||||
|
||||
return ok;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue