diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-11-02 20:45:02 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-11-02 20:45:02 -0500 |
commit | e8422002f84dc4313894a5b3136c44a9005081fd (patch) | |
tree | b41633d9f759306fe9c7c01e209780222b47f5df /source/fud_string.cpp | |
parent | 6c7fd1db481ff10a16ecab958c6542784fa60b9c (diff) |
Allocator deallocate is void rather than returning FudStatus.
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r-- | source/fud_string.cpp | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 4a20630..4dddedb 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -23,7 +23,8 @@ namespace fud { -StringResult String::makeFromCString(const char8_t* cString) { +StringResult String::makeFromCString(const char8_t* cString) +{ return makeFromCString(reinterpret_cast<const char*>(cString)); } @@ -85,14 +86,19 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator) return StringResult::okay(std::move(output)); } -StringResult String::from(const String& rhs) +StringResult String::from(const String& rhs, Option<Allocator*> allocatorOption) { if (!rhs.valid()) { return StringResult::error(FudStatus::ArgumentInvalid); } + auto* allocator = allocatorOption.valueOr(rhs.allocator()); + if (allocator == nullptr || !allocatorValid(allocator)) { + return StringResult::error(FudStatus::ArgumentInvalid); + } + String output{}; - output.m_allocator = rhs.m_allocator; + output.m_allocator = reinterpret_cast<uintptr_t>(allocator); utf8* outputData{nullptr}; size_t outputCapacity{0}; size_t outputLength{0}; @@ -154,12 +160,11 @@ StringResult String::from(StringView view, Allocator* allocator) String::String(String&& rhs) noexcept : m_allocator{rhs.m_allocator}, m_repr{std::move(rhs.m_repr)} { - if (isLarge()) { - rhs.m_repr.large.data = nullptr; - } + rhs.setSmall(); + rhs.m_repr.small.length = 0; } -String::~String() +String::~String() noexcept { cleanup(); } @@ -184,7 +189,7 @@ FudStatus String::copy(const String& rhs) size_t outputLength{}; if (isLarge()) { - auto allocResult = allocator()->allocate(m_repr.large.capacity); + auto allocResult = allocator()->allocate(m_repr.large.capacity, 1); if (allocResult.isError()) { return allocResult.takeError(); } @@ -223,6 +228,7 @@ String& String::operator=(String&& rhs) noexcept if (isLarge()) { rhs.m_repr.large.data = nullptr; } + return *this; } @@ -230,8 +236,7 @@ void String::cleanup() { const auto* allocPtr = allocator(); if (isLarge() && m_repr.large.data != nullptr && allocPtr != nullptr) { - auto deallocStatus = allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity); - static_cast<void>(deallocStatus); + allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity); m_repr.large.data = nullptr; } } @@ -258,16 +263,16 @@ FudStatus String::resize(size_t newCapacity) auto copyResult = copyMem(dataMut(), temp.size(), temp.data(), len); fudAssert(copyResult == FudStatus::Success); - auto deallocStatus = allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity); + allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity); setSmall(); m_repr.small.length = len & smallStringLengthMask; copyMem(m_repr.small.buffer, temp); m_repr.small.buffer[len] = '\0'; - return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success; + return FudStatus::Success; } - auto allocResult = allocator()->allocate(newCapacity); + auto allocResult = allocator()->allocate(newCapacity, 1); if (allocResult.isError()) { return allocResult.takeError(); } @@ -277,9 +282,8 @@ FudStatus String::resize(size_t newCapacity) auto copyResult = copyMem(newData, newCapacity, data(), length()); fudAssert(copyResult == FudStatus::Success); - auto deallocStatus = FudStatus::Success; if (isLarge()) { - deallocStatus = allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity); + allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity); } size_t len = length(); @@ -291,7 +295,7 @@ FudStatus String::resize(size_t newCapacity) m_repr.large.data[m_repr.large.length] = '\0'; fudAssert(valid()); - return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success; + return FudStatus::Success; } bool String::nullTerminated() const @@ -750,7 +754,7 @@ FudStatus String::makeLarge(size_t cap, size_t len, utf8*& outputData) { m_repr.large.capacity = cap; m_repr.large.length = len; - auto dataResult = allocator()->allocate(cap); + auto dataResult = allocator()->allocate(cap, 1); if (dataResult.isError()) { return dataResult.getError(); } |