summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-16 22:37:15 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-16 22:37:15 -0500
commitb32e83ece42cec5aa9dee370bcdf349d23dbc8ba (patch)
treead1b6dcba2c3620e2c5091548dd3923b28414cfe
parent53c4dcf374c66f1e9190f5a62a52d02fe11a69e6 (diff)
Finish rounding out allocator logic in fud_string.
-rw-r--r--source/fud_string.cpp31
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();