DBString - remove ref counting
MODIFY
src/dbstring.cpp
src/dbstring.cpp
@@ -11,7 +11,6 @@ alignas(DBString::size_type) constexpr char DBString::_empty_str[sizeof(size_typ
struct DBStringData {
using size_type = DBString::size_type;
- size_type refcnt;
size_type size;
const char* data() const {
@@ -31,83 +30,32 @@ struct DBStringData {
}
};
-struct DBStringDataDeleter {
- void operator()(DBStringData* p);
-};
-
-using DBStringDataPtr = std::unique_ptr<DBStringData,DBStringDataDeleter>;
-
-class DBStringAllocator {
- public:
- using size_type = DBString::size_type;
-
- static DBStringDataPtr Alloc(StringView str) {
- auto* raw = ::operator new(DBStringData::alloc_size(str));
- auto data = DBStringDataPtr(new (raw) DBStringData());
- data->refcnt = 1;
- data->size = str.size();
- std::memcpy(data->data(), str.data(), data->size);
- data->data()[data->size] = '\0';
-
- return data;
- }
-
- static void Free(DBStringData* data) {
- data->~DBStringData();
- ::operator delete(data);
- }
-
- const char* Acquire(StringView str) {
+static const char* Alloc(StringView str) {
if (str.empty()) {
return DBString::empty_str();
}
- auto ptr = Alloc(str);
- ptr->refcnt += 1;
-
- return ptr.release()->data();
- }
+ auto* raw = ::operator new(DBStringData::alloc_size(str));
+ auto* db = new (raw) DBStringData();
+ db->size = str.size();
+ std::memcpy(db->data(), str.data(), db->size);
+ db->data()[db->size] = '\0';
- const char* Dup(const char* s) {
- if (s != DBString::empty_str()) {
- auto* data = DBStringData::from_data(const_cast<char*>(s));
- data->refcnt += 1;
- }
- return s;
+ return db->data();
}
- void Release(const char* str) {
+static void Free(const char* str) {
if (str == DBString::empty_str()) {
return;
}
+ auto* db = DBStringData::from_data(const_cast<char*>(str));
- auto* data = DBStringData::from_data(const_cast<char*>(str));
- data->refcnt -= 1;
- assert(data->refcnt >= 0);
- if (data->refcnt == 0) {
- Free(data);
- }
- }
-
- static DBStringAllocator& instance() {
- static DBStringAllocator alloc;
- return alloc;
- }
- private:
- constexpr DBStringAllocator() = default;
-};
-
-void DBStringDataDeleter::operator()(DBStringData* p) {
- DBStringAllocator::Free(p);
+ db->~DBStringData();
+ ::operator delete(db);
}
DBString::DBString(StringView s)
- : _storage(DBStringAllocator::instance().Acquire(s))
-{
-}
-
-DBString::DBString(const DBString& o)
- : _storage(DBStringAllocator::instance().Dup(o._storage))
+ : _storage(Alloc(s))
{
}
@@ -115,14 +63,14 @@ DBString& DBString::operator=(const DBString& o) {
if (this != &o) {
// What is strings are the same, skip double lookup?
_reset();
- _storage = DBStringAllocator::instance().Dup(o._storage);
+ _storage = Alloc(StringView(o));
}
return *this;
}
void DBString::_reset() noexcept {
assert(_storage != nullptr);
- DBStringAllocator::instance().Release(_storage);
+ Free(_storage);
}
} // namespace lcf

MODIFY
src/lcf/dbstring.h
src/lcf/dbstring.h
@@ -46,7 +46,7 @@ class DBString {
DBString(const char(&literal)[N]) : DBString(StringView(literal)) {}
DBString(const char* s, size_t len) : DBString(StringView(s, len)) {}
- DBString(const DBString& o);
+ DBString(const DBString& o) : DBString(StringView(o)) {}
DBString& operator=(const DBString&);
DBString(DBString&&) noexcept;
DBString& operator=(DBString&&) noexcept;
@@ -129,7 +129,6 @@ inline DBString::DBString(DBString&& o) noexcept
}
inline DBString& DBString::operator=(DBString&& o) noexcept {
- return operator=(o);
if (this != &o) {
if (!empty()) {
_reset();

DBString - remove ref counting