From b32e83ece42cec5aa9dee370bcdf349d23dbc8ba Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 16 Oct 2024 22:37:15 -0500 Subject: Finish rounding out allocator logic in fud_string. --- source/fud_string.cpp | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'source/fud_string.cpp') diff --git a/source/fud_string.cpp b/source/fud_string.cpp index f33224a..31ba5ba 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -210,8 +210,8 @@ FudStatus String::resize(size_t newCapacity) if (newCapacity <= SSO_BUF_SIZE) { BufType temp{BufType::constFill(0)}; static_cast(copyMem(data(), temp.size(), temp.data(), length())); + m_allocator->deallocate(m_data, m_capacity); m_capacity = SSO_BUF_SIZE; - fudFree(m_data); m_data = nullptr; copyMem(m_buffer, temp); data()[m_length] = '\0'; @@ -219,17 +219,20 @@ FudStatus String::resize(size_t newCapacity) } utf8* newData = nullptr; + auto newDataResult = m_allocator->allocate(newCapacity); + if (newDataResult.isError()) { + return newDataResult.getError(); + } + newData = static_cast(newDataResult.getOkay()); + if (newData == nullptr) { + return FudStatus::AllocFailure; + } + + auto copyResult = copyMem(newData, newCapacity, data(), length()); + fudAssert(copyResult == FudStatus::Success); + if (isLarge()) { - newData = static_cast(fudRealloc(m_data, newCapacity)); - if (newData == nullptr) { - return FudStatus::AllocFailure; - } - } else { - newData = static_cast(fudAlloc(newCapacity)); - if (newData == nullptr) { - return FudStatus::AllocFailure; - } - static_cast(copyMem(newData, newCapacity, m_buffer.data(), length())); + m_allocator->deallocate(data(), m_capacity); } m_capacity = newCapacity; @@ -494,7 +497,11 @@ StringResult String::catenate(const String& rhs) const output.m_capacity = SSO_BUF_SIZE; } if (output.isLarge()) { - output.m_data = static_cast(fudAlloc(output.m_capacity)); + auto allocResult{output.m_allocator->allocate(output.m_capacity)}; + if (allocResult.isError()) { + return StringResult::error(allocResult.getError()); + } + output.m_data = static_cast(allocResult.takeOkay()); } auto* destPtr = output.data(); -- cgit v1.2.3