diff options
-rw-r--r-- | source/fud_string.cpp | 31 |
1 files changed, 19 insertions, 12 deletions
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<void>(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<utf8*>(newDataResult.getOkay()); + if (newData == nullptr) { + return FudStatus::AllocFailure; + } + + auto copyResult = copyMem(newData, newCapacity, data(), length()); + fudAssert(copyResult == FudStatus::Success); + if (isLarge()) { - newData = static_cast<utf8*>(fudRealloc(m_data, newCapacity)); - if (newData == nullptr) { - return FudStatus::AllocFailure; - } - } else { - newData = static_cast<utf8*>(fudAlloc(newCapacity)); - if (newData == nullptr) { - return FudStatus::AllocFailure; - } - static_cast<void>(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<utf8*>(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<utf8*>(allocResult.takeOkay()); } auto* destPtr = output.data(); |