From f52aeead329b53cece70347a17d15a38d0652f1a Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Tue, 23 Mar 2021 01:35:04 +0100 Subject: [PATCH] eliminate the strref friend kludge --- include/cubescript/cubescript.hh | 3 +-- src/cs_strman.cc | 9 ++++----- src/cs_strman.hh | 2 +- src/cs_util.hh | 7 ------- src/lib_str.cc | 10 ++-------- 5 files changed, 8 insertions(+), 23 deletions(-) diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index a34f117..d3adefc 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -297,8 +297,7 @@ LIBCUBESCRIPT_EXPORT bool cs_code_is_empty(cs_bcode *code); struct LIBCUBESCRIPT_EXPORT cs_strref { friend struct cs_value; - /* FIXME: eliminate this */ - friend inline cs_strref cs_make_strref(char const *p, cs_shared_state *cs); + friend struct cs_strman; cs_strref() = delete; cs_strref(cs_shared_state *cs, std::string_view str); diff --git a/src/cs_strman.cc b/src/cs_strman.cc index 2c49b6e..fde2940 100644 --- a/src/cs_strman.cc +++ b/src/cs_strman.cc @@ -42,7 +42,7 @@ char const *cs_strman::ref(char const *ptr) { return ptr; } -char const *cs_strman::steal(char *ptr) { +cs_strref cs_strman::steal(char *ptr) { auto *ss = get_ref_state(ptr); auto sr = std::string_view{ptr, ss->length}; /* much like add(), but we already have memory */ @@ -50,15 +50,14 @@ char const *cs_strman::steal(char *ptr) { if (it != counts.end()) { auto *st = it->second; if (st) { - ++st->refcount; /* the buffer is superfluous now */ cstate->alloc(ss, ss->length + sizeof(cs_strref_state) + 1, 0); - return reinterpret_cast(st + 1); + return cs_strref{reinterpret_cast(st + 1), cstate}; } } - ss->refcount = 1; + ss->refcount = 0; /* cs_strref will increment it */ counts.emplace(sr, ss); - return ptr; + return cs_strref{ptr, cstate}; } void cs_strman::unref(char const *ptr) { diff --git a/src/cs_strman.hh b/src/cs_strman.hh index 21d84db..fd2ac73 100644 --- a/src/cs_strman.hh +++ b/src/cs_strman.hh @@ -58,7 +58,7 @@ struct cs_strman { /* this will use the provided memory, assuming it is a fresh string that * is yet to be added; the memory must be allocated with alloc_buf() */ - char const *steal(char *ptr); + cs_strref steal(char *ptr); /* decrements the reference count and removes it from the system if * that reaches zero; likewise, only safe with pointers that are managed diff --git a/src/cs_util.hh b/src/cs_util.hh index a437be4..5d0b72f 100644 --- a/src/cs_util.hh +++ b/src/cs_util.hh @@ -18,13 +18,6 @@ cs_float cs_parse_float( std::string_view input, std::string_view *end = nullptr ); -struct cs_strman; -struct cs_shared_state; - -inline cs_strref cs_make_strref(char const *p, cs_shared_state *cs) { - return cs_strref{p, cs}; -} - } /* namespace cscript */ #endif /* LIBCUBESCRIPT_CS_UTIL_HH */ diff --git a/src/lib_str.cc b/src/lib_str.cc index 4f7c923..2ebc5d7 100644 --- a/src/lib_str.cc +++ b/src/lib_str.cc @@ -65,10 +65,7 @@ void cs_init_lib_string(cs_state &cs) { for (std::size_t i = 0; i < inps.size(); ++i) { buf[i] = tolower(inps[i]); } - auto const *cbuf = ics->strman->steal(buf); - auto sr = cs_make_strref(cbuf, ics); - ics->strman->unref(cbuf); - res.set_str(sr); + res.set_str(ics->strman->steal(buf)); }); cs.new_command("strupper", "s", [](auto &ccs, auto args, auto &res) { @@ -78,10 +75,7 @@ void cs_init_lib_string(cs_state &cs) { for (std::size_t i = 0; i < inps.size(); ++i) { buf[i] = toupper(inps[i]); } - auto const *cbuf = ics->strman->steal(buf); - auto sr = cs_make_strref(cbuf, ics); - ics->strman->unref(cbuf); - res.set_str(sr); + res.set_str(ics->strman->steal(buf)); }); cs.new_command("escape", "s", [](auto &ccs, auto args, auto &res) {